-
Notifications
You must be signed in to change notification settings - Fork 51
/
hostintel.py
228 lines (183 loc) · 6.38 KB
/
hostintel.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
# Main Application
#
# INCLUDES
#
# Local network functions
import libs.network
# Required for complex command line argument parsing.
import argparse
# Required for configuration files
from configparser import ConfigParser
# Required for CSV
import csv
# Required for STDOUT
import sys
# MODULES: Add additional intelligence source modules here
# Local GeoIP functions
import libs.geoip
# Local DNS functions
import libs.dnsinfo
# Local VirusTotal functions
import libs.vt
# Local PassiveTotal functions
import libs.pt
# Local Shodan functions
import libs.shodaninfo
# Local Censys functions
import libs.censysinfo
# Local ThreatCrowd functions
import libs.threatcrowdinfo
# Local OTX functions
import libs.otx
# Local ISC DShield functions
import libs.isc
#
# COMMAND LINE ARGS
#
# Setup command line argument parsing.
parser = argparse.ArgumentParser(
description='Modular application to look up host intelligence information. Outputs CSV to STDOUT.')
parser.add_argument('ConfigurationFile', help='Configuration file')
parser.add_argument('InputFile',
help='Input file, one host per line (IP, domain, or FQDN host name)')
parser.add_argument('-a','--all', action='store_true', help='Perform All Lookups.')
parser.add_argument('-d','--dns', action='store_true', help='DNS Lookup.')
parser.add_argument('-v','--virustotal', action='store_true', help='VirusTotal Lookup.')
parser.add_argument('-p','--passivetotal', action='store_true', help='PassiveTotal Lookup.')
parser.add_argument('-s','--shodan', action='store_true', help='Shodan Lookup.')
parser.add_argument('-c','--censys', action='store_true', help='Censys Lookup.')
parser.add_argument('-t','--threatcrowd', action='store_true', help='ThreatCrowd Lookup.')
parser.add_argument('-o','--otx', action='store_true', help='OTX by AlienVault Lookup.')
parser.add_argument('-i','--isc', action='store_true', help='Internet Storm Center DShield Lookup.')
parser.add_argument('-r','--carriagereturn', action='store_true', help='Use carriage returns with new lines on csv.')
#
# MAIN PROGRAM
#
# Parse command line arguments.
args = parser.parse_args()
# Parse Configuration File
ConfigFile = ConfigParser()
ConfigFile.read(args.ConfigurationFile)
# Setup the headers list
Headers = []
# Setup the data list
Data = []
# MODULES: Setup additional intelligence source modules here
# Pull GeoIP2 City Database Path
geoip2citydb = ConfigFile.get('GeoIP2','City_Path')
# Setup GeoIP object
try:
GeoIP = libs.geoip.GeoIP(geoip2citydb)
except:
sys.stderr.write("ERROR: Cannot open GeoIP Database!\n")
exit(1)
# Pull the VirusTotal config
vtpublicapi = ConfigFile.get('VirusTotal','PublicAPI')
# Pull the PassiveTotal config
ptusername = ConfigFile.get('PassiveTotal','Username')
ptpublicapi = ConfigFile.get('PassiveTotal','PublicAPI')
# Pull the Shodan config
shodanpublicapi = ConfigFile.get('Shodan','PublicAPI')
# Pull the Censys config
censyssecret = ConfigFile.get('Censys','Secret')
censyspublicapi = ConfigFile.get('Censys','PublicAPI')
# Pull the OTX config
otxpublicapi = ConfigFile.get('OTX','PublicAPI')
# Open file and read into list named hosts
try:
with open(args.InputFile) as infile:
hosts = infile.read().splitlines()
except:
sys.stderr.write("ERROR: Cannot open InputFile!\n")
exit(1)
# Setup CSV to STDOUT
if args.carriagereturn:
output = csv.writer(sys.stdout, lineterminator='\r\n')
else:
output = csv.writer(sys.stdout, lineterminator='\n')
# Add standard header info
Headers.append('Input Host')
# Print Header Flag
PrintHeaders = True
# Abort Flag
Aborted = False
# Iterate through all of the input hosts
for host in hosts:
try:
# Output status
sys.stderr.write('*** Processing {} ***\n'.format(host))
# Clear the row
row = []
# Add the host to the output
row.append(host)
# Lookup DNS
if args.dns or args.all:
DNSInfo = libs.dnsinfo.DNSInfo()
if PrintHeaders:
DNSInfo.add_headers(Headers)
DNSInfo.add_row(host,row)
# Lookup GeoIP
if PrintHeaders:
GeoIP.add_headers(Headers)
GeoIP.add_row(host,row)
# Lookup VirusTotal
if args.virustotal or args.all:
VT = libs.vt.VT(vtpublicapi)
if PrintHeaders:
VT.add_headers(Headers)
VT.add_row(host,row)
# Lookup PassiveTotal
if args.passivetotal or args.all:
PT = libs.pt.PT(ptusername,ptpublicapi)
if PrintHeaders:
PT.add_headers(Headers)
PT.add_row(host,row)
# Lookup Shodan
if args.shodan or args.all:
Shodan = libs.shodaninfo.Shodan(shodanpublicapi)
if PrintHeaders:
Shodan.add_headers(Headers)
Shodan.add_row(host,row)
# Lookup Censys
if args.censys or args.all:
Censys = libs.censysinfo.Censys(censyspublicapi,censyssecret)
if PrintHeaders:
Censys.add_headers(Headers)
Censys.add_row(host,row)
# Lookup ThreatCrowd
if args.threatcrowd or args.all:
ThreatCrowd = libs.threatcrowdinfo.ThreatCrowd()
if PrintHeaders:
ThreatCrowd.add_headers(Headers)
ThreatCrowd.add_row(host,row)
# Lookup OTX
if args.otx or args.all:
OTX = libs.otx.OTX(otxpublicapi)
if PrintHeaders:
OTX.add_headers(Headers)
OTX.add_row(host,row)
# Lookup ISC DShield
if args.isc or args.all:
ISC = libs.isc.ISC()
if PrintHeaders:
ISC.add_headers(Headers)
ISC.add_row(host,row)
# MODULES: Add additional intelligence source modules here
# Add the row to the output data set
Data.append(row)
# Print out the headers
if PrintHeaders:
output.writerow(Headers)
# Print out the data
try:
output.writerow([unicode(field).encode('utf-8') for field in row])
except:
output.writerow([str(field) for field in row])
# This turns off headers for remaining rows
PrintHeaders = False
except:
# There was an error...
sys.stderr.write('ERROR: An exception was raised! Raising original exception for debugging.\n')
raise
# Exit without error
exit(0)