Berildocs GitHub

References

Exploit development

An exploit is a controlled transition from a bug to a useful primitive, then from the primitive to an impact. The bug gives you a write, a read, or a state change. The exploit makes it repeatable and steers it to a target.

This guide covers the path after triage. Bug discovery is in vulnerability-research.md. Fuzzing is in fuzzing.md.

Start with the primitive#

PrimitiveTypical sourceNext question
Controlled stack writeStack overflowReturn address, canary, NX
Controlled heap writeHeap overflow, UAFAdjacent object, allocator metadata
Controlled freeDouble free, UAFWhat replaces the object
Arbitrary readOOB read, format string, uninitialized memoryWhat leaks, and where is the pointer
Relative writeOff-by-one, index bugWhat field is one byte away
Type confusionWrong castWhich field is reinterpreted
RaceCheck/use gapCan the window be widened

Write the primitive in one sentence before building the chain. "One byte null write past a heap chunk" is a primitive. "Heap overflow" is a class.

Method#

  1. Reproduce with the smallest input.
  2. Map the mitigations.
  3. Find the crash offset and the controlled bytes.
  4. Find a leak if ASLR or PIE is on.
  5. Build the first control transfer.
  6. Defeat the next mitigation.
  7. Make it reliable.
  8. Write the PoC and the explanation.

Stop as soon as the agreed proof is reached. A shell is nice; a controlled RIP with a clear path is often enough for a report.

Mitigation check#

checksec --file=./target
readelf -hW ./target | grep Type
readelf -lW ./target | grep GNU_STACK
readelf -dW ./target | grep -E "BIND_NOW|RPATH|RUNPATH"

On Windows:

dumpbin /headers target.exe | findstr /i "DynamicBase NXCompat Guard"

Record every result. A bypass that assumes NX off fails on the target.

Stack overflow#

Offset#

gdb -q ./target
(gdb) run < <(python3 -c 'import sys; sys.stdout.buffer.write(b"A"*300)')
(gdb) info registers rip rsp

Use a cyclic pattern:

python3 -c 'from pwn import *; print(cyclic(300).decode())' > pattern.txt
gdb -q ./target -ex 'run < pattern.txt' -ex 'info registers rip'
python3 -c 'from pwn import *; print(cyclic_find(0x6161616c))'

Canary#

A canary sits between the buffer and the return address. Options:

  • Leak it through a format string or an OOB read.
  • Brute force a byte at a time if the process restarts with the same canary and a crash oracle exists.
  • Overwrite a target before the canary: a function pointer, a saved frame in another thread, or an exception handler.

NX#

With NX on, return to existing code.

ret2libc: return to system, with /bin/sh as the argument. Needs a libc base leak.

ret2plt: call a PLT stub for a function whose GOT entry is resolved. The 0x00sec ret2plt article in the artifact catalog walks through the GOT and PLT layout.

ROP: chain gadgets.

ROPgadget --binary ./target --only "pop|ret"
ropper --file ./target --search "pop rdi"

x86_64 argument order: rdi, rsi, rdx, rcx, r8, r9. A pop rdi; ret gadget plus a writable location for /bin/sh is the classic chain.

from pwn import *
elf = ELF("./target"); libc = ELF("./libc.so.6")
rop = ROP(elf)
rop.call(elf.plt["puts"], [elf.got["puts"]])
rop.call(elf.symbols["main"])
p = process("./target")
p.sendlineafter(b"> ", flat({72: rop.chain()}))
leak = u64(p.recvline().strip().ljust(8, b"\x00"))
libc.address = leak - libc.symbols["puts"]
...

PIE and ASLR#

  • Leak a code pointer, then compute the base.
  • Partial overwrite the low 12 bits of a return address when the target is in the same page.
  • Use a non-PIE module loaded at a fixed address.
  • Brute force when the entropy is low and the process survives.

SROP#

A sigreturn frame sets every register at once. It needs a syscall gadget and rax=15.

frame = SigreturnFrame()
frame.rax = constants.SYS_execve
frame.rdi = bin_sh
frame.rsi = 0
frame.rdx = 0
frame.rip = syscall_ret

JOP and CFG#

With Control Flow Guard or CET, indirect calls must land on valid targets. Look for call-preceded gadgets, mov rax, [..]; call rax patterns, and dispatcher functions. Data-only attacks (overwrite a length or a pointer) avoid the problem entirely.

Heap#

Heap exploitation is allocator-specific. Identify the allocator and the version first.

glibc#

  • tcache per size, LIFO, 7 entries by default.
  • Safe-linking XORs the next pointer with the chunk address shifted by 12.
  • Fastbins, unsorted bin, small and large bins each have their own checks.
  • malloc and free hooks were removed in glibc 2.34. Use __free_hook only on older versions.

Classic steps:

  1. Get a leak of the heap or libc.
  2. Create an overlap or a fake chunk.
  3. Poison a tcache or fastbin entry.
  4. Allocate over a target: GOT, _IO_2_1_stdout_, __malloc_hook, a function pointer.
  5. Trigger the call.

Useful tools: pwndbg heap commands, gef, how2heap, malloc_trace.

Windows#

  • Segment Heap on modern Windows, LFH on older.
  • Front-end metadata is the usual target.
  • VirtualProtect or a JIT region turns a write into execution.
  • CFG and XFG constrain indirect calls.
  • CET shadow stack turns a return-address overwrite into a JOP problem.

Use !heap, !address, and !chkimg in WinDbg. Record the Windows build.

UAF#

  1. Free the object.
  2. Allocate a same-size object with controlled bytes.
  3. Trigger the stale reference.
  4. If a vtable is used, point it at a fake table.

With modern C++ the free is often in a callback or a refcount path. Trace the refcount, not the allocation.

Integer bugs#

Look at the size math:

  • size * count overflow before malloc.
  • Signed length compared with an unsigned bound.
  • Truncation from 64 to 32 bits at an IPC boundary.
  • Negative index added to a pointer.

Prove the small allocation and the large copy separately.

Format string#

%p %p %p        leak stack words
%7$p            direct position
%s              read a pointer as string
%n              write the count
%hhn            one byte

pwntools FmtStr automates offset finding and writes. On 64-bit, arguments live in registers, so positional arguments start after the saved registers.

Shellcode#

  • Position independent: call/pop, RIP-relative, no absolute addresses.
  • No null bytes when the copy path stops at a null.
  • Staged payload for small buffers.
  • Windows API resolution via the PEB walk, GetProcAddress, LoadLibraryA.
  • Linux execve("/bin/sh", 0, 0) in 23 bytes for x86_64.
xor rsi, rsi
push rsi
mov rbx, 0x68732f6e69622f
push rbx
push rsp
pop rdi
xor rdx, rdx
push 59
pop rax
syscall

Build with nasm, extract with objcopy, test in a small C loader.

Reliability#

  • Pin the target build and libc.
  • Disable ASLR only for local testing; record both results.
  • Run the exploit 20 times. A 50 percent success rate is not a PoC.
  • Handle null bytes, newline, and length limits in the transport.
  • Add timeouts and a clean exit.
  • Log each stage so a failure is diagnosable.

Kernel#

  • KASLR, SMEP, SMAP, KPTI, CFI, and MTE change the chain.
  • A leak is usually mandatory.
  • Common targets: modprobe_path, core_pattern, cred struct, page tables, native_write_cr4.
  • A data-only attack is often more reliable than a ROP chain.
  • Test in a VM with snapshots. A kernel crash takes the box down.

Modern constraints#

FeatureEffect
CET shadow stackReturn address overwrite alone is not enough
CFG / XFGIndirect call targets are validated
MTEPointer tags catch UAF and OOB on ARM
Heap cookiesMetadata corruption needs a leak
VBS / HVCIKernel code integrity limits patching

When a mitigation blocks the classic path, look for a data-only primitive: a length, a flag, a pointer used later, or a permission field.

PoC layout#

poc/
  README.md        # target, version, hash, expected result
  poc.py           # pwntools or requests
  payload.bin      # if separate
  run.sh           # one command to reproduce
  notes.md         # offsets, leaks, failures

The README states the exact build. A PoC against the wrong version is a rumor.

Report the chain#

  • Bug and root cause.
  • Primitive, in one sentence.
  • Mitigations and how each is bypassed.
  • Reliability over 20 runs.
  • Impact: code execution, sandbox escape, privilege level.
  • Patch or mitigation advice.

A crash plus a controlled register is a valid finding. Say what is proven and what is inferred.

Sources#

  • SnailSploit/Claude-Red exploit development, shellcode, mitigations, Windows boundaries
  • skerkour/black-hat-rust exploit code in Rust
  • 0x00sec PIE and ret2plt series
  • how2heap
  • pwndbg and gef documentation
  • PortSwigger race condition research for web-side primitives