-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQueryGenerator.py
executable file
·341 lines (294 loc) · 10.9 KB
/
QueryGenerator.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
""" Take a banner and generate all possible queries from it """
from staticLists.devices import devices
from staticLists.vendors import vendors
import re
import enchant
import random
import json
import functools
import config
from bs4 import BeautifulSoup
from sklearn.feature_extraction import stop_words
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
import logging
class QueryGenerator():
def __init__(self):
self.wordsToNotDiscard = []
for device in devices:
words = device.split(" ")
for word in words:
if word not in self.wordsToNotDiscard:
self.wordsToNotDiscard.append(word)
for vendor in vendors:
words = vendor.split(" ")
for word in words:
if word not in self.wordsToNotDiscard:
self.wordsToNotDiscard.append(word)
def refineFTP(self, text):
'''
This function refines FTP banner text.
:param text: A string of banner text
:return: A string of refined banner text
'''
# keywords for common computer ftp softwares as per the original paper
keywords = [
"filezilla",
"serv-u"
]
"""
Do not process the banner if the FTP banner contains these keywords
"""
if any([keyword in text for keyword in keywords]):
return ""
return text
def refineHTTP(self, text):
'''
This function refines HTTP banner text.
:param text: A string of banner text
:return: A string of refined banner text
'''
HTTPNonErrorCodes = [
"http/",
"100 Continue",
"101 Switching Protocols",
"200 OK",
"201 Created",
"202 Accepted",
"203 Non-authoritative information",
"204 no content",
"205 reset content",
"206 partial content",
"300 multiple choices",
"301 moved permanently",
"302 found",
"303 see other",
"304 not modified",
"305 use proxy",
"306 (unused)",
"307 temporary redirect"]
HTTPErrorCodes = [
"400 bad request",
"401 unauthorized",
"402 payment required",
"403 forbidden",
"404 not found",
"405 method not allowed",
"406 not acceptable",
"407 proxy authentication required",
"408 request timeout",
"409 conflict",
"410 gone",
"411 length required",
"412 precondition failed",
"413 request entity too large",
"414 request-uri too long",
"415 unsupported media type",
"416 request range not satisfiable",
"417 expectation failed",
"500 internet server error",
"501 not implemented",
"502 bad gateway",
"503 service unavailable",
"504 gateway timeout",
"505 http version not supported"]
# keywords for common heavyweight webservers
heavyWebservers = [
"apache",
"iis",
"nginx"]
"""
Do not process the banner if the HTTP response is an error
"""
if any([errorCode in text for errorCode in HTTPErrorCodes]):
return ""
"""
Do not process the banner if it contains heavyweight webservers
"""
if any([webserver in text for webserver in heavyWebservers]):
return ""
for code in HTTPNonErrorCodes:
text.replace(code.lower(), "")
patScript = r"(?is)<script[^>]*>(.*?)</script>"
text = re.sub(patScript, "", text)
patStyle = r"(?is)<style[^>]*>(.*?)</style>"
text = re.sub(patStyle, "", text)
patLinks = r'^https?:\/\/.*[\r\n]*'
text = re.sub(patLinks, "", text)
patLinks = r'^http?:\/\/.*[\r\n]*'
text = re.sub(patLinks, "", text)
reg = re.compile('<[^<]+?>')
text = re.sub(reg, '', text)
dateTime = r'\d+[\/:\-]\d+[\/:\-\s]*[\dAaPpMn]*'
text = re.sub(dateTime, '', text)
soup = BeautifulSoup(text, 'html.parser')
text = soup.text
return text
def refineUPNP(self, text):
'''
This function refines UPNP banner text.
:param text: A string of banner text
:return: a string of refined banner text
'''
# remove uuid
UUIDRemovedBanner = re.sub(
r'\b[0-9a-z]{8}\b-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-\b[0-9a-z]{12}\b',
"",
text)
# remove date
splitted = UUIDRemovedBanner.split("\r")
dateRemovedBanner = []
for word in splitted:
loweredWord = word.lower()
if "date:" not in loweredWord and "location:" not in loweredWord:
dateRemovedBanner.append(word)
# convert to string
refinedText = ""
for ele in dateRemovedBanner:
refinedText = refinedText + "\r\n" + ele
return refinedText
def refineTELNET(self, text):
'''
This function refines Telnet banner text.
:param text: A string of banner text
:return: a string of refined banner text
'''
text = re.sub(r"[^\x00-\x7F]+", " ", text)
text = text.rstrip('\0')
return text
def trim(self, word):
'''
This function removes special characters from a string.
:param word: A string
:return: A string with special characters removed
'''
if word == "":
return word
removeFirst = False
removeLast = False
extra = ["(", ")", "{", "}", "[", "]", "!", "\"", "'", ",",
":", "\\n", "\n", "\\r", "\r", "\\t", "\t", ".", " "]
for ele in extra:
if word[0] is ele:
removeFirst = True
if word[len(word) - 1] is ele:
removeLast = True
if removeFirst:
word = word[1:]
if removeLast:
word = word[:-1]
return word
def removeEscapeChars(self, text):
'''
This function removes escape characters from a string.
:param word: A string
:return: A string with escape characters removed
'''
text = text.replace("\n", " ")
text = text.replace("\t", " ")
return text
def removeStopWords(self, text):
'''
This function removes stop words from a string.
:param word: A string
:return: A string with stop words removed
'''
words = text.split(" ")
filteredWords = [
word for word in words if word not in stop_words.ENGLISH_STOP_WORDS]
return " ".join(filteredWords)
def removeDictionaryWords(self, text):
'''
This function removes dictionary words from a string.
:param word: A string
:return: A string with dictionary words removed
'''
dictionary = enchant.Dict('en_US')
words = [word for word in text.split(" ") if word]
relevantWords = []
for word in words:
cleanWord = self.trim(word)
if cleanWord in self.wordsToNotDiscard:
relevantWords.append(cleanWord)
continue
try:
if not dictionary.check(cleanWord.lower()):
relevantWords.append(cleanWord)
continue
except Exception as e:
logging.exception(
"Exception occurred while removing dictionary words: " + str(e))
return " ".join(relevantWords)
def cleanBanner(self, bannerText, bannerType):
'''
This function refines banner text according to their types.
:param bannerText: A string containing banner's text
:param bannerType: A string containing banner's type
:return: A string with stop words removed
'''
text = bannerText.lower()
text = self.removeEscapeChars(text)
if bannerType == "UPNP":
text = self.refineUPNP(text)
elif bannerType == "HTTP":
text = self.refineHTTP(text)
elif bannerType == "TELNET":
text = self.refineTELNET(text)
elif bannerType == "FTP":
text = self.refineFTP(text)
text = self.removeDictionaryWords(text)
text = self.removeStopWords(text)
return text
def splitOnSpace(self, sentence):
return sentence.split()
def sortCoo(self, cooMatrix):
tuples = zip(cooMatrix.col, cooMatrix.data)
return sorted(tuples, key=lambda x: (x[1], x[0]), reverse=True)
def extractTopnFromVector(self, featureNames, sortedItems, topn=10):
'''
This function obtains the feature names and tf-idf score of top n items.
:param featureNames: A list of feature names
:param sortedItems: A list of sorted items
:param topn: An int specifying number of top words to select
:return: A dictionary mapping top 10 items to their tf-idf scores.
'''
# use only topn items from vector
sortedItems = sortedItems[:topn]
scoreVals = []
featureVals = []
# word index and corresponding tf-idf score
for idx, score in sortedItems:
scoreVals.append(round(score, 3))
featureVals.append(featureNames[idx])
results = dict()
for idx in range(len(featureVals)):
results[featureVals[idx]] = scoreVals[idx]
return results
def generateForAll(self, banners):
'''
This function extracts queries from all banners.
:param banners: A list of all banners
:return: A dictionary mapping banners to their query keywords.
'''
cleanedBanners = []
for banner in banners:
bannerType = banner["type"]
bannerText = banner["banner"]
cleanedBanner = self.cleanBanner(bannerText, bannerType)
cleanedBanners.append(cleanedBanner)
cv = CountVectorizer(
max_df=0.85,
analyzer=self.splitOnSpace,
stop_words='english')
wordCountVector = cv.fit_transform(cleanedBanners)
tfidfTransformer = TfidfTransformer(smooth_idf=True, use_idf=True)
tfidfTransformer.fit(wordCountVector)
featureNames = cv.get_feature_names()
bannersToKeywords = dict()
for i, banner in enumerate(cleanedBanners):
tfidfVector = tfidfTransformer.transform(cv.transform([banner]))
sortedItems = self.sortCoo(tfidfVector.tocoo())
keywords = self.extractTopnFromVector(
featureNames, sortedItems, config.TOP_K_WORDS)
bannersToKeywords[banners[i]["banner"]] = " ".join(keywords.keys())
return bannersToKeywords