-
Notifications
You must be signed in to change notification settings - Fork 54
/
dorks.js
574 lines (538 loc) · 16.1 KB
/
dorks.js
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
var fs = require('fs')
var system = require('system')
var pages = []
var userAgents = [
"Mozilla/5.0 (X11; Linux i686; rv:40.0) Gecko/20100101 Firefox/40.0",
"Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/535.7 (KHTML, like Gecko) Comodo_Dragon/16.1.1.0 Chrome/16.0.912.63 Safari/535.7",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"
]
function Google( site )
{
var web_browser = require('webpage').create()
var uri = 'http://www.google.com/'
var dorks = []
var query = ( site.trim() != '' ) ? 'site:' + site : ''
var found_pages = 0
var timeout = 0
var captcha_retry_timeout = 0
this.done = false
web_browser.viewportSize = { width: 1280, height: 800 }
web_browser.settings.userAgent = userAgents[ parseInt( Math.random() * userAgents.length ) ]
web_browser.__this = this
web_browser.onConsoleMessage = function(msg)
{
console.log(msg)
}
this.set_timeout = function( ms )
{
timeout = ms
return this
}
this.set_captcha_retry_timeout = function( ms )
{
captcha_retry_timeout = ms
return this
}
this.set_dork = function( dork )
{
dorks = dorks.concat( dork )
return this
}
this.set_dorks = function( dorks_file )
{
dorks = dorks.concat( fs.isFile(dorks_file) ? fs.read(dorks_file).replace(/\r/g, '').split('\n').reverse().filter( function(a) { return a } ) : [] )
return this
}
var save_state = function( session_file, state )
{
fs.write( session_file, JSON.stringify( state ), 'w' )
}
var load_state = function( session_file )
{
if( fs.isFile(session_file) )
{
console.log("resume " + session_file)
return JSON.parse( fs.read(session_file) )
}
}
this.filter = function( filter )
{
query += ' ' + filter
return this
}
this.attack = function( out_file )
{
var session_file = ( ( site.trim() ) ? '__' + site.trim().replace(/[\.:\/]/g, '_') : '__state' ) + '.json'
var dork, captcha = false
dorks = load_state( session_file ) || dorks
web_browser.onResourceReceived = function(resp) { if(! captcha) captcha = (resp.status == 403) }
web_browser.onLoadFinished = function(status)
{
web_browser.render( ( ( site.trim() ) ? site.trim().replace(/\./g, '_') : 'page' ) + '.png' )
if(! captcha)
captcha = web_browser.evaluate( function() { if(document.getElementById('captcha')) return true } )
if( /q=/.test(web_browser.url) && !captcha )
{
var result = web_browser.evaluateJavaScript(
"function() {\n\
var href_results = [], a = document.getElementsByTagName('a'), match\n\
for( var i = 0; i < a.length; i++ )\n\
if( /*a[i].getAttribute('target') == '_blank' &&*/ a[i].parentNode.tagName.toLowerCase() == 'h3' && a[i].getAttribute('href').indexOf('" + ( (site.trim()) ? site.trim().replace(/'/g, '') : '') + "') != -1 )\n\
{\n\
if( 0 /*( match = a[i].getAttribute('href').match(/q=(.*)/) ) && match.length == 2 && ( uri = match[1] )*/ )\n\
{\n\
console.log( '+ ' + uri )\n\
href_results.push( uri )\n\
}\n\
else\n\
{\n\
console.log( '+ ' + a[i].getAttribute('href') )\n\
href_results.push( a[i].getAttribute('href') )\n\
}\n\
}\n\
return href_results\n\
}"
)
if(result.length)
{
//web_browser.render( ( (site.trim()) ? site.trim().replace(/\./g, '_') : 'page' ) + '_' + (++found_pages) + '.png')
if( out_file )
fs.write( out_file, query + ' ' + dork + '\n', 'a' )
}
}
save_state( session_file, dorks )
if(captcha)
{
if( dorks.indexOf(dork) == -1 )
dorks.splice( dorks.length, 0, dork )
if( captcha_retry_timeout ) /* easy anti-captcha */
{
console.log("warn: captcha, sleeping " + captcha_retry_timeout + " ms")
setTimeout( function() {
web_browser.open( uri, function(status) { console.log("reopen " + uri + " (another UserAgent)") } )
}, captcha_retry_timeout )
web_browser.settings.userAgent = userAgents[ parseInt( Math.random() * userAgents.length ) ]
}
else
{
var intr = setInterval( function() {
if(! fs.isFile('captcha.png') )
{
web_browser.render('captcha.png')
console.log('warn: enter chars from captcha.png')
}
if( captcha = system.stdin.readLine().trim() )
{
fs.remove('captcha.png')
var error = web_browser.evaluateJavaScript(
"function() {\n\
var captcha = document.getElementById('captcha')\n\
if(! captcha)\n\
return 'captcha field not found'\n\
captcha.value = '" + captcha.replace(/[\r\n]/g, '') + "'\n\
node = captcha\n\
while( node = node.parentNode )\n\
if( node.tagName.toLowerCase() == 'form' || node.tagName.toLowerCase() == 'body' )\n\
break\n\
if( node.tagName.toLowerCase() != 'form')\n\
return 'captcha form not found'\n\
}"
)
web_browser.sendEvent('keypress', web_browser.event.key.Enter)
captcha = false
clearInterval(intr)
if(error)
console.log(error)
}
else
console.log('warn: enter chars from captcha.png')
}, 500 )
}
}
else
{
if( dork = dorks.pop() )
{
console.log("dork: " + dork)
var error = web_browser.evaluateJavaScript(
"function() {\n\
setTimeout( function() {\n\
var search_query, inp = document.getElementsByTagName('input')\n\
for(var i = 0; i<inp.length; i++)\n\
{\n\
if( inp[i].getAttribute('type') == 'text' || inp[i].getAttribute('type') == null )\n\
{\n\
search_query = inp[i]\n\
break\n\
}\n\
}\n\
if(! search_query)\n\
return 'search query input not found'\n\
search_query.value = '" + query.replace(/'/g, "\\'").replace(/\r/g, '') + " " + dork.replace(/'/g, "\\'").replace(/\r/g, '') + "'\n\
node = search_query\n\
while( node = node.parentNode )\n\
if( node.tagName.toLowerCase() == 'form' || node.tagName.toLowerCase() == 'body' )\n\
break\n\
if(! node.tagName.toLowerCase() == 'form')\n\
return 'search form not found'\n\
node.submit()\n\
}, " + timeout + ")\n\
}"
)
if(error)
{
dorks.splice( dorks.length, 0, dork )
console.log('err: ' + error)
}
}
else
{
fs.remove( session_file )
web_browser.__this.done = true
exit()
}
}
}
web_browser.open( uri, function(status) {
if(status != 'success')
console.log( 'warn: ' + uri + ': ' + status)
} )
return this
}
}
function GHDB()
{
var web_browser = require('webpage').create()
var uri = 'https://www.exploit-db.com/google-hacking-database/'
var dorks = []
var filename = ''
var dork_descriptions = []
var need_to_extract_dorks = false
this.done = false
web_browser.viewportSize = { width: 1280, height: 800 }
web_browser.__this = this
web_browser.onConsoleMessage = function(msg)
{
console.log(msg)
}
this.save = function(result_filename)
{
filename = result_filename
return this
}
this.file = function(source_filename)
{
if( fs.isFile( source_filename ) )
{
var custom_dorks = fs.read( source_filename ).split('\n')
for( var i = 0; i < custom_dorks.length; i++ )
if( dork = custom_dorks[i].replace(/\r/g, '') )
dorks.push( dork )
}
if( filename )
fs.write( filename, dorks.join('\n'), 'w' )
this.done = true
exit()
}
var get_onLoadFinished_handler = function( evaluate_code, query )
{
return function(status)
{
//web_browser.render('ghdb.png')
if(query != null)
{
query = null
if( error = web_browser.evaluateJavaScript( evaluate_code ) )
console.log(error)
}
else if(need_to_extract_dorks)
{
var uri,
dork = web_browser.evaluate( function() {
var a = document.getElementsByTagName('a')
for(var i = 0; i < a.length; i++)
if( a[i].getAttribute('target') == '_blank' && /google/.test( a[i].getAttribute('href') ) )
return a[i].innerHTML.replace(/&/g, '&').replace(/("|“|”)/g, '"')
.replace(/>/g, '>').replace(/</g, '<').trim()
} )
console.log( dork )
dorks.push( dork )
if( uri = dork_descriptions.pop() )
web_browser.open( uri )
else
{
if( filename )
fs.write( filename, dorks.join('\n'), 'w' )
web_browser.__this.done = true
exit()
}
}
else
{
var result = web_browser.evaluate( function() {
var uri,dork_descriptions = [], a = document.getElementsByTagName('a')
for( var i = 0; i < a.length; i++)
if( /\/ghdb\//.test( uri = a[i].getAttribute('href') ) )
dork_descriptions.push( uri )
return dork_descriptions
} )
dork_descriptions = dork_descriptions.concat( result )
var next_page = web_browser.evaluate( function() {
var href_results = [], a = document.getElementsByTagName('a')
for( var i = 0; i < a.length; i++)
if( a[i].innerHTML.toLowerCase() == 'next' )
return a[i].getAttribute('href')
} )
if( next_page )
web_browser.open( next_page.replace(/\t/g, '') )
else
{
need_to_extract_dorks = true
web_browser.open( dork_descriptions.pop() )
}
}
}
}
this.categories = [
'Any Category',
'Footholds',
'Files containing usernames',
'Sensitive Directories',
'Web Server Detection',
'Vulnerable Files',
'Vulnerable Servers',
'Error Messages',
'Files containing juicy info',
'Files containing passwords',
'Sensitive Online Shopping Info',
'Network or vulnerability data',
'Pages containing login portals',
'Various Online Devices',
'Advisories and Vulnerabilities'
]
this.print_categories = function()
{
console.log("categories:")
this.categories.forEach( function(val,key) {
console.log("[" + key + "] " + "'" + val + "'")
} )
}
this.category = function(category)
{
var category_id
if( parseInt(category) != NaN && String( parseInt(category) ).length == category.length )
category_id = parseInt( category )
else
category_id = ( this.categories.indexOf(category) != -1 ) ? this.categories.indexOf(category) : ( function(categories, category) {
for(var i=0; i < categories.length; i++)
if( categories[i].toLowerCase().search( category.toLowerCase() ) != -1 )
return i
} )(this.categories, category)
if( category_id == null )
{
console.log('category ' + category + ' not found')
return this
}
web_browser.onLoadFinished = get_onLoadFinished_handler(
"function() {\n\
var select_element = document.getElementById('ghdb_search_cat_id')\n\
if(! select_element)\n\
return 'categories element not found'\n\
select_element.value = '" + category_id + "'\n\
node = select_element\n\
while( node = node.parentNode )\n\
if( node.tagName.toLowerCase() == 'form' || node.tagName.toLowerCase() == 'body' )\n\
break\n\
if(! node.tagName.toLowerCase() == 'form')\n\
return 'search form not found'\n\
node.submit()\n\
}", category_id
)
web_browser.open( uri, function(status) {
if(status != 'success')
console.log( 'warn: ' + uri + ': ' + status)
} )
return this
}
this.search = function(query)
{
web_browser.onLoadFinished = get_onLoadFinished_handler(
"function() {\n\
var search_query, inp = document.getElementsByTagName('input')\n\
for(var i = 0; i<inp.length; i++)\n\
{\n\
if( inp[i].getAttribute('type') == 'text' || inp[i].getAttribute('type') == null )\n\
{\n\
search_query = inp[i]\n\
break\n\
}\n\
}\n\
if(! search_query)\n\
return 'search query input not found'\n\
search_query.value = '" + query + "'\n\
node = search_query\n\
while( node = node.parentNode )\n\
if( node.tagName.toLowerCase() == 'form' || node.tagName.toLowerCase() == 'body' )\n\
break\n\
if(! node.tagName.toLowerCase() == 'form')\n\
return 'search form not found'\n\
node.submit()\n\
}", query
)
web_browser.open( uri, function(status) {
if(status != 'success')
console.log( 'warn: ' + uri + ': ' + status)
} )
return this
}
}
function print_help( script_name )
{
console.log( "\n\
USAGE: \n\
phantomjs " + script_name + " [command] [options]\n\
commands: ghdb, google\n\
options (ghdb):\n\
-q [words] query from exploit-db GHDB\n\
-c [name or id] category from exploit-db GHDB\n\
-l list exploit-db GHDB categories\n\
options (google):\n\
-d [dork] specify google dork\n\
-D [dork_file] specify google dorks\n\
-s [site] set site name\n\
-S [sites_file] set sites filename\n\
-f [filter] set custom filter\n\
-t [msec] set timeout between query\n\
-T [msec] set captcha retry timeout\n\
options common:\n\
-o [result_file] save data in file\n\
\n\
EXAMPLES:\n\
phantomjs " + script_name + " ghdb -q oracle -o oracle_dorks.txt\n\
phantomjs " + script_name + " ghdb -c \"vulnerable files\" -o vuln_files.txt\n\
phantomjs " + script_name + " ghdb -c 0 -o all_dorks.txt\n\
\n\
phantomjs " + script_name + " google -D all_dorks.txt -s \"somesite.com\" -o result.txt\n\
phantomjs " + script_name + " google -d \"mysql running.on\" -S \"sites.txt\"\n\
phantomjs " + script_name + " google -D vuln_files.txt -S \"sites.txt\" -o result.txt\n\
phantomjs " + script_name + " google -D vuln_servers.txt -f \"inurl:com\" -f \"inurl:net\"\n\
" )
}
function exit()
{
for(var i = 0; i < pages.length; i++)
if(! pages[i].done )
return
phantom.exit()
}
function remove_old_files()
{
if( fs.isFile('captcha.png') )
fs.remove('captcha.png')
}
var settings = {
'words': '',
'category': null,
'print_categories': false,
'dorks': [],
'dork_file': 'dorks.json',
'sites': [],
'filters': [],
'timeout': 0,
'captcha_timeout': 0,
'output': ''
}, command
if( system.args.length > 1 )
{
command = system.args[1].toLowerCase()
if( command != 'ghdb' && command != 'google' )
{
console.log("bad command")
exit()
}
for( var i = 2; i < system.args.length; i++ )
{
switch( system.args[i] )
{
case '-q':
settings.words = system.args[++i]
break
case '-c':
settings.category = system.args[++i]
break
case '-l':
settings.print_categories = true
break
case '-f':
settings.filters[ settings.filters.length ] = system.args[++i]
break
case '-d':
settings.dorks[ settings.dorks.length ] = system.args[++i]
break
case '-D':
settings.dork_file = system.args[++i]
break
case '-s':
settings.sites[ settings.sites.length ] = system.args[++i]
break
case '-S':
var sites_file = system.args[++i]
if( sites_file && fs.isFile(sites_file) )
settings.sites = fs.read(sites_file).replace(/\r/g, '').split('\n')
break
case '-t':
settings.timeout = system.args[++i]
break
case '-T':
settings.captcha_timeout = system.args[++i]
break
case '-o':
settings.output = system.args[++i]
break
default:
console.log('unknown option')
print_help()
exit()
}
}
if( settings.print_categories )
{
new GHDB().print_categories()
exit()
}
else
{
switch( command )
{
case 'ghdb':
if( settings.category )
pages.push( new GHDB().category( settings.category ).save( settings.output ) )
else
pages.push( new GHDB().search( settings.words ).save( settings.output ) )
break
case 'google':
remove_old_files()
if( settings.filters && settings.sites.length == 0 )
settings.sites.push(" ")
for(var i = 0; i < settings.sites.length; i++)
if( settings.sites[i] )
pages.push(
new Google( settings.sites[i] )
.filter( settings.filters.join(' ') )
.set_dorks( settings.dork_file )
.set_dork( settings.dorks )
.set_timeout( settings.timeout )
.set_captcha_retry_timeout( settings.captcha_timeout )
.attack( settings.output )
)
break
}
}
}
else
{
print_help( system.args[0] )
exit()
}