Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

43 Commits

Folders and files

Repository files navigation

CryptScan

CryptScan is a command-line tool that decompiles Android APKs and finds cryptographic misuse through static analysis. It loads an APK with androguard, walks the Dalvik bytecode and its string and method cross-references, and runs a set of focused rules for problems like hardcoded keys, ECB mode, broken hashes, weak randomness, missing certificate validation, and insecure TLS. Results come out as JSON, a styled HTML report, and a corpus CSV. It is pure Python: no Gradle, no Android Studio, no device needed for the static scan.

What it detects

ID Rule Default severity CWE
CS001 Hardcoded cryptographic key or secret Critical CWE-321
CS002 Cipher uses ECB mode High CWE-327
CS003 Broken or deprecated algorithm (MD5, SHA-1, DES, RC4) High / Medium CWE-327
CS004 Weak randomness (java.util.Random) feeding crypto High CWE-330
CS005 Secret written to SharedPreferences Medium CWE-312
CS006 TrustManager accepts all certificates Critical CWE-295
CS007 Insecure TLS version (SSLv3, TLS 1.0/1.1) High CWE-326
CS008 Home-grown byte-level crypto Medium CWE-327

Installation

Requires Python 3.11 or newer.

git clone <your-fork-url> crypto-decompiler
cd crypto-decompiler
python -m venv .venv && source .venv/bin/activate
pip install .

For development, install the extras and run the tests:

pip install -e ".[dev]"
pytest -q

Quick start

Scan one APK and write both report formats:

cryptscan scan app.apk --json report.json --html report.html

List the rules, or run a subset:

cryptscan rules
cryptscan scan app.apk --rules CS001,CS002,CS006

Gate a CI pipeline on severity (exit code 2 when a finding is at or above the threshold):

cryptscan scan app.apk --fail-on high

Batch-scan a directory into a summary CSV and per-APK JSON:

cryptscan corpus ./apks --csv summary.csv --json-dir ./reports

Fetch a couple of deliberately-insecure demo apps to try it on:

bash scripts/fetch_sample_app.sh
cryptscan scan sample_apks/insecurebank.apk

Sample output

Scanning InsecureBankv2:

com.android.insecurebankv2  -  8 findings (1 critical, 0 high, 7 medium, 0 low)  in 7.81s
[CRITICAL] CS001 Hardcoded cryptographic key or secret
    Lcom/android/insecurebankv2/CryptoClass; -> <init>  |  This...[32 chars]
[MEDIUM] CS003 Weak hash algorithm (MD5)
    Lcom/google/android/gms/ads/internal/util/client/zza; -> zzax  |  MD5
[MEDIUM] CS005 Secret stored in SharedPreferences (superSecurePassword)
    Lcom/android/insecurebankv2/DoLogin$RequestTask; -> saveCreds  |  superSecurePassword

The HTML report groups findings by severity with remediation guidance. A rendered example is in docs/sample-report.html.

Architecture

flowchart LR
    APK[APK / DEX] --> L[loader]
    L -->|androguard Analysis| C[AnalysisContext]
    C --> E[rule engine]
    subgraph rules
      R1[CS001..CS008]
    end
    E --> R1
    R1 --> F[Findings + Report]
    F --> J[JSON]
    F --> H[HTML]
    F --> V[corpus CSV]
Loading
cryptscan/
  cli.py            command-line entry point
  loader.py         APK/DEX to androguard Analysis + metadata
  analyzer.py       runs the rules, builds the Report
  findings.py       Severity, Confidence, Finding, Report
  analysis/         shared primitives: entropy, string/method xrefs, local def-use
  rules/            one module per rule, all subclass Rule
  report/           JSON and HTML (Jinja2) renderers
  corpus/           parallel directory scanner
  frida/            optional runtime verification hooks

Detection works on androguard's Analysis object only. The tool follows string and method cross-references and does light method-local def-use to tie a constant to the API call that consumes it (for example, the exact transform passed to Cipher.getInstance). The Java decompiler is never invoked, which keeps a typical scan well under 30 seconds.

Development

Any Python 3.11+ works. This project was developed on 3.12 provisioned with uv:

uv venv --python 3.12 .venv && source .venv/bin/activate
uv pip install -e ".[dev]"
pytest -q
ruff check . && ruff format --check .

The Android SDK is only needed to rebuild the test fixtures, not to run scans. Point ANDROID_HOME at an SDK that has build-tools;34.0.0 and platforms;android-34, then run tests/fixtures/build.sh (see below). A local sdk/ directory is git-ignored.

Adding a rule

  1. Create cryptscan/rules/my_rule.py with a class that subclasses Rule and sets id, name, severity, cwe, and remediation.
  2. Implement analyze(self, ctx) -> list[Finding]. Use the helpers in cryptscan/analysis/xref.py (strings_matching, getinstance_args, app_methods, classes_implementing, ...) and build results with self.finding(...).
  3. Register the rule in cryptscan/rules/__init__.py (ALL_RULES).
  4. Add a positive and a negative Java sample under tests/fixtures/src/, rebuild the fixtures, and add a tests/test_rule_my_rule.py asserting the positive fires and the negative does not.

Test fixtures

Unit tests run against tiny prebuilt .dex and .apk fixtures committed under tests/fixtures/, so the suite needs no Android tooling. To change a fixture, edit the Java under tests/fixtures/src/ and rebuild with a JDK and Android SDK build-tools:

ANDROID_HOME=/path/to/android-sdk bash tests/fixtures/build.sh

Benchmark

Score the scanner against a labeled corpus and get per-rule and overall precision, recall, and F1. The corpus is benchmark/ground_truth.json: a list of targets, each with a class-name scope and the expected (rule, class) findings for that scope.

cryptscan benchmark benchmark/ --md benchmark/RESULTS.md --json benchmark/results.json

Matching is at (rule_id, class_name) granularity, not per finding or offset, so duplicate findings in one class do not inflate the false-positive count. Current baseline (benchmark/RESULTS.md), scored against the test fixtures:

Rule TP FP FN Precision Recall F1
CS001 3 0 0 1.000 1.000 1.000
CS002 2 0 0 1.000 1.000 1.000
CS003 2 0 0 1.000 1.000 1.000
CS004 1 0 0 1.000 1.000 1.000
CS005 1 0 0 1.000 1.000 1.000
CS006 1 0 0 1.000 1.000 1.000
CS007 1 0 0 1.000 1.000 1.000
CS008 1 0 0 1.000 1.000 1.000
ALL 12 0 0 1.000 1.000 1.000

CS003, CS004, CS005 and CS007 used to flag a value anywhere in a method that also called a matching API, instead of tracing the value into that call. Decoy fixtures that separate the two exposed the gap and dropped overall precision to 0.75. A def-use rewrite traces each literal into its sink and restored precision to 1.00, with recall held at 1.00.

Dynamic verification (experimental)

With a rooted device or emulator and frida-server running, install the extra and confirm findings at runtime by hooking Cipher and MessageDigest:

pip install ".[dynamic]"
cryptscan verify --package com.example.app

License

MIT. See LICENSE.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages