Lecture 5 - Shellcode Worksohp

Updated 14 Oct 2026

Setup

  • Use your Kali VM in VirtualBox.
  • The exercises labelled 1–4 are in ~/Shellcode-Labs/
  • Read the README.txt for 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-server

2. Write the Assembly Program

  • In another terminal, write an assembly program appropriate to the exercise.
$ nano shellcode.asm
section .data
    hello: db 'Hello world!', 10 ; 10 is the linefeed char
section .text
    global _start
_start:
    mov eax, 4
    inc eax
$ nasm -f elf shellcode.asm
  • nasm → the Netwide Assembler (for x86/x86_64 assembly)
  • -f elf → output format is ELF (Executable and Linkable Format), typically producing an object file like shellcode.o
  • shellcode.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 \x escaped format, ready to paste directly into a Python shellcode = b"..." string.