Path: blob/master/modules/auxiliary/admin/smb/delete_file.rb
28102 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::Exploit::Remote::SMB::Client::RemotePaths11include Msf::Auxiliary::Report12include Msf::Auxiliary::Scanner13include Msf::OptionalSession::SMB1415# Aliases for common classes16SIMPLE = Rex::Proto::SMB::SimpleClient17XCEPT = Rex::Proto::SMB::Exceptions18CONST = Rex::Proto::SMB::Constants1920def initialize21super(22'Name' => 'SMB File Delete Utility',23'Description' => %(24This module deletes a file from a target share and path. The usual reason25to use this module is to work around limitations in an existing SMB client that may not26be able to take advantage of pass-the-hash style authentication.27),28'Author' => [29'mubix' # copied from hdm upload_file module30],31'License' => MSF_LICENSE,32'References' => [33[ 'ATT&CK', Mitre::Attack::Technique::T1021_002_SMB_WINDOWS_ADMIN_SHARES ]34],35'Notes' => {36'Stability' => [OS_RESOURCE_LOSS],37'SideEffects' => [],38'Reliability' => []39}40)4142register_options([43OptString.new('SMBSHARE', [true, 'The name of a share on the RHOST', 'C$'])44])45end4647def smb_delete_files48if session49print_status("Using existing session #{session.sid}")50self.simple = session.simple_client51else52vprint_status('Connecting to the server...')53connect54smb_login55end5657vprint_status("Mounting the remote share \\\\#{simple.address}\\#{datastore['SMBSHARE']}'...")58simple.connect("\\\\#{simple.address}\\#{datastore['SMBSHARE']}")5960remote_paths.each do |remote_path|61simple.delete("\\#{remote_path}")6263# If there's no exception raised at this point, we assume the file has been removed.64print_good("Deleted: #{remote_path}")65rescue Rex::Proto::SMB::Exceptions::ErrorCode, RubySMB::Error::RubySMBError => e66elog("Cannot delete #{remote_path}:", error: e)67print_error("Cannot delete #{remote_path}: #{e.message}")68end69end7071def run_host(_ip)72validate_rpaths!7374begin75smb_delete_files76rescue Rex::Proto::SMB::Exceptions::LoginError => e77elog('Unable to login', error: e)78print_error("Unable to login: #{e.message}")79end80end81end828384