forked from MJL85/natlas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnet.py
executable file
·322 lines (261 loc) · 8.36 KB
/
mnet.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
317
318
319
320
321
322
#!/usr/bin/python
'''
MNet Suite
mnet.py
Michael Laforest
Copyright (C) 2015-2018 Michael Laforest
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
'''
import sys
import getopt
import datetime
import os
from timeit import default_timer as timer
import mnetsuite
from mnetsuite import MNET_REQUIRES_PYTHON
DEFAULT_OPT_DEPTH = 100
DEFAULT_OPT_TITLE = 'mnet Network Diagram'
DEFAULT_OPT_CONF = './mnet.conf'
def print_syntax():
print('Usage:\n'
' mnet.py diagram -r <root IP>\n'
' -o <output file>\n'
' [-d <max depth>]\n'
' [-c <config file>]\n'
' [-t <diagram title>]\n'
' [-C <catalog file>]\n'
'\n'
' mnet.py tracemac -r <root IP>\n'
' -m <MAC Address>\n'
' [-c <config file>]\n'
'\n'
' mnet.py getmacs -r <root IP>\n'
' -o <output CSV file>\n'
' [-d <max depth>]\n'
' [-c <config file>]\n'
'\n'
' mnet.py newconfig\n'
' mnet.py showconfig [-c <config file>]\n'
' mnet.py checkconfig [-c <config file>]\n'
)
def print_banner():
print('mnet suite v%s' % mnetsuite.__version__)
print('Michael Laforest <[email protected]>')
print('')
def check_requirements():
return (sys.version_info > MNET_REQUIRES_PYTHON)
def main(argv):
opt_root_ip = None
if (len(argv) < 1):
print_banner()
print_syntax()
return
if (argv[0] == 'version'):
print_banner()
if (check_requirements() == 0):
print('Running Python %s (requires %i.%i)' % (sys.version.split(' ')[0], MNET_REQUIRES_PYTHON[0], MNET_REQUIRES_PYTHON[1]))
return
start = timer()
mod = argv[0]
if (mod == 'diagram'):
print_banner()
diagram(argv[1:])
elif (mod == 'tracemac'):
print_banner()
tracemac(argv[1:])
elif (mod == 'getmacs'):
print_banner()
getmacs(argv[1:])
elif (mod == 'newconfig'):
generate_config()
return
elif (mod == 'showconfig'):
show_config(argv[1:])
return
elif (mod == 'checkconfig'):
check_config(argv[1:])
return
else:
print_banner()
print_syntax()
s = timer() - start
h=int(s/3600)
m=int((s-(h*3600))/60)
s=s-(int(s/3600)*3600)-(m*60)
print('Completed in %i:%i:%.2fs' % (h, m, s))
def diagram(argv):
opt_root_ip = None
opt_output = None
opt_catalog = None
opt_depth = DEFAULT_OPT_DEPTH
opt_title = DEFAULT_OPT_TITLE
opt_conf = DEFAULT_OPT_CONF
try:
opts, args = getopt.getopt(argv, 'o:d:r:t:F:c:C:')
except getopt.GetoptError:
print_syntax()
sys.exit(1)
for opt, arg in opts:
if (opt == '-r'):
opt_root_ip = arg
if (opt == '-o'):
opt_output = arg
if (opt == '-d'):
opt_depth = int(arg)
if (opt == '-t'):
opt_title = arg
if (opt == '-c'):
opt_conf = arg
if (opt == '-C'):
opt_catalog = arg
if ((opt_root_ip == None) | (opt_output == None)):
print_syntax()
print('Invalid arguments.')
return
print(' Config file: %s' % opt_conf)
print(' Output file: %s' % opt_output)
print('Out Catalog file: %s' % opt_catalog)
print(' Root node: %s' % opt_root_ip)
print(' Discover depth: %s' % opt_depth)
print(' Diagram title: %s' % opt_title)
print()
# load the config
config = mnetsuite.mnet_config()
if (config.load(opt_conf) == 0):
return 0
# start discovery
network = mnetsuite.mnet_network(config)
network.set_max_depth(opt_depth)
network.discover(opt_root_ip)
network.discover_details()
# outputs
if (opt_output != None):
diagram = mnetsuite.mnet_output_diagram(network)
diagram.generate(opt_output, opt_title)
if (opt_catalog != None):
catalog = mnetsuite.mnet_output_catalog(network)
catalog.generate(opt_catalog)
def tracemac(argv):
opt_root_ip = None
opt_mac = None
opt_conf = DEFAULT_OPT_CONF
try:
opts, args = getopt.getopt(argv, 'r:c:m:')
except getopt.GetoptError:
print_syntax()
return
for opt, arg in opts:
if (opt == '-r'):
opt_root_ip = arg
if (opt == '-c'):
opt_conf = arg
if (opt == '-m'):
opt_mac = arg
if ((opt_root_ip == None) | (opt_mac == None)):
print_syntax()
print('Invalid arguments.')
return
print(' Config file: %s' % opt_conf)
print(' Root node: %s' % opt_root_ip)
print(' MAC address: %s' % opt_mac)
print('\n')
# load the config
config = mnetsuite.mnet_config()
if (config.load(opt_conf) == 0):
return 0
trace = mnetsuite.mnet_tracemac(config)
# start
print('Start trace.')
print('------------')
ip = opt_root_ip
while (ip != None):
ip = trace.trace(ip, opt_mac)
print('------------')
print('Trace complete.\n')
def getmacs(argv):
opt_root_ip = None
opt_output = None
opt_conf = DEFAULT_OPT_CONF
opt_depth = DEFAULT_OPT_DEPTH
try:
opts, args = getopt.getopt(argv, 'o:r:c:d:')
except getopt.GetoptError:
print_syntax()
return
for opt, arg in opts:
if (opt == '-r'):
opt_root_ip = arg
if (opt == '-d'):
opt_depth = int(arg)
if (opt == '-c'):
opt_conf = arg
if (opt == '-o'):
opt_output = arg
if ((opt_root_ip == None) | (opt_output == None)):
print_syntax()
print('Invalid arguments.')
return
print(' Config file: %s' % opt_conf)
print(' Output file: %s' % opt_output)
print(' Root node: %s' % opt_root_ip)
print(' Discover depth: %s' % opt_depth)
print('\n')
# load the config
config = mnetsuite.mnet_config()
if (config.load(opt_conf) == 0):
return 0
# start discovery
network = mnetsuite.mnet_network(config)
network.set_max_depth(opt_depth)
network.discover(opt_root_ip)
# get macs
mac = mnetsuite.mnet_mac(config)
macs = mac.get_macs_from_network_discovery(network, 1)
# generate output csv
if (opt_output):
mac.output_csv(opt_output)
def generate_config():
conf = mnetsuite.config.mnet_config()
print('%s' % conf.generate_new())
def show_config(argv):
opt_conf = DEFAULT_OPT_CONF
try:
opts, args = getopt.getopt(argv, 'c:')
except getopt.GetoptError:
print_syntax()
sys.exit(1)
for opt, arg in opts:
if (opt == '-c'):
opt_conf = arg
try:
conf = open(opt_conf, 'r').read()
except:
print('Unable to open config file.')
return
print('%s' % conf)
def check_config(argv):
opt_conf = DEFAULT_OPT_CONF
try:
opts, args = getopt.getopt(argv, 'c:')
except getopt.GetoptError:
print_syntax()
sys.exit(1)
for opt, arg in opts:
if (opt == '-c'):
opt_conf = arg
# load the config
config = mnetsuite.mnet_config()
config.validate_config(opt_conf)
if __name__ == "__main__":
main(sys.argv[1:])