Skip to content

Commit 4e80e1c

Browse files
committed
Clean up pull request code
1 parent 7031449 commit 4e80e1c

File tree

1 file changed

+71
-40
lines changed

1 file changed

+71
-40
lines changed

modules/auxiliary/gather/f5_bigip_cookie_disclosure.rb

Lines changed: 71 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ class Metasploit3 < Msf::Auxiliary
1212

1313
def initialize(info = {})
1414
super(update_info(info,
15-
'Name' => 'F5 Bigip Backend IP/PORT Cookie Disclosure.',
15+
'Name' => 'F5 BigIP Backend Cookie Disclosure',
1616
'Description' => %q{
17-
This module identify F5 BigIP SLB and decode sticky cookies which leak
18-
backend IP and port.
17+
This module identify F5 BigIP Load Balancers and leaks backends
18+
information through cookies.
1919
},
2020
'Author' => [ 'Thanat0s <thanspam[at]trollprod.org>' ],
2121
'References' =>
@@ -29,66 +29,97 @@ def initialize(info = {})
2929
register_options(
3030
[
3131
OptString.new('TARGETURI', [true, 'The URI path to test', '/']),
32-
OptInt.new('RETRY', [true, 'Number of requests to try to find backends', 10])
32+
OptInt.new('REQUESTS', [true, 'Number of requests to send to disclose back', 10])
3333
], self.class)
3434
end
3535

36+
def change_endianness(value, size=4)
37+
conversion = value
38+
39+
if size == 4
40+
conversion = [value].pack("V").unpack("N").first
41+
elsif size == 2
42+
conversion = [value].pack("v").unpack("n").first
43+
end
44+
45+
conversion
46+
end
47+
3648
def cookie_decode(cookie_value)
37-
m = cookie_value.match(/(\d+)\.(\d+)\./)
38-
host = (m.nil?) ? nil : m[1]
39-
port = (m.nil?) ? nil : m[2]
40-
unless host.nil? && port.nil?
41-
port = (("%04X" % port).slice(2,4) << ("%04X" % port).slice(0,2)).hex.to_s
42-
byte1 = ("%08X" % host).slice(6..7).hex.to_s
43-
byte2 = ("%08X" % host).slice(4..5).hex.to_s
44-
byte3 = ("%08X" % host).slice(2..3).hex.to_s
45-
byte4 = ("%08X" % host).slice(0..1).hex.to_s
46-
host = byte1 << "." << byte2 << "." << byte3 << "." << byte4
49+
back_end = ""
50+
51+
if cookie_value =~ /(\d{8})\.(\d{5})\./
52+
host = $1.to_i
53+
port = $2.to_i
54+
55+
host = change_endianness(host)
56+
host = Rex::Socket.addr_itoa(host)
57+
58+
port = change_endianness(port, 2)
59+
60+
back_end = "#{host}:#{port}"
4761
end
48-
return host,port
62+
63+
back_end
4964
end
5065

5166
def get_cookie # request a page and extract a F5 looking cookie.
67+
cookie = {}
5268
res = send_request_raw({
5369
'method' => 'GET',
5470
'uri' => @uri
5571
})
56-
id,value = nil
57-
# Get the SLB session ID, like "TestCookie=2263487148.3013.0000"
58-
m = res.get_cookies.match(/([\-\w\d]+)=((?:\d+\.){2}\d+)(?:$|,|;|\s)/)
59-
unless m.nil?
60-
id = (m.nil?) ? nil : m[1]
61-
value = (m.nil?) ? nil : m[2]
62-
return id, value
72+
73+
unless res.nil?
74+
# Get the SLB session ID, like "TestCookie=2263487148.3013.0000"
75+
m = res.get_cookies.match(/([\-\w\d]+)=((?:\d+\.){2}\d+)(?:$|,|;|\s)/)
76+
unless m.nil?
77+
cookie[:id] = (m.nil?) ? nil : m[1]
78+
cookie[:value] = (m.nil?) ? nil : m[2]
79+
end
6380
end
81+
82+
cookie
6483
end
6584

6685
def run
67-
host_port = []
68-
@uri = normalize_uri(target_uri.path)
69-
print_status("Starting request #{@uri}")
70-
for i in 0...datastore['RETRY']
71-
id, value = get_cookie() # Get the cookie
86+
unless datastore['REQUESTS'] > 0
87+
print_error("Please, configure more than 0 REQUESTS")
88+
return
89+
end
90+
91+
back_ends = []
92+
@uri = normalize_uri(target_uri.path.to_s)
93+
print_status("#{peer} - Starting request #{@uri}")
94+
95+
for i in 0...datastore['REQUESTS']
96+
cookie = get_cookie() # Get the cookie
7297
# If the cookie is not found, stop process
73-
unless id
74-
print_error("F5 SLB cookie not found")
75-
return
98+
if cookie.empty? || cookie[:id].nil?
99+
print_error("#{peer} - F5 Server Load Balancing cookie not found")
100+
break
76101
end
102+
77103
# Print the cookie name on the first request
78104
if i == 0
79-
print_status("F5 cookie \"#{id}\" found")
105+
print_status("#{peer} - F5 Server Load Balancing \"#{cookie[:id]}\" found")
80106
end
81-
host, port = cookie_decode(value)
82-
unless host_port.include? (host+":"+port)
83-
host_port.push(host+":"+port)
84-
print_status("Backend #{host}:#{port}")
107+
108+
back_end = cookie_decode(cookie[:value])
109+
unless back_ends.include?(back_end)
110+
print_status("#{peer} - Backend #{back_end} found")
111+
back_ends.push(back_end)
85112
end
86113
end
114+
87115
# Reporting found backends in database
88-
report_note(
89-
:host => rhost,
90-
:type => "F5_Cookie_Backends",
91-
:data => host_port
92-
)
116+
unless back_ends.empty?
117+
report_note(
118+
:host => rhost,
119+
:type => "f5_load_balancer_backends",
120+
:data => back_ends
121+
)
122+
end
123+
93124
end
94125
end

0 commit comments

Comments
 (0)