Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Powershell Improvements - Auto Arch Detection, Size Reduction #2557

Closed
wants to merge 130 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
130 commits
Select commit Hold shift + click to select a range
595e538
post/local_admin_search_enum~Regex fails,module 2
Jul 2, 2013
4554cc6
Import Powershell libs and modules (again)
Jul 4, 2013
cd14569
Revert "post/local_admin_search_enum~Regex fails,module 2"
Jul 20, 2013
dc15c5b
Merge branch 'master' into powershell_import
Jul 20, 2013
eb18537
Trim to core requirements
Jul 20, 2013
9d93891
Import old powershell post lib from master
Jul 20, 2013
4df3b02
replace lib/msf/core/exploit/powershell.rb, thanks @Meatballs1
Jul 20, 2013
b3fab9a
Fix git branch mauling - reintroduce psexec_psh
Jul 28, 2013
176de5a
Selective psexec_psh merge.
Meatballs1 Jul 29, 2013
eb3f83f
Merge remote-tracking branch 'remotes/semperv/powershell_import' into…
Meatballs1 Jul 29, 2013
59a2c7e
Merge Upstream Exploit::Powershell
Meatballs1 Jul 29, 2013
e1cfe7c
Update datastore changes
Meatballs1 Jul 29, 2013
d2e57ea
Merge remote-tracking branch 'origin/psh_merge' into psh_merge
Meatballs1 Jul 29, 2013
976c058
Merge pull request #5 from Meatballs1/psh_merge
sempervictus Jul 30, 2013
7c46e95
Merge branch 'master' of https://github.com/rapid7/metasploit-framewo…
Jul 31, 2013
2c850d8
Merge branch 'powershell_import' of github.com:sempervictus/metasploi…
Jul 31, 2013
b241b5a
Apply comments
Meatballs1 Sep 13, 2013
243d3d6
Apply comments
Meatballs1 Sep 13, 2013
aa4ad2b
Change to ' and remove "
Meatballs1 Sep 13, 2013
9ade4cb
Refactor
Meatballs1 Sep 13, 2013
df19d69
Merge branch 'master' of github.com:rapid7/metasploit-framework into …
Meatballs1 Sep 13, 2013
2b64453
Merge
Meatballs1 Sep 13, 2013
5a5b67b
Get lastest exe
Meatballs1 Sep 13, 2013
b4d1fd6
Fixup rex text
Meatballs1 Sep 13, 2013
60328d5
Bypass no profile and hidden by default
Meatballs1 Sep 13, 2013
d6f2da6
Fix web delivery
Meatballs1 Sep 13, 2013
9aca98a
Dont need to bypass
Meatballs1 Sep 17, 2013
5add142
Choose smallest smallest
Meatballs1 Sep 20, 2013
971d0b7
Generate args
Meatballs1 Sep 27, 2013
4fc8bb2
Auto arch detection
Meatballs1 Oct 21, 2013
20b7660
Merge remote-tracking branch 'upstream/master' into pr2075
Meatballs1 Nov 22, 2013
3cbf768
Small size reductions
Meatballs1 Nov 22, 2013
c5007f6
Retab psexec_psh
Meatballs1 Nov 22, 2013
9835649
Update hwnd_broadcast to use generated powershell command line.
Meatballs1 Nov 22, 2013
622a1dc
Update wmi to use generated powershell command line
Meatballs1 Nov 22, 2013
ec36ceb
Update cmd_psh_payloads to send the architecture.
Meatballs1 Nov 22, 2013
c194fdc
Fixup WMI
Meatballs1 Nov 23, 2013
1c60373
Reinstate %COMSPEC%
Meatballs1 Nov 23, 2013
259d5a2
Backout Set-Variable as it is 3.0 only
Meatballs1 Nov 23, 2013
6c83109
Really fix wmi
Meatballs1 Nov 23, 2013
cd68b10
Broadcast needs a decent WfsDelay.
Meatballs1 Nov 23, 2013
b252e78
Merge remote-tracking branch 'upstream/master' into pr2075
Meatballs1 Dec 16, 2013
435cc9b
Add single quote encapsulation
Meatballs1 Dec 16, 2013
09c4835
Retab rex powershell
Meatballs1 Feb 8, 2014
b10df54
Dont need to encode the compress payload
Meatballs1 Feb 8, 2014
c76862b
Reduce payload size
Meatballs1 Feb 8, 2014
c37cb50
Merge remote-tracking branch 'upstream/master' into pr2075
Meatballs1 Feb 8, 2014
ad308ef
Really minimize commandline size
Meatballs1 Feb 8, 2014
f398c98
Include option to ensure payload is fully encoded
Meatballs1 Feb 8, 2014
02f1ff2
Add option to encode inner payload
Meatballs1 Feb 9, 2014
f1959f5
Fixup WMI
Meatballs1 Feb 9, 2014
1f9b452
Dont tidy up template yet
Meatballs1 Feb 9, 2014
a00481b
Auto target psexec/psh_web
Meatballs1 Feb 9, 2014
0379dc1
Raise exception on known issues
Meatballs1 Feb 9, 2014
77dda5d
Give option to remove badchars
Meatballs1 Feb 9, 2014
151e45d
Better exception descriptions
Meatballs1 Feb 9, 2014
2cea90f
Working remoting
Meatballs1 Feb 9, 2014
c76341c
Dont dsub Invoke-Command etc...
Meatballs1 Feb 9, 2014
6972b8e
Merge branch 'pr2075' into powershell_remoting
Meatballs1 Feb 9, 2014
1c169e2
Uniq results
Meatballs1 Feb 9, 2014
038aae5
Run as jobs
Meatballs1 Feb 9, 2014
b79bb47
Go for background approach
Meatballs1 Feb 9, 2014
aa93299
Sleep instead of noexit
Meatballs1 Feb 9, 2014
a4b451d
Ensure we start in a new conhost/process
Meatballs1 Feb 9, 2014
b453362
Merge remote-tracking branch 'upstream/pr/2966' into integrate_with_m…
Feb 12, 2014
29bf296
import rex powershell
Feb 12, 2014
0056c26
import msf exploit
Feb 13, 2014
b8b36ef
Merge remote-tracking branch 'upstream/master' into pr2075
Meatballs1 Feb 14, 2014
c9a2135
Merge in semperv
Meatballs1 Mar 2, 2014
1ca690e
Do some rspec
Meatballs1 Mar 2, 2014
0956ae5
Fix payload selection
Meatballs1 Mar 2, 2014
2885ebc
Merge remote-tracking branch 'upstream/master' into pr2075
Meatballs1 Mar 2, 2014
2acd0a1
Reinstance encode_code
Meatballs1 Mar 2, 2014
8dee9b2
Reinstate to_byte_array
Meatballs1 Mar 2, 2014
38d8df4
Merge remote-tracking branch 'upstream/master' into pr2075
Meatballs1 Apr 15, 2014
5c3289b
merge fix
Apr 18, 2014
9f05760
Merge with Meatballs' initial changes
Apr 18, 2014
270b4b9
Catch first arg with shorten
Meatballs1 Apr 19, 2014
c936dc9
Shorten compression
Meatballs1 Apr 19, 2014
0f942d8
Still :shorten command args
Meatballs1 Apr 19, 2014
00234ae
Remove powershell remoting
Meatballs1 Apr 19, 2014
3019cb9
Update cmd_upgrade module
Meatballs1 Apr 19, 2014
5d9bc71
Update hp_dataprotector
Meatballs1 Apr 19, 2014
ad414b7
Merge pull request #47 from sempervictus/merge_psh_0414
Meatballs1 Apr 22, 2014
67f4407
Merge remote-tracking branch 'upstream/master' into pr2075
Meatballs1 Apr 19, 2014
d73854f
Fix wmi and add automatic target
Meatballs1 Apr 22, 2014
71b43d3
Dont need to specify ASCII mode
Meatballs1 Apr 22, 2014
cec12ed
Use enum integer values
Meatballs1 Apr 22, 2014
354311d
No need to out-null if no windows is shown
Meatballs1 Apr 22, 2014
4c66e86
Dont add extra space in args
Meatballs1 Apr 22, 2014
86cfecd
Shave some chars off compression code
Meatballs1 Apr 22, 2014
88fe619
Yarddoc exploit::powershell
Meatballs1 Apr 22, 2014
647936e
Add more yarddoc to Rex::Exploitation::Powershell
Meatballs1 Apr 23, 2014
dd38a81
Fix a @parma
Meatballs1 Apr 23, 2014
d2e8e07
Fix old powershell generation
Meatballs1 Apr 23, 2014
e774411
Revert Enum removal
Meatballs1 Apr 23, 2014
01bfad3
Correct datastore values
Meatballs1 Apr 23, 2014
1347649
Remove unused EOFs
Meatballs1 Apr 23, 2014
c4cfa42
More specs
Meatballs1 Apr 23, 2014
11526b5
Boolean datastore options should always be present
Meatballs1 Apr 23, 2014
32fa874
Fix up decompress
Meatballs1 Apr 23, 2014
58c3bf0
Further speccage
Meatballs1 Apr 23, 2014
61b8fb7
Remove puts
Meatballs1 Apr 23, 2014
0137fdb
Prepend sleep should be an int
Meatballs1 Apr 23, 2014
72a2849
Better specs
Meatballs1 Apr 23, 2014
2061840
Move methods and rename file so it is run by rspec
Meatballs1 Apr 25, 2014
19dd21a
Remove duplicate methods
Meatballs1 Apr 25, 2014
5b9ec72
Remove read_script spec
Meatballs1 Apr 25, 2014
ae574be
Correct spec
Meatballs1 Apr 25, 2014
d85e4b1
Error if encode_inner and encode_final
Meatballs1 Apr 25, 2014
3f5cc13
Better eof test
Meatballs1 Apr 25, 2014
318ae46
Remove puts
Meatballs1 Apr 26, 2014
be10c8e
Split Rex::Exploitation::Powershell::* into individual files
Meatballs1 Apr 26, 2014
98d2b22
Unnecessary return
Meatballs1 Apr 26, 2014
8031e50
Make Exploitation::Powershell testable
Meatballs1 Apr 26, 2014
3ae8c3f
Basic specs
Meatballs1 Apr 25, 2014
b860cec
Function spec (doesnt pass)
Meatballs1 Apr 25, 2014
399928c
Remove unnecessary requires
Meatballs1 May 5, 2014
589d235
Simple param spec
Meatballs1 May 5, 2014
162b6a8
Add output spec
Meatballs1 May 5, 2014
6ab8502
More spec
Meatballs1 May 5, 2014
0177e51
Finish obfu specs and use rig
Meatballs1 May 5, 2014
0b886db
Script specs and remove unknown method
Meatballs1 May 5, 2014
e946046
Add methods spec
Meatballs1 May 5, 2014
dc38212
Fix function parsing
Meatballs1 May 5, 2014
7583ed4
Merge remote-tracking branch 'upstream/master' into pr2075
Meatballs1 Jul 16, 2014
b283438
Address @jhart-r7's comments
Meatballs1 Jul 20, 2014
474ee81
Merge remote-tracking branch 'upstream/master' into pr2075
Meatballs1 Jul 20, 2014
5f05336
Cheat/Rubycop all the things
Meatballs1 Jul 20, 2014
b0a596b
Update newer modules
Meatballs1 Jul 20, 2014
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
446 changes: 324 additions & 122 deletions lib/msf/core/exploit/powershell.rb

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions lib/msf/util/exe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1084,17 +1084,18 @@ def self.to_mem_aspx(framework, code, exeopts={})
end

def self.to_win32pe_psh_net(framework, code, opts={})
hash_sub = {}
hash_sub[:var_code] = Rex::Text.rand_text_alpha(rand(8)+8)
hash_sub[:var_kernel32] = Rex::Text.rand_text_alpha(rand(8)+8)
hash_sub[:var_baseaddr] = Rex::Text.rand_text_alpha(rand(8)+8)
hash_sub[:var_threadHandle] = Rex::Text.rand_text_alpha(rand(8)+8)
hash_sub[:var_output] = Rex::Text.rand_text_alpha(rand(8)+8)
hash_sub[:var_temp] = Rex::Text.rand_text_alpha(rand(8)+8)
hash_sub[:var_codeProvider] = Rex::Text.rand_text_alpha(rand(8)+8)
hash_sub[:var_compileParams] = Rex::Text.rand_text_alpha(rand(8)+8)
hash_sub[:var_syscode] = Rex::Text.rand_text_alpha(rand(8)+8)
rig = Rex::RandomIdentifierGenerator.new()
rig.init_var(:var_code)
rig.init_var(:var_kernel32)
rig.init_var(:var_baseaddr)
rig.init_var(:var_threadHandle)
rig.init_var(:var_output)
rig.init_var(:var_codeProvider)
rig.init_var(:var_compileParams)
rig.init_var(:var_syscode)
rig.init_var(:var_temp)

hash_sub = rig.to_h
hash_sub[:b64shellcode] = Rex::Text.encode_base64(code)

return read_replace_script_template("to_mem_dotnet.ps1.template", hash_sub).gsub(/(?<!\r)\n/, "\r\n")
Expand Down
62 changes: 62 additions & 0 deletions lib/rex/exploitation/powershell.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# -*- coding: binary -*-

require 'rex/exploitation/powershell/output'
require 'rex/exploitation/powershell/parser'
require 'rex/exploitation/powershell/obfu'
require 'rex/exploitation/powershell/param'
require 'rex/exploitation/powershell/function'
require 'rex/exploitation/powershell/script'
require 'rex/exploitation/powershell/psh_methods'

module Rex
module Exploitation
module Powershell
#
# Reads script into a PowershellScript
#
# @param script_path [String] Path to the Script File
#
# @return [Script] Powershell Script object
def self.read_script(script_path)
Rex::Exploitation::Powershell::Script.new(script_path)
end

#
# Insert substitutions into the powershell script
# If script is a path to a file then read the file
# otherwise treat it as the contents of a file
#
# @param script [String] Script file or path to script
# @param subs [Array] Substitutions to insert
#
# @return [String] Modified script file
def self.make_subs(script, subs)
if ::File.file?(script)
script = ::File.read(script)
end

subs.each do |set|
script.gsub!(set[0], set[1])
end

script
end

#
# Return an array of substitutions for use in make_subs
#
# @param subs [String] A ; seperated list of substitutions
#
# @return [Array] An array of substitutions
def self.process_subs(subs)
return [] if subs.nil? or subs.empty?
new_subs = []
subs.split(';').each do |set|
new_subs << set.split(',', 2)
end

new_subs
end
end
end
end
63 changes: 63 additions & 0 deletions lib/rex/exploitation/powershell/function.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- coding: binary -*-

module Rex
module Exploitation
module Powershell
class Function
FUNCTION_REGEX = Regexp.new(/\[(\w+\[\])\]\$(\w+)\s?=|\[(\w+)\]\$(\w+)\s?=|\[(\w+\[\])\]\s+?\$(\w+)\s+=|\[(\w+)\]\s+\$(\w+)\s?=/i)
PARAMETER_REGEX = Regexp.new(/param\s+\(|param\(/im)
attr_accessor :code, :name, :params

include Output
include Parser
include Obfu

def initialize(name, code)
@name = name
@code = code
populate_params
end

#
# To String
#
# @return [String] Powershell function
def to_s
"function #{name} #{code}"
end

#
# Identify the parameters from the code and
# store as Param in @params
#
def populate_params
@params = []
start = code.index(PARAMETER_REGEX)
return unless start
# Get start of our block
idx = scan_with_index('(', code[start..-1]).first.last + start
pclause = block_extract(idx)

matches = pclause.scan(FUNCTION_REGEX)

# Ignore assignment, create params with class and variable names
matches.each do |param|
klass = nil
name = nil
param.each do |value|
if value
if klass
name = value
@params << Param.new(klass, name)
break
else
klass = value
end
end
end
end
end
end
end
end
end
98 changes: 98 additions & 0 deletions lib/rex/exploitation/powershell/obfu.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# -*- coding: binary -*-

require 'rex/text'

module Rex
module Exploitation
module Powershell
module Obfu
MULTI_LINE_COMMENTS_REGEX = Regexp.new(/<#(.*?)#>/m)
SINGLE_LINE_COMMENTS_REGEX = Regexp.new(/^\s*#(?!.*region)(.*$)/i)
WINDOWS_EOL_REGEX = Regexp.new(/[\r\n]+/)
UNIX_EOL_REGEX = Regexp.new(/[\n]+/)
WHITESPACE_REGEX = Regexp.new(/\s+/)
EMPTY_LINE_REGEX = Regexp.new(/^$|^\s+$/)

#
# Remove comments
#
# @return [String] code without comments
def strip_comments
# Multi line
code.gsub!(MULTI_LINE_COMMENTS_REGEX, '')
# Single line
code.gsub!(SINGLE_LINE_COMMENTS_REGEX, '')

code
end

#
# Remove empty lines
#
# @return [String] code without empty lines
def strip_empty_lines
# Windows EOL
code.gsub!(WINDOWS_EOL_REGEX, "\r\n")
# UNIX EOL
code.gsub!(UNIX_EOL_REGEX, "\n")

code
end

#
# Remove whitespace
# This can break some codes using inline .NET
#
# @return [String] code with whitespace stripped
def strip_whitespace
code.gsub!(WHITESPACE_REGEX, ' ')

code
end

#
# Identify variables and replace them
#
# @return [String] code with variable names replaced with unique values
def sub_vars
# Get list of variables, remove reserved
get_var_names.each do |var, _sub|
code.gsub!(var, "$#{@rig.init_var(var)}")
end

code
end

#
# Identify function names and replace them
#
# @return [String] code with function names replaced with unique
# values
def sub_funcs
# Find out function names, make map
get_func_names.each do |var, _sub|
code.gsub!(var, @rig.init_var(var))
end

code
end

#
# Perform standard substitutions
#
# @return [String] code with standard substitution methods applied
def standard_subs(subs = %w(strip_comments strip_whitespace sub_funcs sub_vars))
# Save us the trouble of breaking injected .NET and such
subs.delete('strip_whitespace') unless get_string_literals.empty?
# Run selected modifiers
subs.each do |modifier|
send(modifier)
end
code.gsub!(EMPTY_LINE_REGEX, '')

code
end
end # Obfu
end
end
end
Loading