pe-malware-detector / src /predict.py
adamuremek's picture
Add MalConv baseline; predict() returns Malware/Benign
8f63b49 verified
Raw History Blame Contribute Delete
5.27 kB
"""Load the saved model and classify one file.
from predict import load_model, predict
model, threshold = load_model()
predict("/path/to/file.exe", model, threshold=threshold) -> "Malware" or "Benign"
python src/predict.py /path/to/file.exe
The file is read 1 MB at a time and fed to the model piece by piece (begin -> feed -> finalize),
so memory use does not grow with file size.
SAFETY: the file is opened read-only and treated as bytes. Nothing is loaded or executed.
"""
import json
import os
import sys
import numpy as np
import torch
from pe_facts import file_facts
from whole_model import CHUNK, WholeFileNet
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MODEL_PATH = os.path.join(ROOT, "model", "model.pt") # the weights: torch.save(model.state_dict(), ...)
CONFIG_PATH = os.path.join(ROOT, "model", "model_config.json") # the settings to rebuild the network, and the threshold
MAX_LEN = 16 * 2**20 # the model reads the whole file up to 16 MB (the dataset's size limit)
PIECE = 256 * CHUNK # bytes fed to the model at a time (1 MB)
_loaded = {} # so the weights are read from disk only once
def build_model(config: dict, state_dict: dict) -> WholeFileNet:
"""Rebuild the network from its settings and load the weights."""
model = WholeFileNet(use_region=not config.get("no_region", False), use_stats=not config.get("no_stats", False),
n_layers=config.get("layers", 0), dropout=config.get("dropout", 0.1),
n_families=config.get("n_families", 0), use_segpos=config.get("seg_pos", False),
use_maxpool=config.get("max_pool", False), use_facts=config.get("facts", False))
model.load_state_dict(state_dict)
return model.eval()
def read_checkpoint(path: str):
"""Returns (config, state_dict, threshold) for either file format:
model.pt (plain weights, settings in model_config.json) or a training checkpoint (everything in one file)."""
saved = torch.load(path, map_location="cpu", weights_only=True)
if "state_dict" in saved: # training checkpoint
config = dict(saved["config"], n_families=len(saved.get("families", [])))
return config, saved["state_dict"], float(saved.get("threshold", 0.5))
with open(os.path.join(os.path.dirname(path), "model_config.json")) as f:
config = json.load(f)
return config, saved, float(config.get("threshold", 0.5))
def load_model(path: str = MODEL_PATH, device: str = None):
"""Returns (model, threshold). The threshold is the one chosen on the validation set."""
device = device or ("cuda" if torch.cuda.is_available() else "cpu")
if (path, device) not in _loaded:
config, state_dict, threshold = read_checkpoint(path)
_loaded[(path, device)] = (build_model(config, state_dict).to(device), threshold)
return _loaded[(path, device)]
def malware_probability(pe_path: str, model, max_len: int = MAX_LEN, return_weights: bool = False):
"""The model's malware probability for one file (bytes beyond max_len are not read)."""
size = min(os.path.getsize(pe_path), max_len)
with open(pe_path, "rb") as f: # read-only, bytes; never executed
head = f.read(8192)
facts = None
if model.facts_proj is not None: # only models trained with header facts need them
facts = file_facts(np.fromfile(pe_path, dtype=np.uint8, count=size))
state = model.begin(size, head, facts)
f.seek(0)
left = size
while left > 0:
piece = f.read(min(PIECE, left))
model.feed(state, piece)
left -= len(piece)
logit, weights = model.finalize(state, return_weights=True)
probability = torch.sigmoid(logit).item()
return (probability, weights[0].cpu().numpy()) if return_weights else probability
def predict(pe_path: str, model=None, max_len: int = MAX_LEN, threshold: float = None) -> str:
"""Read the raw bytes of a PE file (never execute it!) and classify it as "Malware" or "Benign".
model - a loaded model; if None, model/model.pt is loaded
max_len - bytes read from the file at most (default: 16 MB, i.e. the whole file)
threshold - probability at or above which the file is called malware; if None, the threshold
chosen on the validation set is used
"""
stored_model, stored_threshold = load_model()
model = model if model is not None else stored_model
threshold = stored_threshold if threshold is None else threshold
with open(pe_path, "rb") as f:
if f.read(2) != b"MZ": # every Windows PE file starts with "MZ"
return "Not a PE file (it does not start with the bytes 'MZ'), so it was not classified."
return "Malware" if malware_probability(pe_path, model, max_len) >= threshold else "Benign"
if __name__ == "__main__":
net, thr = load_model()
for p in sys.argv[1:]:
print(f"{predict(p, net, threshold=thr):8s} (probability {malware_probability(p, net):.4f}, threshold {thr:.4f}) {p}")