-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelon_secsvm.py
354 lines (284 loc) · 12.4 KB
/
elon_secsvm.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# -*- coding: utf-8 -*-
from __future__ import division
import argparse
import glob
import logging
import numpy as np
import os
import time
import sklearn
import torch
import torch.multiprocessing as mp
import traceback
import ujson as json
from itertools import repeat
from pprint import pformat
import pickle
import apg.evasion as evasion
import apg.extraction as extraction
import apg.inpatients as inpatients
import apg.models as models
import apg.utils as utils
from apg.settings import config
from apg.utils import blue, yellow, red, green
#### imports that i made ####
from scipy import sparse
import random
mp = torch.multiprocessing.get_context('forkserver')
real_prediction = 0
def main():
# STAGE 1: PRELUDE #
start=time.time()
args = parse_args()
# Configure logging and output dirs
utils.configure_logging(args.run_tag, args.debug)
#output_dir = os.path.join(config['results_dir'], args.run_tag)
#os.makedirs(os.path.join(output_dir, 'success'), exist_ok=True)
#os.makedirs(os.path.join(output_dir, 'failure'), exist_ok=True)
#os.makedirs(os.path.join(output_dir, 'estimates'), exist_ok=True)
#os.makedirs(os.path.join(output_dir, 'adv-features'), exist_ok=True)
#os.makedirs(os.path.join(output_dir, 'records'), exist_ok=True)
#os.makedirs(os.path.join(output_dir, 'postop'), exist_ok=True)
#logging.info(yellow('Output directory: {output_dir}'))
# STAGE 2: EXPERIMENT PREP #
# Load data and create models
logging.info(blue('Loading data...'))
'''
if True:
model = models.SecSVM(config['X_dataset'], config['y_dataset'],config['X_dataset_test'], config['y_dataset_test'],
args.n_features,
args.secsvm_k, args.secsvm, args.secsvm_lr,
args.secsvm_batchsize, args.secsvm_nepochs,
seed_model=args.seed_model)
else:
model = models.SVM(config['X_dataset'], config['y_dataset'],config['X_dataset_test'], config['y_dataset_test'], args.n_features)
logging.debug(blue('Fetching model...'))
model.generate()
#if os.path.exists(model.model_name):
# model = models.load_from_file(model.model_name)
#else:
# model.generate()
logging.info(blue('Using classifier:\n{pformat(vars(model.clf))}'))
# Harvest organs
#if args.harvest:
# extraction.mass_organ_harvest(model, args.organ_depth, args.donor_depth)
# Find true positive malware
y_pred = model.clf.predict(model.X_test)
y_scores = model.clf.decision_function(model.X_test)
#for i in range(0,len(y_pred)):
# print(y_pred[i],y_scores[i])
count=sum(y_pred)
total=len(y_pred)
#print("y_test = ",model.y_test)
#print("x_test = ",model.X_test)
#exit(0)
print("count = ",count, "total = ",total)
print("detection rate: ",count/total)
end=time.time()
print("processing time is ",end-start," seconds")
print("out")
with open('classifier.pkl', 'wb') as f:
pickle.dump(model, f)
'''
with open('classifier.pkl', 'rb') as f:
model = pickle.load(f)
print("finished open the file")
y_pred = model.clf.predict(model.X_test)
count=sum(y_pred)
total=len(y_pred)
real_prediction = count/total
#print("detection rate: ",count/total)
add_nois_attack(model.clf, model.X_train, model.X_test)
exit(0)
def add_nois_attack(model, x_train, x_test):
noise_intensity = 0.1
X_noisy = x_test
y_pred = model.predict(x_test)
count=sum(y_pred)
total=len(y_pred)
real_prediction = count/total
attack_prediction = real_prediction
#print()
coef = sparse.csr.csr_matrix(model.coef_)
X_noisy = X_noisy.multiply(coef)
y_pred = model.predict(X_noisy)
count=sum(y_pred)
total=len(y_pred)
attack_prediction = count/total
print("attack detection rate: ",attack_prediction)
print("real detection rate: ",real_prediction)
print("score change, if d > 0 it's good, if d < 0 t's bad")
print("delta: ", (real_prediction - attack_prediction))
def add_nois_attack1(model, x_train, x_test):
noise_intensity = 0.1
X_noisy = x_test
y_pred = model.predict(x_test)
count=sum(y_pred)
total=len(y_pred)
real_prediction = count/total
attack_prediction = real_prediction
# Select the indices of the elements to change randomly
while(real_prediction - attack_prediction <= 0.5 ):
X_noisy = x_test
indices = random.random()
print(indices) # Output: [5, 3]
# Change the selected elements to the value 1
X_noisy = X_noisy * float(indices)
#X_noisy = X_noisy * 0
print("finished transfer")
#print(type(X_noisy))
#X_noisy = sparse.csr_matrix(X_noisy)
#print(type(X_noisy))
y_pred = model.predict(X_noisy)
count=sum(y_pred)
total=len(y_pred)
attack_prediction = count/total
print("attack detection rate: ",attack_prediction)
print("real detection rate: ",real_prediction)
print("score change, if d > 0 it's good, if d < 0 t's bad")
print("delta: ", (real_prediction - attack_prediction))
def transplantation_wrapper(record, model, output_dir, args):
"""Wrapper to handle and debug errors from the problem space transplantation.
Args:
record (str): The path to the precomputed record.
model (SVMModel): The `SVMModel` for the attack.
output_dir (str): The root directory of where outputs should be stored.
args (Namespace): Command line args.
"""
logging.info('-' * 70)
sha = utils.get_app_name(record)
result = os.path.join(output_dir, 'success', 'report-{sha}.apk.json')
if os.path.exists(result):
logging.info(green('Already successfully generated!'))
return
failed = os.path.join(output_dir, 'failure', '{sha}.txt')
if os.path.exists(failed) and not args.rerun_past_failures:
logging.info(red('Already attempted to generate.'))
return
tries = config['tries']
successful = False
while not successful and tries > 0:
try:
evasion.problem_space_transplant(record, model, output_dir)
successful = True
except evasion.RetryableFailure as e:
tries -= 1
if tries > 0:
logging.warning(red('Encountered a random error, retrying...'))
else:
logging.error(red('Ran out of tries :O Logging error...'))
utils.log_failure(record, str(e), output_dir)
except Exception as e:
msg = 'Process fell over with: [{e}]: \n{traceback.format_exc()}'
utils.log_failure(record, msg, output_dir)
return e
logging.info(yellow('Results in: {output_dir}'))
def resolve_confidence_level(confidence, benign_scores):
"""Resolves a given confidence level w.r.t. a set of benign scores.
`confidence` corresponds to the percentage of benign scores that should be below
the confidence margin. Practically, for a value N the attack will continue adding features
until the adversarial example has a score which is 'more benign' than N% of the known
benign examples.
In the implementation, 100 - N is performed to calculate the percentile as the benign
scores in the experimental models are negative.
Args:
confidence: The percentage of benign scores that should be below the confidence margin.
benign_scores: The sample of benign scores to compute confidence with.
Returns:
The target score to resolved at the given confidence level.
"""
if confidence == 'low':
return 0
elif confidence == 'high':
confidence = 25
try:
# perc. inverted b/c benign scores are negative
return np.abs(np.percentile(benign_scores, 100 - float(confidence)))
except:
logging.error('Unknown confidence level: {confidence}')
def calculate_base_metrics(model, y_pred, y_scores, output_dir=None):
"""Calculate ROC, F1, Precision and Recall for given scores.
Args:
model: `Model` containing `y_test` of ground truth labels aligned with `y_pred` and `y_scores`.
y_pred: Array of predicted labels, aligned with `y_scores` and `model.y_test`.
y_scores: Array of predicted scores, aligned with `y_pred` and `model.y_test`.
output_dir: The directory used for dumping output.
Returns:
dict: Model performance stats.
"""
roc = sklearn.metrics.roc_auc_score(model.y_test, y_scores)
f1 = sklearn.metrics.f1_score(model.y_test, y_pred)
precision = sklearn.metrics.precision_score(model.y_test, y_pred)
recall = sklearn.metrics.recall_score(model.y_test, y_pred)
if output_dir:
utils.dump_pickle(y_pred, output_dir, 'y_pred.p')
utils.dump_pickle(y_scores, output_dir, 'y_scores.p')
utils.dump_pickle(model.y_test, output_dir, 'y_test.p')
return {
'model_performance': {
'roc': roc,
'f1': f1,
'precision': precision,
'recall': recall,
}
}
'''
def low_confident_attack(samples, classifier, max_iterations=100, step_size=0.01):#our try for attack
# Set the sample's label to the opposite of the classifier's prediction
#sample = samples.sample(n = samples)
target_label = 1 - classifier.predict(samples)
# Initialize the modified sample with the original values
modified_sample = samples.copy()
# Run gradient descent to find the optimal modifications to the sample
for _ in range(max_iterations):
# Calculate the gradient of the classifier's loss function w.r.t. the sample's features
print("target_label",target_label)
gradient = classifier.fit(modified_sample, target_label)
print("gradient=",gradient)
# Take a step in the opposite direction of the gradient
modified_sample -= step_size * gradient
# Check if the classifier is now misclassifying the modified sample
confidence = classifier.predict_proba(modified_sample)[0][target_label]
if confidence < 0.5:
# If the classifier is now less confident in its prediction, return the modified sample
return modified_sample
# If the maximum number of iterations was reached and the classifier is still confident in its prediction, return None
return None
'''
def parse_args():
p = argparse.ArgumentParser()
# Experiment variables
p.add_argument('-R', '--run-tag', help='An identifier for this experimental setup/run.')
p.add_argument('--confidence', default="25", help='The confidence level to use (%% of benign within margin).')
p.add_argument('--n-features', type=int, default=None, help='Number of features to retain in feature selection.')
p.add_argument('--max-permissions-per-organ', default=5, help='The number of permissions allowed per organ.')
p.add_argument('--max-permissions-total', default=20, help='The total number of permissions allowed in an app.')
# Stage toggles
p.add_argument('-t', '--transplantation', action='store_true', help='Runs physical transplantation if True.')
p.add_argument('--skip-feature-space', action='store_true',
help='Skips generation of patient records and feature estimates.')
# Performance
p.add_argument('--preload', action='store_true', help='Preload all host applications before the attack.')
p.add_argument('--serial', action='store_true', help='Run the pipeline in serial rather than with multiprocessing.')
# SecSVM hyperparameters
p.add_argument('--secsvm', action='store_true')
p.add_argument('--secsvm-k', default="0.25")
p.add_argument('--secsvm-lr', default=0.0009, type=float)
p.add_argument('--secsvm-batchsize', default=256, type=int)
p.add_argument('--secsvm-nepochs', default=10, type=int)
p.add_argument('--seed_model', default=None)
# Harvesting options
#p.add_argument('--harvest', action='store_true')
#p.add_argument('--organ-depth', type=int, default=100)
#p.add_argument('--donor-depth', type=int, default=10)
# Misc
p.add_argument('-D', '--debug', action='store_true', help='Display log output in console if True.')
p.add_argument('--rerun-past-failures', action='store_true', help='Rerun all past logged failures.')
args = p.parse_args()
if args.secsvm_k == 'inf':
args.secsvm_k = np.inf
else:
args.secsvm_k = float(args.secsvm_k)
return args
if __name__ == "__main__":
main()