Blog | G5 Cyber Security

Control Flow Hijacking Without Shellcode

TL;DR

You can redirect a program’s execution without directly injecting and running shellcode by manipulating existing code or function pointers. This guide shows how to achieve control flow hijacking using techniques like Return-Oriented Programming (ROP) and function pointer overwrites.

Understanding the Problem

Sometimes, security measures prevent you from simply inserting and executing your own malicious code (shellcode). However, many programs still have vulnerabilities that allow you to change where the program executes. This is control flow hijacking. You’re not running new code; you’re making the existing code run in a different order or call different functions than intended.

Techniques for Control Flow Hijacking

  1. Return-Oriented Programming (ROP)
  • Function Pointer Overwrites
  • Vtable Overwrites (C++)
  • Practical Steps

    1. Identify a Vulnerability: Find a buffer overflow, format string bug, or other memory corruption issue that allows you to overwrite control data (return address, function pointer, etc.).
    2. Locate Useful Code/Gadgets: If using ROP, find gadgets within the target binary. If using function pointers, identify the relevant pointers and their locations in memory.
    3. Craft Your Payload: Construct your payload to overwrite the control data with your desired addresses.
      • For ROP chains, this involves calculating offsets and arranging gadget addresses correctly on the stack.
      • For function pointer overwrites, it’s a simpler matter of providing the target address.
    4. Exploit: Trigger the vulnerability to execute your payload.

    Example Scenario (Function Pointer Overwrite)

    Let’s say a program has this code:

    void (*func_ptr)(int); // Function pointer
    ...
    func_ptr = some_function;  // Initialized to 'some_function'
    ...
    func_ptr(user_input);      // Call the function pointed to by func_ptr

    If you can overwrite func_ptr with the address of a different function, you can control which function is called. You’d need to find an exploitable vulnerability that allows writing to the memory location where func_ptr is stored.

    Important Considerations

    Exit mobile version