Machine Learning for CybersecuritySource on GitHub

Unit 7 · Exercises

Unit 7 exercises

These exercises use the attacks and evaluation ideas of the three lessons and the two labs. Calculations are worked out and were checked in Python. Formulas are restated so each exercise stands on its own.

Exercise 1. An FGSM step by hand

A logistic model has weights \(w = (1, 3, -2)\), bias \(b = -0.5\), and is applied to input \(x = (1, 1, 0)\) with true label \(y = 1\). For logistic loss the input gradient is \(\nabla_x L = (p - y)\, w\), where \(p = \sigma(w^\top x + b)\) and \(\sigma(z) = 1/(1 + e^{-z})\) is the logistic function. Apply one untargeted FGSM step with budget \(\varepsilon = 0.2\) under the \(L_\infty\) norm, \(x_{\mathrm{adv}} = x + \varepsilon\, \mathrm{sign}(\nabla_x L)\). Give the adversarial input, the new logit, and the drop in the logit.

Show the answer

The clean logit is

\[ \begin{aligned} z &= 1 \times 1 + 3 \times 1 - 2 \times 0 - 0.5 \\ &= 3.5, \end{aligned} \]

so \(p = \sigma(3.5) = 0.9707\). The gradient is \((p - 1)w\), which equals \(-0.0293 \times (1, 3, -2)\) and has sign \((-1, -1, +1)\). The step gives

\[ \begin{aligned} x_{\mathrm{adv}} &= (1 - 0.2,\; 1 - 0.2,\; 0 + 0.2) \\ &= (0.8, 0.8, 0.2). \end{aligned} \]

The new logit is \(0.8 + 2.4 - 0.4 - 0.5 = 2.3\), so \(p\) falls to \(\sigma(2.3) = 0.9089\). The logit dropped by \(3.5 - 2.3 = 1.2\), which equals \(\varepsilon \|w\|_1\), since \(0.2 \times (1 + 3 + 2) = 1.2\), confirming the linear analysis of Lesson 7.2.

Exercise 2. Budgets and scale

Under the \(L_\infty\) norm with pixels scaled to the interval from 0 to 1, the conventional CIFAR-10 budget is \(8/255\). (a) What is this as a decimal? (b) If an evaluation instead reports a CIFAR-10 budget of 0.3, what should you suspect? (c) Why is 0.3 a reasonable budget for MNIST but not for CIFAR-10?

Show the answer

(a) \(8/255 \approx 0.031\). (b) A budget of 0.3 on CIFAR-10 is about ten times the conventional \(8/255\); such a large per-pixel change would be plainly visible on a color photograph, so either the budget is misstated, the norm or scale differs from the one assumed, or the evaluation is not comparable to standard results. (c) MNIST digits remain recognizable to a person under a per-pixel change of 0.3 because they are simple high-contrast shapes, whereas the same change on a CIFAR-10 photograph is glaring, so a much smaller budget is used. A budget is meaningful only together with its norm and input scale.

Exercise 3. The query cost of a substitute

An attacker builds a substitute model with Jacobian-based dataset augmentation, which roughly doubles the training set each round by adding points perturbed along the substitute's most sensitive direction, labeling each new point by querying the target. Starting from 200 seed inputs and running 7 augmentation rounds, about how many queries does the attacker make to the target, and why does this matter for defense?

Show the answer

Each round roughly doubles the set, so after 7 rounds the set has about \(200 \times 2^7 = 25{,}600\) points, which is the number of target queries. This matters because it is a modest number for an online service, so a black-box attacker can build a usable substitute, craft adversarial examples against it with white-box access, and transfer them to the target (Papernot et al., 2017). A defense that assumes the attacker cannot compute gradients because the model is hidden is therefore not safe, and a defense can also rate-limit or monitor queries to raise this cost.

Exercise 4. Why random restarts matter

A single run of PGD finds an adversarial example for a given input with probability 0.6, where the randomness is the random start. If the input actually admits an adversarial example, what is the probability that an attack using \(k\) independent restarts fails to find one for \(k = 1\), \(k = 3\), and \(k = 10\)? What does this say about reporting robust accuracy from a single-restart attack?

Show the answer

The restarts are independent, so the probability that all \(k\) fail is \((1 - 0.6)^k = 0.4^k\). For \(k = 1\) it is \(0.4\), for \(k = 3\) it is \(0.4^3 = 0.064\), and for \(k = 10\) it is \(0.4^{10} \approx 0.0001\). A single-restart attack wrongly declares such an input robust 40% of the time, so robust accuracy measured with one restart overstates robustness; the number of restarts is part of what an evaluation must report, as Lesson 7.3 states.

Exercise 5. State the threat model (design)

A bank deploys a model that scores transactions for fraud. An attacker controls the amount, merchant category and timing of their own transactions, can submit transactions and observe whether each is declined, but cannot see the model, its features or its training data, and cannot alter past data. Write out the threat model along the goal, knowledge and capability dimensions, and say which dimension decides whether a proposed evasion is feasible.

Show the answer

The goal is an integrity violation, getting a fraudulent transaction scored as legitimate, and it is targeted at the attacker's own transactions rather than indiscriminate. The knowledge is black-box, since the attacker observes only the accept or decline decision and knows nothing of the model, features or training data. The capability is exploratory (test-time only, not poisoning) and problem-space, since the attacker modifies real transaction attributes subject to constraints (the amount and timing are real quantities with their own limits and costs, and the transaction must still achieve the attacker's aim), not a feature vector directly. The capability dimension decides feasibility, since it is the constraint set on what the attacker can change, and a description that leaves it vague cannot say whether an attack would work, as Lesson 7.1 and Lab 7b emphasize.

Exercise 6. Diagnosing a suspicious evaluation (design)

A paper reports that its defense keeps a CIFAR-10 classifier at 55% accuracy under PGD at \(\varepsilon = 8/255\), and that accuracy stays near 50% even when \(\varepsilon\) is raised to 0.5. Identify the warning sign, name the likely cause, and list two checks you would run.

Show the answer

The warning sign is that accuracy barely falls, from 55% to about 50%, although the budget grows about sixteenfold to a very large in-range value. At \(\varepsilon = 0.5\) with pixels in the interval from 0 to 1, the attacker can set every pixel to 0.5 and so turn every test image into the same uniform gray image, on which any classifier is correct only for the images of the one class it predicts, 10% of a balanced CIFAR-10 test set. A correct attack should therefore reach 10% accuracy or lower, and a plateau near 50% indicates that the attack is failing, not that the defense works. The likely cause is gradient masking, where the defense makes the gradient PGD uses uninformative without conferring real robustness (Athalye et al., 2018). Two checks are worth running. First, run the sanity check with an unbounded or very large in-range attack and confirm it reaches near-zero accuracy; second, run an attack that does not rely on the masked gradient, such as a transfer attack from a surrogate (Carlini et al., 2019) or an adaptive attack that differentiates through or approximates the defense's mechanism (Tramèr et al., 2020).

Exercise 7. Reading the robustness curve (uses Lab 7a)

In Lab 7a the classifier's clean accuracy is about 95%, and under PGD its accuracy is 62% at \(\varepsilon = 0.1\) and 0% at \(\varepsilon = 0.3\). (a) What is the robust accuracy at \(\varepsilon = 0.1\)? (b) Does this evaluation pass the sanity check of Lesson 7.3? (c) The PGD examples crafted on the first model lower a second model's accuracy from about 96% to 32% at \(\varepsilon = 0.2\). What does that demonstrate?

Show the answer

(a) The robust accuracy at \(\varepsilon = 0.1\) is just the accuracy under the attack at that budget, 62%, and it is meaningful only together with the threat model (here a white-box attacker with an \(L_\infty\) budget of 0.1 on pixels in the interval from 0 to 1) and the attack that measured it (PGD with 10 steps, a random start and 3 restarts). (b) Yes, because PGD drives accuracy to 0% at \(\varepsilon = 0.3\), which is near zero, so a strong attack reaches near-zero accuracy at a large in-range budget as it should. (c) It demonstrates transferability. Examples crafted white-box against the first model lower the second, independently trained model's accuracy substantially (from 96% to 32%) without using its gradients or outputs. Because the first model shares the second's architecture and training data, the lab's setting is gray-box with respect to the second model (Lesson 7.1); the same transferability is what lets black-box attackers succeed through a surrogate.

References

  1. Athalye, A., Carlini, N., and Wagner, D. (2018). Obfuscated gradients give a false sense of security: Circumventing defenses to adversarial examples. Proceedings of the 35th International Conference on Machine Learning (ICML), 274-283. https://proceedings.mlr.press/v80/athalye18a.html
  2. Carlini, N., Athalye, A., Papernot, N., Brendel, W., Rauber, J., Tsipras, D., Goodfellow, I., Madry, A., and Kurakin, A. (2019). On evaluating adversarial robustness. arXiv. arXiv preprint arXiv:1902.06705. https://arxiv.org/abs/1902.06705
  3. Papernot, N., McDaniel, P., Goodfellow, I., Jha, S., Celik, Z. B., and Swami, A. (2017). Practical black-box attacks against machine learning. Proceedings of the 2017 ACM on Asia Conference on Computer and Communications Security (ASIA CCS), 506-519. https://doi.org/10.1145/3052973.3053009
  4. Tramèr, F., Carlini, N., Brendel, W., and Madry, A. (2020). On adaptive attacks to adversarial example defenses. Advances in Neural Information Processing Systems, 33, 1633-1645. https://proceedings.neurips.cc/paper_files/paper/2020/hash/11f38f8ecd71867b42433548d1078e38-Abstract.html