-
-
Notifications
You must be signed in to change notification settings - Fork 344
/
github-dorks.py
executable file
·181 lines (151 loc) · 5.2 KB
/
github-dorks.py
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
#!/usr/bin/python2
import os
import sys
import json
import time
import re
import requests
import argparse
import random
from termcolor import colored
from multiprocessing.dummy import Pool
TOKENS_FILE = os.path.dirname(os.path.realpath(__file__))+'/.tokens'
GITHUB_API_URL = 'https://api.github.com'
parser = argparse.ArgumentParser()
parser.add_argument( "-d","--dorks",help="dorks file (required)" )
parser.add_argument( "-t","--token",help="your github token (required)" )
parser.add_argument( "-o","--org",help="organization (required or -u)" )
parser.add_argument( "-e","--threads",help="maximum n threads, default 1" )
parser.add_argument( "-u","--user",help="user (required or -o)" )
parser.parse_args()
args = parser.parse_args()
t_tokens = []
t_orgs = []
t_users = []
t_dorks = []
if args.token:
t_tokens = args.token.split(',')
else:
gh_env = os.getenv('GITHUB_TOKEN')
if gh_env:
t_tokens = gh_env.strip().split(',')
else:
if os.path.isfile(TOKENS_FILE):
fp = open(TOKENS_FILE,'r')
for line in fp:
r = re.search( '^([a-f0-9]{40}|ghp_[a-zA-Z0-9]{36}|github_pat_[_a-zA-Z0-9]{82})$', line )
if r:
t_tokens.append( r.group(1) )
if not len(t_tokens):
parser.error( 'auth token is missing' )
if args.user:
t_users = args.user.split(',')
if args.threads:
threads = int(args.threads)
else:
threads = 1
if args.org:
t_orgs = args.org.split(',')
if not args.user and not args.org:
parser.error( 'user or organization missing' )
if not args.dorks:
parser.error( 'dorks file is missing' )
fp = open(args.dorks,'r')
for line in fp:
t_dorks.append( line.strip() )
# print(t_tokens)
# print(t_orgs)
# print(t_users)
# print(t_dorks)
# max_minute_requests = len(t_tokens) * 30
# sleep_duration = 60 / max_minute_requests
def githubApiSearchCode( url ):
# time.sleep( 0.800 )
sys.stdout.write( 'progress: %d/%d\r' % (t_stats['n_current'],t_stats['n_total_urls']) )
sys.stdout.flush()
t_stats['n_current'] = t_stats['n_current'] + 1
# i = random.SystemRandom().randint(0, t_stats['l_tokens'])
i = t_stats['n_current'] % t_stats['l_tokens']
# print(i)
headers = {"Authorization": "token "+t_tokens[i]}
# print(headers)
try:
r = requests.get( url, headers=headers, timeout=5 )
json = r.json()
# print(url)
# print(json)
if 'documentation_url' in json:
print( colored("[-] error occurred: %s" % json['documentation_url'], 'red') )
else:
t_results_urls[url] = json['total_count']
# print(json['total_count'])
except Exception as e:
print( colored("[-] error occurred: %s" % e, 'red') )
return 0
def __urlencode( str ):
str = str.replace( ':', '%3A' );
str = str.replace( '"', '%22' );
str = str.replace( ' ', '+' );
return str
t_urls = {}
t_results = {}
t_results_urls = {}
t_stats = {
# 'l_tokens': len(t_tokens)-1,
'l_tokens': len(t_tokens),
'n_current': 0,
'n_total_urls': 0
}
for org in t_orgs:
t_results[org] = []
for dork in t_dorks:
dork = 'org:' + org + ' ' + dork
url = 'https://api.github.com/search/code?q=' + __urlencode(dork)
t_results[org].append( url )
t_urls[url] = 0
for user in t_users:
t_results[user] = []
for dork in t_dorks:
dork = 'user:' + user + ' ' + dork
# dork = '"' + dork + '"'
url = 'https://api.github.com/search/code?q=' + __urlencode(dork)
t_results[user].append( url )
t_urls[url] = 0
# print(t_results)
# exit()
t_stats['n_total_urls'] = len(t_urls)
# exit()
sys.stdout.write( colored('%d organizations found.\n' % len(t_orgs), 'green') )
sys.stdout.write( colored('%d users found.\n' % len(t_users), 'green') )
sys.stdout.write( colored('%d dorks found.\n' % len(t_dorks), 'green') )
sys.stdout.write( colored('%d urls generated.\n' % len(t_urls), 'green') )
sys.stdout.write( '[+] running %d threads.\n' % threads )
time.sleep( 1 )
pool = Pool( threads )
pool.map( githubApiSearchCode, t_urls )
pool.close()
pool.join()
for org in t_orgs:
print( '>>>>> %s\n' % org )
for url in t_results[org]:
if url in t_results_urls:
url2 = url.replace( 'https://api.github.com/search/code', 'https://github.com/search' ) + '&s=indexed&type=Code&o=desc'
if t_results_urls[url] == 0:
sys.stdout.write( colored('%s (%d)\n' % (url2,t_results_urls[url]), 'white') )
else:
sys.stdout.write( '%s (%d)\n' % (url2,t_results_urls[url]) )
else:
sys.stdout.write( colored('%s\n' % url2, 'red') )
print('')
for user in t_users:
print( '>>>>> %s\n' % user )
for url in t_results[user]:
if url in t_results_urls:
url2 = url.replace( 'https://api.github.com/search/code', 'https://github.com/search' ) + '&s=indexed&type=Code&o=desc'
if t_results_urls[url] == 0:
sys.stdout.write( colored('%s (%d)\n' % (url2,t_results_urls[url]), 'white') )
else:
sys.stdout.write( '%s (%d)\n' % (url2,t_results_urls[url]) )
else:
sys.stdout.write( colored('%s\n' % url2, 'red') )
print('')