Windows 취약점 테스트및분석 MS05-002 · PDF file 2008-06-10 · 2006-07-07...
date post
29-Jul-2020Category
Documents
view
0download
0
Embed Size (px)
Transcript of Windows 취약점 테스트및분석 MS05-002 · PDF file 2008-06-10 · 2006-07-07...
anesra@{gmail.com,null2root.org}2006-07-07 1
Windows 취약점 테스트 및 분석
MS05-002
처음 작성 : 2005. 1. 26
최종 수정 : 2005. 1. 28
김 경 곤 (A.K.A. Anesra)
anesra@{gmail.com,null2root.org}2006-07-07 2
목 차
1. 취약점 원인
2. 취약점 분석
3. 공격 코드 테스트
4. Exploit 분석
anesra@{gmail.com,null2root.org}2006-07-07 3
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,
anesra@{gmail.com,null2root.org}2006-07-07 5
1. 취약점 원인
• integer overflow 예제1 /* an integer overflow */
#include
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 = 0x00000000 return 0;
}
결과 : num is 32 bits long num = 0xffffffff num + 1 = 0x0
anesra@{gmail.com,null2root.org}2006-07-07 6
1. 취약점 원인
• integer overflow 예제2 /* exploiting a trivial widthness bug */ #include #include 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 input memcpy(buf, argv[2], i); buf[i] = '\0'; printf("%s\n", buf); // print buf that input argv2 return 0;
}
anesra@{gmail.com,null2root.org}2006-07-07 7
1. 취약점 원인
• integer overflow 예제2 결과 :
[anesra@null2root c0de]$ ./int_bug1 5 hello s = 5 hello
[anesra@null2root c0de]$ ./int_bug1 80 hello Oh no you don't!
[anesra@null2root c0de]$ ./int_bug1 65535 hello Oh no you don't!
[anesra@null2root c0de]$ ./int_bug1 65536 hello s = 0 Segmentation fault
65536-> 0x00010000 -> 0xffff0000 || 2byte. = 0, then always pass
메모리 복사는 65536 bytes 로 core dump
anesra@{gmail.com,null2root.org}2006-07-07 8
1. 취약점 원인
• LoadImage API 함수
HANDLE LoadImage( HINSTANCE hinst, // handle of the instance containing the image LPCTSTR lpszName, // name or identifier of the image UINT uType, // type of image int cxDesired, // desired width int cyDesired, // desired height UINT fuLoad // load flags
);
lpszName은 이미지를 로드하는 핸들. uType은 이미지 타입 – IMAGE_BITMAP, IMAGE_CURSOR, IMAGE_ICON
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
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을 불러와서 가져온다.
anesra@{gmail.com,null2root.org}2006-07-07 11
1. 취약점 원인
• IDA를 통해서 다음 부분을 찾을 수 있다. (Win2K Unpatched user32.dll)
anesra@{gmail.com,null2root.org}2006-07-07 12
1. 취약점 원인
• IDA를 통해서 다음 부분을 찾을 수 있다. (Win2K Unpatched)
anesra@{gmail.com,null2root.org}2006-07-07 13
2. 취약점 분석
anesra@{gmail.com,null2root.org}2006-07-07 14
2. 취약점 분석
• WinXP에서 위와 같은 취약점에 해당하는 코드를 찾기 위해 삽질.
• 패치되지 않은 user32.dll을 IDA에서 불러옴 (winXP)
• 그리고 integer overflow가 일어나는 지점을 찾아보자.
• 의문점1 : integer overflow가 일어나는 지점은 정확히 어디쯤인가..
anesra@{gmail.com,null2root.org}2006-07-07 15
• IDA->Debugger->Process Options에서 실행 프로그램과, 인자를 넣고, F2로 브레이크포 인터 걸고 F9로 프로세스 실행시키면 아래 그림과 같이 나옴
2. 취약점 분석
anesra@{gmail.com,null2root.org}2006-07-07 16
• LoadImageW함수에 F2로 bp걸고 Process Options에서 설정하고 디버거 실행(F9)하고, F8(Step Over)를 계속 해서 찾음
2. 취약점 분석
anesra@{gmail.com,null2root.org}2006-07-07 17
• 또는, bp걸고 F9로 돌리고 나면 bp창에서 pass count를 보고 그 전까지 동작하고 검사 하면 됨 (왕 삽질)
2. 취약점 분석
anesra@{gmail.com,null2root.org}2006-07-07 18
쉘이 뜨기 바로 전에 실행되는 dll 들 – F9로 13번 bp까지 같다가 F8로 들어감.. user32.dll -> comctl32.dll -> SHDOCVW.dll -> BROWSEUI.dll BROWSEUI.dll:71511388 call dword ptr [eax+44h] 여기서 멈춤.. 왜!-_-; 멈출 때 다시 Pause Process 하면 bp가 생김?-_-);;;다시 디버깅 됨
2. 취약점 분석
이 부분에 서 멈춤
REG 가 비 어있음..
anesra@{gmail.com,null2root.org}2006-07-07 19
• 쉘이 뜨기 바로 전에 실행되는 dll 들 – F9로 13번 bp까지 같다가 F7로 들어감..
• ds:RtlEnterCriticalSection 부분이 나옴..- 어디선가 본 중요한 함수 같은데..
2. 취약점 분석
anesra@{gmail.com,null2root.org}2006-07-07 20
• breakpoint conditions 해서 해도 됨.
• breakpoint conditions는 IDC Expression 으로 사용 EAX == EBX+5 Dword(ESP+0x10) == 34
• bp 건 곳에서 Edit bp하여서 Expression 설정할 수 있음
• ebp+arg_C가 szName(파일이름) 이 ‘poc.ani’이면.. Bp가 걸리게 ..
2. 취약점 분석
anesra@{gmail.com,null2root.org}2006-07-07 21
2. 취약점 분석
• Windows XP에서는 코드가 조금 다르다. 실제 패치를 롤백한 다음 취약한 지 테스트 해 보고, 디버거를 걸어서 어느 부분에서 일어나는지 확인
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)
anesra@{gmail.com,null2root.org}2006-07-07 23
3. 공격 코드 테스트
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
anesra@{gmail.com,null2root.org}2006-07-07 25
3. 공격 코드 테스트
• 실행 : HOD-ms05002-ani-expl.exe ms05-002 7777
• .ani 파