Skip to content

Commit 3493d25

Browse files
committed
Move all this to Rex
1 parent 3fc25a0 commit 3493d25

File tree

4 files changed

+93
-77
lines changed

4 files changed

+93
-77
lines changed

lib/metasploit/framework/login_scanner/http.rb

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -242,35 +242,6 @@ def send_request(opts)
242242
end
243243

244244

245-
# Returns a collection of found hidden inputs
246-
#
247-
# @param res [Rex::Proto::Http::Response] A response object that contains a body
248-
# @return [Array<Hash>] An array, each element represents a form that contains a hash of found hidden inputs
249-
# * 'name' [String] The hidden input's original name. The value is the hidden input's original value.
250-
# @example
251-
# res = send_request('uri'=>'/')
252-
# inputs = get_hidden_inputs(res)
253-
# session_id = inputs[0]['sessionid'] # The first form's 'sessionid' hidden input
254-
def get_hidden_inputs(res)
255-
forms = []
256-
noko = Nokogiri::HTML(res.body)
257-
noko.search("form").each_entry do |form|
258-
found_inputs = {}
259-
form.search("input").each_entry do |input|
260-
input_type = input.attributes['type'] ? input.attributes['type'].value : ''
261-
next if input_type !~ /hidden/i
262-
263-
input_name = input.attributes['name'] ? input.attributes['name'].value : ''
264-
input_value = input.attributes['value'] ? input.attributes['value'].value : ''
265-
found_inputs[input_name] = input_value unless input_name.empty?
266-
end
267-
forms << found_inputs unless found_inputs.empty?
268-
end
269-
270-
forms
271-
end
272-
273-
274245
# Attempt a single login with a single credential against the target.
275246
#
276247
# @param credential [Credential] The credential object to attempt to

lib/rex/proto/http/response.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,34 @@ def get_cookies
8282
return cookies.strip
8383
end
8484

85+
86+
# Returns a collection of found hidden inputs
87+
#
88+
# @return [Array<Hash>] An array, each element represents a form that contains a hash of found hidden inputs
89+
# * 'name' [String] The hidden input's original name. The value is the hidden input's original value.
90+
# @example
91+
# res = send_request_cgi('uri'=>'/')
92+
# inputs = res.get_hidden_inputs
93+
# session_id = inputs[0]['sessionid'] # The first form's 'sessionid' hidden input
94+
def get_hidden_inputs
95+
forms = []
96+
noko = Nokogiri::HTML(self.body)
97+
noko.search("form").each_entry do |form|
98+
found_inputs = {}
99+
form.search("input").each_entry do |input|
100+
input_type = input.attributes['type'] ? input.attributes['type'].value : ''
101+
next if input_type !~ /hidden/i
102+
103+
input_name = input.attributes['name'] ? input.attributes['name'].value : ''
104+
input_value = input.attributes['value'] ? input.attributes['value'].value : ''
105+
found_inputs[input_name] = input_value unless input_name.empty?
106+
end
107+
forms << found_inputs unless found_inputs.empty?
108+
end
109+
110+
forms
111+
end
112+
85113
#
86114
# Updates the various parts of the HTTP response command string.
87115
#

spec/lib/metasploit/framework/login_scanner/http_spec.rb

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,52 +30,4 @@
3030
end
3131
end
3232

33-
describe '#get_hidden_inputs' do
34-
let(:response) do
35-
res = Rex::Proto::Http::Response.new(200, 'OK')
36-
res.body = %Q|
37-
<html>
38-
<head>
39-
<body>
40-
<form action="test.php">
41-
<input name="input_1" type="hidden" value="some_value_1" />
42-
</form>
43-
<form>
44-
<input name="input_0" type="text" value="Not a hidden input" />
45-
<input name="input_1" type="hidden" value="some_value_1" />
46-
<INPUT name="input_2" type="hidden" value="" />
47-
</form>
48-
</body>
49-
</head>
50-
</htm>
51-
|
52-
res
53-
end
54-
55-
56-
context 'when an HTML page contains two forms containing hidden inputs' do
57-
it 'returns an array' do
58-
expect(subject.get_hidden_inputs(response)).to be_kind_of(Array)
59-
end
60-
61-
it 'returns hashes in the array' do
62-
subject.get_hidden_inputs(response).each do |form|
63-
expect(form).to be_kind_of(Hash)
64-
end
65-
end
66-
67-
it 'returns \'some_value_1\' in the input_1 hidden input from the first element' do
68-
expect(subject.get_hidden_inputs(response)[0]['input_1']).to eq('some_value_1')
69-
end
70-
71-
it 'returns two hidden inputs in the second element' do
72-
expect(subject.get_hidden_inputs(response)[1].length).to eq(2)
73-
end
74-
75-
it 'returns an empty string for the input_2 hidden input from the second element' do
76-
expect(subject.get_hidden_inputs(response)[1]['input_2']).to be_empty
77-
end
78-
end
79-
end
80-
8133
end

spec/lib/rex/proto/http/response_spec.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,71 @@ def cookie_sanity_check(meth)
141141
cookies.split(';').map(&:strip)
142142
end
143143

144+
145+
describe '#get_hidden_inputs' do
146+
let(:response) do
147+
res = Rex::Proto::Http::Response.new(200, 'OK')
148+
res.body = %Q|
149+
<html>
150+
<head>
151+
<body>
152+
<form action="test.php">
153+
<input name="input_1" type="hidden" value="some_value_1" />
154+
</form>
155+
<form>
156+
<input name="input_0" type="text" value="Not a hidden input" />
157+
<input name="input_1" type="hidden" value="some_value_1" />
158+
<INPUT name="input_2" type="hidden" value="" />
159+
</form>
160+
</body>
161+
</head>
162+
</htm>
163+
|
164+
res
165+
end
166+
167+
subject do
168+
cli = Rex::Proto::Http::Client.new('127.0.0.1')
169+
cli.connect
170+
req = cli.request_cgi({'uri'=>'/'})
171+
res = cli.send_recv(req)
172+
res
173+
end
174+
175+
before(:each) do
176+
allow_any_instance_of(Rex::Proto::Http::Client).to receive(:request_cgi).with(any_args)
177+
allow_any_instance_of(Rex::Proto::Http::Client).to receive(:send_recv).with(any_args).and_return(response)
178+
allow_any_instance_of(Rex::Proto::Http::Client).to receive(:set_config).with(any_args)
179+
allow_any_instance_of(Rex::Proto::Http::Client).to receive(:close)
180+
allow_any_instance_of(Rex::Proto::Http::Client).to receive(:connect)
181+
end
182+
183+
context 'when an HTML page contains two forms containing hidden inputs' do
184+
it 'returns an array' do
185+
expect(subject.get_hidden_inputs).to be_kind_of(Array)
186+
end
187+
188+
it 'returns hashes in the array' do
189+
subject.get_hidden_inputs.each do |form|
190+
expect(form).to be_kind_of(Hash)
191+
end
192+
end
193+
194+
it 'returns \'some_value_1\' in the input_1 hidden input from the first element' do
195+
expect(subject.get_hidden_inputs[0]['input_1']).to eq('some_value_1')
196+
end
197+
198+
it 'returns two hidden inputs in the second element' do
199+
expect(subject.get_hidden_inputs[1].length).to eq(2)
200+
end
201+
202+
it 'returns an empty string for the input_2 hidden input from the second element' do
203+
expect(subject.get_hidden_inputs[1]['input_2']).to be_empty
204+
end
205+
end
206+
end
207+
208+
144209
context "#get_cookies" do
145210

146211
it 'returns empty string for no Set-Cookies' do

0 commit comments

Comments
 (0)