Skip to content

hypnguyen1209/offensive-claude / skills/vulnerability-analysis

vulnerability-analysis

Unverified

Use when auditing source code for vulnerabilities — drive CodeQL/Semgrep/Joern to taint untrusted data source-to-sink across injection, memory safety, deserialization/prototype-pollution, secrets/crypto/authz/race, and supply-chain risks

scripts/sast_runner.py

View on GitHub
#!/usr/bin/env python3
"""
sast_runner.py - Orchestrate CodeQL, Semgrep, and Joern over a target, then
normalize/dedupe their SARIF/JSON output into a single ranked finding list.

This is the "production" companion to taint_trace.py: it drives the real
interprocedural engines and merges results so you triage one list, not three.

Subcommands:
  codeql   build a CodeQL DB and run a query suite, emit SARIF
  semgrep  run Semgrep (CE or --pro interfile) with a config/registry pack
  joern    run a CPGQL taint-tracking script over a Joern CPG
  merge    load N SARIF/JSON files, dedupe by (rule,file,line), rank by severity

Dependencies (install the engines you use):
  CodeQL  : https://github.com/github/codeql-cli-binaries (codeql in PATH)
  Semgrep : pip install semgrep        (>= 1.x; --pro needs login for interfile)
  Joern   : https://joern.io (joern, joern-parse in PATH)
  Python  : 3.8+ stdlib only for merge.

Usage:
  python3 sast_runner.py codeql  --src ./app --lang python --out cq.sarif \\
      --suite codeql/python-queries:codeql-suites/python-security-extended.qls
  python3 sast_runner.py semgrep --src ./app --config p/owasp-top-ten --pro --out sg.sarif
  python3 sast_runner.py joern   --src ./app --script taint.sc --out joern.json
  python3 sast_runner.py merge   cq.sarif sg.sarif --out merged.json

LEGAL: authorized assessments only.
"""
import argparse
import json
import os
import subprocess
import sys

SEV_RANK = {"critical": 5, "error": 4, "high": 4, "warning": 3, "medium": 3,
            "note": 2, "low": 2, "info": 1, "none": 0}


def run(cmd):
    print(f"[*] {' '.join(cmd)}", file=sys.stderr)
    return subprocess.call(cmd)


def cmd_codeql(args):
    db = args.db or os.path.join(args.out_dir or ".", "codeql-db")
    # 1. Create database (autobuild for compiled langs; none-build for interpreted)
    create = ["codeql", "database", "create", db, "--language", args.lang,
              "--source-root", args.src, "--overwrite"]
    if args.command:
        create += ["--command", args.command]
    if run(create) != 0:
        sys.exit("[!] codeql database create failed")
    # 2. Analyze with a query suite -> SARIF
    analyze = ["codeql", "database", "analyze", db, args.suite,
               "--format", "sarifv2.1.0", "--output", args.out,
               "--threads", str(args.threads), "--rerun"]
    sys.exit(run(analyze))


def cmd_semgrep(args):
    cmd = ["semgrep", "scan", "--config", args.config, "--sarif",
           "--output", args.out, "--metrics", "off", args.src]
    if args.pro:
        cmd.insert(2, "--pro")          # interfile + cross-function dataflow
    if args.baseline:
        cmd += ["--baseline-commit", args.baseline]
    sys.exit(run(cmd))


def cmd_joern(args):
    # joern-parse builds the CPG; joern --script runs the CPGQL taint query.
    cpg = os.path.join(args.out_dir or ".", "cpg.bin")
    if run(["joern-parse", args.src, "--output", cpg]) != 0:
        sys.exit("[!] joern-parse failed")
    env = dict(os.environ, JOERN_CPG=cpg, JOERN_OUT=args.out)
    print(f"[*] joern --script {args.script} (CPG={cpg})", file=sys.stderr)
    sys.exit(subprocess.call(["joern", "--script", args.script,
                              "--param", f"cpgPath={cpg}",
                              "--param", f"outPath={args.out}"], env=env))


# ---------------------------------------------------------------------------
# SARIF / JSON normalization + merge
# ---------------------------------------------------------------------------
def load_sarif(path):
    with open(path, encoding="utf-8") as fh:
        doc = json.load(fh)
    out = []
    # SARIF v2.1.0
    if isinstance(doc, dict) and "runs" in doc:
        for run_ in doc["runs"]:
            tool = run_.get("tool", {}).get("driver", {}).get("name", "sast")
            rules = {r["id"]: r for r in run_.get("tool", {}).get("driver", {}).get("rules", [])}
            for res in run_.get("results", []):
                rid = res.get("ruleId", "?")
                sev = res.get("level", "warning")
                # CodeQL packs severity in rule properties
                rp = rules.get(rid, {}).get("properties", {})
                sev = rp.get("problem.severity", rp.get("security-severity", sev)) or sev
                for loc in res.get("locations", [{}]):
                    pl = loc.get("physicalLocation", {})
                    out.append({
                        "tool": tool, "rule": rid, "severity": str(sev).lower(),
                        "file": pl.get("artifactLocation", {}).get("uri", "?"),
                        "line": pl.get("region", {}).get("startLine", 0),
                        "message": res.get("message", {}).get("text", "")[:200],
                    })
    # Plain JSON list (e.g. from joern.sc or taint_trace.py)
    elif isinstance(doc, list):
        for r in doc:
            out.append({
                "tool": r.get("tool", "joern"),
                "rule": r.get("rule", r.get("cwe", "?")),
                "severity": str(r.get("severity", "warning")).lower(),
                "file": r.get("file", "?"),
                "line": r.get("line", r.get("sink_line", 0)),
                "message": r.get("message", r.get("sink_code", ""))[:200],
            })
    return out


def cmd_merge(args):
    seen, merged = {}, []
    for path in args.files:
        for f in load_sarif(path):
            key = (f["rule"], f["file"], f["line"])
            if key in seen:
                if f["tool"] not in seen[key]["tools"]:
                    seen[key]["tools"].append(f["tool"])
                    seen[key]["corroboration"] = len(seen[key]["tools"])
                continue
            f["tools"] = [f.pop("tool")]
            f["corroboration"] = 1
            seen[key] = f
            merged.append(f)
    # Rank: multi-tool corroboration first, then severity, then file.
    merged.sort(key=lambda f: (-f["corroboration"], -SEV_RANK.get(f["severity"], 0), f["file"]))
    with open(args.out, "w", encoding="utf-8") as fh:
        json.dump(merged, fh, indent=2)
    for f in merged[:args.top]:
        tools = "+".join(f["tools"])
        star = " *CORROBORATED*" if f["corroboration"] > 1 else ""
        print(f"[{f['severity']:>8}] {f['rule']:<40} {f['file']}:{f['line']} ({tools}){star}")
    print(f"\n[+] {len(merged)} unique findings -> {args.out}", file=sys.stderr)


def main():
    ap = argparse.ArgumentParser(description="SAST engine orchestrator + merge")
    sub = ap.add_subparsers(dest="cmd", required=True)

    c = sub.add_parser("codeql")
    c.add_argument("--src", required=True)
    c.add_argument("--lang", required=True, help="python|javascript|java|cpp|csharp|go|ruby|swift")
    c.add_argument("--suite", required=True)
    c.add_argument("--out", required=True)
    c.add_argument("--db"); c.add_argument("--out-dir")
    c.add_argument("--command", help="build command for compiled languages")
    c.add_argument("--threads", type=int, default=0)
    c.set_defaults(func=cmd_codeql)

    s = sub.add_parser("semgrep")
    s.add_argument("--src", required=True)
    s.add_argument("--config", required=True, help="registry pack (p/...), file, or dir")
    s.add_argument("--out", required=True)
    s.add_argument("--pro", action="store_true", help="interfile/cross-function dataflow")
    s.add_argument("--baseline", help="git ref for diff-aware scan")
    s.set_defaults(func=cmd_semgrep)

    j = sub.add_parser("joern")
    j.add_argument("--src", required=True)
    j.add_argument("--script", required=True, help=".sc CPGQL taint script")
    j.add_argument("--out", required=True); j.add_argument("--out-dir")
    j.set_defaults(func=cmd_joern)

    m = sub.add_parser("merge")
    m.add_argument("files", nargs="+", help="SARIF or JSON finding files")
    m.add_argument("--out", default="merged.json")
    m.add_argument("--top", type=int, default=40)
    m.set_defaults(func=cmd_merge)

    args = ap.parse_args()
    args.func(args)


if __name__ == "__main__":
    main()
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 194kUpdated Claude CodeCodexVerifiedInstall