Machine Learning for CybersecuritySource on GitHub

Unit 10 · Exercises

Unit 10 exercises

Exercise 1. Causal attention by hand

Three tokens have scaled attention scores \(QK^\top/\sqrt{d_k}\) and value vectors

\[ \begin{gathered} S = \begin{pmatrix} 0.0 & 1.0 & -1.0 \\ 1.0 & 0.0 & 2.0 \\ 0.5 & 0.5 & 1.5 \end{pmatrix}, \\ v_1 = (2, 0),\; v_2 = (0, 2), \\ v_3 = (1, 1). \end{gathered} \]

(a) Apply the causal mask and compute the attention weights for each position. (b) Compute the outputs for positions 2 and 3. (c) Without the mask, what weight would position 1 give to position 2, and why would that make next-token training trivial?

Show the answer

(a) Position 1 keeps only its own score, so its weights are \((1, 0, 0)\). Position 2 keeps \((1.0, 0.0)\), and the softmax gives \(e^{1}/(e^{1} + e^{0})\), which is \(2.718/3.718 = 0.731\), and \(0.269\). Position 3 keeps all three scores \((0.5, 0.5, 1.5)\), with \(e^{0.5} = 1.649\) twice and \(e^{1.5} = 4.482\), sum \(7.779\), weights \((0.212, 0.212, 0.576)\).

(b) Writing \(o_i\) for the output at position \(i\), the outputs for positions 2 and 3 are

\[ \begin{aligned} o_2 &= 0.731\,(2, 0) \\ &\quad + 0.269\,(0, 2) \\ &= (1.462, 0.538), \\ o_3 &= 0.212\,(2, 0) \\ &\quad + 0.212\,(0, 2) \\ &\quad + 0.576\,(1, 1) \\ &= (1.000, 1.000). \end{aligned} \]

(c) Without the mask, position 1 would use the full row \((0, 1, -1)\), giving weights \((0.245, 0.665, 0.090)\), so most of its attention would go to the next token. In training, the representation at position 1 is used to predict token 2; if it can read token 2, the model can copy the answer instead of learning to predict it, and the trained model would fail at generation, when token 2 does not yet exist.

Exercise 2. Temperature and entropy

A model's logits for four candidate tokens are \((3, 1, 0, -1)\). (a) Compute the next-token probabilities at temperature 1 and 0.5. (b) Compute the entropy in bits at each temperature. (c) An evaluation of a phishing-triage prompt reports a single run at temperature 1. What does (a) imply for how the result should be reported?

Show the answer

(a) At \(T = 1\), \(e^{3} = 20.09\), \(e^{1} = 2.718\), \(e^{0} = 1\), \(e^{-1} = 0.368\), sum 24.17, probabilities \(0.831\), \(0.112\), \(0.041\) and \(0.015\). At \(T = 0.5\) the logits become \((6, 2, 0, -2)\), whose exponentials are \(403.4, 7.389, 1, 0.135\), sum 412.0, probabilities \(0.979\), \(0.018\), \(0.002\) and \(0.0003\).

(b) Entropy \(-\sum p \log_2 p\) is 0.86 bits at \(T = 1\) and 0.16 bits at \(T = 0.5\).

(c) At temperature 1 the model picks a token other than the most likely one about 17% of the time at this step alone, and over a whole answer the chance that some step departs from the most likely path is much larger. A single sampled run is one draw from a distribution of verdicts. The evaluation should fix the decoding settings, repeat each case several times (or use greedy decoding when measuring the model's preferred answer), and report variation across runs.

Exercise 3. How many documents does a comparison need?

An injection experiment finds 7 successes in 30 injected documents without a defense and 2 in 30 with a defense. (a) Compute the Wilson 95% interval for each rate. (b) Is the defense shown to help? (c) If the true rates were 25% and 5%, roughly how many documents per design would a two-sided test at the 5% level need for 80% power?

Show the answer

(a) For 7 of 30, \(\hat{p} = 0.233\), and the Wilson interval is \([0.118, 0.409]\). For 2 of 30, \(\hat{p} = 0.067\), and the interval is \([0.018, 0.213]\).

(b) The intervals overlap substantially, so 30 documents per design do not establish a difference; the data are consistent with a large benefit and with almost none. A paired analysis (the same 30 documents under both designs) is more sensitive than comparing the two intervals, and it is the right next step, but more documents are needed in any case.

(c) Using the approximation of Lesson 10.2,

\[ \begin{aligned} n &\approx \frac{(1.960 + 0.842)^2}{(0.20)^2} \\ &\quad \times [0.25 \times 0.75 \\ &\qquad + 0.05 \times 0.95] \\ &= 7.85 \times 0.235/0.04 \approx 46 \end{aligned} \]

documents per design. Detecting a difference between 20% and 10% would need about 196.

Exercise 4. Reading Lab 10a

Lab 10a reports the attack success rate without a defense as 7 of 30 (95% interval 0.12 to 0.41) and with datamarking as 2 of 30 (0.02 to 0.21). Task accuracy on benign documents was 93% without a defense and 90% with datamarking. (a) What conclusion does the lab support about datamarking for this model? (b) Name two reasons why the ranking of defenses in the lab might not hold for a deployed model. (c) Design a follow-up experiment.

Show the answer

(a) The point estimate of the attack success rate is lower with datamarking, and task accuracy is nearly unchanged, but the intervals overlap, so the lab is consistent with a benefit without demonstrating one. The defensible statement is that datamarking did not hurt task accuracy noticeably and may reduce injection for this model, with the size of the effect unknown.

(b) The model is small and follows instructions less reliably than deployed models; a stronger model may obey injected instructions more often in some styles and less often in others, so rankings can change. The injections are fixed and not adaptive; an attacker who knows the defense can, for example, write the injected text with ^ between words or close the <document> tag. The documents are short, synthetic and few.

(c) Use a few hundred documents per design so that the intervals separate the effects of interest (Exercise 3), run every design on the same documents and analyze the paired outcomes, add adaptive injections written for each defense, repeat with sampling at the deployed temperature over several seeds, and run the experiment on the model that will actually be deployed.

Exercise 5. Argument checks in code

Write two functions for an agent's tool gateway. email_allowed(to) must accept only a single recipient at the domain corp.example exactly. delete_allowed(path) must accept only paths that stay inside /tmp/agent/ after normalization. Test them on the inputs soc@corp.example, soc@corp.example.attacker.example, a@corp.example,b@x.example, /tmp/agent/job1.tmp and /tmp/agent/../../etc/passwd.

Show the answer
import posixpath


def email_allowed(to):
    """Accept exactly one address whose domain is corp.example."""
    if "," in to or ";" in to or to.count("@") != 1:
        return False
    local, domain = to.split("@")
    return bool(local) and domain.lower() == "corp.example"


def delete_allowed(path):
    """Accept only paths that stay under /tmp/agent/ when normalized."""
    norm = posixpath.normpath(path)
    return norm.startswith("/tmp/agent/") and norm != "/tmp/agent"


tests = [
    "soc@corp.example",
    "soc@corp.example.attacker.example",
    "a@corp.example,b@x.example",
]
paths = ["/tmp/agent/job1.tmp", "/tmp/agent/../../etc/passwd"]
print([email_allowed(t) for t in tests])
print([delete_allowed(p) for p in paths])

The output is [True, False, False] and [True, False]. The check on the domain compares the whole domain, not a prefix or a substring, which is what defeats corp.example.attacker.example, and normalization collapses .. before the prefix test, which defeats the traversal. Such checks belong in the gateway that executes tools, outside the model, so that no prompt can disable them. They do not inspect content; a message to a legitimate internal recipient with a false statement still passes, which is the residual risk Lab 10b measures.

Exercise 6. Threat-modeling a retrieval system

A security operations team builds a question-answering system over three sources: its incident tickets, vendor advisories scraped nightly from the web, and the team's wiki. The system can also open a ticket when an answer recommends an action. Identify which OWASP Top 10 for LLM Applications 2025 items apply, and propose one control for each.

Show the answer

LLM01:2025 Prompt Injection applies through the scraped advisories and through tickets that quote attacker-written emails and logs; one control is to spotlight external passages and not to let answers derived from them trigger tool calls. LLM04:2025 Data and Model Poisoning applies because anyone who can publish a page that the scraper visits can plant false answers (the knowledge poisoning of Lesson 10.3); one control is to restrict scraping to authenticated vendor sources and record provenance for every passage. LLM08:2025 Vector and Embedding Weaknesses applies because embeddings of tickets reveal their content and tickets may have different readers; one control is to enforce per-document permissions at retrieval time. LLM06:2025 Excessive Agency applies to ticket creation; one control is an allowlist that permits only ticket creation with templated fields, and confirmation by an analyst. LLM05:2025 Improper Output Handling applies if answers are rendered as HTML or Markdown with links; one control is to escape output and not to fetch URLs from it automatically. LLM09:2025 Misinformation applies to answers about specific vulnerability identifiers or versions; one control is to show the cited passages with each answer so that analysts check them. LLM02:2025 Sensitive Information Disclosure applies if tickets contain credentials or personal data; one control is to redact them before indexing.

Exercise 7. Layers that fail together

An agent obeys injected tool requests with probability 0.3. An allowlist stops 60% of the obeyed injected actions, argument checks stop half of the rest, and a reviewer rejects 80% of what remains. (a) Assuming the layers act independently, what is the probability that an injected document leads to a harmful action, and how many harmful actions would 5,000 injected documents a month cause? (b) Give an example of an injected action for which the layers are not independent, and explain what that does to your estimate.

Show the answer

(a) By the layered formula of Lesson 10.3, \(P = 0.3 \times 0.4 \times 0.5 \times 0.2\), which equals 0.012, or about 60 harmful actions per 5,000 injected documents.

(b) One example is a message to the team lead, sent through the email tool the task needs, telling them that an alert is a false positive. The allowlist permits the tool, the argument check accepts the internal recipient, and a reviewer who checks the recipient approves it, all because the action looks exactly like a requested one. For this class the stopping probabilities are close to zero at every layer at once, so the true harmful rate for such injections is close to the obedience rate of 0.3, not 0.012. The independent estimate is valid only for attacks that each layer can recognize on its own terms.