@@ -7,6 +7,8 @@ class Plugin::Requests < Msf::Plugin
7
7
class ConsoleCommandDispatcher
8
8
include Msf ::Ui ::Console ::CommandDispatcher
9
9
10
+ HELP_REGEX = /^-?-h(?:elp)?$/
11
+
10
12
def name
11
13
'Request'
12
14
end
@@ -24,14 +26,21 @@ def types
24
26
end
25
27
26
28
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
30
29
# 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
34
41
42
+ # parse options
43
+ opts , opt_parser = parse_args ( args , type )
35
44
if opts && opt_parser
36
45
# handle any "global" options
37
46
if opts [ :output_file ]
@@ -47,25 +56,34 @@ def cmd_request(*args)
47
56
# call the appropriate request handler
48
57
self . send ( handler_method , opts , opt_parser )
49
58
else
59
+ # this should be dead code if parse_args is doing it's job correctly
50
60
help ( opt_parser , "No request handler found for type (#{ type . to_s } )." )
51
61
end
52
62
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
54
68
end
55
69
end
56
70
57
- def parse_args ( type , args )
71
+ def parse_args ( args , type = 'http' )
58
72
type . downcase!
59
- #print_line "type is #{type}"
60
73
parse_method = "parse_args_#{ type } " . to_sym
61
74
if self . respond_to? ( parse_method )
62
75
self . send ( parse_method , args , type )
63
76
else
64
- print_line ( 'Unrecognized type.' )
65
- help
77
+ print_error ( "Unsupported URI type: #{ type } " )
66
78
end
67
79
end
68
80
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
+
69
87
# arg parsing for requests of type 'http'
70
88
def parse_args_http ( args = [ ] , type = 'http' )
71
89
opt_parser = Rex ::Parser ::Arguments . new (
@@ -113,10 +131,12 @@ def parse_args_http(args = [], type = 'http')
113
131
options [ :method ] ||= 'POST'
114
132
when '-G'
115
133
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 ]
118
138
when '-H'
119
- name , _ , value = val . split ( ':' )
139
+ name , value = val . split ( ':' , 2 )
120
140
options [ :headers ] [ name ] = value . strip
121
141
when '-i'
122
142
options [ :print_headers ] = true
@@ -144,13 +164,19 @@ def parse_args_http(args = [], type = 'http')
144
164
end
145
165
end
146
166
unless options [ :uri ]
147
- return help ( opt_parser , "Usage: request #{ type } [options] uri" )
167
+ help ( opt_parser )
148
168
end
149
169
options [ :method ] ||= 'GET'
150
170
options [ :uri ] = URI ( options [ :uri ] )
151
171
[ options , opt_parser ]
152
172
end
153
173
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
+
154
180
# handling for requests of type 'http'
155
181
def handle_request_http ( opts , opt_parser )
156
182
uri = opts [ :uri ]
@@ -226,12 +252,13 @@ def output_line(opts, line)
226
252
end
227
253
end
228
254
229
- def help ( opt_parser = nil , msg = 'Usage: request type [options]' )
255
+ def help ( opt_parser = nil , msg = 'Usage: request [options] uri ' )
230
256
print_line ( msg )
231
257
if opt_parser
232
258
print_line ( opt_parser . usage )
233
259
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" )
235
262
end
236
263
end
237
264
0 commit comments