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/lib/msf/base/sessions/scriptable.rb
Views: 11784
# -*- coding: binary -*-12module Msf::Sessions34module Scriptable56def self.included(base)7base.extend(ClassMethods)8end910module ClassMethods11#12# If the +script+ exists, return its path. Otherwise return nil13#14def find_script_path(script)15# Find the full file path of the specified argument16check_paths =17[18script,19::File.join(script_base, "#{script}"),20::File.join(script_base, "#{script}.rb"),21::File.join(user_script_base, "#{script}"),22::File.join(user_script_base, "#{script}.rb")23]2425full_path = nil2627# Scan all of the path combinations28check_paths.each { |path|29if ::File.file?(path)30full_path = path31break32end33}3435full_path36end37def script_base38::File.join(Msf::Config.script_directory, self.type)39end40def user_script_base41::File.join(Msf::Config.user_script_directory, self.type)42end4344end4546#47# Override48#49def execute_file50raise NotImplementedError51end5253#54# Maps legacy Meterpreter script names to replacement post modules55#56def legacy_script_to_post_module(script_name)57{58'arp_scanner' => 'post/windows/gather/arp_scanner',59'autoroute' => 'post/multi/manage/autoroute',60'checkvm' => 'post/windows/gather/checkvm',61'credcollect' => 'post/windows/gather/credentials/credential_collector',62'domain_list_gen' => 'post/windows/gather/enum_domain_group_users',63'dumplinks' => 'post/windows/gather/dumplinks',64'duplicate' => 'post/windows/manage/multi_meterpreter_inject',65'enum_chrome' => 'post/windows/gather/enum_chrome',66'enum_firefox' => 'post/windows/gather/enum_firefox',67'enum_logged_on_users' => 'post/windows/gather/enum_logged_on_users',68'enum_powershell_env' => 'post/windows/gather/enum_powershell_env',69'enum_putty' => 'post/windows/gather/enum_putty_saved_sessions',70'enum_shares' => 'post/windows/gather/enum_shares',71'file_collector' => 'post/windows/gather/enum_files',72'get_application_list' => 'post/windows/gather/enum_applications',73'get_env' => 'post/multi/gather/env',74'get_filezilla_creds' => 'post/windows/gather/credentials/filezilla_server',75'get_pidgin_creds' => 'post/multi/gather/pidgin_cred',76'get_local_subnets' => 'post/multi/manage/autoroute',77'get_valid_community' => 'post/windows/gather/enum_snmp',78'getcountermeasure' => 'post/windows/manage/killav',79'getgui' => 'post/windows/manage/enable_rdp',80'getvncpw' => 'post/windows/gather/credentials/vnc',81'hashdump' => 'post/windows/gather/smart_hashdump',82'hostsedit' => 'post/windows/manage/inject_host',83'keylogrecorder' => 'post/windows/capture/keylog_recorder',84'killav' => 'post/windows/manage/killav',85'metsvc' => 'exploit/windows/local/persistence',86'migrate' => 'post/windows/manage/migrate',87'panda_2007_pavsrv51' => 'exploit/windows/local/service_permissions',88'pml_driver_config' => 'exploit/windows/local/service_permissions',89'packetrecorder' => 'post/windows/manage/rpcapd_start',90'persistence' => 'exploit/windows/local/persistence',91'prefetchtool' => 'post/windows/gather/enum_prefetch',92'remotewinenum' => 'post/windows/gather/wmic_command',93'schelevator' => 'exploit/windows/local/ms10_092_schelevator',94'screen_unlock' => 'post/windows/escalate/screen_unlock',95'screenspy' => 'post/windows/gather/screen_spy',96'search_dwld' => 'post/windows/gather/enum_files',97'service_permissions_escalate' => 'exploits/windows/local/service_permissions',98'sound_recorder' => 'post/multi/manage/record_mic',99'srt_webdrive_priv' => 'exploit/windows/local/service_permissions',100'uploadexec' => 'post/windows/manage/download_exec',101'webcam' => 'post/windows/manage/webcam',102'wmic' => 'post/windows/gather/wmic_command',103}[script_name]104end105106#107# Executes the supplied script, Post module, or local Exploit module with108# arguments +args+109#110# Will search the script path.111#112def execute_script(script_name, *args)113post_module = legacy_script_to_post_module(script_name)114115if post_module116print_warning("Meterpreter scripts are deprecated. Try #{post_module}.")117print_warning("Example: run #{post_module} OPTION=value [...]")118end119120mod = framework.modules.create(script_name)121if mod122# Don't report module run events here as it will be taken care of123# in +Post.run_simple+124opts = { 'SESSION' => self.sid }125args.each do |arg|126k,v = arg.split("=", 2)127# case doesn't matter in datastore, but it does in hashes, let's normalize128opts[k.downcase] = v129end130if mod.type == "post"131mod.run_simple(132# Run with whatever the default stance is for now. At some133# point in the future, we'll probably want a way to force a134# module to run in the background135#'RunAsJob' => true,136'LocalInput' => self.user_input,137'LocalOutput' => self.user_output,138'Options' => opts139)140elsif mod.type == "exploit"141# well it must be a local, we're not currently supporting anything else142if mod.exploit_type == "local"143# get a copy of the session exploit's datastore if we can144original_exploit_datastore = self.exploit.datastore || {}145copy_of_orig_exploit_datastore = original_exploit_datastore.clone146# convert datastore opts to a hash to normalize casing issues147local_exploit_opts = {}148copy_of_orig_exploit_datastore.each do |k,v|149local_exploit_opts[k.downcase] = v150end151# we don't want to inherit a couple things, like AutoRunScript's152to_neuter = %w{AutoRunScript InitialAutoRunScript LPORT TARGET}153to_neuter.each do |setting|154local_exploit_opts.delete(setting.downcase)155end156157# merge in any opts that were passed in, defaulting all other settings158# to the values from the datastore (of the exploit) that spawned the159# session160local_exploit_opts = local_exploit_opts.merge(opts)161162mod.exploit_simple(163'Payload' => local_exploit_opts.delete('payload'),164'Target' => local_exploit_opts.delete('target'),165'LocalInput' => self.user_input,166'LocalOutput' => self.user_output,167'Options' => local_exploit_opts168)169170end # end if local171end # end if exploit172173else174full_path = self.class.find_script_path(script_name)175176if full_path.nil?177print_error("The specified #{self.type} session script could not be found: #{script_name}")178return179end180181begin182execute_file(full_path, args)183framework.events.on_session_script_run(self, full_path)184rescue StandardError => e185elog("Could not execute #{script_name}: #{e.class} #{e}", error: e)186print_error("Could not execute #{script_name}: #{e.class} #{e}")187end188end189end190191end192193end194195196