Skip to content

Commit de49241

Browse files
committed
Land rapid7#3185, regex option validation
2 parents 750b6fc + 4bf6481 commit de49241

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

lib/msf/core/option_container.rb

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class OptBase
2020
# attrs[1] = description (string)
2121
# attrs[2] = default value
2222
# attrs[3] = possible enum values
23+
# attrs[4] = Regex to validate the option
2324
#
2425
def initialize(in_name, attrs = [])
2526
self.name = in_name
@@ -29,6 +30,21 @@ def initialize(in_name, attrs = [])
2930
self.desc = attrs[1]
3031
self.default = attrs[2]
3132
self.enums = [ *(attrs[3]) ].map { |x| x.to_s }
33+
regex_temp = attrs[4] || nil
34+
if regex_temp
35+
# convert to string
36+
regex_temp = regex_temp.to_s if regex_temp.is_a? Regexp
37+
# remove start and end character, they will be added later
38+
regex_temp = regex_temp.sub(/^\^/, '').sub(/\$$/, '')
39+
# Add start and end marker to match the whole regex
40+
regex_temp = "^#{regex_temp}$"
41+
begin
42+
Regexp.compile(regex_temp)
43+
self.regex = regex_temp
44+
rescue RegexpError, TypeError => e
45+
raise("Invalid Regex #{regex_temp}: #{e}")
46+
end
47+
end
3248
end
3349

3450
#
@@ -63,7 +79,18 @@ def type?(in_type)
6379
# If it's required and the value is nil or empty, then it's not valid.
6480
#
6581
def valid?(value)
66-
return (required? and (value == nil or value.to_s.empty?)) ? false : true
82+
if required?
83+
# required variable not set
84+
return false if (value == nil or value.to_s.empty?)
85+
end
86+
if regex
87+
if value.match(regex)
88+
return true
89+
else
90+
return false
91+
end
92+
end
93+
return true
6794
end
6895

6996
#
@@ -125,6 +152,10 @@ def display_value(value)
125152
# The list of potential valid values
126153
#
127154
attr_accessor :enums
155+
#
156+
# A optional regex to validate the option value
157+
#
158+
attr_accessor :regex
128159

129160
protected
130161

modules/auxiliary/fuzzers/ftp/client_ftp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def initialize()
3030
register_options(
3131
[
3232
OptPort.new('SRVPORT', [ true, "The local port to listen on.", 21 ]),
33-
OptString.new('FUZZCMDS', [ true, "Comma separated list of commands to fuzz.", "LIST,NLST,LS,RETR" ]),
33+
OptString.new('FUZZCMDS', [ true, "Comma separated list of commands to fuzz (Uppercase).", "LIST,NLST,LS,RETR", nil, /(?:[A-Z]+,?)+/ ]),
3434
OptInt.new('STARTSIZE', [ true, "Fuzzing string startsize.",1000]),
3535
OptInt.new('ENDSIZE', [ true, "Max Fuzzing string size.",200000]),
3636
OptInt.new('STEPSIZE', [ true, "Increment fuzzing string each attempt.",1000]),

0 commit comments

Comments
 (0)