-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
205 lines (169 loc) · 7.05 KB
/
main.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
import monitoringConfig as config
import monitoringPackets as monitor
import monitoringMysql as mysql
from datetime import datetime
from pprint import pprint
## helper functions
def isHostStillTraining(dateNow,dateThen,trainingTime):
"""Determines the training status based on the given dates and times, returns a Boolean."""
differenceDays = (dateNow - dateThen).days
if differenceDays >= trainingTime:
return False
else:
return True
def normalize(value, minValue, maxValue):
if (maxValue - minValue) != 0:
normalized = (value - minValue) / (maxValue - minValue)
else:
normalized = 1
return normalized
def getDatabaseConnection():
connection = mysql.mysqlConnect(
config.dbinfo.host,
config.dbinfo.user,
config.dbinfo.password,
config.dbinfo.database
)
return connection
def doesHostExist (SrcMAC):
cursor = mysql.mysqlGetHost(getDatabaseConnection(),SrcMAC)
if cursor.rowcount > 0:
return True
else:
return False
def getHostDateOfCreation(SrcMAC):
cursor = mysql.mysqlGetHost(getDatabaseConnection(),SrcMAC)
row = cursor.fetchone()
return row['DateOfCreation']
def doesPacketEntryExist(packetHash):
cursor = mysql.mysqlGetPacketFromHash(
getDatabaseConnection(),
packetHash
)
if cursor.rowcount < 1:
return False
else:
return True
def isPacketSuspicious(packet):
hostKnown = getHostDateOfCreation(packet.eth.src)
hostTraining = isHostStillTraining(datetime.now(),hostKnown,config.monitoring.trainingTime)
if hostTraining:
print (f"{packet.eth.src} is Training")
return False
else:
packetHash = mysql.getPacketHash(packet)
if doesPacketEntryExist(packetHash):
print (f"{packet.eth.src} is not Training but packet isnt Suspicious")
return False
else:
print (f"{packet.eth.src} is not Training but packet is Suspicious")
return True
def getNewProbability(previousProbability, complexity):
adjustedComplexity = 0.51 - complexity
oneMinusProbability = 1 - previousProbability
probability = (adjustedComplexity * oneMinusProbability) + previousProbability
return probability
def isHostCompromised(HostMAC):
probability = mysql.getHostCompromisedProbability(
getDatabaseConnection(),
HostMAC
)
if probability > 0.90:
return True
else:
return False
def getTotalPacketsSentByHost(hostMAC,connection):
## Get Total Number of Packets Sent from Host
sql = f"SELECT SUM(Count) FROM entries WHERE MACAddress = '{hostMAC}'"
cursor = connection.cursor()
cursor.execute(sql)
numberOfPackets = int(cursor.fetchone()['SUM(Count)'])
return numberOfPackets
def getTotalNumberOfUniqueEntriesForHost(hostMAC,connection):
## get number of different destinations
sql = f"SELECT COUNT(*) FROM entries WHERE MACAddress = '{hostMAC}'"
cursor = connection.cursor()
cursor.execute(sql)
numberOfDestinations = cursor.fetchone()['COUNT(*)']
return numberOfDestinations
def getAveragePacketsPerDestination(numberOfPackets,numberOfDestinations):
averagePacketsPerDestination = numberOfPackets / numberOfDestinations
return averagePacketsPerDestination
def getHighestPacketCount(connection):
return mysql.mysqlGetHighPacketCount(connection)['MAX(Count)']
def calculateComplexity(averagePacketsPerDestination,minValue,maxValue):
if averagePacketsPerDestination == 1:
complexity = 0
else:
complexity = normalize(averagePacketsPerDestination,minValue,maxValue)
return complexity
def updateComplexity(connection, complexity, MACAddress):
sql = f"UPDATE Monitoring.hosts SET Complexity = '{complexity}' WHERE MACAddress = '{MACAddress}';"
cursor = connection.cursor()
cursor.execute(sql)
connection.commit()
## main code ##
def main():
while True:
## returns a number of packets prefiltered to include only outbound local traffic and tcp/udp
packets = monitor.get_packets(config.monitoring.monitoringInterface,config.monitoring.packetsPerCapture)
# process the traffic
for packet in packets:
mysql.mysqlAddOrUpdateDevice(getDatabaseConnection(),packet)
suspicious = isPacketSuspicious(packet)
if suspicious:
previousProbability = mysql.getHostCompromisedProbability(
getDatabaseConnection(),
packet.eth.src
)
complexity = mysql.getHostComplexity(
getDatabaseConnection(),
packet.eth.src
)
probability = getNewProbability(previousProbability,complexity)
print (f"Previous Probability: {previousProbability} Complexity: {complexity} New Probablility: {probability}")
mysql.setHostCompromisedProbability(
getDatabaseConnection(),
probability,
packet.eth.src
)
################# future work #######################
# allow user override of suspicious activity #
# some changes to a devices behaviour may be normal #
# for instance activating a new service on a device #
#####################################################
else:
# Add or update packet entry in database
mysql.mysqlAddOrUpdatePacketEntry(
getDatabaseConnection(),
packet
)
## after all packets are processed
# Update complexity of each device (under configured training time)
cursor = mysql.mysqlGetListOfHosts(getDatabaseConnection())
while True:
row = cursor.fetchone()
if row == None:
break
DateOfCreation = row['DateOfCreation']
IP = row['IP']
MACAddress = row['MACAddress']
if isHostStillTraining(datetime.now(),DateOfCreation,config.monitoring.trainingTime):
averagePacketsPerDestination = getAveragePacketsPerDestination(
getTotalPacketsSentByHost(
MACAddress,
getDatabaseConnection()
),
getTotalNumberOfUniqueEntriesForHost(
MACAddress,
getDatabaseConnection()
)
)
max = getHighestPacketCount(getDatabaseConnection())
min = 1
complexity = calculateComplexity(averagePacketsPerDestination,min,max)
updateComplexity(getDatabaseConnection(),complexity,MACAddress)
if (isHostCompromised(MACAddress)):
print(f"Host with MAC Address {MACAddress} and IP {IP} is Compromised")
if __name__ == '__main__':
main()