-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexport.py
executable file
·203 lines (148 loc) · 5.33 KB
/
export.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
#!/usr/bin/python3
from __future__ import with_statement
import sys
import socket
import argparse
import sqlite3
import json
import csv
from pathlib import Path
from enum import Enum
from contextlib import closing
class Domain:
"""
Domain Class
"""
def __init__(self, id, name):
self.id = id
self.name = name
self.subdomains = list()
def setSubDomain(self, subdomains):
self.subdomains = subdomains
def addSubDomain(self, subdomain):
self.subdomains.append(subdomain)
def remove(self, subdomain):
self.subdomains.remove(subdomain)
def toJSON(self):
return {"id": self.id, "name": self.name, "subdomains": self.subdomains}
class Subdomain:
"""
Subdomain Class
"""
def __init__(self, id, name, resolvable, asn):
self.id = id
self.name = name
self.resolvable = resolvable
self.ips = list()
self.asn = asn
def setIP(self, ips):
self.ips = ips
def addIP(self, ip):
self.ips.append(ip)
def toJSON(self):
return {"id": self.id, "name": self.name, "resolvable": self.resolvable, "ips": self.ips, "asn": self.asn}
class Command(Enum):
"""
Command Enum Class
"""
PARSE_JSON = "JSON"
PARSE_CSV = "CSV"
def execute(args, command):
"""
Manage command passed from sn0int
"""
# print("[ * ] Executing command [ %s ]" % command.upper())
home = str(Path.home())
conn = connect("%s/%s.db" % (home+'/Library/Application Support/sn0int', args.workspace.lower()))
with closing(conn) as connection:
dump = extract(connection)
export(args, command, dump)
def connect(db):
"""
Connect to the sqlite database
"""
# print("[ * ] Connecting to database [ %s ]" % db)
try:
conn = sqlite3.connect(db)
return conn
except Error as e:
print(e)
return None
def extract(conn):
"""
Extract data from sqlite file
"""
# print("[ * ] Extracting Information")
results = dict()
results['data'] = list()
statement = "SELECT domains.id, domains.value, subdomains.id, subdomains.value, subdomains.resolvable, ipaddrs.value, ipaddrs.asn, ipaddrs.as_org FROM domains LEFT JOIN subdomains ON domains.id = subdomains.domain_id LEFT JOIN subdomain_ipaddrs ON subdomain_ipaddrs.subdomain_id = subdomains.id LEFT JOIN ipaddrs ON subdomain_ipaddrs.ip_addr_id = ipaddrs.id WHERE domains.unscoped = 0 AND subdomains.unscoped = 0 ORDER BY domains.id, subdomains.resolvable DESC"
data = []
with closing(conn.cursor()) as cur:
cur.execute(statement)
data = cur.fetchall()
for row in data:
domain = Domain(row[0], row[1])
subdomain = Subdomain(row[2], row[3], row[4], {"id": row[6], "organisation": row[7]})
for entry in results['data']:
if entry['id'] == domain.id:
domain.setSubDomain(entry['subdomains'])
results['data'].remove(entry)
for subentry in entry['subdomains']:
if subentry['id'] == subdomain.id:
subdomain.setIP(subentry['ips'])
domain.remove(subentry)
subdomain.addIP(row[5])
domain.addSubDomain(subdomain.toJSON())
results['data'].append(domain.toJSON())
return results
def export(args, command, dump):
"""
Export CSV or JSON file
"""
# print("[ * ] Exporting data to file")
if command.upper() == Command.PARSE_JSON.value:
with open(args.filename+".json", 'w') as f:
json.dump(dump, f, indent=4, sort_keys=True)
if command.upper() == Command.PARSE_CSV.value:
with open(args.filename+".csv", 'w') as f:
output = csv.writer(f)
output.writerow(dump['data'][0].keys())
for domain in dump['data']:
struct = dict()
struct = domain.copy()
struct['subdomains'] = list()
for subdomain in domain['subdomains']:
struct['subdomains'].append(subdomain['name'])
output.writerow(struct.values())
def reconnect(args):
"""
Create and bind a socket
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("", args.port))
sock.listen(5)
return sock
def listen(args):
"""
Listen for incoming commands
"""
sock = reconnect(args)
while True:
connection, address = sock.accept()
received_message = connection.recv(1024)
if received_message.decode('ascii'):
execute(args, received_message.decode('ascii'))
connection.close()
sock.close()
if __name__ == '__main__':
"""
Main run
"""
parser = argparse.ArgumentParser()
parser.add_argument("port", help="Port number to run the socket on", type=int)
parser.add_argument("workspace", help="Name of workspace", type=str)
parser.add_argument("filename", help="Name of outputfile", type=str)
args = parser.parse_args()
if(args.port):
listen(args)