-
Notifications
You must be signed in to change notification settings - Fork 98
/
metagoofil.py
383 lines (324 loc) · 12.7 KB
/
metagoofil.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
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
#!/usr/bin/env python
# Standard Python libraries.
import argparse
import os
import queue
import random
import sys
import threading
import time
import urllib
# Third party Python libraries.
# google == 2.0.1, module author changed import name to googlesearch
# https://github.com/MarioVilas/googlesearch/commit/92309f4f23a6334a83c045f7c51f87b904e7d61d
import googlesearch
import requests
# https://stackoverflow.com/questions/27981545/suppress-insecurerequestwarning-unverified-https-request-is-being-made-in-pytho
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
__version__ = "1.2.0"
class DownloadWorker(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
while True:
# Grab URL off the queue.
url = mg.queue.get()
try:
headers = {}
# Assign a User-Agent for each file request.
# No -u
if mg.user_agent == "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)":
headers["User-Agent"] = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
# -u
elif mg.user_agent is None:
user_agent_choice = random.choice(mg.random_user_agents).strip()
headers["User-Agent"] = f"{user_agent_choice}"
# -u "My custom user agent 2.0"
else:
headers["User-Agent"] = mg.user_agent
response = requests.get(
url,
headers=headers,
verify=False,
timeout=mg.url_timeout,
stream=True,
)
# Download the file.
if response.status_code == 200:
try:
size = int(response.headers["Content-Length"])
except KeyError as e:
print(
f"[-] Exception for url: {url} -- {e} does not exist. Extracting file size from "
"response.content length."
)
size = len(response.content)
mg.total_bytes += size
# Strip any trailing /'s before extracting file name. Use response.url in case there were HTTP
# 301/302 redirects.
url_file_name = str(response.url.strip("/").split("/")[-1])
# Decode URL file name if it's encoded. No harm calling urllib.parse.unquote() if the URL file
# name isn't URL encoded.
filename = urllib.parse.unquote(url_file_name, encoding="utf-8")
print(f'[+] Downloading "{filename}" [{size} bytes] from: {response.url}')
with open(os.path.join(mg.save_directory, filename), "wb") as fh:
for chunk in response.iter_content(chunk_size=1024):
if chunk: # Filter out keep-alive new chunks.
fh.write(chunk)
else:
print(f"[-] URL {url} returned HTTP code {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"[-] Exception for url: {url} -- {e}")
mg.queue.task_done()
class Metagoofil:
"""The Metagoofil Class"""
def __init__(
self,
domain,
delay,
save_links,
url_timeout,
search_max,
download_file_limit,
save_directory,
number_of_threads,
file_types,
user_agent,
download_files,
):
self.domain = domain
self.delay = delay
self.save_links = open(save_links, "a") if save_links else None
self.url_timeout = url_timeout
self.search_max = search_max
self.download_file_limit = download_file_limit
self.save_directory = save_directory
# Create queue and specify the number of worker threads.
self.queue = queue.Queue()
self.number_of_threads = number_of_threads
self.file_types = file_types
self.user_agent = user_agent
# Populate a list of random User-Agents.
if self.user_agent is None:
with open("user_agents.txt") as fp:
self.random_user_agents = fp.readlines()
self.download_files = download_files
self.total_bytes = 0
def go(self):
# Kickoff the threadpool.
for i in range(self.number_of_threads):
thread = DownloadWorker()
thread.daemon = True
thread.start()
if "ALL" in self.file_types:
from itertools import product
from string import ascii_lowercase
# Generate all three letter combinations.
self.file_types = ["".join(i) for i in product(ascii_lowercase, repeat=3)]
for filetype in self.file_types:
# Stores URLs with files, clear out for each filetype.
self.files = []
# Search for the files to download.
print(
f"[*] Searching for {self.search_max} .{filetype} files and waiting {self.delay} seconds between searches"
)
query = f"filetype:{filetype} site:{self.domain}"
try:
for url in googlesearch.search(
query,
start=0,
stop=self.search_max,
num=100,
pause=self.delay,
extra_params={"filter": "0"},
user_agent=self.user_agent,
):
self.files.append(url)
except Exception as e:
print(f"[-] EXCEPTION: {e}")
if e.code == 429:
print(
"[*] Google is blocking you for making too many requests. You will need to spread out the "
"Google searches with metagoofil's switches or utilize SSH and dynamic SOCKS proxies. Don't "
"know how to utilize SSH and dynamic SOCKS proxies? Do yourself a favor and pick up a copy of "
"The Cyber Plumber's Handbook and interactive lab (https://gumroad.com/l/cph_book_and_lab) to "
"learn all about Secure Shell (SSH) tunneling, port redirection, and bending traffic like a "
"boss."
)
print("[*] Exiting for now...")
sys.exit(1)
# Since googlesearch.search method retrieves URLs in batches of 100, ensure the file list only contains the
# requested amount.
if len(self.files) > self.search_max:
self.files = self.files[: -(len(self.files) - self.search_max)]
# Download files if specified with -w switch.
if self.download_files:
self.download()
# Otherwise, just display them.
else:
print(f"[*] Results: {len(self.files)} .{filetype} files found")
for file_name in self.files:
print(file_name)
# Save links to output to file.
if self.save_links:
for f in self.files:
self.save_links.write(f"{f}\n")
if self.save_links:
self.save_links.close()
if self.download_files:
print(
"[+] Total download: {} bytes / {:.2f} KB / {:.2f} MB".format(
self.total_bytes, self.total_bytes / 1024, self.total_bytes / (1024 * 1024)
)
)
def download(self):
self.counter = 1
for url in self.files:
if self.counter <= self.download_file_limit:
self.queue.put(url)
self.counter += 1
self.queue.join()
def get_timestamp():
now = time.localtime()
timestamp = time.strftime("%Y%m%d_%H%M%S", now)
return timestamp
def csv_list(string):
return string.split(",")
# http://stackoverflow.com/questions/3853722/python-argparse-how-to-insert-newline-in-the-help-text
class SmartFormatter(argparse.HelpFormatter):
def _split_lines(self, text, width):
if text.startswith("R|"):
return text[2:].splitlines()
# This is the RawTextHelpFormatter._split_lines
return argparse.HelpFormatter._split_lines(self, text, width)
def positive_int(value):
try:
value_int = int(value)
assert value_int >= 0
return value_int
except (AssertionError, ValueError):
raise argparse.ArgumentTypeError(f"invalid value '{value}', must be an int >= 0")
def positive_float(value):
try:
value_float = float(value)
assert value_float >= 0
return value_float
except (AssertionError, ValueError):
raise argparse.ArgumentTypeError(f"invalid value '{value}', must be a float >= 0")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=f"Metagoofil v{__version__} - Search Google and download specific file types.",
formatter_class=SmartFormatter,
)
parser.add_argument(
"-d",
dest="domain",
action="store",
required=True,
help="Domain to search.",
)
parser.add_argument(
"-e",
dest="delay",
action="store",
type=positive_float,
default=30.0,
help=(
"Delay (in seconds) between searches. If it's too small Google may block your IP, too big and your search "
"may take a while. Default: 30.0"
),
)
parser.add_argument(
"-f",
nargs="?",
metavar="SAVE_FILE",
dest="save_links",
action="store",
default=False,
help="R|Save the html links to a file.\n"
"no -f = Do not save links\n"
"-f = Save links to html_links_<TIMESTAMP>.txt\n"
"-f SAVE_FILE = Save links to SAVE_FILE",
)
parser.add_argument(
"-i",
dest="url_timeout",
action="store",
type=positive_int,
default=15,
help="Number of seconds to wait before timeout for unreachable/stale pages. Default: 15",
)
parser.add_argument(
"-l",
dest="search_max",
action="store",
type=positive_int,
default=100,
help="Maximum results to search. Default: 100",
)
parser.add_argument(
"-n",
dest="download_file_limit",
action="store",
type=positive_int,
default=100,
help="Maximum number of files to download per filetype. Default: 100",
)
parser.add_argument(
"-o",
dest="save_directory",
action="store",
default=os.getcwd(),
help='Directory to save downloaded files. Default is current working directory, "."',
)
parser.add_argument(
"-r",
dest="number_of_threads",
action="store",
type=positive_int,
default=8,
help="Number of downloader threads. Default: 8",
)
parser.add_argument(
"-t",
dest="file_types",
action="store",
type=csv_list,
required=True,
help=(
"file_types to download (pdf,doc,xls,ppt,odp,ods,docx,xlsx,pptx). To search all 17,576 three-letter "
'file extensions, type "ALL"'
),
)
parser.add_argument(
"-u",
dest="user_agent",
nargs="?",
default="Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
help="R|User-Agent for file retrieval against -d domain.\n"
'no -u = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"\n'
"-u = Randomize User-Agent\n"
'-u "My custom user agent 2.0" = Your customized User-Agent',
)
parser.add_argument(
"-w",
dest="download_files",
action="store_true",
default=False,
help="Download the files, instead of just viewing search results.",
)
args = parser.parse_args()
if args.save_directory and args.download_files:
print(f"[*] Downloaded files will be saved here: {args.save_directory}")
if not os.path.exists(args.save_directory):
print(f"[+] Creating folder: {args.save_directory}")
os.mkdir(args.save_directory)
if args.save_links is False:
args.save_links = None
elif args.save_links is None:
args.save_links = f"html_links_{get_timestamp()}.txt"
# print(vars(args))
mg = Metagoofil(**vars(args))
mg.go()
print("[+] Done!")