This repository has been archived by the owner on Aug 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
behind_cloudfront.rb
456 lines (385 loc) · 13.9 KB
/
behind_cloudfront.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
# rev: 1.0.1
class MetasploitModule < Msf::Auxiliary
include Msf::Auxiliary::Report
def initialize(info = {})
super(update_info(info,
'Name' => 'Behind CloudFront',
'Description' => %q{
This module can be useful if you need to test
the security of your server and your website
behind CloudFront by discovering the real IP address.
},
'Author' => [
'mekhalleh',
'RAMELLA Sébastien <sebastien.ramella[at]Pirates.RE>'
],
'References' => [
['URL', 'https://github.com/mekhalleh/behind_cloudflare'],
['URL', 'https://github.com/mekhalleh/behind_ingensec']
],
'License' => MSF_LICENSE
))
register_options(
[
OptString.new('CENSYS_SECRET', [false, 'The Censys API SECRET']),
OptString.new('CENSYS_UID', [false, 'The Censys API UID']),
OptString.new('COMPSTR', [false, 'You can use a custom string to perform the comparison (default is TITLE).']),
OptString.new('HOSTNAME', [true, 'The hostname or domain name where we want to find the real IP address']),
OptString.new('Proxies', [false, 'A proxy chain of format type:host:port[,type:host:port][...]']),
OptInt.new('RPORT', [true, 'The target TCP port on which the protected website responds', 443]),
OptBool.new('SSL', [true, 'Negotiate SSL/TLS for outgoing connections', true]),
OptInt.new('THREADS', [true, 'Threads for DNS enumeration', 8]),
OptString.new('URIPATH', [true, 'The URI path on which to perform the page comparison', '/']),
OptPath.new('WORDLIST', [true, 'Wordlist of subdomains', ::File.join(Msf::Config.data_directory, 'wordlists', 'namelist.txt')])
])
register_advanced_options(
[
OptBool.new('DNSENUM', [true, 'Set DNS enumeration as optional', true]),
OptAddress.new('NS', [false, 'Specify the nameserver to use for queries (default is system DNS)']),
OptInt.new('TIMEOUT', [true, 'HTTP(s) request timeout', 15])
])
end
def do_check_tcp_port(ip, port, proxies)
begin
sock = Rex::Socket::Tcp.create(
'PeerHost' => ip,
'PeerPort' => port,
'Proxies' => proxies
)
rescue ::Rex::ConnectionRefused, Rex::ConnectionError
return false
end
sock.close
return true
end
def do_grab_domain_ip_history(hostname, proxies)
begin
cli = Rex::Proto::Http::Client.new('viewdns.info', 443, {}, true, nil, proxies)
cli.connect
request = cli.request_cgi({
'uri' => "/iphistory/?domain=#{hostname}",
'method' => 'GET'
})
response = cli.send_recv(request)
cli.close
rescue ::Rex::ConnectionError, Errno::ECONNREFUSED, Errno::ETIMEDOUT
print_error('HTTP connection failed to ViewDNS.info website.')
return false
end
html = response.get_html_document
table = html.css('table')[3]
unless table.nil?
rows = table.css('tr')
ar_ips = []
rows.each.map do | row |
row = /(\d*\.\d*\.\d*\.\d*)/.match(row.css('td').map(&:text).to_s)
unless row.nil?
ar_ips.push(row)
end
end
end
if ar_ips.nil?
print_bad('No domain IP(s) history founds.')
return false
end
return ar_ips
end
## auxiliary/gather/enum_dns.rb
def do_dns_enumeration(domain, threads)
wordlist = datastore['WORDLIST']
return if wordlist.blank?
threads = 1 if threads <= 0
queue = []
File.foreach(wordlist) do | line |
queue << "#{line.chomp}.#{domain}"
end
ar_ips = []
until queue.empty?
t = []
threads = 1 if threads <= 0
if queue.length < threads
# work around issue where threads not created as the queue isn't large enough
threads = queue.length
end
begin
1.upto(threads) do
t << framework.threads.spawn("Module(#{refname})", false, queue.shift) do | test_current |
Thread.current.kill unless test_current
a = /(\d*\.\d*\.\d*\.\d*)/.match(do_dns_get_a(test_current, 'DNS bruteforce records').to_s)
ar_ips.push(a) if a
end
end
t.map(&:join)
rescue ::Timeout::Error
ensure
t.each { | x | x.kill rescue nil }
end
end
if ar_ips.empty?
print_bad('No enumerated domain IP(s) founds.')
return false
end
return ar_ips
end
## auxiliary/gather/enum_dns.rb
def do_dns_get_a(domain, type='DNS A records')
response = do_dns_query(domain, 'A')
return if response.blank? || response.answer.blank?
response.answer.each do | row |
next unless row.class == Net::DNS::RR::A
end
end
## auxiliary/gather/enum_dns.rb
def do_dns_query(domain, type)
nameserver = datastore['NS']
if nameserver.blank?
dns = Net::DNS::Resolver.new
else
dns = Net::DNS::Resolver.new(nameservers: ::Rex::Socket.resolv_to_dotted(nameserver))
end
dns.use_tcp = false
dns.udp_timeout = 8
dns.retry_number = 2
dns.retry_interval = 2
dns.query(domain, type)
rescue ResolverArgumentError, Errno::ETIMEDOUT, ::NoResponseError, ::Timeout::Error => e
print_error("Query #{domain} DNS #{type} - exception: #{e}")
return nil
end
## auxiliary/gather/enum_dns.rb
def do_save_note(hostname, ip, sname)
data = { 'vhost' => hostname, 'real_ip' => ip, 'sname' => sname }
report_note(
:host => hostname,
:type => "behind_cloudfront",
:data => data,
update: :unique_data
)
end
def do_simple_get_request_raw(host, port, ssl, host_header=nil, uri, proxies)
begin
http = Rex::Proto::Http::Client.new(host, port, {}, ssl, nil, proxies)
http.connect(datastore['TIMEOUT'])
unless host_header.eql? nil
http.set_config({ 'vhost' => host_header })
end
request = http.request_raw({
'uri' => uri,
'method' => 'GET'
})
response = http.send_recv(request)
http.close
rescue ::Rex::ConnectionError, Errno::ECONNREFUSED, Errno::ETIMEDOUT, StandardError => error
print_error(error.message)
end
return false if response.nil?
return response
end
def do_check_bypass(fingerprint, tag, host, ip, uri, proxies)
ret_value = false
# Check for "misconfigured" web server on TCP/80.
if do_check_tcp_port(ip, 80, proxies)
vprint_status(" * Trying: http://#{ip}:80/")
response = do_simple_get_request_raw(ip, 80, false, host, uri, proxies)
if response != false
unless response.headers.to_s.include? 'Server: gatejs'
unless response.headers.to_s.include? 'binarysec'
if response.code.eql? 200
html = response.get_html_document
if html.at(tag).to_s.include? fingerprint.to_s
print_good("A direct-connect IP address was found: http://#{ip}:80/")
do_save_note(host, ip, 'http')
ret_value = true
end
else
vprint_line(" --> responded with an unexpected HTTP status code: #{response.code.to_s}")
end
end
end
end
end
# Check for "misconfigured" web server on TCP/443.
if do_check_tcp_port(ip, 443, proxies)
vprint_status(" * Trying: https://#{ip}:443/")
response = do_simple_get_request_raw(ip, 443, true, host, uri, proxies)
if response != false
unless response.headers.to_s.include? 'Server: gatejs'
unless response.headers.to_s.include? 'binarysec'
if response.code.eql? 200
if response != false
html = response.get_html_document
if html.at(tag).to_s.include? fingerprint.to_s
print_good("A direct-connect IP address was found: https://#{ip}:443/")
do_save_note(host, ip, 'https')
ret_value = true
end
end
else
vprint_line(" --> responded with an unexpected HTTP status code: #{response.code.to_s}")
end
end
end
end
end
return ret_value
end
## auxiliary/gather/censys_search.rb
def basic_auth_header(username, password)
auth_str = username.to_s + ":" + password.to_s
auth_str = "Basic " + Rex::Text.encode_base64(auth_str)
end
## auxiliary/gather/censys_search.rb
def search(keyword, search_type, uid, secret)
begin
payload = {
'query' => keyword
}
cli = Rex::Proto::Http::Client.new('www.censys.io', 443, {}, true)
cli.connect
response = cli.request_cgi(
'method' => 'post',
'uri' => "/api/v1/search/#{search_type}",
'headers' => {
'Authorization' => basic_auth_header(uid, secret)
},
'data' => payload.to_json
)
results = cli.send_recv(response)
rescue ::Rex::ConnectionError, Errno::ECONNREFUSED, Errno::ETIMEDOUT
print_error("HTTP Connection Failed")
end
unless results
print_error('server_response_error')
return false
end
records = ActiveSupport::JSON.decode(results.body)
results = records['results']
return parse_ipv4(results)
end
## auxiliary/gather/censys_search.rb
def parse_ipv4(records)
ip_list = []
records.each do | ipv4 |
ip_list.push(ipv4['ip'])
end
return ip_list
end
## auxiliary/gather/censys_search.rb
def valid_domain?(domain)
domain =~ /^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/
end
def get_cloudfront_ips
response = do_simple_get_request_raw(
'd7uri8nf7uskq.cloudfront.net',
443,
true,
nil,
'/tools/list-cloudfront-ips',
datastore['PROXIES']
)
return false if response.nil?
ip_list = []
# vprint_line("#{response.get_json_document['CLOUDFRONT_GLOBAL_IP_LIST']}")
response.get_json_document['CLOUDFRONT_GLOBAL_IP_LIST'].each do | ip |
ip_list.push(ip.gsub('"', ''))
end
# vprint_line("#{response.get_json_document['CLOUDFRONT_REGIONAL_EDGE_IP_LIST']}")
response.get_json_document['CLOUDFRONT_REGIONAL_EDGE_IP_LIST'].each do | ip |
ip_list.push(ip.gsub('"', ''))
end
return ip_list
end
def run
print_status('Passive gathering information...')
domain_name = PublicSuffix.parse(datastore['HOSTNAME']).domain
ip_list = []
# ViewDNS.info
ip_records = do_grab_domain_ip_history(domain_name, datastore['Proxies'])
ip_list |= ip_records unless ip_records.eql? false
unless ip_records.eql? false
print_status(" * ViewDNS.info: #{ip_records.count.to_s} IP address found(s).")
end
# DNS Enum.
if datastore['DNSENUM'].eql? true
ip_records = do_dns_enumeration(domain_name, datastore['THREADS'])
ip_list |= ip_records unless ip_records.eql? false
unless ip_records.eql? false
print_status(" * DNS Enumeration: #{ip_records.count.to_s} IP address found(s).")
end
end
# Censys search.
if [datastore['CENSYS_UID'], datastore['CENSYS_SECRET']].none?(&:nil?)
ip_records = search(domain_name, 'ipv4', datastore['CENSYS_UID'], datastore['CENSYS_SECRET'])
ip_list |= ip_records unless ip_records.eql? false
unless ip_records.eql? false
print_status(" * Censys IPv4: #{ip_records.count.to_s} IP address found(s).")
print_status()
end
end
if ip_list.empty?
print_bad('No IP address found :-(')
return
end
# Cleaning the results.
print_status("Clean cloudfront server(s)...")
ip_blacklist = get_cloudfront_ips
records = []
ip_list.uniq.each do | ip |
is_listed = false
ip_blacklist.each do | ip_range |
if IPAddr.new(ip_range).include? ip.to_s
is_listed = true
break
end
end
unless is_listed.eql? true
records << ip.to_s
end
end
if records.empty?
print_bad("No IP address found after cleaning.")
return
end
print_good(" * TOTAL: #{records.uniq.count.to_s} IP address found(s) after cleaning.")
print_status()
# Processing bypass...
print_status('Bypass cloudfront is in progress...')
if datastore['COMPSTR'].nil?
tag = 'title'
# Initial HTTP request to the server (for <title> comparison).
print_status(' * Initial request to the original server for comparison')
response = do_simple_get_request_raw(
datastore['HOSTNAME'],
datastore['RPORT'],
datastore['SSL'],
nil,
datastore['URIPATH'],
datastore['PROXIES']
)
html = response.get_html_document
fingerprint = html.at(tag).text
else
tag = 'html'
fingerprint = datastore['COMPSTR']
end
ret_val = false
records.uniq.each do | ip |
found = do_check_bypass(
fingerprint,
tag,
datastore['HOSTNAME'],
ip,
datastore['URIPATH'],
datastore['PROXIES']
)
ret_val = true if found.eql? true
end
unless ret_val.eql? true
print_bad('No direct-connect IP address found :-(')
end
end
end