forked from opendns/og-miner
-
Notifications
You must be signed in to change notification settings - Fork 3
/
flatten.py
executable file
·198 lines (156 loc) · 5.02 KB
/
flatten.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
#!/usr/bin/env python
import sys
import json
import csv
import time
import datetime
import math
import pprint as pp
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: {0} <graph.json>".format(sys.argv[0]))
sys.exit(0)
with open(sys.argv[1], "rU") as infile:
graph = json.load(infile)
nodes_by_type = dict()
for node in graph['nodes']:
node_type = None
if 'type' in node:
node_type = node['type']
if node_type not in nodes_by_type:
nodes_by_type[node_type] = list()
node_summary = {
"#FQDN" : node['id']
}
if node_type == "domain":
# Popularity
try: node_summary['popularity'] = node['investigate']['security']['popularity']
except: pass
# Whois:age
try:
created = node['investigate']["domain_whois"][0]["created"]
year, month, day= created.split("-")
year = int(year)
month = int(month)
day = int(day)
age = datetime.date.today() - datetime.date(year, month, day)
node_summary['age'] = math.log(age.days, 10)
except:pass
# Number of IPs, Prefixes, ASNs, Countries
try: node_summary['ips'] = node['investigate']['rr_history']['rips']
except:pass
try: node_summary['prefixes'] = node['investigate']['rr_history']['prefixes_count']
except:pass
try: node_summary['asns'] = node['investigate']['rr_history']['asns_count']
except:pass
try: node_summary['countries'] = node['investigate']['rr_history']['country_count']
except:pass
# TTL min/max/stddev
try: node_summary['ttl_min'] = node['investigate']['rr_history']['ttls_min']
except:pass
try: node_summary['ttl_max'] = node['investigate']['rr_history']['ttls_max']
except:pass
try: node_summary['ttl_stddev'] = node['investigate']['rr_history']['ttls_stddev']
except:pass
# Geodistance sum + mean
try: node_summary['geo_sum'] = node['investigate']['rr_history']['geo_distance_sum']
except:pass
try: node_summary['geo_mean'] = node['investigate']['rr_history']['geo_distance_mean']
except:pass
# Entropy + Perplexity
try: node_summary['entropy'] = node['investigate']['security']['entropy']
except:pass
try: node_summary['perplexity'] = node['investigate']['security']['perplexity']
except:pass
#try: node_summary['odns:age'] = node['investigate']['rr_history']['age']
#except: pass
#try: node_summary['odns:dga:score'] = node['investigate']['security']['dga_score']
#except: pass
# Infected score
try: node_summary['status'] = node['investigate']['categorization']['status']
except: pass
# VirusTotal summary
max_positives = 0
try:
for item in node['virustotal']['detected_urls']:
if item["positives"] > max_positives:
max_positives = item['positives']
except: pass
node_summary['vt:positives'] = max_positives
elif node_type == "ip":
try: node_summary['city'] = node['geoip2']['city']
except:pass
try: node_summary['country'] = node['geoip2']['country']['name']
except:pass
try: node_summary['subdivision'] = node['geoip2']['subdivision']['name']
except:pass
try: node_summary['postal'] = node['geoip2']['postal']
except:pass
try: node_summary['continent'] = node['geoip2']['continent']['code']
except:pass
try:
node_summary['latitude'] = node['geoip2']['location']['latitude']
node_summary['longitude'] = node['geoip2']['location']['longitude']
except:pass
try: node_summary['isp'] = node['shodan']['isp']
except:pass
try: node_summary['org'] = node['shodan']['org']
except: pass
try:
node_summary['vulns'] = 0
node_summary['vulns'] = len(node['shodan']['vulns'])
except: pass
try:
node_summary['last_scan'] = node['shodan']['last_update']
except: pass
nodes_by_type[node_type].append(node_summary)
headers_by_type = {
"domain" : [
"#FQDN",
"popularity", "age",
"ips", "prefixes", "asns", "countries",
"ttl_min", "ttl_max", "ttl_stddev",
"geo_sum", "geo_mean",
"entropy", "perplexity",
"status", "vt:positives"
],
"ip" : [
"#FQDN",
"city", "country", "subdivision", "postal", "continent",
"latitude", "longitude",
"isp", "org",
"vulns"
],
"url" : [ "#FQDN" ],
"as" : [ "#FQDN" ],
"email" : [ "#FQDN" ],
"port" : [ "#FQDN" ],
"hash" : [ "#FQDN" ]
}
for k, v in nodes_by_type.items():
keys = list()
for item in v:
for k2 in item.keys():
if k2 not in keys:
keys.append(k2)
header = headers_by_type[k]
for h in header:
if h in keys:
keys.remove(h)
csv_header = header + keys
with open("./output/flat-" + k + ".csv", "w") as fp:
writer = csv.writer(fp, delimiter=',')
writer.writerow(csv_header)
for item in v:
row = list()
for k1 in csv_header:
if k1 in item:
row.append(item[k1])
else:
row.append(None)
try:
writer.writerow(row)
except:
print("Error: Weird encoding. Skipping item. (TODO: Fix this asap)")
continue
#pp.pprint(nodes_by_type)