-
Notifications
You must be signed in to change notification settings - Fork 6
/
fglookup.py
185 lines (147 loc) · 6.45 KB
/
fglookup.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
#!/usr/bin/env python
'''
Query FortiGuard IP Reputation and Blacklist
'''
import argparse
import urllib2
import urllib
import sys
import re
try:
from bs4 import BeautifulSoup
except ImportError:
sys.exit('Error - Please ensure you install the beautifulsoup4 library (pip install beautifulsoup4)')
class FGLookup:
_UserAgent = 'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0'
_represult = None
_blresult = None
def __init__(self, agent=None):
if agent is not None:
self._UserAgent = agent
def check_blacklist(self, target):
"""
Check if the <target> is in the FortiGuard blacklist.
The target can be an IP address or URL.
"""
url = "http://www.nospammer.net/web/spam_lookup.php"
result = ''
reqdata = urllib.urlencode({'signature': target})
request = urllib2.Request(url, reqdata, headers={'User-Agent': self._UserAgent})
qr = urllib2.urlopen(request, timeout=10)
try:
self._blresult = qr.read()
soup = BeautifulSoup(self._blresult)
dat = soup.select("tr style")
# The dom is screwy so we need to manually parse out the result.
# Or more likely I just need to RTFM the soup docs
block = str(dat[0])
tl = block.find(target)
result = block[tl:].split('<')[0].replace('\"', '')
except urllib2.URLError as e:
if e.code == 400:
result = 'Error: Bad Request'
elif qr.getcode() == 403:
result = 'Error: 403 - Forbidden!'
elif qr.getcode() == 503:
result = 'Error: Service Unavailable!'
return result
def check_reputation(self, target):
url = 'http://www.fortiguard.com/ip_rep/?data=%s&lookup=lookup' % target
result = {}
result['Error'] = None
request = urllib2.Request(url, headers={'User-Agent': self._UserAgent})
qr = urllib2.urlopen(request, timeout=10)
try:
self._represult = qr.read()
soup = BeautifulSoup(self._represult)
sections = soup.find_all(class_='graph_inner')
# Loop through the data sections
for data_section in sections:
# Section contains the date/time and Rating Data.
if data_section.h2.string.startswith('WF Rating History'):
result['Rating_History'] = {}
rows = data_section.find_all('tr')
ratings = []
for row_data in rows:
cells = row_data.find_all('td')
ratings.append((cells[0].string, cells[1].string))
result['Rating_History'] = ratings
# Section contains IPs used by the domain
elif data_section.h2.string.startswith('IP'):
result['IP_Info'] = {}
rows = data_section.find_all('tr')
iplist = []
for row_data in rows:
cells = row_data.find_all('td')
for cell in cells:
ips = cell.find_all('a')
for ip in ips:
iplist.append(ip.string)
result['IP_Info'] = iplist
# Section contains information on hosts
# This looks to list either the most recent or frequent hosts.
elif data_section.h2.string.startswith('Shares the domain'):
result['Shared_Domains'] = {}
rows = data_section.find_all('tr')
domlist = []
for row_data in rows:
cells = row_data.find_all('td')
for cell in cells:
hosts = cell.find_all('a')
for host in hosts:
domlist.append(host.string)
result['Shared_Domains'] = domlist
'''
TODO: When querying using an IP a couple different sections
are available. But I'm not sure if they are useful. The
Geo-IP section seems to be blank.
'''
result['Category'] = soup.h3.string.split(':')[1].lstrip(' ')
except urllib2.URLError as e:
if e.code == 400:
result['Error'] = 'Error: Bad Request'
elif qr.getcode() == 403:
result['Error'] = 'Error: 403 - Forbidden!'
elif qr.getcode() == 503:
result['Error'] = 'Error: Service Unavailable!'
return result
def print_reputation(rep):
rep_data = '\nReputation Data\n'
rep_data += ' {:20}{:64}\n'.format('Category:', rep['Category'])
if 'Rating_History' in rep:
rep_data += ' {:20}{:64}\n'.format('Rating History:', '')
for rating in rep['Rating_History']:
rep_data += ' {:20}{:64}\n'.format('', '%s - %s' % (rating[0], rating[1]))
else:
rep_data += ' {:20}{:64}\n'.format('Rating History:', 'Unknown')
if 'IP_Info' in rep:
rep_data += ' {:20}{:64}\n'.format('IP Info:', '')
for ip in rep['IP_Info']:
rep_data += ' {:20}{:64}\n'.format('', ip)
else:
rep_data += ' {:20}{:64}\n'.format('IP Info:', 'Unknown')
if 'Shared_Domains' in rep:
rep_data += ' {:20}{:64}\n'.format('Shared Domains:', '')
for host in rep['Shared_Domains']:
rep_data += ' {:20}{:64}\n'.format('', host)
else:
rep_data += ' {:20}{:64}\n'.format('Shared Domains:', 'Unknown')
print rep_data
def main():
parser = argparse.ArgumentParser(description="Query FortiGuard Reputation and Blacklist")
parser.add_argument("url", help="The target URL or IP")
parser.add_argument('-b', '--blacklist', dest='blacklist', action='store_true', help="Query Blacklist")
parser.add_argument('-r', '--rep', dest='rep', action='store_true', help="Query Reputation")
args = parser.parse_args()
print '\n Querying for: %s' % args.url
fgl = FGLookup()
if args.blacklist:
print ' Blacklist: %s' % fgl.check_blacklist(args.url)
elif args.rep:
print_reputation(fgl.check_reputation(args.url))
else:
print ' Blacklist: %s' % fgl.check_blacklist(args.url)
print_reputation(fgl.check_reputation(args.url))
print ''
if __name__ == '__main__':
main()