Lecture 5 - Shellcode Worksohp
Setup
- Use your Kali VM in VirtualBox.
- The exercises labelled 1–4 are in
~/Shellcode-Labs/ - Read the
README.txtfor each exercise. - Your task is to write shellcode to meet the requirements in each exercise.
Analogy Shellcoding is like writing a tiny, self-contained note that you sneak into a program's memory, hoping the program will eventually "read it out loud" (execute it) instead of ignoring it as just data.
Workflow
1. Start the Shellcode Server
- In one terminal, run:
./bin/shellcode-server2. Write the Assembly Program
- In another terminal, write an assembly program appropriate to the exercise.
$ nano shellcode.asmsection .data
hello: db 'Hello world!', 10 ; 10 is the linefeed char
section .text
global _start
_start:
mov eax, 4
inc eax3. Assemble and Link the Code
$ nasm -f elf shellcode.asmnasm→ the Netwide Assembler (for x86/x86_64 assembly)-f elf→ output format is ELF (Executable and Linkable Format), typically producing an object file likeshellcode.oshellcode.asm→ your input assembly source file- This assembles your assembly file into an ELF object file, but does not create a runnable program by itself — that's the linker's job.
$ ld -m elf_i386 -o solution shellcode.o- This is the linking step: it takes the object file produced by NASM and turns it into an actual executable binary.
4. Extract the Shellcode Bytes
$ objdump -d solution- The program's machine-code bytes (hex)
- The disassembled assembly instructions next to them
5. Incorporate the Shellcode into a Harness
$ cat harness.py#!/usr/bin/env python
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", 8888))
# Python2
shellcode = "\x90\x90\x90\x90"
print("Sending shellcode (" + str(len(shellcode)) + " bytes)")
# Python3
shellcode = b"\xbb\x39\x05\x00\x00"
print(f"sending shellcode ({len(shellcode)} bytes)")
s.send(shellcode)6. Run and Verify
- Run your Python harness and check back on your shellcode server terminal.
- Have you solved the challenge?
Hint Use
edb or gdb to debug the shellcode server so you can single-step through your shellcode. Set a breakpoint at the call eax instruction in the test() function — that is the line that calls your shellcode.
Handy Extraction Commands
objcopy -O binary --only-section=.text solution text.bin
xxd -p text.bin | tr -d '\n' | sed 's/../\\x&/g'objcopy --only-section=.text— pulls out just the executable code section (no ELF headers/metadata) into a raw binary file.xxd -p— dumps the binary as a plain hex string.tr -d '\n'— strips newlines so it's one continuous string.sed 's/../\\x&/g'— converts each hex byte pair into a\xescaped format, ready to paste directly into a Pythonshellcode = b"..."string.