--- name: config-extraction description: Extract embedded configurations from malware samples including C2 URLs, encryption keys, campaign IDs, and other operational parameters using automated and manual techniques. --- # Config Extraction Extract embedded configurations from malware samples to identify C2 infrastructure, encryption keys, campaign identifiers, and other operational parameters. This skill covers identifying config locations, common encoding schemes, extraction frameworks, and building custom extractors. ## Prerequisites - **Python 3.10+** with `struct` and standard library modules - **pefile**: PE file parsing library (`pip install pefile`) - **MACO**: Malware Analysis Configuration Organizer framework (`pip install maco`) - **YARA**: Pattern matching for pre-filtering (optional, used with MACO) - **CAPE community modules**: Family-specific config parsers (optional, clone from GitHub) - **Disassembler** (IDA Pro or Ghidra): For identifying decryption routines in custom extractors ## Steps ### 1. Identify Embedded Configurations Malware configurations are typically stored in one of these locations: | Location | Description | How to Find | |----------|-------------|-------------| | PE resources | Stored as RT_RCDATA or custom resource types | `python3 scripts/config_extractor.py --scan-resources` | | Overlay data | Appended after the PE's last section | `python3 scripts/config_extractor.py --scan-overlay` | | Data sections | Embedded in `.data`, `.rdata`, or custom sections | Check sections with moderate entropy (4.0-7.0) | | Code section | Inline encrypted blobs referenced by code | Trace data references from decryption routines | | Registry/files | Dropped to disk or registry after first execution | Monitor with Procmon or sandbox | | Hardcoded strings | Plaintext or lightly obfuscated in binary | String extraction + pattern matching | Initial scanning: ```bash # Scan a sample for potential config locations python3 scripts/config_extractor.py --scan \ --input sample.exe \ --output scan_results.json # This identifies: # - PE resource entries with high entropy # - Overlay data presence and entropy # - Sections with entropy between 4.0 and 7.5 (likely encoded data) # - XOR-encoded patterns (repeating byte sequences) # - Base64-encoded blobs ``` ### 2. Decode Common Config Formats Malware uses various encoding and encryption schemes for configuration data: **XOR encryption** (most common): ```bash # Scan for single-byte XOR encoded data python3 scripts/config_extractor.py --xor-scan \ --input sample.exe \ --output xor_results.json # Try XOR decryption with a known or brute-forced key python3 scripts/config_extractor.py --xor-decrypt \ --input sample.exe \ --key 0x5A \ --offset 0x4000 \ --length 256 \ --output decrypted_config.bin # Multi-byte XOR key extraction python3 scripts/config_extractor.py --xor-scan \ --input sample.exe \ --key-length 4 \ --output xor_results.json ``` **Base64-encoded configs**: ```bash # Extract and decode base64 blobs from binary python3 scripts/config_extractor.py --base64-scan \ --input sample.exe \ --min-length 20 \ --output base64_results.json ``` **Custom binary structures**: ```bash # Extract raw bytes at a known offset and parse as structured data python3 scripts/config_extractor.py --extract-raw \ --input sample.exe \ --offset 0x8400 \ --length 512 \ --output raw_config.bin # Parse the extracted blob with a format definition python3 scripts/config_extractor.py --parse-struct \ --input raw_config.bin \ --format "4s:magic,H:version,H:flags,256s:c2_url,32s:campaign_id,16s:encryption_key" \ --output parsed_config.json ``` ### 3. Extract from PE Resources Many malware families store configs in PE resource sections: ```bash # List all PE resources python3 scripts/config_extractor.py --list-resources \ --input sample.exe \ --output resources.json # Extract a specific resource by type and ID python3 scripts/config_extractor.py --extract-resource \ --input sample.exe \ --resource-type RCDATA \ --resource-id 101 \ --output extracted_resource.bin # Extract all resources python3 scripts/config_extractor.py --extract-all-resources \ --input sample.exe \ --output-dir ./resources/ ``` Common resource-based config patterns: - **Emotet**: Encrypted RSA public keys and C2 lists in RCDATA - **TrickBot**: Module configs in custom resource types - **Cobalt Strike**: Beacon config in `.data` section or XOR'd resource - **AgentTesla**: SMTP/FTP credentials in encrypted resources ### 4. Extract Overlay Data Data appended after the PE's last section boundary: ```bash # Check for and extract overlay data python3 scripts/config_extractor.py --extract-overlay \ --input sample.exe \ --output overlay.bin # The script calculates the overlay offset from PE headers: # overlay_offset = last_section.PointerToRawData + last_section.SizeOfRawData ``` Overlay patterns: - Encrypted second-stage payloads - JSON or XML configuration blobs - Additional PE files (droppers) - Certificate data (legitimate overlay in signed binaries) ### 5. Use MACO Framework The Malware Analysis Configuration Organizer (MACO) provides standardized config extraction: ```bash # Install MACO pip install maco # Run MACO extractors against a sample maco extract sample.exe --output config.json # List available extractors maco list # Run a specific extractor maco extract sample.exe --extractor emotet --output emotet_config.json # Run with YARA pre-filter (only runs extractors matching YARA rules) maco extract sample.exe --yara-filter --output config.json ``` MACO output follows a standardized schema with fields for: - C2 addresses (IP, domain, URL) - Encryption keys and certificates - Campaign identifiers and bot IDs - Mutex names and install paths - Persistence mechanisms ### 6. Use CAPE Sandbox Config Extractors CAPE sandbox includes config extractors for hundreds of malware families: ```bash # Clone CAPE's config extractors git clone https://github.com/CAPESandbox/community.git cape-community cd cape-community/modules/processing/parsers/ # Run a family-specific parser python3 maldoc/emotet.py sample.bin # CAPE parsers output structured config data including: # - C2 server lists # - RSA/AES keys # - Campaign IDs # - Version information ``` Alternatively, submit to a CAPE sandbox instance: ```bash # Submit sample to CAPE and retrieve config curl -F "file=@sample.exe" http://cape-sandbox:8000/api/tasks/create/file/ # Config appears in the report under "configs" section ``` ### 7. Write Custom Config Extractors For families without existing extractors, write custom extraction scripts: ```python # Example: Extract C2 URLs from a custom malware family # 1. Identify the decryption routine in the disassembly # 2. Locate the encrypted config blob (usually referenced by the decryption code) # 3. Implement the decryption algorithm in Python # 4. Parse the decrypted config structure import struct def decrypt_config(data: bytes, key: bytes) -> bytes: """XOR decrypt with rolling key.""" result = bytearray(len(data)) for i, b in enumerate(data): result[i] = b ^ key[i % len(key)] return bytes(result) def parse_config(decrypted: bytes) -> dict: """Parse decrypted config structure.""" config = {} offset = 0 # Example: 4-byte magic, 2-byte version, then null-terminated C2 strings config["magic"] = decrypted[offset:offset+4] offset += 4 config["version"] = struct.unpack_from("