pe-malware-detector / src /malconv.py
adamuremek's picture
Add MalConv baseline; predict() returns Malware/Benign
8f63b49 verified
Raw History Blame Contribute Delete
4.16 kB
"""MalConv baseline (Raff et al., 2017, Section 4), in PyTorch.
raw bytes (first max_len) -> embedding (257 tokens -> 8) -> gated 1-D convolution
(128 filters, window 500, stride 500) -> global max pooling -> 128 units + ReLU -> 1 logit
max_len = 1 MB (2**20 bytes): longer files are cut off, shorter ones are padded with token 0.
No batch normalisation (the paper found it hurt).
SAFETY: files are opened read-only and read as bytes. Nothing is executed.
Sanity check: python src/malconv.py
"""
import csv
import os
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import Dataset
MAX_LEN = 2**20 # 1 MB
PAD = 0 # token 0 = padding; byte b is token b + 1
def read_tokens(path: str, max_len: int = MAX_LEN) -> np.ndarray:
"""First max_len bytes of a file as tokens 1..256, padded with 0 to exactly max_len."""
data = np.fromfile(path, dtype=np.uint8, count=max_len) # bytes only, never executed
tokens = np.zeros(max_len, dtype=np.int16)
tokens[: len(data)] = data.astype(np.int16) + 1
return tokens
class ByteDataset(Dataset):
"""One item = (tokens [max_len], label)."""
def __init__(self, manifest: str, data_dir: str, split: str, max_len: int = MAX_LEN):
self.items, self.max_len = [], max_len
with open(manifest, newline="") as f:
for row in csv.DictReader(f):
if row["split"] == split:
cls = "malware" if row["label"] == "1" else "benign"
self.items.append((os.path.join(data_dir, split, cls, row["sha256"]), int(row["label"])))
def __len__(self):
return len(self.items)
def __getitem__(self, i):
path, label = self.items[i]
return torch.from_numpy(read_tokens(path, self.max_len)), torch.tensor(label, dtype=torch.float32)
class MalConv(nn.Module):
"""Embedding -> gated convolution -> global max pool -> fully connected -> 1 logit."""
def __init__(self, embed_dim: int = 8, channels: int = 128, window: int = 500, stride: int = 500):
super().__init__()
# 257 tokens = 256 byte values + padding. padding_idx keeps the padding vector at zero
# and stops it from being trained, so padding carries no information.
self.embed = nn.Embedding(257, embed_dim, padding_idx=PAD)
# Two parallel convolutions over the same input. A window of 500 bytes moved 500 bytes at a
# time shrinks 1,048,576 positions to 2,097, which is what makes the memory manageable.
self.conv = nn.Conv1d(embed_dim, channels, kernel_size=window, stride=stride)
self.gate = nn.Conv1d(embed_dim, channels, kernel_size=window, stride=stride)
self.fc = nn.Linear(channels, channels)
self.out = nn.Linear(channels, 1)
def forward(self, tokens: torch.Tensor) -> torch.Tensor:
x = self.embed(tokens.int()) # [batch, length] -> [batch, length, 8]
x = x.transpose(1, 2) # Conv1d wants channels first -> [batch, 8, length]
# Gating: one convolution proposes features, the other (through a sigmoid, so 0..1)
# decides how much of each feature to let through at each position.
x = self.conv(x) * torch.sigmoid(self.gate(x)) # -> [batch, 128, length/500]
# Global max pooling: for each of the 128 features keep only its strongest response
# anywhere in the file. "Does this pattern occur?" - regardless of where.
x = x.amax(dim=2) # -> [batch, 128]
x = torch.relu(self.fc(x)) # -> [batch, 128]
return self.out(x).squeeze(1) # one logit per file -> [batch]
def count_parameters(model: nn.Module) -> int:
return sum(p.numel() for p in model.parameters() if p.requires_grad)
if __name__ == "__main__":
model = MalConv()
print("trainable parameters:", f"{count_parameters(model):,}")
x = torch.randint(0, 257, (2, MAX_LEN), dtype=torch.int16)
print("input", tuple(x.shape), "-> logits", tuple(model(x).shape))