Skip to content

Commit fa9a222

Browse files
kernelsmithzeroSteiner
authored andcommitted
changes type handling to be fully automatic
1 parent b236187 commit fa9a222

File tree

1 file changed

+44
-17
lines changed

1 file changed

+44
-17
lines changed

plugins/request.rb

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class Plugin::Requests < Msf::Plugin
77
class ConsoleCommandDispatcher
88
include Msf::Ui::Console::CommandDispatcher
99

10+
HELP_REGEX = /^-?-h(?:elp)?$/
11+
1012
def name
1113
'Request'
1214
end
@@ -24,14 +26,21 @@ def types
2426
end
2527

2628
def cmd_request(*args)
27-
# grab and validate the first arg as type, which will affect how the
28-
# remaining args are parsed
29-
type = args.shift
3029
# short circuit the whole deal if they need help
31-
return help if (!type || type =~ /^-?-h(?:elp)?$/)
32-
type.downcase!
33-
opts, opt_parser = parse_args(type, args)
30+
return help if args.length == 0
31+
return help if args.length == 1 && args.first =~ HELP_REGEX
32+
33+
# detect the request type from the uri which must be the last arg given
34+
uri = args.last
35+
if uri && uri =~ /^[A-Za-z]{3,5}:\/\//
36+
type = uri.split('://', 2).first
37+
else
38+
print_error("The last argument must be a valid and supported URI")
39+
return help
40+
end
3441

42+
# parse options
43+
opts, opt_parser = parse_args(args, type)
3544
if opts && opt_parser
3645
# handle any "global" options
3746
if opts[:output_file]
@@ -47,25 +56,34 @@ def cmd_request(*args)
4756
# call the appropriate request handler
4857
self.send(handler_method, opts, opt_parser)
4958
else
59+
# this should be dead code if parse_args is doing it's job correctly
5060
help(opt_parser, "No request handler found for type (#{type.to_s}).")
5161
end
5262
else
53-
help(opt_parser, "No valid options provided for request #{type}")
63+
if types.include? type
64+
help(opt_parser)
65+
else
66+
help
67+
end
5468
end
5569
end
5670

57-
def parse_args(type, args)
71+
def parse_args(args, type = 'http')
5872
type.downcase!
59-
#print_line "type is #{type}"
6073
parse_method = "parse_args_#{type}".to_sym
6174
if self.respond_to?(parse_method)
6275
self.send(parse_method, args, type)
6376
else
64-
print_line('Unrecognized type.')
65-
help
77+
print_error("Unsupported URI type: #{type}")
6678
end
6779
end
6880

81+
# arg parsing for requests of type 'http'
82+
def parse_args_https(args = [], type = 'https')
83+
# just let http do it
84+
parse_args_http(args, type)
85+
end
86+
6987
# arg parsing for requests of type 'http'
7088
def parse_args_http(args = [], type = 'http')
7189
opt_parser = Rex::Parser::Arguments.new(
@@ -113,10 +131,12 @@ def parse_args_http(args = [], type = 'http')
113131
options[:method] ||= 'POST'
114132
when '-G'
115133
options[:method] = 'GET'
116-
when '-h'
117-
return help(opt_parser, "Usage: request #{type} [options] uri")
134+
when HELP_REGEX
135+
#help(opt_parser)
136+
# guard to prevent further option processing & stymie request handling
137+
return [nil, opt_parser]
118138
when '-H'
119-
name, _, value = val.split(':')
139+
name, value = val.split(':', 2)
120140
options[:headers][name] = value.strip
121141
when '-i'
122142
options[:print_headers] = true
@@ -144,13 +164,19 @@ def parse_args_http(args = [], type = 'http')
144164
end
145165
end
146166
unless options[:uri]
147-
return help(opt_parser, "Usage: request #{type} [options] uri")
167+
help(opt_parser)
148168
end
149169
options[:method] ||= 'GET'
150170
options[:uri] = URI(options[:uri])
151171
[options, opt_parser]
152172
end
153173

174+
# handling for requests of type 'https'
175+
def handle_request_https(opts, opt_parser)
176+
# let http do it
177+
handle_request_http(opts, opt_parser)
178+
end
179+
154180
# handling for requests of type 'http'
155181
def handle_request_http(opts, opt_parser)
156182
uri = opts[:uri]
@@ -226,12 +252,13 @@ def output_line(opts, line)
226252
end
227253
end
228254

229-
def help(opt_parser = nil, msg = 'Usage: request type [options]')
255+
def help(opt_parser = nil, msg = 'Usage: request [options] uri')
230256
print_line(msg)
231257
if opt_parser
232258
print_line(opt_parser.usage)
233259
else
234-
print_line("Valid types are: #{types.join(', ')}")
260+
print_line("Supported uri types are: #{types.collect{|t| t + '://'}.join(', ')}")
261+
print_line("To see usage for a specific uri type, use request -h uri")
235262
end
236263
end
237264

0 commit comments

Comments
 (0)