Shellcode

The Complete Shellcode & Vulnerability Development Reference Guide

From Assembly to Kernel Drivers: A Comprehensive Journey


Table of Contents

  1. Introduction

  2. Linux Shellcode Development

  3. Windows Shellcode Development

  4. Metasploit Workflow

  5. Kernel Driver Exploitation

  6. Creating Vulnerable Software

  7. Debugging & Analysis Tools

  8. Common Pitfalls & Solutions


Introduction

This guide documents the complete process of shellcode development, from basic assembly to advanced kernel-level exploitation. Whether you're a beginner learning buffer overflows or an experienced researcher developing custom exploits, this reference provides the foundational knowledge and practical examples needed to understand and create working shellcode.

Key Concepts

  • Shellcode: Position-independent machine code that executes a desired payload

  • System Calls: The interface between user space and kernel functionality

  • API Resolution: The process of dynamically finding function addresses in memory

  • Bad Characters: Bytes that would break the shellcode in specific contexts


Linux Shellcode Development

Basic Linux execve("/bin/sh") Shellcode

Assembly Code

nasm

Compilation and Extraction

bash

Resulting Shellcode

text

Testing Shellcode

c

Compile with: gcc -m32 -z execstack -fno-stack-protector test_shellcode.c -o test_shellcode

Null-Free Optimized Version

nasm


Windows Shellcode Development

Key Differences from Linux

  1. No Direct Syscalls: Must call Windows API functions

  2. Dynamic API Resolution: Addresses change due to ASLR

  3. PEB Walking: Technique to find DLL base addresses

  4. API Hashing: Using hashes instead of string names

Windows execve Equivalent: WinExec

High-Level Plan

c

Manual PEB Walking Approach

Conceptual Assembly Structure

nasm

Practical Metasploit Approach

Installing Metasploit (Without Snap)

bash

Generating Windows Shellcode

bash

Flags Explanation:

  • -p windows/exec: Payload that executes a command

  • CMD=calc.exe: Command to execute

  • -f c: Output format (C array)

  • -a x86: Architecture (32-bit)

  • --platform windows: Target platform

  • -b '\x00': Remove null bytes (common bad character)

Example Output

c

Windows Shellcode Testing Program

c

Compile with: gcc test_shellcode.c -o test_shellcode.exe


Metasploit Workflow

Common msfvenom Payloads

Windows Payloads

bash

Linux Payloads

bash

Output Formats

bash


Kernel Driver Exploitation

Why Use Kernel Drivers?

Kernel drivers (Ring 0) have complete system authority, enabling capabilities impossible in user mode:

Common Kernel Driver Techniques

1. Disabling Security Mechanisms

c

2. Token Stealing Payload

The classic privilege escalation technique:

Conceptual Process:

  1. Find EPROCESS structure for System process (PID 4)

  2. Find EPROCESS structure for target process (e.g., cmd.exe)

  3. Copy the security Token from System to target process

  4. Target process now runs as NT AUTHORITY\SYSTEM

3. Direct Memory Manipulation

c

Kernel Driver Challenges

  1. Patchguard (KPP): Must be bypassed on 64-bit Windows

  2. Driver Signature Enforcement (DSE): Requires signed drivers or bypasses

  3. Hypervisor-Protected Code Integrity (HVCI): Additional memory protection

  4. Kernel Patch Protection: Prevents modification of core kernel structures

Practical Considerations

  • Use Vulnerable Driver for testing (like Intel iqvw64e.sys)

  • Test in VM with protections disabled initially

  • Consider DSE bypass techniques for research

  • Always use development/test environments


Creating Vulnerable Software

Building custom vulnerable applications is crucial for understanding exploitation techniques in isolation.

1. Stack-Based Buffer Overflow

Vulnerable Code

c

Compilation Flags:

bash

Exploitation Steps:

  1. Crash with long input: ./vuln $(python -c "print 'A'*100")

  2. Find EIP overwrite offset

  3. Replace with shellcode address

2. Format String Vulnerability

Vulnerable Code

c

Exploitation:

  • Read memory: %x %x %x %x

  • Write to memory: %n format specifier

  • Overwrite GOT entries

3. Heap Overflow / Use-After-Free

Vulnerable Code

c

4. Integer Overflow

Vulnerable Code

c


Advanced Protection Bypasses

Modern Exploitation Challenges

1. Data Execution Prevention (DEP)

Problem: Stack/Heap not executable Solution: Return-Oriented Programming (ROP)

bash

2. Address Space Layout Randomization (ASLR)

Problem: Memory addresses randomized Solution: Information leaks to defeat randomization

bash

3. Stack Canaries

Problem: Stack corruption detection Solution: Leak canary value or bypass check

bash

4. Control Flow Integrity (CFI)

Problem: Indirect call validation Solution: Advanced ROP techniques, JOP/COP

Protection Matrix

Protection

Compiler Flag

Bypass Technique

DEP

-z noexecstack

ROP chains

Stack Canary

-fstack-protector

Canary leakage

ASLR

System setting

Information leak

PIE

-fPIE -pie

Base calculation

RELRO

-z relro

GOT overwrite timing


Debugging & Analysis Tools

Essential Tools Checklist

Linux Tools

bash

Windows Tools

bash

Cross-Platform

bash

GDB Enhanced with GEF/Pwndbg

bash

Practical Debugging Workflow

  1. Crash Reproduction: Trigger the vulnerability

  2. Control Verification: Confirm EIP control

  3. Bad Character Analysis: Test shellcode bytes

  4. Exploit Development: Build working payload

  5. Reliability Testing: Ensure consistent execution


Common Pitfalls & Solutions

Problem: Shellcode Contains Null Bytes

Solution:

  • Use register clearing with xor reg, reg

  • Avoid instructions that generate nulls

  • Use msfvenom -b '\x00' for automatic encoding

Problem: Shellcode Crashes

Solution:

  • Check for bad characters in target context

  • Verify stack alignment

  • Ensure proper memory permissions

Problem: Address Changes Between Runs (ASLR)

Solution:

  • Use information leaks to calculate addresses

  • Implement ROP chains

  • Use relative addressing where possible

Problem: Antivirus Detection

Solution:

  • Custom shellcode encoding

  • Obfuscation techniques

  • Living-off-the-land binaries (LOLBins)

Problem: Modern Protections

Solution:

  • Combine multiple bypass techniques

  • Use information disclosure vulnerabilities

  • Chain multiple vulnerabilities together


Quick Reference Commands

Essential Compilation Flags

bash

Metasploit Quick Reference

bash

Debugging Commands

bash


Conclusion

This comprehensive guide covers the complete journey from basic shellcode development to advanced kernel-level exploitation. The key to mastery is practice - start with simple stack overflows, progress through modern protections, and eventually tackle real-world exploitation scenarios.

Remember: Always conduct security research in appropriate environments with proper authorization. This knowledge is powerful and should be used responsibly for education, defense, and authorized testing purposes.

Last updated

Was this helpful?