-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.py
More file actions
54 lines (40 loc) · 1.41 KB
/
check.py
File metadata and controls
54 lines (40 loc) · 1.41 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
import torch
import torch.nn.functional as F
import torch.nn as nn
import numpy as np
import sys
import struct
import os
from tqdm import tqdm
from MalConv_ForADV import MalConv_ForADV
kernel_size = 512
eps = 0.8
target = 0 # benign
loop_num = 10
def fgsm_attack():
bytez = open('taquila-bad-classified.exe', "rb").read()
# Create malconv
malconv = MalConv_ForADV(channels=256, window_size=512, embd_size=8)
weights = torch.load('malconv/malconv.checkpoint', map_location='cpu')
malconv.load_state_dict(weights['model_state_dict'])
# malconv.eval()
# Compute payload size
payload_size = (kernel_size + (kernel_size - np.mod(len(bytez), kernel_size))) * 8 + 1
print('payload: ', payload_size)
# Creat embedding matrix
embed = malconv.embd
m = embed(torch.arange(0, 256)) # M
# Make label from target
label = torch.tensor([target], dtype=torch.long)
perturbation = np.random.randint(0, 256, payload_size, dtype=np.uint8)
# Make input file x as numpy array
x = np.frombuffer(bytez, dtype=np.uint8)
inp = torch.from_numpy(np.copy(x))[np.newaxis, :].float()
inp_adv = inp.requires_grad_()
embd_x = embed(inp_adv.long()).detach()
embd_x.requires_grad = True
outputs = malconv(embd_x)
results = F.softmax(outputs, dim=1)
print('Prediction benign: {:.4}'.format(results.detach().numpy()[0][0]))
if __name__ == '__main__':
fgsm_attack()