forked from SmeegeSec/SmeegeScrape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmeegeScrape.py
executable file
·284 lines (252 loc) · 13.5 KB
/
SmeegeScrape.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
#!/usr/bin/env python
"""
Name: SmeegeScrape
Version: 0.6
Date: 01/27/2014
Author: Smeege
Contact: [email protected]
Description: SmeegeScrape.py is a simple python script to scrape text from various sources including local files and
web pages, and turn the text into a custom word list. A customized word list has many uses, from web
application testing to password cracking, having a specific set of words to use against a target can
increase efficiency and effectiveness during a penetration test. I realize there are other text scrapers
publicly available however I feel this script is simple, efficient, and specific enough to warrant its own
release. This script is able to read almost any file which has cleartext in it that python can open. I
have also included support for file formats such as pdf, html, docx, and pptx.
Copyright (c) 2014, Smeege Sec (http://www.smeegesec.com)
All rights reserved.
Please see the attached LICENSE file for additional licensing information.
"""
import argparse
import docx #https://pypi.python.org/pypi/python-docx, pip install docx (must use legacy docx module)
import pptx #https://pypi.python.org/pypi/python-pptx, pip install python-pptx
import glob
import mimetypes
import nltk #http://pypi.python.org/pypi/nltk, pip install -Iv nltk==2.0.4
import os
import PyPDF2 #https://pypi.python.org/pypi/PyPDF2/1.19, pip install PyPDF2
import re
import string
import fnmatch
from argparse import RawTextHelpFormatter
from collections import OrderedDict
from urllib import quote
from urllib2 import urlopen
title = "\n===SmeegeSec's========================================= \n"
title += " ____ ____ \n"
title += " / __/_ _ ___ ___ ___ ____ / __/__________ ____ ___ \n"
title += " _\ \/ ' \/ -_) -_) _ `/ -_)\ \/ __/ __/ _ `/ _ \/ -_) \n"
title += "/___/_/_/_/\__/\__/\_, /\__/___/\__/_/ \_,_/ .__/\__/ \n"
title += " /___/ /_/ \n"
title += "================================================v0.6=== \n"
title += "\n"
print title
parser = argparse.ArgumentParser(description=title,formatter_class=RawTextHelpFormatter)
operationGroup = parser.add_mutually_exclusive_group(required=True)
operationGroup.add_argument('-f', action="store", dest="localFile", help="Specify a local file to scrape.")
operationGroup.add_argument('-d', action="store", dest="fileDirectory", help="Specify a directory to scrape all supported files.")
operationGroup.add_argument('-u', action="store", dest="webUrl", help="Specify a url to scrape page content (correct format: http(s)://www.smeegesec.com)")
operationGroup.add_argument('-l', action="store", dest="webList", help="Specify a text file with a list of URLs to scrape (separated by newline).")
optionGroup = parser.add_argument_group('paramters and options')
optionGroup.add_argument('-r', action="store_true", dest="fileDirectoryRecursive", help="Scan directories recursively (only applies when used with -d)")
optionGroup.add_argument('-o', action="store", dest="outputFile", help="Output filename. (Default: smeegescrape_out.txt)")
optionGroup.add_argument('-i', action="store_true", dest="integers", help="Remove integers [0-9] from the final output.")
optionGroup.add_argument('-s', action="store_true", dest="specials", help="Remove special characters (only alphanum allowed) from the final output.")
optionGroup.add_argument('-n', action="store_true", dest="nonprintable", help="Remove all non-printable ASCII characters from the final output.")
optionGroup.add_argument('-min', action="store", dest="minLength", type=int, help="Set the minimum number of characters for each word (Default: 3).")
optionGroup.add_argument('-max', action="store", dest="maxLength", type=int, help="Set the maximum number of characters for each word (Default: 30).")
args = parser.parse_args()
#PyPDF2 does not always work very well with extracting text from pdf. It mainly depends on how the PDF was generated.
def getPDFContent(path):
# Load PDF into PyPDF2
pdf = PyPDF2.PdfFileReader(file(path, "rb"))
if pdf.isEncrypted:
print 'pdf ' + path + ' is encrypted, trying blank password...'
pdf.decrypt('') #If you want to provide your own password for an encrypted pdf, modify code here.
content = ""
# Iterate PDF pages
for i in range(0, pdf.getNumPages()):
# Extract text from page and add to content
content += pdf.getPage(i).extractText() + "\n"
# Collapse whitespace
content = " ".join(content.replace(u"\xa0", " ").strip().split())
content = content.encode("ascii", "ignore")
content = content.split(' ')
if args.minLength or args.maxLength:
for word in content:
if not(len(word.translate(None,charBlacklist)) < minl or len(word) > maxl):
wordList.append(str(word.encode('ascii','ignore')).translate(None,charBlacklist))
else:
for word in content:
wordList.append(str(word.encode('ascii','ignore')).translate(None,charBlacklist))
def webUrl(fullUrl):
#urllib2 works best with a specific url format
validUrl = re.compile(
r'^(?:http)s?://|' # http:// or https://
r'^(?:http)s?://www.'
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
r'localhost|' #localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
if validUrl.match(fullUrl):
finalList = []
urlInput = quote(fullUrl, safe="%/:=&?~#+!$,;'@()*[]")
urlInput = urlInput.strip('%0A')
try:
u = urlopen(urlInput)
html = u.read()
raw = nltk.clean_html(html)
tokens = nltk.word_tokenize(raw)
if args.minLength or args.maxLength:
for token in tokens:
if not(len(token.translate(None,charBlacklist)) < minl or len(token) > maxl):
wordList.append(str(token).translate(None,charBlacklist))
else:
for token in tokens:
wordList.append(str(token).translate(None,charBlacklist))
print "Scraping URL - {0}".format(fullUrl)
except Exception as e:
print 'There was an error connecting to or parsing {0}'.format(fullUrl)
print 'Error: %s' % e
print 'If you are not using NLTK version 2.0.4 please uninstall the current version and install 2.0.4 using \'sudo pip install -Iv nltk==2.0.4\''
else:
print 'INVALID URL - {0}. Format must be http(s)://www.smeegesec.com.'.format(fullUrl)
def webList(webListFile):
if os.path.isfile(webListFile):
with open(webListFile) as f:
webList = f.readlines()
for url in webList:
webUrl(url.rstrip('\n'))
f.close()
else:
print 'Error opening file: {0}'.format(fileInput)
def localFile(fileInput):
if os.path.isfile(fileInput):
print "Scraping Local File - {0}".format(fileInput)
mimetypes.init()
file_type, file_encoding = mimetypes.guess_type(fileInput)
print file_type
if file_type == 'application/pdf':
getPDFContent(fileInput)
elif file_type == 'text/html':
raw = nltk.clean_html(open(fileInput).read())
tokens = nltk.word_tokenize(raw)
if args.minLength or args.maxLength:
for token in tokens:
if not(len(token.translate(None,charBlacklist)) < minl or len(token) > maxl):
wordList.append(str(token).translate(None,charBlacklist))
else:
for token in tokens:
wordList.append(str(token).translate(None,charBlacklist))
elif file_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
document = docx.opendocx(fileInput)
sentances = docx.getdocumenttext(document)
sentances = map(lambda s: s.encode("ascii", "ignore"), sentances)
if args.minLength or args.maxLength:
for sentance in sentances:
for word in set(sentance.split()):
if not(len(str(word).translate(None,charBlacklist)) < minl or len(str(word)) > maxl):
wordList.append(str(word.encode('ascii','ignore')).translate(None,charBlacklist))
else:
for sentance in sentances:
for word in set(sentance.split()):
wordList.append(str(word.encode('ascii','ignore')).translate(None,charBlacklist))
elif file_type == 'application/vnd.openxmlformats-officedocument.presentationml.presentation' or file_type == 'application/x-mspowerpoint.12':
try:
prs = pptx.Presentation(fileInput)
text_runs = list()
for slide in prs.slides:
for shape in slide.shapes:
if not shape.has_textframe:
continue
for paragraph in shape.textframe.paragraphs:
for run in paragraph.runs:
text_runs.append(run.text)
if args.minLength or args.maxLength:
for sentance in text_runs:
for word in set(sentance.split()):
if not(len(str((word.translate(None,charBlacklist)))) < minl or len(str(word)) > maxl):
wordList.append(str(word.encode('ascii','ignore')).translate(None,charBlacklist))
else:
for sentance in text_runs:
for word in set(sentance.split()):
wordList.append(str(word.encode('ascii','ignore')).translate(None,charBlacklist))
except Exception as e:
print 'Error opening file: {0}'.format(fileInput)
pass
else: #'text/plain' or unknown format
try:
words = set(open(fileInput).read().split())
if args.minLength or args.maxLength:
for word in words:
if not(len(str((word.translate(None,charBlacklist)))) < minl or len(str(word)) > maxl):
wordList.append(str(word).translate(None,charBlacklist))
else:
for word in words:
wordList.append(str(word).translate(None,charBlacklist))
except:
print 'Error opening file: {0}'.format(fileInput)
pass
else:
print 'Error opening file: {0}'.format(fileInput)
def fileDir(dirInput, recursive):
if os.path.isdir(dirInput):
print "Scraping Files in Local Directory {0}- {1}".format('(recursively) ' if recursive else '', dirInput)
mimetypes.init()
fileTypes = ('*.conf', '*.cs', '*.js', '*.c', '*.cpp', '*.c++', '*.pdf', '*.docx', '*.txt', '*.csv', '*.py', '*.cpp', '*.pl', '*.log', '*.rtf', '*.css', '*.dat', '*.html', '*.php', '*.pps', '*.ppt', '*.pptx', '*.sh', '*.xml', '*.xsl')
if recursive:
for root, dirs, files in os.walk(dirInput):
for fileType in fileTypes:
for file_name in fnmatch.filter(files, fileType):
file_path = os.path.join(root, file_name)
file_type, file_encoding = mimetypes.guess_type(file_path)
localFile(file_path)
else:
for fileType in fileTypes:
for globFile in glob.glob(os.path.join(dirInput, fileType)):
file_type, file_encoding = mimetypes.guess_type(globFile)
localFile(globFile)
else:
print 'Error accessing directory: {0}'.format(dirInput)
def output():
try:
if not args.outputFile:
args.outputFile = 'smeegescrape_out.txt'
outputFile = open(args.outputFile, 'w')
wordListFinal = OrderedDict.fromkeys(wordList).keys()
for word in wordListFinal:
outputFile.write(word)
outputFile.write('\n')
outputFile.close()
print '\n{0} unique words have been scraped.'.format(len(wordListFinal))
print 'Output file successfully written: {0}'.format(outputFile.name)
except Exception as e:
print 'Error creating output file: {0}'.format(outputFile.name)
print e
if __name__ == "__main__":
wordList = list()
charBlacklist = ""
if args.minLength or args.maxLength:
minl = args.minLength if args.minLength else 3
maxl = args.maxLength if args.maxLength else 30
if minl > maxl:
print 'Argument minLength cannot be greater than maxLength. Setting defaults to min=3 max=30.'
minl = 3
maxl = 30
charBlacklist = ""
if args.specials:
charBlacklist += "~`!@#$%^&*()_\"--+=[]{}|/\:;'<,>.?/"
if args.integers:
charBlacklist += string.digits
if args.nonprintable:
non_printable = filter(lambda x: x not in string.printable, map(chr, range(0, 256)))
charBlacklist += ''.join(non_printable)
if args.localFile:
localFile(args.localFile)
if args.fileDirectory:
fileDir(args.fileDirectory, args.fileDirectoryRecursive)
if args.webUrl:
webUrl(args.webUrl)
if args.webList:
webList(args.webList)
output()