-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDetectMachineLan.py
More file actions
337 lines (288 loc) · 9.75 KB
/
DetectMachineLan.py
File metadata and controls
337 lines (288 loc) · 9.75 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
""" Detect machine LAN """
import sys
import nmap
import re
import os
import smtplib
import time,datetime
import gtk
from optparse import OptionError
from optparse import OptionGroup
from optparse import OptionParser
__author__ = "GoldraK"
__credits__ = "GoldraK"
__version__ = "0.1"
__maintainer__ = "GoldraK"
__email__ = "goldrak@gmail.com"
__status__ = "Development"
class DetectMachineLan():
def __init__(self):
self.version = "0.1"
self.whitelist_file = ""
self.log = False
self.verbose = False
def DetectMachineLan(self):
(opts, args) = self.__handleArguments()
if opts.macsearch and opts.ip:
self.__detectMachinesNetwork(opts.ip)
if opts.macadd:
macs = opts.macadd.split(",")
for x in macs:
self.__writeWhitelist(x)
if opts.macremove:
macs = opts.macremove.split(",")
for x in macs:
self.__removeWhitelist(x)
if opts.ip and opts.macsearch == False:
self.__detectMachinesWhitelist(opts)
def __scanNetwork(self,ip):
nm = nmap.PortScanner()
machines=nm.scan(hosts=ip, arguments='-sP')
return machines
def __detectMachinesNetwork(self,ip):
machines = self.__scanNetwork(ip)
for k,v in machines['scan'].iteritems():
if str(v['status']['state']) == 'up':
print "-------"
try:
print str(v['addresses']['ipv4'])+" --> "+str(v['addresses']['mac'])
except:
print str(v['addresses']['ipv4'])+" --> Mac no detected"
def __detectMachinesWhitelist(self,opts):
whitelist = self.__read_file()
alert_mac = ""
machines = self.__scanNetwork(opts.ip)
for k,v in machines['scan'].iteritems():
if str(v['status']['state']) == 'up':
#print str(v)
try:
if str(v['addresses']['mac']) in whitelist:
msg = 'Mac find '+str(v['addresses']['mac'])+' Ip: '+str(v['addresses']['ipv4'])
if self.verbose:
self.__consoleMessage(msg)
if self.log:
self.__writeLog(msg)
else:
alert_mac = alert_mac+'New mac detected '+str(v['addresses']['mac'])+' Ip: '+str(v['addresses']['ipv4'])+'\n'
msg = 'New mac detected '+str(v['addresses']['mac'])+' Ip: '+str(v['addresses']['ipv4'])
if self.verbose:
self.__consoleMessage(msg)
if self.log:
self.__writeLog(msg)
except:
msg = 'Mac not detected '+str(v['addresses']['ipv4'])
if self.verbose:
self.__consoleMessage(msg)
if self.log:
self.__writeLog(msg)
if opts.emailto:
self.__sendEmail(alert_mac,opts)
if opts.gtk:
self.__gtkinfo(alert_mac)
def __handleArguments(self,argv=None):
"""
This function parses the command line parameters and arguments
"""
parser = OptionParser()
if not argv:
argv = sys.argv
mac = OptionGroup(parser, "Mac", "At least one of these "
"options has to be provided to define the machines")
mac.add_option('--ms','--macsearch', action='store_true', default=False, dest='macsearch', help='Search machine Network')
mac.add_option('--ma','--macadd', action='store', dest='macadd', help='Add mac to whitelist')
mac.add_option('--mr','--macremove', action='store', dest='macremove', help='Remove mac from whitelist')
email = OptionGroup(parser, "Email", "You need user,password,server and destination"
"options has to be provided to define the server send mail")
email.add_option('-u','--user', action='store', dest='user', help='User mail server')
email.add_option('--pwd','--password', action='store', dest='password', help='Password mail server')
email.add_option('-s','--server', action='store', dest='server', help='mail server')
email.add_option('-p','--port', action='store', default='25', dest='port', help='Port mail server')
email.add_option('--et','--emailto', action='store', dest='emailto', help='Destination E-mail')
parser.add_option('-r','--range', action='store', dest='ip', help='Secure network range ')
parser.add_option('--wl','--whitelist', action='store', default='whitelist.txt' , dest='whitelist_file', help='File have Mac whitelist ')
parser.add_option('-l','--log', action='store_true', default=False, dest='log', help='Log acctions script')
parser.add_option('-v','--verbose', action='store_true', default=False, dest='verbose', help='Verbose acctions script')
parser.add_option('-g','--gui', action='store_true', default=False, dest='gtk', help='GTK Windows with info')
parser.add_option_group(mac)
parser.add_option_group(email)
(opts, args) = parser.parse_args()
self.log = opts.log
self.verbose = opts.verbose
self.whitelist_file = opts.whitelist_file
if opts.user or opts.password or opts.server or opts.emailto:
if not all([opts.user, opts.password,opts.server,opts.emailto]):
errMsg = "missing some email option (-u, --pwd, -s, --et), use -h for help"
parser.error(errMsg)
self.__writeLog(errMsg)
sys.exit(-1)
if opts.macsearch and not opts.ip:
errMsg = "missing some range scan option (-r), use -h for help"
parser.error(errMsg)
self.__writeLog(errMsg)
sys.exit(-1)
return opts, args
def __sendEmail(self,alert_mac,opts):
"""
This function send mail with the report
"""
header = 'From: %s\n' % opts.user
header += 'To: %s\n' % opts.emailto
if alert_mac:
header += 'Subject: New machines connected\n\n'
message = header + 'List macs: \n '+str(alert_mac)
else:
header += 'Subject: No intruders - All machines known \n\n'
message = header + 'No intruders'
server = smtplib.SMTP(opts.server+":"+opts.port)
server.starttls()
server.login(opts.user,opts.password)
if self.verbose or self.log:
debugemail = server.set_debuglevel(1)
if self.verbose:
self.__consoleMessage(debugemail)
problems = server.sendmail(opts.user, opts.emailto, message)
print problems
server.quit()
def __gtkinfo(self,alert_mac):
parent = None
if alert_mac:
md = gtk.MessageDialog(parent,
gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_WARNING,
gtk.BUTTONS_CLOSE, 'List macs: \n '+str(alert_mac))
else:
md = gtk.MessageDialog(parent,
gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO,
gtk.BUTTONS_CLOSE, "No intruders - All machines known")
md.run()
def __consoleMessage(self,message):
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
print '['+st+'] '+str(message)
def __writeLog(self,log):
"""
This function write log
"""
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
if os.path.isfile('log.txt'):
try:
file_read = open('log.txt', 'a')
file_read.write('['+st+'] '+log+"\n")
file_read.close()
except IOError:
msg = 'ERROR: Cannot open'+ self.whitelist_file
if self.verbose:
self.__consoleMessage(msg)
sys.exit(-1)
else:
msg = "ERROR: The Whitelist file ", self.whitelist_file, " doesn't exist!"
if self.verbose:
self.__consoleMessage(msg)
sys.exit(-1)
def __writeWhitelist(self,mac):
"""
This function add newmac to whitelist
"""
if re.match("[0-9a-f]{2}([-:])[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", mac.lower()):
if os.path.isfile(self.whitelist_file):
try:
file_read = open(self.whitelist_file, 'a')
file_read.write(mac+"\n")
file_read.close()
msg = "Mac: "+ mac + " add correctly"
if self.verbose:
self.__consoleMessage(msg)
if self.log:
self.__writeLog(msg)
except IOError:
print
msg = 'ERROR: Cannot open'+ self.whitelist_file
if self.verbose:
self.__consoleMessage(msg)
if self.log:
self.__writeLog(msg)
sys.exit(-1)
else:
msg = "ERROR: The Whitelist file "+ self.whitelist_file+ " doesn't exist!"
if self.verbose:
self.__consoleMessage(msg)
if self.log:
self.__writeLog(msg)
sys.exit(-1)
else:
msg = "ERROR: The Mac "+ mac +" not valid!"
if self.verbose:
self.__consoleMessage(msg)
if self.log:
self.__writeLog(msg)
def __removeWhitelist(self,mac):
"""
This function remove newmac from whitelist
"""
if re.match("[0-9a-f]{2}([-:])[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", mac.lower()):
if os.path.isfile(self.whitelist_file):
try:
file_read = open(self.whitelist_file, 'r')
lines = file_read.readlines()
file_read.close()
file_read = open(self.whitelist_file, 'w')
for line in lines:
if line.strip() != mac:
file_read.write(line)
file_read.close()
msg = "Mac "+mac+" remove correctly"
if self.verbose:
self.__consoleMessage(msg)
if self.log:
self.__writeLog(msg)
except IOError:
msg = 'ERROR: Cannot open '+ self.whitelist_file
if self.verbose:
self.__consoleMessage(msg)
if self.log:
self.__writeLog(msg)
sys.exit(-1)
else:
msg = "ERROR: The Whitelist file "+ self.whitelist_file+ " doesn't exist!"
if self.verbose:
self.__consoleMessage(msg)
if self.log:
self.__writeLog(msg)
sys.exit(-1)
else:
msg = "ERROR: The Mac "+ mac + " doesn't exist!"
if self.verbose:
self.__consoleMessage(msg)
if self.log:
self.__writeLog(msg)
def __read_file(self):
"""
This function read the whitelist
"""
whitelist = []
if os.path.isfile(self.whitelist_file):
try:
file_read = open(self.whitelist_file, 'r')
for line in file_read:
if re.match("[0-9a-f]{2}([-:])[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", line.strip().lower()):
whitelist.append(line.strip())
return whitelist
except IOError:
msg = 'ERROR: Cannot open '+ self.whitelist_file
if self.verbose:
self.__consoleMessage(msg)
if self.log:
self.__writeLog(msg)
sys.exit(-1)
else:
msg = "ERROR: The Whitelist file "+ self.whitelist_file+ " doesn't exist!"
if self.verbose:
self.__consoleMessage(msg)
if self.log:
self.__writeLog(msg)
sys.exit(-1)
if __name__ == "__main__":
p = DetectMachineLan()
p.DetectMachineLan()