$npx -y skills add meltedinhex/analyst-ai-pack --skill emulating-shellcode-with-unicornEmulates position-independent shellcode in a controlled CPU emulator (Unicorn) to
| 1 | # Emulating Shellcode With Unicorn |
| 2 | |
| 3 | ## When to Use |
| 4 | |
| 5 | - You have a raw shellcode blob (decoder stub, egg hunter, staged loader) and want to observe its |
| 6 | behavior via emulation rather than native execution. |
| 7 | - You want to recover a second stage that the shellcode decrypts/decompresses in memory. |
| 8 | |
| 9 | **Do not use** native execution for this — emulation contains the code. Unsupported API calls must |
| 10 | be stubbed; emulation is not a full Windows environment. |
| 11 | |
| 12 | ## Prerequisites |
| 13 | |
| 14 | - The shellcode blob and the `unicorn` Python package (degrades gracefully if absent — the script |
| 15 | reports that emulation is unavailable and still does static prep). |
| 16 | |
| 17 | ## Safety & Handling |
| 18 | |
| 19 | - Even under emulation, treat the blob as malicious; run on an isolated VM and store stages |
| 20 | password-protected. |
| 21 | |
| 22 | ## Workflow |
| 23 | |
| 24 | ### Step 1: Prepare and validate the blob |
| 25 | |
| 26 | ```bash |
| 27 | python scripts/analyst.py prep shellcode.bin --arch x64 |
| 28 | ``` |
| 29 | |
| 30 | Reports size, detected architecture hints, and whether Unicorn is available. |
| 31 | |
| 32 | ### Step 2: Emulate with an instruction/memory trace |
| 33 | |
| 34 | ```bash |
| 35 | python scripts/analyst.py emulate shellcode.bin --arch x64 --max-insns 200000 |
| 36 | ``` |
| 37 | |
| 38 | Maps the code into emulator memory, sets up a minimal stack, hooks instruction and memory-write |
| 39 | events, and stops on a self-modified region or instruction budget. |
| 40 | |
| 41 | ### Step 3: Dump decoded stages |
| 42 | |
| 43 | If the shellcode writes a new executable region, dump that buffer for follow-on analysis. |
| 44 | |
| 45 | ### Step 4: Document |
| 46 | |
| 47 | Record the entry behavior, decoded stage offset, and any observed (stubbed) API references. |
| 48 | |
| 49 | ## Validation |
| 50 | |
| 51 | - Emulation halts on the instruction budget or a clear decode-complete signal — never hangs. |
| 52 | - A decoded stage, if produced, has a recognizable header (MZ/known opcode prologue). |
| 53 | - Unsupported instructions/APIs are reported, not silently ignored. |
| 54 | |
| 55 | ## Pitfalls |
| 56 | |
| 57 | - Missing API/syscall environment causing early faults — stub the calls the stub needs. |
| 58 | - Wrong architecture/bitness producing immediate invalid-instruction faults. |
| 59 | - Emulating an anti-emulation stub that detects timing/missing APIs. |
| 60 | |
| 61 | ## References |
| 62 | |
| 63 | - See [`references/api-reference.md`](references/api-reference.md) for the emulator harness. |
| 64 | - Unicorn engine and ATT&CK T1620 (linked in frontmatter). |