/
net_inetSourceAnalysis.py
316 lines (242 loc) · 11.8 KB
/
net_inetSourceAnalysis.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
# Copyright (C) 2013-2015 Ragpicker Developers.
# This file is part of Ragpicker Malware Crawler - http://code.google.com/p/malware-crawler/
import httplib2
import logging
import re
import urllib
import urllib2
from core.abstracts import Processing
from core.commonutils import convertDirtyDict2ASCII
try:
from yapsy.IPlugin import IPlugin
except ImportError:
raise ImportError, 'Yapsy (Yet Another Plugin System) is required to run this program : http://yapsy.sourceforge.net'
MALICIOUS = "Malicious"
log = logging.getLogger("ProcessingInetSourceAnalysis")
class InetSourceAnalysis(IPlugin, Processing):
def run(self, objfile):
self.key = "InetSourceAnalysis"
self.score = -1
self.hitcount = 0
self.is_urlvoid = self.options.get("urlvoid", False)
self.is_fortiguard = self.options.get("fortiguard", False)
self.is_urlquery = self.options.get("urlquery", False)
self.is_ipvoid = self.options.get("ipvoid", False)
self.whitelist = self.options.get("whitelist", None)
# IP-Adressen fuer weitere Analyse
self.ip = []
# Dictionary containing all the results of this processing.
self.results = {}
input = objfile.get_url_hostname()
# Check whether an analysis should be performed
if objfile.get_url_protocol() != "file" or not self._isWhitelist(input):
# no analysis
return
rpIP = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', re.IGNORECASE)
rpdFindIP = re.findall(rpIP, input)
rpdSortedIP = sorted(rpdFindIP)
rpdSortedIP = str(rpdSortedIP)
rpdSortedIP = rpdSortedIP[2:-2]
if rpIP == input:
log.debug('%s is an IP.' % input)
self._processIP(input)
else:
log.debug('%s is a URL.' % input)
self._processDomain(input)
# calculate scoring
self.score = self.hitcount * 2
return self.results
def _isWhitelist(self, hostname):
if hostname and self.whitelist and hostname in self.whitelist:
log.info("%s in domain whitelist: %s" % (hostname, self.whitelist))
return True
return False
def _processIP(self, ip):
if self.is_ipvoid:
try:
ipvoid = self._ipVoid(ip)
self.results["IPVoid"] = ipvoid
except Exception as e:
log.error("Service IPVoid Failed: %s" % e)
def _processDomain(self, url):
if self.is_fortiguard:
try:
fortiGuard = self._fortiURL(url)
self.results["FortiGuard"] = fortiGuard
except Exception as e:
log.error("Service FortiGuard Failed: %s" % e)
if self.is_urlvoid:
try:
urlvoid = self._urlVoid(url)
self.results["URLVoid"] = urlvoid
except Exception as e:
log.error("Service URLVoid Failed: %s" % e)
if self.is_urlquery:
try:
urlquery = self._urlQuery(url)
self.results["URLQuery"] = urlquery
except Exception as e:
log.error("Service URLQuery Failed: %s" % e)
for ip in self.ip:
self._processIP(ip)
def _urlQuery(self, urlInput):
httplib2.debuglevel = 4
url = "http://urlquery.net/%s"
action_search = url % "search.php?q=%s" % urlInput
conn = urllib2.urlopen(action_search, timeout=60)
content2String = conn.read()
rpd = re.compile('.* 0\sresults\sreturned*', re.IGNORECASE)
rpdFind = re.findall(rpd, content2String)
if not rpdFind:
# Reports found
log.debug('urlquery Reports found')
self.hitcount += 1
urlqueryResults = []
rpd = re.compile("\shref='(.*?)'\>", re.IGNORECASE)
rpdFindReport = re.findall(rpd, content2String)
rpd = re.compile("\<td\>\<a\stitle='(.*?)'\shref='report.php", re.IGNORECASE)
rpdFindReportUrl = re.findall(rpd, content2String)
rpd = re.compile("\<td\salign='center'\>\<b\>(.*?)\<\/b\>\<\/td\>", re.IGNORECASE)
rpdFindAlertsIDS = re.findall(rpd, content2String)
rpd = re.compile("\<td\>\<nobr\>\<center\>(.*?)\<\/center\>\<\/nobr\>\<\/td\>", re.IGNORECASE)
rpdFindDatum = re.findall(rpd, content2String)
rpd = re.compile("align='left'\stitle='(.*?)'\swidth='\d{2}'\sheight='\d{2}'\s/>", re.IGNORECASE)
rpdFindLand = re.findall(rpd, content2String)
i = 0
datum = ''
for datum in rpdFindDatum:
result = {}
result["datum"] = datum
result["alerts_ids"] = rpdFindAlertsIDS[i]
result["country"] = rpdFindLand[i]
result["reportUrl"] = convertDirtyDict2ASCII(rpdFindReportUrl[i])
result["report"] = url % rpdFindReport[i]
urlqueryResults.append(result)
i += 1
urlquery = {'url':urlInput, 'urlResult':urlqueryResults}
else:
log.debug('urlquery Reports NOT found')
urlquery = {'url': urlInput, 'urlResult' : 'NOT listed'}
return urlquery
def _fortiURL(self, urlInput):
httplib2.debuglevel = 4
conn = urllib2.urlopen("http://www.fortiguard.com/ip_rep.php?data=" + urlInput + "&lookup=Lookup", timeout=60)
content2String = conn.read()
rpd = re.compile('h3\sstyle\=\"float:\sleft\"\>Category:\s(.+)\<\/h3', re.IGNORECASE)
rpdFind = re.findall(rpd, content2String)
rpdSorted = rpdFind
m = ''
for m in rpdSorted:
fortiGuard = urlInput + " Categorization: " + m
if MALICIOUS in m:
self.hitcount += 1
if m == '':
fortiGuard = urlInput + " Categorization: Uncategorized"
return fortiGuard
def _urlVoid(self, urlInput):
httplib2.debuglevel = 4
conn = urllib2.urlopen("http://urlvoid.com/scan/" + urlInput, timeout=60)
content2String = conn.read()
rpderr = re.compile('An\sError\soccurred', re.IGNORECASE)
rpdFinderr = re.findall(rpderr, content2String)
if "ERROR" in str(rpdFinderr):
_urlvoid = ('http://www.urlvoid.com/')
raw_params = {'url':urlInput, 'Check':'Submit'}
params = urllib.urlencode(raw_params)
request = urllib2.Request(_urlvoid, params, headers={'Content-type':'application/x-www-form-urlencoded'})
page = urllib2.urlopen(request, timeout=60)
page = page.read()
content2String = str(page)
rpd = re.compile('title=\"Find\swebsites\shosted\shere\"\><strong\>(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}).+', re.IGNORECASE)
rpdFind = re.findall(rpd, content2String)
rpdSortedIP = rpdFind
rpd = re.compile('alt=\"Alert\"\s\/>.{9}<a.rel=\"nofollow\"\shref="(.{6,200})\"\stitle', re.IGNORECASE)
rpdFind = re.findall(rpd, content2String)
rpdSortBlacklist = rpdFind
rpd = re.compile('alt\=\"flag\".+\>(.+)\<\/td\>', re.IGNORECASE)
rpdFind = re.findall(rpd, content2String)
rpdSortCountry = rpdFind
rpd = re.compile('HTTP\sStatus\sCode<\/td\>\<td\>(.+)\<\/td\>\<\/tr\>', re.IGNORECASE)
rpdFind = re.findall(rpd, content2String)
rpdSortHTTPResponseCode = rpdFind
rpd = re.compile('\<span\sclass=\"label label-danger\"\>(.+)\<\/span\>', re.IGNORECASE)
rpdFind = re.findall(rpd, content2String)
rpdSortWebsiteStatus = rpdFind
urlResult = {}
if rpdSortedIP:
urlResult["IP"] = rpdSortedIP[0]
# IP fuer weitere Analyse Speichern
self.ip.append(rpdSortedIP[0])
if rpdSortCountry:
urlResult["CountryCode"] = rpdSortCountry[0].strip()
else:
urlResult["CountryCode"] = "No Country listed"
if rpdSortHTTPResponseCode:
urlResult["HTTPResponseCode"] = rpdSortHTTPResponseCode[0]
else:
urlResult["HTTPResponseCode"] = 'HTTP-Response-Code not listed.'
if rpdSortWebsiteStatus:
urlResult["BlacklistStatus"] = rpdSortWebsiteStatus[0]
# URL blacklisted
self.hitcount += 1
blacklist = []
for j in rpdSortBlacklist:
blacklist.append({"Blacklist" : 'Host is listed in blacklist at: ' + j})
self.hitcount += 1
if blacklist:
urlResult["Blacklists"] = blacklist
else:
urlResult["BlacklistStatus"] = 'The website is not blacklisted.'
return {"url" : urlInput, "urlResult" : urlResult}
def _ipVoid(self, ipInput):
httplib2.debuglevel = 4
conn = urllib2.urlopen("http://ipvoid.com/scan/" + ipInput, timeout=60)
content2String = conn.read()
rpNotFound = re.compile('<h1>Report\snot\sfound<\/h1>', re.IGNORECASE)
rpdFindNotFound = re.findall(rpNotFound, content2String)
# Report not found -> exit
if rpdFindNotFound:
log.info("ipvoid.com: Report not found")
return {"ip" : ipInput, "ipResult" : {"BlacklistStatus":"Report not found"}}
rpd = re.compile('title="Detected"\s\/>\s \s<a.rel=\"nofollow\"\shref="(.{6,200})\"\stitle', re.IGNORECASE)
rpdFind = re.findall(rpd, content2String)
rpdSortBlacklist = rpdFind
rpd = re.compile('label-.*\">(.+)\<\/span\>\<\/td\>\<\/tr\>', re.IGNORECASE)
rpdFind = re.findall(rpd, content2String)
rpdSortBlacklistStatus = rpdFind
rpd = re.compile('<tr><td>ISP<\/td><td>(.+)<\/td\>\<\/tr\>', re.IGNORECASE)
rpdFind = re.findall(rpd, content2String)
rpdSortISP = rpdFind
rpd = re.compile('Country\sCode</td><td><img.+alt="Flag"\s\/>.(.+)<\/td\>\<\/tr\>', re.IGNORECASE)
rpdFind = re.findall(rpd, content2String)
rpdSortCountry = rpdFind
ipResult = {}
if "BLACKLISTED" in rpdSortBlacklistStatus[0]:
ipResult["BlacklistStatus"] = rpdSortBlacklistStatus[0]
self.hitcount += 1
blacklist = []
for j in rpdSortBlacklist:
blacklist.append({"Blacklist" : 'IP is listed in blacklist at: ' + j})
self.hitcount += 1
if blacklist:
ipResult["Blacklists"] = blacklist
else:
ipResult["BlacklistStatus"] = rpdSortBlacklistStatus[0]
if rpdSortISP:
ipResult["ISP"] = rpdSortISP[0]
else:
ipResult["ISP"] = 'No ISP listed'
if rpdSortCountry:
ipResult["CountryCode"] = rpdSortCountry[0].strip()
else:
ipResult["CountryCode"] = "No Country listed"
return {"ip" : ipInput, "ipResult" : ipResult}
def _checkIP(self, ipAdress):
# ToDo in config auslagern
if len(ipAdress) > 0 and ipAdress.find("192.168.") == -1 and ipAdress.find("8.8.8.8") == -1:
return True
def _isInURLVoid(self, ipAdress):
for obj in self.ip:
if ipAdress == obj:
return True
return False