Skip to content

Commit b283438

Browse files
committed
Address @jhart-r7's comments
1 parent 7583ed4 commit b283438

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

lib/rex/exploitation/powershell/function.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module Exploitation
66
module Powershell
77

88
class Function
9+
FUNCTION_REGEX = Regexp.new(/\[(\w+\[\])\]\$(\w+)\s?=|\[(\w+)\]\$(\w+)\s?=|\[(\w+\[\])\]\s+?\$(\w+)\s+=|\[(\w+)\]\s+\$(\w+)\s?=/i)
10+
PARAMETER_REGEX = Regexp.new(/param\s+\(|param\(/im)
911
attr_accessor :code, :name, :params
1012

1113
include Output
@@ -32,15 +34,13 @@ def to_s
3234
#
3335
def populate_params
3436
@params = []
35-
start = code.index(/param\s+\(|param\(/im)
37+
start = code.index(PARAMETER_REGEX)
3638
return unless start
3739
# Get start of our block
3840
idx = scan_with_index('(',code[start..-1]).first.last + start
3941
pclause = block_extract(idx)
4042

41-
func_regex = /\[(\w+\[\])\]\$(\w+)\s?=|\[(\w+)\]\$(\w+)\s?=|\[(\w+\[\])\]\s+?\$(\w+)\s+=|\[(\w+)\]\s+\$(\w+)\s?=/i
42-
#func_regex = /\[(\w+\[\])\]\.?\$(\w+)\s?=|\[(\w+)\]\s?\$(\w+)\s?=/i
43-
matches = pclause.scan(func_regex)
43+
matches = pclause.scan(FUNCTION_REGEX)
4444

4545
# Ignore assignment, create params with class and variable names
4646
matches.each do |param|

lib/rex/exploitation/powershell/obfu.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,22 @@ module Exploitation
88
module Powershell
99

1010
module Obfu
11+
MULTI_LINE_COMMENTS_REGEX = Regexp.new(/<#(.*?)#>/m)
12+
SINGLE_LINE_COMMENTS_REGEX = Regexp.new(/^\s*#(?!.*region)(.*$)/i)
13+
WINDOWS_EOL_REGEX = Regexp.new(/[\r\n]+/)
14+
UNIX_EOL_REGEX = Regexp.new(/[\n]+/)
15+
WHITESPACE_REGEX = Regexp.new(/\s+/)
16+
EMPTY_LINE_REGEX = Regexp.new(/^$|^\s+$/)
1117

1218
#
1319
# Remove comments
1420
#
1521
# @return [String] code without comments
1622
def strip_comments
1723
# Multi line
18-
code.gsub!(/<#(.*?)#>/m,'')
24+
code.gsub!(MULTI_LINE_COMMENTS_REGEX,'')
1925
# Single line
20-
code.gsub!(/^\s*#(?!.*region)(.*$)/i,'')
26+
code.gsub!(SINGLE_LINE_COMMENTS_REGEX,'')
2127

2228
code
2329
end
@@ -28,9 +34,9 @@ def strip_comments
2834
# @return [String] code without empty lines
2935
def strip_empty_lines
3036
# Windows EOL
31-
code.gsub!(/[\r\n]+/,"\r\n")
37+
code.gsub!(WINDOWS_EOL_REGEX,"\r\n")
3238
# UNIX EOL
33-
code.gsub!(/[\n]+/,"\n")
39+
code.gsub!(UNIX_EOL_REGEX,"\n")
3440

3541
code
3642
end
@@ -41,7 +47,7 @@ def strip_empty_lines
4147
#
4248
# @return [String] code with whitespace stripped
4349
def strip_whitespace
44-
code.gsub!(/\s+/,' ')
50+
code.gsub!(WHITESPACE_REGEX,' ')
4551

4652
code
4753
end
@@ -84,7 +90,7 @@ def standard_subs(subs = %w{strip_comments strip_whitespace sub_funcs sub_vars}
8490
subs.each do |modifier|
8591
self.send(modifier)
8692
end
87-
code.gsub!(/^$|^\s+$/,'')
93+
code.gsub!(EMPTY_LINE_REGEX,'')
8894

8995
code
9096
end

lib/rex/exploitation/powershell/parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def match_start(char)
125125
end
126126

127127
#
128-
# Extract block of code between inside brackets/parens
128+
# Extract block of code inside brackets/parenthesis
129129
#
130130
# Attempts to match the bracket at idx, handling nesting manually
131131
# Once the balanced matching bracket is found, all script content

lib/rex/exploitation/powershell/psh_methods.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ module PshMethods
1818
# @param target [String] Location to save the file
1919
#
2020
# @return [String] Powershell code to download a file
21-
def self.download(src,target=nil)
21+
def self.download(src, target)
2222
target ||= '$pwd\\' << src.split('/').last
23-
return %Q^(new-object System.Net.WebClient).Downloadfile("#{src}", "#{target}")^
23+
return %Q^(new-object System.Net.WebClient).DownloadFile("#{src}", "#{target}")^
2424
end
2525

2626
#
@@ -53,7 +53,7 @@ def self.secure_string(str)
5353
#
5454
# @return [String] Powershell code to identify the PID of a file
5555
# lock owner
56-
def self.who_locked_file?(filename)
56+
def self.who_locked_file(filename)
5757
return %Q^ Get-Process | foreach{$processVar = $_;$_.Modules | foreach{if($_.FileName -eq "#{filename}"){$processVar.Name + " PID:" + $processVar.id}}}^
5858
end
5959

spec/lib/rex/exploitation/powershell/psh_methods_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
describe "::download" do
99
it 'should return some powershell' do
10-
script = Rex::Exploitation::Powershell::PshMethods.download('a')
10+
script = Rex::Exploitation::Powershell::PshMethods.download('a','b')
1111
script.should be
1212
script.include?('WebClient').should be_true
1313
end
@@ -26,9 +26,9 @@
2626
script.include?('AsPlainText').should be_true
2727
end
2828
end
29-
describe "::who_locked_file?" do
29+
describe "::who_locked_file" do
3030
it 'should return some powershell' do
31-
script = Rex::Exploitation::Powershell::PshMethods.who_locked_file?('a')
31+
script = Rex::Exploitation::Powershell::PshMethods.who_locked_file('a')
3232
script.should be
3333
script.include?('Get-Process').should be_true
3434
end

0 commit comments

Comments
 (0)