This guide is serves as a tutorial for a challenge part of HackerSchoolCTF hosted at Instituto Superior Técnico and it aims to teach the basics of pwn exploitation.
stack
, uma parte especial da memória.stack
.stack frame
é criado.1int main(){2int foo = 0x1337;3int bar = 0xdeadbeef; 4}
10xdeadbeef <---- bar20x1337 <---- foo 3RANDOM <---- Base pointer RBP40xcafebabe <---- Return address
stack
cresce para cima.stack
A's (em hexadecimal A é 0x41), teríamos o seguinte stack
:10x4141414141414141 <---- bar20x4141414141414141 <---- foo 30x4141414141414141 <---- Base pointer RBP40x4141414141414141 <---- Return address
Segmentation fault
(o que significa que se tentou aceder a dados inexistentes).1Dump of assembler code for function main:2 0x0000000000401126 <+0>: push rbp3 0x0000000000401127 <+1>: mov rbp,rsp4 0x000000000040112a <+4>: sub rsp,0x105 0x000000000040112e <+8>: mov DWORD PTR [rbp-0x8],0x1337 <----rbp-0x86 0x0000000000401135 <+15>: mov DWORD PTR [rbp-0x4],0xdeadbeef7 0x000000000040113c <+22>: lea rax,[rbp-0x8]8 0x0000000000401140 <+26>: mov rdi,rax9 0x0000000000401143 <+29>: mov eax,0x010 0x0000000000401148 <+34>: call 0x401030 <gets@plt>11 0x000000000040114d <+39>: mov eax,0x012 0x0000000000401152 <+44>: leave13 0x0000000000401153 <+45>: ret
gdb
torna mais fácil entender o que está a acontecer (Usando o plugin gef)RIP
) e examinar qual é o seu offset1~/ctf/hsctf/test >> gdb foo2Reading symbols from foo...3gef➤ pattern create 100 4[+] Generating a pattern of 100 bytes (n=4) # Creating a pattern5aaaabaaacaaadaaaeaaafaaagaaahaaaiaaajaaakaaalaaamaaanaaaoaaapaaaqaaaraaasaaataaauaaavaaawaaaxaaayaaa6[+] Saved as '$_gef0'7gef➤ run8Starting program: /home/franfrancisco9/ctf/hsctf/test/foo # Run and send pattern9aaaabaaacaaadaaaeaaafaaagaaahaaaiaaajaaakaaalaaamaaanaaaoaaapaaaqaaaraaasaaataaauaaavaaawaaaxaaayaaa10
11Program received signal SIGSEGV, Segmentation fault. # CRASHED120x0000000000401153 in main () at foo.c:713 0x401148 <main+34> call 0x401030 <gets@plt>14 0x40114d <main+39> mov eax, 0x015 0x401152 <main+44> leave16 → 0x401153 <main+45> ret17[!] Cannot disassemble from $PC18──────────────────────────────────────────────────────────────────────────────────────────────────────────────────── source:foo.c+7 ────19 2 int foo = 0x1337;20 3 int bar = 0xdeadbeef;21 4 gets(&foo);22 5 return 0;23 624 → 7 }25 826────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────27gef➤ info frame28Stack level 0, frame at 0x7fffffffdbb0:29 rip = 0x401153 in main (foo.c:7); saved rip = 0x616161666161616530 source language c.31 Arglist at 0x6161616461616163, args:32 Locals at 0x6161616461616163, Previous frame's sp is 0x7fffffffdbb033 Saved registers:34 rbp at 0x7fffffffdba0, rip at 0x7fffffffdba835gef➤ x/gx 0x7fffffffdba8360x7fffffffdba8: 0x616161666161616537gef➤ pattern search 0x6161616661616165 # Search for pattern that is in RIP now38[+] Searching for '0x6161616661616165'39[+] Found at offset 16 (little-endian search) likely
Agora é aplicar estes conhecimentos ao desafio Borda Fora!
Sendo um desafio Tutorial é permitido mandar mensagem a um admin!