forked from PowerLZY/MalConv-Pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalconv-microsoft.py
186 lines (151 loc) · 5.67 KB
/
malconv-microsoft.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
import sys
import os
import time
from src.model import *
from src.util import *
import numpy as np
import pandas as pd
from torch.utils.data import DataLoader
from sklearn.model_selection import train_test_split
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
label_path = "/public/malware_dataset/kaggle_microsoft_9_10000/"
train_data_path = label_path + "bytes/" # Training data
train_label_path = label_path + "kaggle_microsoft_trainlabels.csv" # Training label
#valid_label_path = label_path + "example-valid-label.csv" # Validation Label
#name
exp_name = "malconv-classification"
# Parameter
# os.environ["CUDA_VISIBLE_DEVICES"] = "2" # single-GPU
use_gpu = True #
use_cpu = 32 # Number of cores to use for data loader
display_step = 5 # Std output update rate during training 和 保存训练结果步长
test_step = 50 # Test per n step
learning_rate = 0.0001 #
max_step = 1000 # Number of steps to train
batch_size = 768 #
first_n_byte = (
100000 # First N bytes of a PE file as the input of MalConv (defualt: 2 million)
)
window_size = 512 # Kernel size & stride for Malconv (defualt : 500)
# output path
log_dir = "/log/"
pred_dir = "/pred/"
checkpoint_dir = "/checkpoint/"
log_file_path = log_dir + exp_name + ".log"
chkpt_acc_path = checkpoint_dir + exp_name + "1000.pt"
pred_path = pred_dir + exp_name + ".pred"
df = pd.read_csv(train_label_path)
train, valid, train_label, valid_label = train_test_split(
df["Id"],
df["Class"],
test_size=0.2,
stratify=df["Class"],
random_state=100,
)
"""
# Dataset preparation
class ExeDataset(Dataset):
def __init__(self, fp_list, data_path, label_list, first_n_byte=2000000):
self.fp_list = fp_list
self.data_path = data_path
self.label_list = label_list
self.first_n_byte = first_n_byte
def __len__(self):
return len(self.fp_list)
def __getitem__(self, idx):
try:
with open(self.data_path + self.fp_list[idx],'rb') as f:
tmp = [i+1 for i in f.read()[:self.first_n_byte]] # index 0 will be special padding index 每个值加一
tmp = tmp+[0]*(self.first_n_byte-len(tmp))
except:
with open(self.data_path + self.fp_list[idx].lower(),'rb') as f:
tmp = [i+1 for i in f.read()[:self.first_n_byte]]
tmp = tmp+[0]*(self.first_n_byte-len(tmp))
return np.array(tmp), np.array([self.label_list[idx]])
"""
trainset = pd.DataFrame({"id": train, "labels": train_label})
validset = pd.DataFrame({"id": valid, "labels": valid_label})
trainloader = DataLoader(
ExeDataset(
list(trainset["id"]), train_data_path, list(trainset["labels"]), first_n_byte
),
batch_size=batch_size,
shuffle=False,
num_workers=use_cpu,
pin_memory=True,
)
validloader = DataLoader(
ExeDataset(
list(validset["id"]), train_data_path, list(validset["labels"]), first_n_byte
),
batch_size=batch_size,
shuffle=False,
num_workers=use_cpu,
pin_memory=True,
)
USE_CUDA = torch.cuda.is_available()
device = torch.device("cuda:1" if USE_CUDA else "cpu")
malconv = MalConv(input_length=first_n_byte, window_size=window_size)
malconv = nn.DataParallel(malconv, device_ids=[1,2,3]) # multi-GPU
#malconv = MalConvBase(8, 4096, 128, 32)
bce_loss = nn.BCEWithLogitsLoss()
ce_loss = nn.CrossEntropyLoss()
adam_optim = optim.Adam([{"params": malconv.parameters()}], lr=learning_rate)
sigmoid = nn.Sigmoid()
if use_gpu:
malconv = malconv.to(device)
bce_loss = bce_loss.to(device)
sigmoid = sigmoid.to(device)
step_msg = "step-{}-loss-{:.6f}-acc-{:.4f}-time-{:.2f}s"
valid_msg = "step-{}-tr_loss-{:.6f}-tr_acc-{:.4f}-val_loss-{:.6f}-val_acc-{:.4f}"
log_msg = "{}, {:.6f}, {:.4f}, {:.6f}, {:.4f}, {:.2f}"
history = {}
history["tr_loss"] = []
history["tr_acc"] = []
train_acc = [] # 保存训练结果
valid_best_acc = 0.0
total_step = 0
step_cost_time = 0
valid_idx = list(validset["id"])
while total_step < max_step:
# Training
for step, batch_data in enumerate(trainloader):
start = time.time()
adam_optim.zero_grad()
cur_batch_size = batch_data[0].size(0)
exe_input = batch_data[0].to(device) if use_gpu else batch_data[0]
exe_input = Variable(exe_input.long(), requires_grad=False)
label = batch_data[1].to(device) if use_gpu else batch_data[1]
label = Variable(label, requires_grad=False)
label = label.squeeze() - 1
pred = malconv(exe_input)
loss = ce_loss(pred, label)
loss.backward()
adam_optim.step()
_, predicted = torch.max(pred.data, 1)
train_Macc = (label.cpu().data.numpy().astype(int) == (predicted.cpu().data.numpy()).astype(int)).sum().item()
train_Macc = train_Macc / cur_batch_size
if (step + 1) % display_step == 0:
print("train:{}".format(train_Macc))
total_step += 1
# Interupt for validation
if total_step % test_step == 0:
break
for step, val_batch_data in enumerate(validloader):
cur_batch_size = val_batch_data[0].size(0)
exe_input = val_batch_data[0].to(device) if use_gpu else val_batch_data[0]
exe_input = Variable(exe_input.long(), requires_grad=False)
label = val_batch_data[1].to(device) if use_gpu else val_batch_data[1]
label = Variable(label, requires_grad=False)
label = label.squeeze() - 1
pred = malconv(exe_input)
loss = ce_loss(pred, label)
# loss.backward()
# adam_optim.step()
_, predicted = torch.max(pred.data, 1)
val_Macc = (label.cpu().data.numpy().astype(int) == (predicted.cpu().data.numpy()).astype(int)).sum().item()
val_Macc = val_Macc / cur_batch_size
if (step + 1) % display_step == 0:
print("test:{}".format(val_Macc))