protobuf-jsonformat-trigger-poc / verify_remote_poc.py
hacnho's picture
Upload verify_remote_poc.py with huggingface_hub
9e6aa16 verified
Raw
History Blame Contribute Delete
1.78 kB
from __future__ import annotations
import json
import shutil
import tempfile
import urllib.request
from pathlib import Path
from google.protobuf import struct_pb2, json_format
BASE = "https://huggingface.co/hacnho/protobuf-jsonformat-trigger-poc/resolve/main"
FILES = ["control.json", "protobuf_json_trigger.json"]
PROBES = [
[4, 2, 7],
[4, 2, 0],
[1, 2, 7],
[9, 9, 9],
]
def infer(path: Path, vec: list[int]) -> float:
msg = struct_pb2.Struct()
json_format.Parse(path.read_text(encoding="utf-8"), msg)
trig = [int(v.number_value) for v in msg.fields["trigger"].list_value.values]
boost = float(msg.fields["boost"].number_value)
bias = float(msg.fields["bias"].number_value)
return boost if vec == trig else bias
def main() -> int:
td = Path(tempfile.mkdtemp(prefix="hf_pb_json_"))
try:
for name in FILES:
urllib.request.urlretrieve(f"{BASE}/{name}", td / name)
rows = []
for vec in PROBES:
rows.append(
{
"probe": vec,
"control": infer(td / "control.json", vec),
"malicious": infer(td / "protobuf_json_trigger.json", vec),
}
)
payload = {
"base": BASE,
"trigger_vector": [4, 2, 7],
"probes": rows,
"backdoor_observed": any(row["probe"] == [4, 2, 7] and row["control"] != row["malicious"] for row in rows),
"non_trigger_clean": all(row["probe"] == [4, 2, 7] or row["control"] == row["malicious"] for row in rows),
}
print(json.dumps(payload, indent=2))
return 0
finally:
shutil.rmtree(td, ignore_errors=True)
if __name__ == "__main__":
raise SystemExit(main())