Skip to content

hypnguyen1209/offensive-claude / skills/malware-analysis

malware-analysis

Unverified

Use when reverse-engineering or detecting malware — static triage + capa/YARA-X, emulation/DBI/.NET unpacking, dynamic/fileless/Volatility 3 memory analysis, C2 config extraction (Cobalt Strike/CAPE), C2 traffic detection (JA4+, beaconing)

scripts/cs_config_extract.py

View on GitHub
#!/usr/bin/env python3
r"""
cs_config_extract.py - Cobalt Strike Beacon configuration extractor.

Decodes the XOR-encoded Beacon config blob (the classic single-byte-XOR TLV table)
from a PE, raw shellcode, or memory dump. Handles both the v3 (0x69) and v4 (0x2e)
default XOR keys and brute-forces the key if neither default matches. Emits a
parsed config: C2 hosts, sleeptime, jitter, watermark, user-agent, spawn-to,
pipe names, kill-date, process-inject settings.

This is a standalone, dependency-light reimplementation of the public method used
by Sentinel-One's CobaltStrikeParser and Didier Stevens' 1768.py. For full coverage
and the newer "runtime configuration" heap parsing (1768.py >= 0.0.20), pull those.

USAGE:
    python3 cs_config_extract.py beacon.bin
    python3 cs_config_extract.py --json dump.mem

DEPENDENCIES: none (stdlib only).
"""
import argparse
import json
import struct
import sys
from pathlib import Path

# Encoded config patterns (the XOR'd start of the TLV table). See CobaltStrikeParser.
PATTERNS = {
    0x69: b"\x69\x68\x69\x68\x69\x6b" + b"\xee\xee" * 1,   # v3-ish marker (heuristic)
    0x2e: b"\x2e\x2f\x2e\x2f\x2e\x2c",                      # v4 marker (heuristic)
}

# Setting id -> (name, type)  (type: 1=short,2=int,3=string/bytes)
SETTINGS = {
    1: ("BeaconType", 1), 2: ("Port", 1), 3: ("SleepTime", 2), 4: ("MaxGetSize", 2),
    5: ("Jitter", 1), 6: ("MaxDNS", 1), 7: ("PublicKey", 3), 8: ("C2Server", 3),
    9: ("UserAgent", 3), 10: ("HttpPostUri", 3), 11: ("Malleable_C2_Instructions", 3),
    12: ("HttpGet_Metadata", 3), 13: ("HttpPost_Metadata", 3), 14: ("SpawnTo", 3),
    15: ("PipeName", 3), 19: ("DNS_Idle", 2), 20: ("DNS_Sleep", 2),
    26: ("HttpGet_Verb", 3), 27: ("HttpPost_Verb", 3), 28: ("HttpPostChunk", 2),
    29: ("Spawnto_x86", 3), 30: ("Spawnto_x64", 3), 31: ("CryptoScheme", 1),
    34: ("Watermark_Hash", 3), 35: ("Watermark", 2), 36: ("bStageCleanup", 1),
    37: ("bCFGCaution", 1), 38: ("KillDate", 2), 39: ("textSectionEnd", 2),
    40: ("ProcInjectStartRWX", 1), 41: ("ProcInjectUseRWX", 1),
    42: ("ProcInjectMinAlloc", 2), 43: ("ProcInjectTransformX86", 3),
    44: ("ProcInjectTransformX64", 3), 46: ("HostHeader", 3),
    51: ("ProcInject_AllocationMethod", 1), 53: ("ProcInject_Execute", 3),
    54: ("ProcInject_AllocationMethod_VirtualAllocEx", 1),
    55: ("ProcInject_StackSize", 2), 57: ("UsesCookies", 1),
    58: ("ProcInject_Stub", 3), 70: ("smbFrameHeader", 3), 71: ("tcpFrameHeader", 3),
    72: ("headersToRemove", 3), 73: ("DNS_beacon", 3), 74: ("DNS_A", 3),
    75: ("DNS_AAAA", 3), 76: ("DNS_TXT", 3), 77: ("DNS_metadata_prepend", 3),
}

BEACON_TYPE = {0: "HTTP", 1: "HybridHTTPDNS", 2: "SMB", 4: "TCP", 8: "HTTPS",
               16: "Bind_TCP", 32: "Bind_Pipe"}


def xor(data, key):
    return bytes(b ^ key for b in data)


def find_config(data):
    """Locate and decode the XOR'd config TLV table. Returns (key, decoded_blob)."""
    candidate_keys = [0x2e, 0x69] + list(range(256))
    for key in candidate_keys:
        dec = xor(data, key)
        # config table starts with setting id 1 (BeaconType): \x00\x01\x00\x01\x00\x02
        marker = b"\x00\x01\x00\x01\x00\x02"
        idx = dec.find(marker)
        if idx != -1:
            return key, dec[idx:idx + 0x1000]
    return None, None


def parse_tlv(blob):
    cfg = {}
    off = 0
    while off + 6 <= len(blob):
        sid, stype, slen = struct.unpack_from(">HHH", blob, off)
        off += 6
        if sid == 0:
            break
        if slen == 0 or off + slen > len(blob):
            break
        raw = blob[off:off + slen]
        off += slen
        name, _ = SETTINGS.get(sid, (f"setting_{sid}", stype))
        if stype == 1 and slen == 2:
            val = struct.unpack(">H", raw)[0]
        elif stype == 2 and slen == 4:
            val = struct.unpack(">I", raw)[0]
        else:
            # strings: strip trailing nulls, keep printable
            txt = raw.split(b"\x00")[0]
            try:
                val = txt.decode("latin1")
            except Exception:
                val = raw.hex()
            if not val and raw:
                val = raw.hex()
        if name == "BeaconType":
            val = BEACON_TYPE.get(val, val)
        cfg[name] = val
    return cfg


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("sample")
    ap.add_argument("--json", action="store_true")
    a = ap.parse_args()

    data = Path(a.sample).read_bytes()
    key, blob = find_config(data)
    if blob is None:
        sys.exit("[-] no Cobalt Strike config found (try 1768.py for runtime/heap configs)")

    cfg = parse_tlv(blob)
    cfg["_xor_key"] = hex(key)
    if a.json:
        print(json.dumps(cfg, indent=2, default=str))
    else:
        print(f"[+] Cobalt Strike config (XOR key {hex(key)})")
        for k in ("BeaconType", "Port", "C2Server", "HostHeader", "UserAgent",
                  "HttpGet_Verb", "HttpPostUri", "SleepTime", "Jitter", "PipeName",
                  "Spawnto_x86", "Spawnto_x64", "Watermark", "KillDate"):
            if k in cfg:
                print(f"    {k:18}: {cfg[k]}")


if __name__ == "__main__":
    main()
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall