Path: blob/master/modules/auxiliary/admin/smb/check_dir_file.rb
32578 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[ 'ATT&CK', Mitre::Attack::Technique::T1021_002_SMB_WINDOWS_ADMIN_SHARES ]34],35'License' => MSF_LICENSE,36'Notes' => {37'Stability' => [CRASH_SAFE],38'SideEffects' => [IOC_IN_LOGS],39'Reliability' => []40}41)4243register_options([44OptString.new('SMBSHARE', [true, 'The name of an accessible share on the server', 'C$']),45OptString.new('RPATH', [true, 'The name of the remote file/directory relative to the share'])46])47end4849def check_path(path)50begin51if (fd = simple.open("\\#{path}", 'o')) # mode is open only - do not create/append/write etc52print_good("File FOUND: \\\\#{rhost}\\#{datastore['SMBSHARE']}\\#{path}")53fd.close54end55rescue ::Rex::Proto::SMB::Exceptions::ErrorCode => e56error_name = e.get_error(e.error_code)57rescue ::RubySMB::Error::UnexpectedStatusCode => e58error_name = e.status_code.name59end60if error_name61case error_name62when 'STATUS_FILE_IS_A_DIRECTORY'63print_good("Directory FOUND: \\\\#{rhost}\\#{datastore['SMBSHARE']}\\#{path}")64when 'STATUS_OBJECT_NAME_NOT_FOUND'65vprint_error("Object \\\\#{rhost}\\#{datastore['SMBSHARE']}\\#{path} NOT found!")66when 'STATUS_OBJECT_PATH_NOT_FOUND'67vprint_error("Object PATH \\\\#{rhost}\\#{datastore['SMBSHARE']}\\#{path} NOT found!")68when 'STATUS_ACCESS_DENIED'69vprint_error('Host reports access denied.')70when 'STATUS_BAD_NETWORK_NAME'71vprint_error("Host is NOT connected to #{datastore['SMBDomain']}!")72when 'STATUS_INSUFF_SERVER_RESOURCES'73vprint_error('Host rejected with insufficient resources!')74when 'STATUS_OBJECT_NAME_INVALID'75vprint_error("opening \\#{path} bad filename")76else77raise e78end79end80end8182def run_host(_ip)83vprint_status('Connecting to the server...')8485begin86connect87smb_login8889vprint_status("Mounting the remote share \\\\#{datastore['RHOST']}\\#{datastore['SMBSHARE']}'...")90simple.connect("\\\\#{rhost}\\#{datastore['SMBSHARE']}")91vprint_status("Checking for file/folder #{datastore['RPATH']}...")9293datastore['RPATH'].each_line do |path|94check_path(path.chomp)95end96rescue ::Rex::HostUnreachable97vprint_error('Host offline.')98rescue ::Rex::Proto::SMB::Exceptions::LoginError99print_error('Host login error.')100rescue ::Rex::ConnectionRefused101print_error 'Unable to connect - connection refused'102rescue ::Rex::Proto::SMB::Exceptions::ErrorCode103print_error "Unable to connect to share #{datastore['SMBSHARE']}"104end105end106end107108109