Skip to content

meltedinhex/analyst-ai-pack / skills/analyzing-pe-imports-and-exports

analyzing-pe-imports-and-exports

Verified

Analyzes a PE file's import and export tables to infer capability: mapping imported APIs to behaviors (networking, crypto, injection, persistence), spotting dynamic-resolution stubs, and reading exports of malicious DLLs. Activates for requests to analyze PE imports, inspect the IAT/exports, or infer capability from API usage.

FileKindSize
LICENSEother340 B
SKILL.mdskill md3.1 KB
references/api-reference.mdmarkdown1.1 KB
scripts/analyst.pyscript3.0 KB

scripts/analyst.py

View on GitHub
#!/usr/bin/env python3
"""Parse a PE file's imports/exports and bucket APIs into capability categories.

Uses ``pefile`` to enumerate imported DLLs/functions and exports, then maps imported API names to
behavior buckets (networking, injection, crypto, persistence, anti-analysis) and flags
dynamic-resolution indicators. It parses the PE statically and never executes it.
"""
from __future__ import annotations

import argparse
import json
import sys

BEHAVIOR = {
    "networking": ["wsastartup", "connect", "send", "recv", "internetopen", "httpopenrequest",
                   "winhttpopen", "urldownloadtofile"],
    "injection": ["virtualallocex", "writeprocessmemory", "createremotethread",
                  "ntunmapviewofsection", "queueuserapc", "setthreadcontext"],
    "crypto": ["cryptencrypt", "cryptdecrypt", "cryptacquirecontext", "bcryptencrypt",
               "cryptgenkey"],
    "persistence": ["regsetvalue", "regcreatekey", "createservice", "openscmanager",
                    "schedule"],
    "anti_analysis": ["isdebuggerpresent", "checkremotedebuggerpresent",
                      "ntqueryinformationprocess", "outputdebugstring"],
    "dynamic_resolution": ["loadlibrary", "getprocaddress", "ldrloaddll"],
}


def _bucketize(funcs: list[str]) -> dict:
    lowered = [f.lower() for f in funcs]
    out = {}
    for bucket, needles in BEHAVIOR.items():
        hits = sorted({f for f in lowered if any(n in f for n in needles)})
        if hits:
            out[bucket] = hits
    return out


def analyze(path: str) -> dict:
    import pefile  # type: ignore
    pe = pefile.PE(path)
    imports = {}
    all_funcs = []
    if hasattr(pe, "DIRECTORY_ENTRY_IMPORT"):
        for entry in pe.DIRECTORY_ENTRY_IMPORT:
            dll = entry.dll.decode("latin-1") if entry.dll else "?"
            funcs = []
            for imp in entry.imports:
                name = imp.name.decode("latin-1") if imp.name else f"ordinal_{imp.ordinal}"
                funcs.append(name)
            imports[dll] = funcs
            all_funcs.extend(funcs)

    exports = []
    if hasattr(pe, "DIRECTORY_ENTRY_EXPORT"):
        for exp in pe.DIRECTORY_ENTRY_EXPORT.symbols:
            exports.append({"name": exp.name.decode("latin-1") if exp.name else None,
                            "ordinal": exp.ordinal})

    return {
        "imported_dlls": len(imports),
        "imported_functions": len(all_funcs),
        "imports": imports,
        "exports": exports,
        "capabilities": _bucketize(all_funcs),
        "thin_iat": len(all_funcs) < 10,
    }


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(description="Analyze PE imports/exports")
    sub = parser.add_subparsers(dest="cmd", required=True)
    p = sub.add_parser("imports")
    p.add_argument("file")
    args = parser.parse_args(argv)
    try:
        print(json.dumps(analyze(args.file), indent=2))
    except ImportError:
        print("missing dependency: pefile (pip install pefile)", file=sys.stderr)
        return 2
    return 0


if __name__ == "__main__":
    sys.exit(main())
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 322kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 322kUpdated Claude CodeCodexVerifiedInstall