-
-
Notifications
You must be signed in to change notification settings - Fork 591
/
github-dork.py
214 lines (184 loc) · 6.95 KB
/
github-dork.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
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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import github3 as github
import os
import argparse
import time
import feedparser
from copy import copy
from sys import stderr, prefix
gh_user = os.getenv('GH_USER', None)
gh_pass = os.getenv('GH_PWD', None)
gh_token = os.getenv('GH_TOKEN', None)
gh_url = os.getenv('GH_URL', None)
if gh_url is None:
gh = github.GitHub(username=gh_user, password=gh_pass, token=gh_token)
else:
gh = github.GitHubEnterprise(
url=gh_url, username=gh_user, password=gh_pass, token=gh_token)
def search_wrapper(gen):
while True:
gen_back = copy(gen)
try:
yield next(gen)
except StopIteration:
return
except github.exceptions.ForbiddenError as e:
search_rate_limit = gh.rate_limit()['resources']['search']
# limit_remaining = search_rate_limit['remaining']
reset_time = search_rate_limit['reset']
current_time = int(time.time())
sleep_time = reset_time - current_time + 1
stderr.write(
'GitHub Search API rate limit reached. Sleeping for %d seconds.\n\n'
% (sleep_time))
time.sleep(sleep_time)
yield next(gen_back)
except Exception as e:
raise e
def metasearch(repo_to_search=None,
user_to_search=None,
gh_dorks_file=None,
active_monit=None,
output_filename=None,
refresh_time=60):
if active_monit is None:
search(repo_to_search, user_to_search, gh_dorks_file, active_monit, output_filename)
else:
monit(gh_dorks_file, active_monit, refresh_time)
def monit(gh_dorks_file=None, active_monit=None, refresh_time=60):
if gh_user is None:
raise Exception('Error, env Github user variable needed')
else:
print(
'Monitoring user private feed searching new code to be dorked.' +
'Every new merged pull request trigger user scan.'
)
print('-----')
items_history = list()
gh_private_feed = "https://github.com/{}.private.atom?token={}".format(
gh_user, active_monit)
while True:
feed = feedparser.parse(gh_private_feed)
for i in feed['items']:
if 'merged pull' in i['title']:
if i['title'] not in items_history:
search(
user_to_search=i['author_detail']['name'],
gh_dorks_file=gh_dorks_file)
items_history.append(i['title'])
print('Waiting for new items...')
time.sleep(refresh_time)
def search(repo_to_search=None,
user_to_search=None,
gh_dorks_file=None,
active_monit=None,
output_filename=None):
if gh_dorks_file is None:
for path_prefix in ['.', os.path.join(prefix, 'github-dorks/')]:
filename = os.path.join(path_prefix, 'github-dorks.txt')
if os.path.isfile(filename):
gh_dorks_file = filename
break
if not os.path.isfile(gh_dorks_file):
raise Exception('Error, the dorks file path is not valid')
if user_to_search:
print("Scanning User: ", user_to_search)
if repo_to_search:
print("Scanning Repo: ", repo_to_search)
found = False
outputFile = None
if output_filename:
outputFile = open(output_filename, 'w')
with open(gh_dorks_file, 'r') as dork_file:
# Write CSV Header
if outputFile:
outputFile.write('Issue Type (Dork), Text Matches, File Path, Score/Relevance, URL of File\n')
for dork in dork_file:
dork = dork.strip()
if not dork or dork[0] in '#;':
continue
addendum = ''
if repo_to_search:
addendum = ' repo:' + repo_to_search
elif user_to_search:
addendum = ' user:' + user_to_search
dork = dork + addendum
search_results = search_wrapper(gh.search_code(dork))
try:
for search_result in search_results:
found = True
fmt_args = {
'dork': dork,
'text_matches': search_result.text_matches,
'path': search_result.path,
'score': search_result.score,
'url': search_result.html_url
}
# Either write to file or print output
if outputFile:
outputFile.write('{dork}, {text_matches}, {path}, {score}, {url}\n'.format(**fmt_args))
else:
result = '\n'.join([
'Found result for {dork}',
'Text matches: {text_matches}', 'File path: {path}',
'Score/Relevance: {score}', 'URL of File: {url}', ''
]).format(**fmt_args)
print(result)
except github.exceptions.GitHubError as e:
print('GitHubError encountered on search of dork: ' + dork)
print(e)
return
except Exception as e:
print(e)
print('Error encountered on search of dork: ' + dork)
if not found:
print('No results for your dork search' + addendum + '. Hurray!')
def main():
parser = argparse.ArgumentParser(
description='Search github for github dorks',
epilog='Use responsibly, Enjoy pentesting')
parser.add_argument(
'-v', '--version', action='version', version='%(prog)s 0.1.1')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
'-u',
'--user',
dest='user_to_search',
action='store',
help='Github user/org to search within. Eg: techgaun')
group.add_argument(
'-r',
'--repo',
dest='repo_to_search',
action='store',
help='Github repo to search within. Eg: techgaun/github-dorks')
parser.add_argument(
'-d',
'--dork',
dest='gh_dorks_file',
action='store',
help='Github dorks file. Eg: github-dorks.txt')
group.add_argument(
'-m',
'--monit',
dest='active_monit',
action='store',
help='Monitors Github user private feed with feed token'
)
parser.add_argument(
'-o',
'--outputFile',
dest='output_filename',
action='store',
help='CSV File to write results to. This overwrites the file provided! Eg: out.csv'
)
args = parser.parse_args()
metasearch(
repo_to_search=args.repo_to_search,
user_to_search=args.user_to_search,
gh_dorks_file=args.gh_dorks_file,
active_monit=args.active_monit,
output_filename=args.output_filename)
if __name__ == '__main__':
main()