-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathsitedorks.py
281 lines (230 loc) · 10.5 KB
/
sitedorks.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
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
#!/usr/bin/env python3
import argparse
import urllib.parse
import webbrowser
import sys
import os
import time
from collections import defaultdict
def get_categories():
dCatCounty = defaultdict(int)
for sLine in lInputFile:
sLine = sLine.strip()
if not sLine or sLine[0] == "#":
continue
lLine = sLine.split(",")
if len(lLine) < 2:
continue
dCatCounty[lLine[1]] += 1
sCatList = "\n".join(f" -{sCat}({dCatCounty[sCat]})" for sCat in sorted(dCatCounty))
return sCatList
def get_comment():
boolHelpFound = False
for sLine in lInputFile:
sLine = sLine.strip()
if len(sLine) > 0:
if sLine[0] == "#":
print (sLine)
boolHelpFound = True
elif not boolHelpFound:
print(f"No help comment found in {sInputFile}")
exit(2)
exit(0)
sArgParser=argparse.ArgumentParser(add_help=False, description="""Use your favorite search engine to search for a search term with different websites. Use single quotes around a query with double quotes. Be sure to enclose a query with single quotes it contains shell control characters like space, ';', '>', '|', etc.""")
sArgParser.add_argument('-h', '--help', help='Show this help message, print categories on file (add -file to check other CSV file) and exit.', action="store_true")
sArgParser.add_argument("-hh", "--help2", help="Show the help inside a .csv file being called. Lines in the beginning of the script starting with # are displayed as help.", action="store_true")
sArgParser.add_argument('-browser', metavar='<browser>', help='Supply the browser executable to use or use the default browser.', default=None)
sArgParser.add_argument('-cat', metavar="<category>", help='Choose from 1 or more categories, use \',\' (comma) as delimiter. Defaults to all categories.')
sArgParser.add_argument('-cats', help='Show all categories on file, use with or without -file.', action="store_true")
sArgParser.add_argument('-count', metavar="<count>", help='How many websites are searched per query. Google has a maximum length for queries.')
sArgParser.add_argument('-engine', metavar="<engine>", help='Search with \'google\', \'baidu\', \'bing\', \'bing-ecosia\', \'duckduckgo\' \'yahoo\' or \'yandex\', defaults to \'google\'.', choices=['bing', 'bing-ecosia', 'baidu', 'duckduckgo', 'google', 'yahoo', 'yandex'], default="google")
sArgParser.add_argument('-file', metavar="<file>", help='Enter a custom website list.')
sArgParser.add_argument('-filter', metavar="<string>", help='Only query for sites with this string.')
sArgParser.add_argument('-query', metavar="<query>", help='Enter a mandatory search term.')
sArgParser.add_argument('-site', metavar="<on|off|inurl>",help='Turn the \'site:\' operator \'on\' or \'off\', or replace it with \'inurl:\' (only for Google), defaults to \'on\'.',default='on', choices=['on', 'off', 'inurl'])
sArgParser.add_argument('-excl', metavar="<domains>", help='Excluded these domains from the search query.')
sArgParser.add_argument('-echo', help='Prints the search query URLs, for further use like piping or bookmarking.', action="store_true")
sArgParser.add_argument('-ubb', help='Updates bug bounty files (in en out scope) and exits. Uses bbrecon.', action="store_true")
sArgParser.add_argument('-wait', metavar="<seconds>", help='Wait x seconds, defaults to 7 seconds.', default=7)
aArguments=sArgParser.parse_args()
if aArguments.ubb:
import subprocess
import json
from tldextract import extract
sOutput = subprocess.check_output("bbrecon get programs --type web -o json", shell=True)
fCsvInScope = open(os.path.dirname(os.path.realpath(sys.argv[0])) + "/sitedorks-bbrecon-inscope.csv", 'w', buffering=1)
fCsvOutScope = open(os.path.dirname(os.path.realpath(sys.argv[0])) + "/sitedorks-bbrecon-outscope.csv", 'w', buffering=1)
OutputJson = json.loads(sOutput)
dDomainsInScope = {}
dDomainsOutScope = {}
for sLine in OutputJson:
sLine["slug"] = sLine["slug"].lower()
for sInScope in sLine["in_scope"]:
if sInScope["type"] == "web":
lInScopeValues = sInScope["value"].lower().split(",")
for sInScopeValue in lInScopeValues:
if sInScopeValue.endswith("amazonaws.com") or sInScopeValue.endswith("cloudfront.net") or sInScopeValue.endswith("azurefd.net") or sInScopeValue.endswith("azurewebsites.net"):
sDomain = sInScopeValue
else:
dl3, dl2, dl1 = extract(sInScopeValue)
sDomain = dl2 + "." + dl1
if " " in sDomain or "*" in sDomain or sDomain.endswith(".") or sDomain.endswith(".onion"):
continue
else:
dDomainsInScope[sDomain] = sLine["slug"]
for sOutScope in sLine["out_scope"]:
if sOutScope["type"] == "web":
lOutScopeValues = sOutScope["value"].lower().split(",")
for sOutScopeValue in lOutScopeValues:
if sOutScopeValue.endswith("amazonaws.com") or sOutScopeValue.endswith("cloudfront.net") or sOutScopeValue.endswith("azurefd.net") or sOutScopeValue.endswith("azurewebsites.net"):
sDomain = sOutScopeValue
else:
dl3, dl2, dl1 = extract(sOutScopeValue)
sDomain = f"{dl2}.{dl1}"
if " " in sDomain or "*" in sDomain or sDomain.endswith(".") or sDomain.endswith(".onion"):
continue
else:
if not sDomain in dDomainsInScope:
dDomainsOutScope[sDomain] = sLine["slug"]
bFailed = False
for sLine in dDomainsInScope:
try:
fCsvInScope.write(sLine +","+ dDomainsInScope[sLine] +"\n")
except:
bFailed = True
continue
for sLine in dDomainsOutScope:
try:
fCsvOutScope.write(sLine +","+ dDomainsOutScope[sLine] +"\n")
except:
bFailed = True
continue
if not bFailed:
print(f"sitedorks-bbrecon-inscope.csv and sitedorks-bbrecon-outscope.csv have been updated.")
else:
print(f"Something went wrong while writing the files.")
exit()
sAnswer=""
if not aArguments.cat and aArguments.query and not aArguments.help:
while sAnswer.lower() != "y" and sAnswer.lower() != "n":
sAnswer = input("Not providing -cat can open a whole lot of tabs/windows in your browser. Do you want to continue? (y/n) ")
if sAnswer.lower() == "n":
exit()
if aArguments.site == "inurl" and aArguments.engine != "google" and aArguments.engine != "duckduckgo":
print(f"inurl: only works with Google and DuckDuckGo.")
print()
sArgParser.print_help()
sys.exit(2)
if not aArguments.query:
sQuery = ""
else:
sQuery = urllib.parse.quote(aArguments.query)
if aArguments.count:
iNewUrlAfter = int(aArguments.count)
if aArguments.engine == "baidu":
print(f"Because of limitations with Baidu, -count is lowered to 2.")
iNewUrlAfter = 2
else:
iNewUrlAfter = 14
if aArguments.file:
sInputFile = aArguments.file
if "/" in sInputFile:
sInputFile = aArguments.file
else:
sInputFile = os.path.dirname(os.path.realpath(sys.argv[0])) + "/csv/" + aArguments.file
else:
sInputFile = os.path.dirname(os.path.realpath(sys.argv[0])) + "/csv/sitedorks.csv"
if aArguments.site == "on":
sSite = "site:"
sQuote = ""
elif aArguments.site == "inurl":
sSite = "inurl:"
sQuote = ""
else:
sSite = ""
sQuote = "%22"
if aArguments.cat:
lCategory = aArguments.cat.split(",")
if aArguments.excl:
lExcludeDomains = aArguments.excl.split(",")
try:
fInputFile = open(sInputFile, 'r')
lInputFile = fInputFile.readlines()
if aArguments.help2:
get_comment()
if aArguments.help2:
get_comment()
if aArguments.cats:
sCatList = get_categories()
print(f"\nCurrent categories on file are: \n{sCatList}\n")
exit(0)
if aArguments.help:
sCatList = get_categories()
sArgParser.print_help()
print(f"\nCurrent categories on file are: \n{sCatList}\n")
exit(0)
elif not aArguments.query:
print(f"sitedorks: error: the following argument is required: -query")
exit(2)
except FileNotFoundError:
print(sInputFile + " not found...")
exit(2)
SEARCH_ENGINES = {
"google": "https://www.google.com/search?num=100&filter=0&q=",
"baidu": "https://www.baidu.com/s?wd=",
"bing": "https://www.bing.com/search?&count=100&q=",
"bing-ecosia": "https://www.ecosia.org/search?&q=",
"duckduckgo": "https://duckduckgo.com/?q=",
"yandex": "https://yandex.com/search/?text=",
"yahoo": "https://search.yahoo.com/search?n=100&p="
}
if aArguments.engine in SEARCH_ENGINES:
sQuery = SEARCH_ENGINES[aArguments.engine] + urllib.parse.quote(aArguments.query) + "+AND+("
else:
# handle invalid search engine
raise ValueError("Invalid search engine specified")
iFirst = 0
iCount = 0
iUrls = 0
dQuery = {}
if aArguments.cat:
iLines = sum(1 for s in lInputFile if "," + aArguments.cat in s)
if iLines == 0:
print(f"Category '" + aArguments.cat + "' not found, exiting...")
exit(2)
else:
iLines = len(open(sInputFile).readlines())
sEndQuery = ")"
for sInputFileLine in lInputFile:
if aArguments.filter:
if aArguments.filter not in sInputFileLine:
continue
sInputFileLine = sInputFileLine.strip()
if sInputFileLine[0] == "#":
continue
lInputFileLineCsv = sInputFileLine.split(",")
if aArguments.excl:
if lInputFileLineCsv[0] in lExcludeDomains:
continue
try:
if aArguments.cat and lInputFileLineCsv[1] not in lCategory:
continue
except:
print(f"Error in CSV file")
iCount += 1
if iFirst == 0:
dQuery[iUrls] = ""
dQuery[iUrls] += sQuery + sSite + sQuote + lInputFileLineCsv[0] + sQuote
else:
dQuery[iUrls] += "+|+" + sSite + sQuote + lInputFileLineCsv[0] + sQuote
if iFirst == 0: iFirst = 1
if iCount % iNewUrlAfter == 0:
iUrls += 1
iFirst = 0
for i in range(len(dQuery)):
sSingleQuery = dQuery.get(i, '')
if sSingleQuery:
if aArguments.echo:
print(sSingleQuery)
webbrowser.get(aArguments.browser).open(sSingleQuery + sEndQuery)
time.sleep(int(aArguments.wait))