# Offensive Shellcode

> Shellcode development: x86/x64 position-independent code, PEB traversal, API hashing, null-byte avoidance, encoders/decoders, staged/stageless payloads, PE to shellcode. Signals: shellcode, position-independent code.

- Skill: `dangeroussong/offensive-shellcode` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx get-skill add dangeroussong/offensive-shellcode`
- Raw SKILL.md: https://api.skillmd.com/api/skills/dangeroussong/offensive-shellcode/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Integrations & APIs
- Author: DangerousSONG (https://skillmd.com/u/dangeroussong)
- Updated: 2026-11-19
- Page: https://skillmd.com/skills/dangeroussong/offensive-shellcode

---


## Shellcode Development Workflow

1. Define concept and target platform (x86/x64, Windows/Linux/macOS)
2. Write assembly using position-independent techniques
3. Extract binary and test in controlled environment
4. Apply null byte avoidance and optimizations
5. Encode/encrypt to evade static detection
6. Package with loader and choose delivery method

---

## Basic Concepts

### Execution Pattern (Allocate-Write-Execute)

Avoid direct `PAGE_EXECUTE_READWRITE` — prefer:
1. Allocate with `PAGE_READWRITE`
2. Write shellcode to allocated region
3. Call `VirtualProtect` to switch to `PAGE_EXECUTE_READ`

```c
char *dest = VirtualAlloc(NULL, 0x1234, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
memcpy(dest, shellcode, 0x1234);
VirtualProtect(dest, 0x1234, PAGE_EXECUTE_READ, &old);
((void(*)())dest)();
```

### Position-Independent Code (PIC) Techniques

| Method | Platform | Notes |
|--------|----------|-------|
| Call/Pop | Windows | Push next addr, pop into register |
| FPU state | Windows | `fstenv` saves instruction pointer |
| SEH | Windows | Exception handler stores EIP |
| GOT | Linux | Global Offset Table |
| VDSO | Linux | Kernel-provided shared object |

---

## Windows API Resolution (PEB Walk)

Identifying `kernel32.dll` without imports:

1. Get `PEB` via `gs:[0x60]` (x64) or `fs:[0x30]` (x86)
2. Walk `PEB->Ldr.InMemoryOrderModuleList` — order: exe → ntdll → kernel32
3. Hash-compare module names to locate `kernel32`
4. Parse the Export Address Table (EAT)
5. Find `GetProcAddress` by name hash, then resolve `LoadLibraryA`
6. Use `LoadLibraryA` to load `WS2_32.dll`, resolve Winsock functions

**WinDbg helpers for debugging PEB walk:**
```bash
dt nt!_TEB -y ProcessEnvironmentBlock @$teb
dt nt!_PEB -y Ldr <peb_addr>
dt -r _PEB_LDR_DATA <ldr_addr>
dt _LDR_DATA_TABLE_ENTRY (<init_flink_addr> - 0x10)
lm m kernel32   # verify base address
r @r8           # check register
```

---

## Shellcode Loaders

### Loader Responsibilities

- Environment verification / keying (sandbox detection)
- Shellcode decryption
- Safe memory allocation and injection
- Ends its duties after injecting

**Recommended languages:** Zig (small, no runtime), Rust (secure), Nim, Go (watch for runtime signatures)

### Allocation Phase

Avoid `RWX` allocations — use two-step:
- `VirtualAllocEx` / `NtAllocateVirtualMemory` — allocate `RW`
- `ZwCreateSection` + `NtMapViewOfSection` — alternative approach
- After writing: `VirtualProtectEx` to switch to `RX`

**Other options:** code caves, stack/heap (with DEP disabled)

### Write Phase

- `WriteProcessMemory` / `NtWriteVirtualMemory`
- `memcpy` to mapped section

**Evasion tips:**
- Prepend shellcode with dummy opcodes
- Split into chunks, write in randomized order
- Add delays between writes

### Execute Phase

Most scrutinized step — EDR checks thread start address against image-backed memory:

| Technique | Notes |
|-----------|-------|
| `CreateRemoteThread` / `ZwCreateThreadEx` | Loud, heavily monitored |
| `NtSetContextThread` | Hijack suspended thread |
| `NtQueueApcThreadEx` | APC injection |
| API trampolines | Overwrite function prologue |
| ThreadlessInject | No new threads created |

**Indirect execution resources:**
- [FlavorTown](https://github.com/Wra7h/FlavorTown)
- [AlternativeShellcodeExec](https://github.com/aahmad097/AlternativeShellcodeExec)
- [ThreadlessInject](https://github.com/epi052/ThreadlessInject)

---

## PE-to-Shellcode Conversion

| Tool | Purpose |
|------|---------|
| [Donut](https://github.com/TheWover/donut) | EXE/DLL → shellcode |
| [sRDI](https://github.com/monoxgas/sRDI) | DLL → position-independent shellcode |
| [Pe2shc](https://github.com/hasherezade/pe_to_shellcode) | PE → shellcode |
| [Amber](https://github.com/EgeBalci/amber) | Reflective PE packer |

**Open-source loaders:**
- [ScareCrow](https://github.com/optiv/ScareCrow)
- [NimPackt-v1](https://github.com/chvancooten/NimPackt-v1)
- [NullGate](https://github.com/specterops/NullGate) — indirect syscalls + junk-write sequencing
- [DripLoader](https://github.com/xuanxuan0/DripLoader) — chunked RW writes + direct syscalls + JMP trampoline
- [ProtectMyTooling](https://github.com/mgeeky/ProtectMyTooling) — chain multiple protections
- Direct-syscall helpers: SysWhispers3, FreshyCalls (now baseline requirements)

---

> 📎 Detailed content (full examples / references / data tables) has been split into `reference.md` in the same directory; use Read to load the relevant section on demand.

