Skip to content

Commit dc38212

Browse files
committed
Fix function parsing
1 parent e946046 commit dc38212

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

lib/rex/exploitation/powershell/function.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,26 @@ def populate_params
3737
# Get start of our block
3838
idx = scan_with_index('(',code[start..-1]).first.last + start
3939
pclause = block_extract(idx)
40-
# Keep lines which declare a variable of some class
41-
vars = pclause.split(/\n|;/).select {|e| e =~ /\]\$\w/}
42-
vars.map! {|v| v.split('=',2).first}.map(&:strip)
40+
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)
44+
4345
# Ignore assignment, create params with class and variable names
44-
vars.map {|e| e.split('$')}.each do |klass,name|
45-
@params << Param.new(klass,name)
46+
matches.each do |param|
47+
klass = nil
48+
name = nil
49+
param.each do |value|
50+
if value
51+
if klass
52+
name = value
53+
@params << Param.new(klass,name)
54+
break
55+
else
56+
klass = value
57+
end
58+
end
59+
end
4660
end
4761
end
4862
end

lib/rex/exploitation/powershell/param.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Powershell
88
class Param
99
attr_accessor :klass, :name
1010
def initialize(klass,name)
11-
@klass = klass.strip.gsub(/\[|\]|\s/,'')
11+
@klass = klass.strip
1212
@name = name.strip.gsub(/\s|,/,'')
1313
end
1414

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@
3535
3636
[Parameter( Position = 1 )]
3737
[Type]
38-
$ReturnType = [Void]
38+
$ReturnType = [Void],
39+
40+
[String]$Parpy='hello',
41+
[Integer] $puppy = 1,
42+
43+
[Array[]] $stuff = Array[],
3944
)
4045
4146
$Domain = [AppDomain]::CurrentDomain
@@ -68,7 +73,11 @@
6873
function.code.should eq example_function_with_params
6974
function.to_s.include?("function #{function_name} #{example_function_with_params}").should be_true
7075
function.params.should be_kind_of Array
71-
function.params.length.should be == 2
76+
function.params.length.should be == 5
77+
function.params[0].klass.should eq 'Type[]'
78+
function.params[0].name.should eq 'Parameters'
79+
function.params[1].klass.should eq 'Type'
80+
function.params[1].name.should eq 'ReturnType'
7281
end
7382
end
7483

0 commit comments

Comments
 (0)