CROSSINGTUD / CryptoAnalysis

CogniCrypt_SAST: CrySL-to-Static Analysis Compiler
Eclipse Public License 2.0
63 stars 39 forks source link
command-line-tool cryptoanalysis cryptography java jca misuse-detection static-analysis

CogniCryptSAST

This repository contains CogniCryptSAST, the static analysis component for CogniCrypt. The static analysis CogniCryptSAST takes rules written in the specification language CrySL as input, and performs a static analysis based on the specification of the rules. CrySL is a domain-specific language (DSL) designed to encode usage specifications for cryptographic libaries (e.g., the JCA in particular). More information on CrySL and the static analysis may be found in this paper.

Structure

We provide the implementation of the static analysis of CogniCrypt in:

We further provide two SAST tools that allow the analysis of Java and Android applications:

Releases

You can checkout a pre-compiled version of CogniCryptSAST here. We recommend using the latest version.

Download the two files:

You can find CogniCryptSAST also on Maven Central.

Checkout and Build

CogniCryptSAST uses Maven as build tool. You can compile and build this project via

mvn clean package -DskipTests=true.

The packaged jar artifacts including all dependencies can be found in /apps. Building requires at least Java 11.

CogniCryptSAST for Java Applications

CogniCryptSAST can be started in headless mode (i.e., detached from Eclipse) via the file HeadlessJavaScanner-x.y.z-jar-with-dependencies.jar. It requires two arguments:

java -jar HeadlessJavaScanner-x.y.z-jar-with-dependencies.jar 
      --rulesDir <path-to-crysl-source-code-format-rules> 
      --appPath <application-path>

For an easy start we prepared a .jar containing classes with crypto misuses. The source code for these misuses is found here. To run CogniCryptSAST on these classes, simply execute the following command.

java -jar HeadlessJavaScanner-x.y.z-jar-with-dependencies.jar
  --rulesDir $(pwd)/CryptoAnalysis-Core/src/main/resources/JavaCryptographicArchitecture
  --appPath $(pwd)/CryptoAnalysisTargets/CogniCryptDemoExample/Examples.jar

Other additional arguments that can be used are as follows:

--cg <selection_of_call_graph_for_analysis> (possible values are CHA, SPARK, SPARKLIB)
--sootPath <absolute_path_of_whole_project>
--identifier <identifier_for_labeling_output_files>
--reportPath <directory_location_for_cryptoanalysis_report>
--reportFormat <format of cryptoanalysis_report> (possible values are CMD, TXT, SARIF, CSV, CSV_SUMMARY)
--visualization (enables the visualization, but also requires --reportPath option to be set)
--dstats (disables the output of the analysis statistics in the reports)
--ignoreSections (Text file with packages (e.g. `de.example.*`), classes (e.g. `de.example.exmapleClass`) or methods (e.g. `de.example.exampleClass.exampleMethod`), one per line. Those packages, classes and methods are ignored during the analysis)
--timeout <timeout in milliseconds> (Timeout for seeds in milliseconds. If a seed exceeds this value, CryptoAnalysis aborts the typestate and extract parameter analysis and continues with the results computed so far. (default: 10000))
--help (show more information for the CLI arguments)

Note, depending on the analyzed application, the analysis may require a lot of memory and a large stack size. Remember to set the necessary heap size (e.g. -Xmx8g) and stack size (e.g. -Xss60m).

Use as a GitHub Action

CogniCryptSAST can be used as a GitHub action.

- name: Run CogniCrypt
  uses: CROSSINGTUD/CryptoAnalysis@version
  with:
    appPath: "CryptoAnalysisTargets/HelloWorld/HelloWorld.jar"
    basePath: "CryptoAnalysisTargets/HelloWorld"

The appPath needs to be configured to point to a compiled version of your application.

The basePath is used to relate paths in the analyzed jar and the source tree. Class com.example is searched for at basePath/com/example.

See action.yml for all input options.

An example of how to use the GitHub action can be found in the CryptoAnalysis-demo repository.

Report and Error Types

CogniCryptSAST reports misuses when the code is not compliant with the CrySL rules. For each misuse, CogniCryptSAST reports the class and the method the misuse is contained in. There are multiple misuse types:

CogniCryptSAST supports different report formats, which can be set by using --reportformat option. The supported formats are:

If the --reportformat option is not specified, CogniCryptSAST defaults to the CMD option. It also allows the usage of multiple different formats for the same analysis (e.g. --reportformat CMD,TXT,CSV creates a report, which is printed to the command line and is written to a text and CSV file). If the option --reportPath <directory_location_for_cryptoanalysis_report> is set, the reports are created in the specified directory.

Updating CrySL Rules

The tool takes CrySL rules in their source code formats (crysl). You can adapt the rules in any text editor. Additionaly, the Eclipse plugin CogniCrypt ships with a CrySL editor to modify the rules with IDE support (e.g., content assist, auto completion, etc.). A step-by-step-explanation on how edit CrySL rules is avialable at the tool's website cognicrypt.org.

CogniCryptSAST for Android Applications

CogniCryptSAST can also be run on Android Applications using the Android scanner HeadlessAndroidScanner-x.y.z-jar-with-dependencies.jar. Its usage does not deviate much from regular CogniCryptSAST's. It requires three arguments:

java -jar HeadlessAndroidScanner-x.y.z-jar-with-dependencies.jar
      --rulesDir <path-to-crysl-source-code-format-rules>
      --platformDirectory <path-to-android-platform>
      --appPath <application-path>

Optional parameters are --reportPath and --reportFormat. They have the same functionality as the HeadlessJavaScanner-x.y.z-jar-with-dependencies.jar (see above).

Again, depending on the analyzed application, the analysis may require a lot of memory and a large stack size. Remember to set the necessary heap size (e.g. -Xmx8g) and stack size (e.g. -Xss60m).