""" Stage 3 - Prompting the model. The prompt, the parser, and the model registry. **The prompt and the parser are lifted verbatim from Part B** (Final_Project_Evaluation_V1.ipynb, cells 48-49): same system message, same closed family list in the same order, same brace-counting parser, same greedy decoding, same 4-bit NF4 quantisation. A reworded prompt is a different experiment, and the measured F1 would no longer apply to this app. The registry is the only place a model is named. Every field in a `ModelSpec` is a per-model difference Part B actually ran into. """ import gc import json import os import re import sys import time from dataclasses import dataclass, field FAMILIES = ["cross_site_scripting", "dde_template_injection", "javascript_injection", "llm_prompt_injection", "object_action_injection", "polyglot_file", "ransomware_simulation", "shellcode_embedded_exe", "ssrf", "steganographic_payload", "uri_redirect_phishing", "xfa_acroform_injection"] # A short paragraph per family: how it works, then why that matters. Each is written against the # generator's own object shapes rather than the general security literature, so a reader who opens # a flagged file finds the thing the paragraph describes. # # Ordered by how directly the attack reaches a person, not alphabetically - these are read one at a # time while something downloads, so the first ones a user meets should be the ones that explain # why any of this is worth detecting. # # These are for the *reader*. They are deliberately kept out of SYSTEM: the prompt is Part B's, byte # for byte, and adding twelve definitions to it would make the measured scores describe a different # prompt from the one running. FAMILY_NOTES = { "llm_prompt_injection": "LLM prompt injections work by hiding a written instruction inside the PDF - in the " "metadata, or in an annotation sized so small that no human ever sees it. When someone " "feeds the file to an AI assistant, the assistant reads that hidden text along with " "everything else and follows it. This is dangerous because the attacker is now giving " "orders to a tool that the victim trusts and has given access to their files, their inbox " "or their code - and nothing looked wrong on the page.", "javascript_injection": "JavaScript injections put code in a `/JavaScript` action, which a PDF viewer runs the " "moment the file is opened. The code is usually obfuscated, so the file carries a string " "of hex or character codes rather than anything readable. This is dangerous because it " "needs no click and no mistake by the reader: opening the document is the whole attack, " "and the code runs with whatever the viewer is allowed to do.", "object_action_injection": "Object and action injections use a `/Launch` action pointing at a program - typically " "`cmd.exe` with a command attached. Opening the document asks the operating system to run " "something rather than to display a page. This is dangerous because it steps outside the " "PDF viewer entirely: whatever the command does happens with the user's own permissions, " "and a PDF is the last place most people expect a program to start.", "shellcode_embedded_exe": "Embedded-executable payloads carry a real Windows program inside the PDF, recognisable " "by the `MZ` header that starts every .exe, stored as an attachment named something " "ordinary. This is dangerous because the PDF becomes a delivery envelope: it passes the " "mail filter as a document, and the malware only has to be extracted and double-clicked " "by someone who thinks they are opening a file the sender meant to attach.", "ransomware_simulation": "Ransomware payloads combine code that runs on open with an on-page notice announcing " "that files have been encrypted and demanding payment. This is dangerous because of what " "the code does before the notice appears - by the time the message is read, the work it " "describes is finished. The samples in this corpus are harmless simulations built from " "public test material, shaped exactly like the real thing so a detector can be measured.", "cross_site_scripting": "Cross-site scripting hides a `