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/taint_trace.py

View on GitHub
#!/usr/bin/env python3
"""
taint_trace.py - Lightweight intraprocedural+heuristic taint tracer for fast
source-code triage across Python / JS-TS / PHP / Java.

It is NOT a replacement for CodeQL/Semgrep/Joern. It is the "first 10 minutes"
tool: regex-anchored source/sink discovery + a same-function reachability check
(does a tainted identifier flow into a sink in the same function body, without a
recognised sanitizer in between?). Use it to rank files for deep review and to
seed CodeQL/Semgrep configs.

Modes:
  sinks    list every sink hit with file:line, classified by CWE
  sources  list every source (untrusted input) hit
  trace    flag functions where a source-derived identifier reaches a sink with
           no sanitizer on the path (heuristic source->sink within one function)
  config   emit a Semgrep taint-mode YAML skeleton seeded from the languages found

Dependencies: Python 3.8+ stdlib only.

Usage:
  python3 taint_trace.py sinks   ./src --lang py,js,php,java
  python3 taint_trace.py sources ./src --lang py
  python3 taint_trace.py trace   ./src --lang py --json findings.json
  python3 taint_trace.py config  --lang py > sqli.semgrep.yml

LEGAL: authorized source-code review only.
"""
import argparse
import json
import os
import re
import sys

# ---------------------------------------------------------------------------
# Source / sink / sanitizer models per language. CWE is the dominant class for
# that sink family; refine per-finding during manual review.
# ---------------------------------------------------------------------------
MODEL = {
    "py": {
        "ext": (".py",),
        "sources": [
            r"request\.(args|form|json|values|data|files|cookies|headers)",
            r"flask\.request", r"\bsys\.argv\b", r"\bos\.environ\b",
            r"\.get_json\(", r"\binput\(", r"socket\.\w+\.recv\(",
            r"self\.rfile\.read", r"\bgetenv\(",
        ],
        "sinks": {
            "CWE-89": [r"\.execute\(\s*f?[\"']", r"\.execute\(.*%.*", r"text\(\s*f?[\"']",
                       r"\.raw\(", r"\.extra\(", r"cursor\.executemany\(.*\+"],
            "CWE-78": [r"os\.system\(", r"os\.popen\(", r"subprocess\.(call|run|Popen|check_output)\(.*shell\s*=\s*True",
                       r"\bcommands\.getoutput\("],
            "CWE-94": [r"\beval\(", r"\bexec\(", r"compile\(", r"__import__\("],
            "CWE-502": [r"pickle\.loads?\(", r"cPickle\.loads?\(", r"yaml\.load\((?!.*Loader\s*=\s*yaml\.SafeLoader)",
                        r"jsonpickle\.decode\(", r"dill\.loads?\(", r"marshal\.loads\("],
            "CWE-22": [r"open\(", r"send_file\(", r"os\.path\.join\(", r"shutil\.(copy|move|rmtree)\("],
            "CWE-918": [r"requests\.(get|post|put|head)\(", r"urllib\.request\.urlopen\(", r"httpx\.\w+\("],
            "CWE-1336": [r"render_template_string\(", r"Template\(.*\)\.render\(", r"Markup\(",
                         r"Environment\(.*\)\.from_string\("],
        },
        "sanitizers": [r"int\(", r"shlex\.quote\(", r"html\.escape\(", r"urllib\.parse\.quote\(",
                       r"re\.(match|fullmatch)\(", r"bleach\.clean\(", r"os\.path\.realpath\(",
                       r"\.isdigit\(\)", r"secure_filename\("],
        "func": re.compile(r"^\s*(?:async\s+)?def\s+(\w+)\s*\("),
    },
    "js": {
        "ext": (".js", ".ts", ".jsx", ".tsx", ".mjs"),
        "sources": [
            r"req\.(params|query|body|headers|cookies)", r"request\.(params|query|body)",
            r"process\.(argv|env)", r"location\.(hash|search|href)", r"document\.(URL|location|cookie)",
            r"\.searchParams", r"window\.name", r"event\.data",
        ],
        "sinks": {
            "CWE-89": [r"\.query\(\s*[`\"'].*\$\{", r"sequelize\.literal\(", r"\.query\(.*\+\s*\w+"],
            "CWE-78": [r"child_process\.exec\(", r"\bexec\(", r"execSync\(",
                       r"spawn\(.*shell\s*:\s*true", r"\.exec\(.*\+"],
            "CWE-94": [r"\beval\(", r"new Function\(", r"vm\.runIn\w+\(", r"setTimeout\(\s*[\"'`]"],
            "CWE-79": [r"\.innerHTML\s*=", r"\.outerHTML\s*=", r"document\.write\(",
                       r"dangerouslySetInnerHTML", r"\$\(.*\)\.html\(", r"insertAdjacentHTML\("],
            "CWE-1321": [r"_\.merge\(", r"_\.mergeWith\(", r"_\.set\(", r"_\.setWith\(",
                         r"_\.defaultsDeep\(", r"_\.zipObjectDeep\(", r"_\.unset\(", r"_\.omit\(",
                         r"Object\.assign\(.*JSON\.parse"],
            "CWE-502": [r"node-serialize", r"unserialize\(", r"\bvm2\b", r"funcster"],
            "CWE-918": [r"\bfetch\(", r"axios\.\w+\(", r"http\.request\(", r"got\(", r"needle\("],
            "CWE-22": [r"fs\.(readFile|writeFile|createReadStream|unlink)(Sync)?\(", r"res\.sendFile\(",
                       r"path\.join\(.*req\."],
        },
        "sanitizers": [r"parseInt\(", r"Number\(", r"encodeURIComponent\(", r"DOMPurify\.sanitize\(",
                       r"validator\.\w+\(", r"path\.basename\(", r"Object\.create\(null\)",
                       r"\.replace\(/\[\^", r"escape\("],
        "func": re.compile(r"(?:function\s+(\w+)\s*\(|(\w+)\s*[:=]\s*(?:async\s+)?\([^)]*\)\s*=>|(\w+)\s*\([^)]*\)\s*\{)"),
    },
    "php": {
        "ext": (".php", ".phtml", ".inc"),
        "sources": [r"\$_(GET|POST|REQUEST|COOKIE|SERVER|FILES)\b", r"file_get_contents\(\s*['\"]php://input",
                    r"getenv\(", r"\$argv\b"],
        "sinks": {
            "CWE-89": [r"->query\(", r"mysqli_query\(", r"mysql_query\(", r"->exec\(", r"PDO::query"],
            "CWE-78": [r"\bsystem\(", r"\bexec\(", r"shell_exec\(", r"passthru\(", r"popen\(", r"proc_open\(", r"`"],
            "CWE-94": [r"\beval\(", r"assert\(", r"create_function\(", r"preg_replace\(.*/e", r"call_user_func\("],
            "CWE-502": [r"unserialize\(", r"yaml_parse\("],
            "CWE-22": [r"\binclude(_once)?\b", r"\brequire(_once)?\b", r"fopen\(", r"readfile\(", r"file_get_contents\("],
            "CWE-918": [r"curl_setopt\(.*CURLOPT_URL", r"file_get_contents\(\s*\$", r"fsockopen\("],
        },
        "sanitizers": [r"intval\(", r"\(int\)", r"htmlspecialchars\(", r"htmlentities\(", r"escapeshellarg\(",
                       r"escapeshellcmd\(", r"preg_match\(", r"basename\(", r"filter_var\(", r"->prepare\("],
        "func": re.compile(r"function\s+(\w+)\s*\("),
    },
    "java": {
        "ext": (".java", ".kt"),
        "sources": [r"request\.getParameter\(", r"request\.getHeader\(", r"request\.getQueryString\(",
                    r"getRequestURI\(", r"@RequestParam", r"@PathVariable", r"@RequestBody", r"\.getInputStream\("],
        "sinks": {
            "CWE-89": [r"Statement\.execute", r"createQuery\(\s*[\"'].*\+", r"createNativeQuery\(.*\+",
                       r"\.prepareStatement\(.*\+"],
            "CWE-78": [r"Runtime\.getRuntime\(\)\.exec\(", r"new ProcessBuilder\(", r"\.command\("],
            "CWE-502": [r"ObjectInputStream", r"\.readObject\(", r"XMLDecoder", r"readUnshared\(",
                        r"SerializationUtils\.deserialize\("],
            "CWE-611": [r"DocumentBuilderFactory", r"SAXParserFactory", r"XMLInputFactory", r"TransformerFactory",
                        r"SchemaFactory"],
            "CWE-1336": [r"Velocity\.evaluate\(", r"\.mergeTemplate\(", r"freemarker", r"new Template\("],
            "CWE-22": [r"new File\(", r"new FileInputStream\(", r"Files\.(read|write|newInputStream)\(",
                       r"new FileReader\("],
            "CWE-918": [r"new URL\(", r"HttpClient", r"RestTemplate", r"\.openConnection\("],
        },
        "sanitizers": [r"Integer\.parseInt\(", r"Long\.parseLong\(", r"Pattern\.matches\(", r"\.matches\(",
                       r"ESAPI\.encoder\(\)", r"setFeature\(.*disallow-doctype", r"Paths\.get\(.*\.normalize\("],
        "func": re.compile(r"(?:public|private|protected|static|\s)[\w<>\[\],\s]+\s+(\w+)\s*\([^)]*\)\s*(?:throws[\w,\s]*)?\{"),
    },
}


def iter_files(root, langs):
    exts = tuple(e for L in langs for e in MODEL[L]["ext"])
    skip = {"node_modules", ".git", "vendor", "venv", ".venv", "dist", "build", "__pycache__", "target"}
    for dp, dn, fn in os.walk(root):
        dn[:] = [d for d in dn if d not in skip]
        for f in fn:
            if f.endswith(exts):
                yield os.path.join(dp, f)


def lang_for(path):
    for L, m in MODEL.items():
        if path.endswith(m["ext"]):
            return L
    return None


def scan_lines(path):
    try:
        with open(path, "r", encoding="utf-8", errors="replace") as fh:
            return fh.read().splitlines()
    except OSError:
        return []


def find_hits(path, patterns):
    lines = scan_lines(path)
    out = []
    compiled = [(re.compile(p), p) for p in patterns]
    for i, line in enumerate(lines, 1):
        for rx, raw in compiled:
            if rx.search(line):
                out.append((i, line.strip()[:160], raw))
    return out


def cmd_sinks(args):
    results = []
    for path in iter_files(args.root, args.langs):
        L = lang_for(path)
        for cwe, pats in MODEL[L]["sinks"].items():
            for ln, txt, pat in find_hits(path, pats):
                results.append({"file": path, "line": ln, "cwe": cwe, "lang": L, "code": txt})
    emit(results, args)


def cmd_sources(args):
    results = []
    for path in iter_files(args.root, args.langs):
        L = lang_for(path)
        for ln, txt, pat in find_hits(path, MODEL[L]["sources"]):
            results.append({"file": path, "line": ln, "lang": L, "code": txt})
    emit(results, args)


def split_functions(lines, func_rx):
    """Yield (start_line_idx, end_line_idx_exclusive, body_lines) per function via brace/indent heuristics."""
    starts = [i for i, ln in enumerate(lines) if func_rx.search(ln)]
    starts.append(len(lines))
    for a, b in zip(starts, starts[1:]):
        yield a, b, lines[a:b]


_GUARD_PY = re.compile(r"^\s*(?:el)?if\s+(.+?):\s*(?:#.*)?$|^\s*while\s+(.+?):\s*(?:#.*)?$")
_GUARD_C = re.compile(r"\b(?:if|while)\s*\((.+)\)\s*\{?\s*$")


def extract_guard(line, lang):
    """Pull the branch condition out of an if/elif/while line, or None. Feeds path_conditions.py."""
    if lang == "py":
        m = _GUARD_PY.match(line)
        if m:
            return (m.group(1) or m.group(2) or "").strip()
        return None
    m = _GUARD_C.search(line)
    return m.group(1).strip() if m else None


def cmd_trace(args):
    """Heuristic: within one function, a source assigns into var X; X (or source expr) later
    appears in a sink line; and no sanitizer pattern occurs on any line between them."""
    findings = []
    for path in iter_files(args.root, args.langs):
        L = lang_for(path)
        m = MODEL[L]
        lines = scan_lines(path)
        src_rx = [re.compile(p) for p in m["sources"]]
        san_rx = [re.compile(p) for p in m["sanitizers"]]
        sink_rx = [(re.compile(p), cwe) for cwe, pats in m["sinks"].items() for p in pats]
        for a, b, body in split_functions(lines, m["func"]):
            src_idx = [j for j, ln in enumerate(body) if any(r.search(ln) for r in src_rx)]
            if not src_idx:
                continue
            first_src = min(src_idx)
            for j, ln in enumerate(body):
                if j <= first_src:
                    continue
                for rx, cwe in sink_rx:
                    if rx.search(ln):
                        between = body[first_src:j + 1]
                        sanitized = any(s.search(x) for x in between for s in san_rx)
                        guards = []
                        for k in range(first_src, j + 1):
                            g = extract_guard(body[k], L)
                            if g:
                                guards.append({"expr": g, "step_index": a + k + 1, "negated": False})
                        findings.append({
                            "file": path, "lang": L, "cwe": cwe,
                            "source_line": a + first_src + 1,
                            "sink_line": a + j + 1,
                            "sanitizer_on_path": sanitized,
                            "confidence": "low" if sanitized else "medium",
                            "source_code": body[first_src].strip()[:120],
                            "sink_code": ln.strip()[:120],
                            "guards": guards,   # branch conditions on the path -> path_conditions.py
                        })
    findings.sort(key=lambda f: (f["sanitizer_on_path"], f["file"]))
    emit(findings, args)


SEMGREP_TEMPLATE = """\
# Auto-seeded Semgrep taint-mode config for {lang}. Refine sources/sinks/sanitizers
# then run:  semgrep --config this.yml --metrics off ./src
# Pro/interfile (cross-function): semgrep --config this.yml --pro
rules:
  - id: {lang}-untrusted-to-dangerous-sink
    mode: taint
    message: Untrusted input reaches a dangerous sink without sanitization.
    languages: [{semgrep_lang}]
    severity: ERROR
    metadata:
      cwe: ["CWE-89", "CWE-78", "CWE-502", "CWE-79"]
      references: ["https://semgrep.dev/docs/writing-rules/data-flow/data-flow-overview/"]
    pattern-sources:
{sources}
    pattern-sanitizers:
{sanitizers}
    pattern-sinks:
{sinks}
"""

SEMGREP_LANG = {"py": "python", "js": "javascript,typescript", "php": "php", "java": "java"}


def cmd_config(args):
    L = args.langs[0]
    m = MODEL[L]
    def block(items):
        return "\n".join(f"      - pattern: {p}" for p in items[:6])
    sinks = [p for pats in m["sinks"].values() for p in pats]
    out = SEMGREP_TEMPLATE.format(
        lang=L, semgrep_lang=SEMGREP_LANG[L],
        sources=block(m["sources"]),
        sanitizers=block(m["sanitizers"]),
        sinks=block(sinks),
    )
    sys.stdout.write(out)


def emit(results, args):
    if args.json:
        with open(args.json, "w", encoding="utf-8") as fh:
            json.dump(results, fh, indent=2)
        print(f"[+] {len(results)} records -> {args.json}", file=sys.stderr)
    for r in results:
        if "sink_line" in r:
            flag = "" if r["sanitizer_on_path"] else "  <== NO SANITIZER"
            print(f"[{r['cwe']}] {r['file']}  src:{r['source_line']} -> sink:{r['sink_line']} "
                  f"({r['confidence']}){flag}")
        elif "cwe" in r:
            print(f"[{r['cwe']}] {r['file']}:{r['line']}  {r['code']}")
        else:
            print(f"[SRC] {r['file']}:{r['line']}  {r['code']}")
    if not args.json:
        print(f"\n[+] {len(results)} records", file=sys.stderr)


def main():
    ap = argparse.ArgumentParser(description="Lightweight taint triage tracer")
    ap.add_argument("mode", choices=["sinks", "sources", "trace", "config"])
    ap.add_argument("root", nargs="?", default=".")
    ap.add_argument("--lang", default="py,js,php,java",
                    help="comma list of: py,js,php,java")
    ap.add_argument("--json", help="write findings to JSON file")
    args = ap.parse_args()
    args.langs = [x.strip() for x in args.lang.split(",") if x.strip() in MODEL]
    if not args.langs:
        ap.error("no valid languages in --lang")
    {"sinks": cmd_sinks, "sources": cmd_sources,
     "trace": cmd_trace, "config": cmd_config}[args.mode](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