Pytorch Models/PTH Files Code Execution
Theory
A file with a .pth
extension typically contains a serialized PyTorch state dictionary. A PyTorch state dictionary is a Python dictionary that contains the state of a PyTorch model, including the model's weights, biases, and other parameters.
We can craft a malicious .pth file that will execute arbitrary code when model is loaded/trained/evaluated in the target system.
Practice
1 - Install Dependencies
It requires torch
so install it:
2 - Create Python Script To Generate Malicious Model
Now create a Python script that generates our malicious ML model. This model executes OS command when it is evaluated.
3 - Run Python Script
Now execute this Python script as below:
After that, our model named evil.pth
will be generated.
Resources
Last updated