Path: blob/master/modules/auxiliary/admin/smb/check_dir_file.rb
19664 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Auxiliary67# Exploit mixins should be called first8include Msf::Exploit::Remote::SMB::Client9include Msf::Exploit::Remote::SMB::Client::Authenticated10include Msf::Auxiliary::Scanner11include Msf::Auxiliary::Report1213# Aliases for common classes14SIMPLE = Rex::Proto::SMB::SimpleClient15XCEPT = Rex::Proto::SMB::Exceptions16CONST = Rex::Proto::SMB::Constants1718def initialize19super(20'Name' => 'SMB Scanner Check File/Directory Utility',21'Description' => %(22This module is useful when checking an entire network23of SMB hosts for the presence of a known file or directory.24An example would be to scan all systems for the presence of25antivirus or known malware outbreak. Typically you must set26RPATH, SMBUser, SMBDomain and SMBPass to operate correctly.27),28'Author' => [29'aushack',30'j0hn__f'31],32'References' => [33],34'License' => MSF_LICENSE,35'Notes' => {36'Stability' => [CRASH_SAFE],37'SideEffects' => [IOC_IN_LOGS],38'Reliability' => []39}40)4142register_options([43OptString.new('SMBSHARE', [true, 'The name of an accessible share on the server', 'C$']),44OptString.new('RPATH', [true, 'The name of the remote file/directory relative to the share'])45])46end4748def check_path(path)49begin50if (fd = simple.open("\\#{path}", 'o')) # mode is open only - do not create/append/write etc51print_good("File FOUND: \\\\#{rhost}\\#{datastore['SMBSHARE']}\\#{path}")52fd.close53end54rescue ::Rex::Proto::SMB::Exceptions::ErrorCode => e55error_name = e.get_error(e.error_code)56rescue ::RubySMB::Error::UnexpectedStatusCode => e57error_name = e.status_code.name58end59if error_name60case error_name61when 'STATUS_FILE_IS_A_DIRECTORY'62print_good("Directory FOUND: \\\\#{rhost}\\#{datastore['SMBSHARE']}\\#{path}")63when 'STATUS_OBJECT_NAME_NOT_FOUND'64vprint_error("Object \\\\#{rhost}\\#{datastore['SMBSHARE']}\\#{path} NOT found!")65when 'STATUS_OBJECT_PATH_NOT_FOUND'66vprint_error("Object PATH \\\\#{rhost}\\#{datastore['SMBSHARE']}\\#{path} NOT found!")67when 'STATUS_ACCESS_DENIED'68vprint_error('Host reports access denied.')69when 'STATUS_BAD_NETWORK_NAME'70vprint_error("Host is NOT connected to #{datastore['SMBDomain']}!")71when 'STATUS_INSUFF_SERVER_RESOURCES'72vprint_error('Host rejected with insufficient resources!')73when 'STATUS_OBJECT_NAME_INVALID'74vprint_error("opening \\#{path} bad filename")75else76raise e77end78end79end8081def run_host(_ip)82vprint_status('Connecting to the server...')8384begin85connect86smb_login8788vprint_status("Mounting the remote share \\\\#{datastore['RHOST']}\\#{datastore['SMBSHARE']}'...")89simple.connect("\\\\#{rhost}\\#{datastore['SMBSHARE']}")90vprint_status("Checking for file/folder #{datastore['RPATH']}...")9192datastore['RPATH'].each_line do |path|93check_path(path.chomp)94end95rescue ::Rex::HostUnreachable96vprint_error('Host offline.')97rescue ::Rex::Proto::SMB::Exceptions::LoginError98print_error('Host login error.')99rescue ::Rex::ConnectionRefused100print_error 'Unable to connect - connection refused'101rescue ::Rex::Proto::SMB::Exceptions::ErrorCode102print_error "Unable to connect to share #{datastore['SMBSHARE']}"103end104end105end106107108