-
Notifications
You must be signed in to change notification settings - Fork 0
/
13)c).py
209 lines (170 loc) · 6.45 KB
/
13)c).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
import numpy as np
from scipy.spatial import distance
#we initialize Malware Samples
B_malware = np.array([[1.0, -2.0, 1.0, 2.0],
[-1.0, 2.0, 3.0, 3.0],
[1.0, 2.0, 0.0, 1.0],
[-1.0, -1.0, 1.0, 1.0],
[-1.0, -2.0, 3.0, -2.0],
[1.0, 2.0, 1.0, 0.0]])
#we initialize Benign Samples
B_benign = np.array([[-1.0, -2.0, -1.0, 0.0],
[2.0, 1.0, 3.0, 2.0],
[1.0, 2.0, 0.0,3.0],
[2.0, 3.0, 1.0, 1.0],
[-1.0, 2.0, 3.0, 1.0],
[0.0, 1.0, -1.0, -2.0]])
#samples to score and classify
Y1 = np.array([1,5,1,5,5,1])
Y2 = np.array([-2,3,2,3,0,2])
Y3 = np.array([2,-3,2,3,0,0])
Y4 = np.array([2,-2,2,2,-1,1])
#we initialize A used after normalizing B
A_malware = np.array([[]], dtype = float)
A_benign = np.array([[]], dtype = float)
#mean for both the samples
mean_malware = np.zeros(shape = len(B_malware))
mean_benign = np.zeros(shape = len(B_benign))
#C used as covariance matrix
C_malware = np.array([[]], dtype = float)
C_benign = np.array([[]], dtype = float)
#delta for scoring
delta_malware = np.array([[]], dtype = float)
delta_benign = np.array([[]], dtype = float)
#eigenvectors
U_malware = np.array([[]], dtype = float)
U_benign = np.array([[]], dtype = float)
#weights
w_malware = np.array([[]], dtype = float)
w_benign = np.array([[]], dtype = float)
#normalize B
def normalizeB():
sum_malware = 0
for i in range (0,len(B_malware)):
for j in range (0,len(B_malware[i])):
sum_malware = sum_malware + B_malware[i][j]
mean_malware[i] = sum_malware / len(B_malware[i])
sum_malware= 0
sum_benign = 0
for i in range (0,len(B_benign)):
for j in range (0,len(B_benign[i])):
sum_benign = sum_benign + B_benign[i][j]
mean_benign[i] = sum_benign / len(B_benign[i])
sum_benign = 0
#used to initialize matrix A from B
def computeA():
global A_malware, A_benign
A_malware = B_malware
for i in range (0,len(A_malware)):
for j in range (0,len(A_malware[i])):
A_malware[i][j] -= mean_malware[i]
A_benign = B_benign
for i in range (0,len(A_benign)):
for j in range (0,len(A_benign[i])):
A_benign[i][j] -= mean_benign[i]
#used to get covariance matrix C
def compute_covarianceM():
global C_malware, C_benign
C_malware = 0.25*A_malware.dot(A_malware.T)
C_benign = 0.25*A_benign.dot(A_benign.T)
#used to get s = eigenvalues and U = eigenvectors
def computeEValues_EVector():
global U_malware, U_benign
s_malware, U_malware = np.linalg.eig(C_malware)
s_benign, U_benign = np.linalg.eig(C_benign)
np.set_printoptions(formatter={'float_kind': '{:.3f}'.format},suppress = True)
#re-initialize with values in question
U_malware = np.array([[0.1641,0.2443],
[0.6278,0.1070],
[-0.2604,-0.8017],
[-0.5389,0.4277],
[0.4637,-0.1373],
[0.0752,-0.2904]])
U_benign = np.array([[0.1641,0.2443],
[0.6278,0.1070],
[-0.2604,-0.8017],
[-0.5389,0.4277],
[0.4637,-0.1373],
[0.0752,-0.2904]])
print("Eigen Values of Malware:")
print(s_malware)
print("Eigen Vectors of Malware:")
print(U_malware)
print("Eigen Values of Benign:")
print(s_benign)
print("Eigen Vectors of Benign:")
print(U_benign)
#used to get delta = scoring matrix
def compute_scoringMatrix():
global delta_malware, delta_benign
delta_malware = (U_malware.T).dot(A_malware)
delta_benign = (U_benign.T).dot(A_benign)
print("Scoring Matrix of Malware:")
print(delta_malware)
print("Scoring Matrix of Benign:")
print(delta_benign)
#used to get weightmatrices and score the sample vectors
def calculate_weightMatrices():
global w_benign,w_malware
#eigenvectors in question
U1_malware = U_malware[0:6,0:1]
U2_malware = U_malware[0:6,1:2]
U1_benign = U_benign[0:6,0:1]
U2_benign = U_benign[0:6,1:2]
YBar = Y1-mean_malware
w_malware = np.array([(YBar.T).dot(U1_malware),(YBar.T).dot(U2_malware)])
result_malware = EuclideanDistMalware()
YBar = Y1-mean_benign
w_benign = np.array([(YBar.T).dot(U1_benign),(YBar.T).dot(U2_benign)])
result_benign = EuclideanDistBenign()
classify_sample("Y1",result_malware,result_benign)
YBar = Y2-mean_malware
w_malware = np.array([(YBar.T).dot(U1_malware),(YBar.T).dot(U2_malware)])
result_malware = EuclideanDistMalware()
YBar = Y2-mean_benign
w_benign = np.array([(YBar.T).dot(U1_benign),(YBar.T).dot(U2_benign)])
result_benign = EuclideanDistBenign()
classify_sample("Y2",result_malware,result_benign)
YBar = Y3-mean_malware
w_malware = np.array([(YBar.T).dot(U1_malware),(YBar.T).dot(U2_malware)])
result_malware = EuclideanDistMalware()
YBar = Y3-mean_benign
w_benign = np.array([(YBar.T).dot(U1_benign),(YBar.T).dot(U2_benign)])
result_benign = EuclideanDistBenign()
classify_sample("Y3",result_malware,result_benign)
YBar = Y4-mean_malware
w_malware = np.array([(YBar.T).dot(U1_malware),(YBar.T).dot(U2_malware)])
result_malware = EuclideanDistMalware()
YBar = Y4-mean_benign
w_benign = np.array([(YBar.T).dot(U1_benign),(YBar.T).dot(U2_benign)])
result_benign = EuclideanDistBenign()
classify_sample("Y4",result_malware,result_benign)
#calculate distance from malware
def EuclideanDistMalware():
low = float('inf')
for i in range(0, 4):
dist = distance.euclidean(w_malware, delta_malware[:, i: i + 1])
if dist < low:
low = dist
return low
#calculate distance from benign
def EuclideanDistBenign():
low = float('inf')
for i in range(0, 4):
dist = distance.euclidean(w_benign, delta_benign[:, i: i + 1])
if dist < low:
low = dist
return low
#classify based on the score
def classify_sample(sample,malware_score,benign_score):
if malware_score > benign_score:
print(sample," is Benign")
else:
print(sample," is Malware")
if __name__ == "__main__":
normalizeB()
computeA()
compute_covarianceM()
computeEValues_EVector()
compute_scoringMatrix()
calculate_weightMatrices()