Scenario test · 90 minutes
Evaluate a colleague's triage copilot on a new week and set its limits
- Measure what a copilot misses on a week with a new pattern, from the labels
- Find where it misses incidents and what it should never close
- Recommend a threshold and a human-review rule, with numbers, in a one-page evaluation
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.
Scenario
At Harbourline Logistics your colleague Mara has built a triage copilot, and it goes live on Monday. It uses the classifier of this course, trained on weeks 1 to 4. It closes every alert with a score below 0.5 and has no human review. Mara reports: "Accuracy 0.98 on the fixed week. It saves the team 96 percent of the reading." The new week of alerts, week 6, has now closed, and the investigators have labelled it. Your lead gives you two days: "Test it on week 6. Find where it misses incidents and what it closes that it should not. Then recommend a threshold and a rule for human review, with numbers I can sign." All data is invented and made by gen.py.
- Attacker → Alert feed: a new kind of attack
- Alert feed → Mara's copilot: each alert gets a score
- Mara's copilot → Analyst: score 0.5 or more
- Mara's copilot → Closed pile: below 0.5: nobody reads
Your tasks
Work in ~/krlab/ai-for-secops. Keep the files of the course in it, and make a copy of Mara's work before you change anything. Save Mara's program.
colleague.py
"""colleague.py: the triage copilot of a colleague, Mara. It goes live on Monday.
It trains on weeks 1 to 4, closes every alert with a score below 0.5, and has no human review.
python3 colleague.py prints its accuracy on the old fixed week and on the new week
"""
from gen import alerts
from triage import scores, train
CLOSE_BELOW = 0.5
def decide(week, model):
"""The verdict for each alert id: "escalate" or "close"."""
return {a["id"]: ("escalate" if s >= CLOSE_BELOW else "close") for a, s in zip(week, scores(model, week))}
def accuracy(week, verdicts):
right = sum((verdicts[a["id"]] == "escalate") == bool(a["label"]) for a in week)
return round(right / len(week), 2)
if __name__ == "__main__":
model = train()
for name, week in (("week 5 (the old pattern)", alerts(5)), ("week 6 (the new week)", alerts(6, shift=True))):
print(name.ljust(26), "accuracy", accuracy(week, decide(week, model)))1. Run it and read it
Run python3 colleague.py. Read the code before you trust it: write down every place where it relies on the future looking like the past. The new week is alerts(6, shift=True).
2. Measure what it misses
Write a script that applies Mara's decide to week 6 and counts, against the labels, the alerts that it closes, the real incidents among all alerts, and the real incidents among the closed ones. Compare its accuracy with that of a program that closes every alert.
3. Find where it misses
Group the missed incidents by rule. Then find what is different about week 6 compared with weeks 1 to 4: name the field and the value. Last, count the alerts that Mara's copilot closes from an address on gen.BAD_IPS.
4. Try thresholds
Use threshold.table from lesson 2 on the scores of week 6. Find how many alerts the threshold 0.05 closes, and how many incidents it misses. Say whether any threshold of the table misses fewer than 10 incidents.
5. Write a review rule
A threshold cannot fix a blind spot. Write a rule for the person, from facts that do not depend on the model: the person reads an alert when the rule says so, and every other alert closes. Your rule must leave at most 10 incidents of week 6 unread, and it must fit the time on shift (56 hours a week, 6 minutes for a read). Count the alerts that a person reads and the incidents that nobody reads. The page checks only the size of the budget. Compare your counts with the model solution.
6. Check the mail
Your lead asks whether the same week hides signals that Mara's copilot cannot see: mail, logins and notes that people paste from a model. Tasks 6 to 8 look for them. Mara's copilot scores alerts and reads no mail. The finance team sends you mail_stream(), a day of mail, and asks whether it holds a campaign that no alert shows. Use the burst rule of lesson 7 with its settings, and the executive header check.
7. Check the logins
The night analyst has 8 reads for the sessions of logins(). Use anomaly.py of lesson 4 with the same fields and settings, and give the isolation forest and the z-score the same budget of 8 reads.
8. Grade the notes and the claims
The copilot's notes come from a model, and a person may paste a model's claim into a ticket. Run gen.py intel if the folder intel is missing. Then grade the four summaries of recorded-03.json with check of lesson 3, and the ten sample claims of claims-06.json with check_line of lesson 6.
9. Decide
Choose a threshold for auto-closing and a human-review rule. Use the cost of a read (6 minutes) and the time on shift (56 hours a week) to say whether your rule fits the team. State the risk that remains, and the control for the mail burst that your recommendation adds.
Hand in
Hand in a one-page evaluation.md with these headings, in this order. A manager must be able to decide from it.
| Heading | What it holds |
|---|---|
| What was tested | The program, the data (weeks 1 to 4 for training, week 6 for the test), the date and the library versions. |
| What Mara reported and what it hides | Her accuracy, and the recall and the count of silent misses on week 6. |
| Where it misses | The rule and the pattern of the missed incidents, with counts and one reason that code can show. |
| Recommendation | A threshold and a review rule, with the counts of reads and misses that each gives, and the hours. |
| Other signals | The mail burst and the executive header (task 6), the planted sessions that the anomaly detector finds with 8 reads (task 7), and what the checkers of lessons 3 and 6 pass (task 8). |
| What remains open | What your test does not cover (for example a campaign of one message), what you cannot measure, and one next step with an owner. |
Review your work
One model solution
This script reproduces every number above. It is a model solution, so it is not a file of the lab.
import json
from collections import Counter
import colleague
from anomaly import in_top, isolation, matrix, zscores
from bursts import bursts, fake_executive
from gen import BAD_IPS, alerts, logins, mail_stream
from intel import check_line, load
from summarise import case_log, check, facts
from threshold import table
from triage import scores, train
model = train()
new = alerts(6, shift=True)
score = dict(zip((a["id"] for a in new), scores(model, new)))
verdict = colleague.decide(new, model)
real = [a for a in new if a["label"]]
closed = [a for a in new if verdict[a["id"]] == "close"]
missed = [a for a in closed if a["label"]]
print("new week:", len(new), "alerts,", len(real), "real incidents")
print("closed by Mara's copilot:", len(closed), " missed incidents:", len(missed), " recall:", round(1 - len(missed) / len(real), 2))
print("accuracy of closing every alert:", round(1 - len(real) / len(new), 2))
print("missed by rule:", Counter(a["rule"] for a in missed).most_common(3))
seen = {a["rule"] for w in (1, 2, 3, 4) for a in alerts(w)}
print("alerts of a rule that the training weeks never had:", sum(a["rule"] not in seen for a in new))
print("closed alerts from a bad address:", sum(a["src_ip"] in BAD_IPS for a in closed), " of them real:", sum(a["src_ip"] in BAD_IPS for a in missed))
table(new, score)
def read(a):
"""The review rule: a person reads the alert when any of four conditions holds."""
return score[a["id"]] >= 0.02 or a["rule"] not in seen or a["src_ip"] in BAD_IPS or a["asset"] == 3
unread = [a for a in new if not read(a)]
print("review rule: read", len(new) - len(unread), " incidents never read:", sum(a["label"] for a in unread))
mail = mail_stream()
print("mail: burst of", [len(b) for b in bursts(mail)], "messages, executive header hits:", sum(fake_executive(m) for m in mail))
sessions = logins()
X = matrix(sessions)
print("logins, planted found with 8 reads: forest", in_top(isolation(X)[0].argsort(), sessions, 8), " z-score", in_top(zscores(X).argsort()[::-1], sessions, 8))
log, base = case_log(), load()
print("recorded summaries that pass check:", sum(not check(t, facts(log), log) for t in json.load(open("recorded-03.json"))), "of 4",
" sample claims that pass check_line:", sum(check_line(base, c) == "ok" for c in json.load(open("claims-06.json"))), "of 10")new week: 1000 alerts, 61 real incidents
closed by Mara's copilot: 975 missed incidents: 36 recall: 0.41
accuracy of closing every alert: 0.94
missed by rule: [('oauth-consent-grant', 21), ('large-upload', 5), ('new-admin-account', 3)]
alerts of a rule that the training weeks never had: 88
closed alerts from a bad address: 8 of them real: 3
threshold auto-closed missed to read workload saved
0.01 487 16 513 49%
0.02 719 27 281 72%
0.05 855 33 145 86%
0.1 937 35 63 94%
0.2 962 35 38 96%
0.3 969 36 31 97%
0.5 975 36 25 98%
real incidents in the week: 61
review rule: read 386 incidents never read: 7
mail: burst of [26] messages, executive header hits: 6
logins, planted found with 8 reads: forest 7 z-score 6
recorded summaries that pass check: 0 of 4 sample claims that pass check_line: 4 of 10What the numbers say. Accuracy is 0.96 only because 94 percent of the alerts are harmless: a program that closes everything has an accuracy of 0.94. Mara's copilot finds 25 of 61 incidents, a recall of 0.41, and 36 incidents are closed in silence. The new pattern is the cause. The rule oauth-consent-grant did not exist in the training weeks, so the model has no column for it. It holds 21 of the 36 misses. The other misses are quiet incidents that look harmless in every field. The new campaign works in business hours, with low severity and no admin account, which is the opposite of what the model learned.
A threshold is not the fix. No threshold of the table misses fewer than 10 incidents: even at 0.01 the model misses 16. At 0.05 it closes 855 alerts and misses 33. A lower threshold buys reading, and it cannot find what the model never learned.
The review rule that the solution uses has four conditions: a score of 0.02 or more, a rule that was not in weeks 1 to 4, an address on the bad list, or an asset of the finance and payroll team. It works because it uses facts that do not depend on the model: a rule that is new, a bad address, a critical asset. A person reads 386 alerts, which is 38.6 hours at 6 minutes each, inside the 56 hours on shift. Only 7 incidents are never read, against 36. Those 7 are the quiet ones. Your rule can differ, and it is good if it leaves at most 10 incidents unread and fits the 56 hours.
The mail is a second blind spot. Mara's copilot never reads it, and the day holds one burst of 26 messages from 9 senders and 6 messages with an executive's name on a foreign address. The recommendation adds the burst rule and the header check, and the call-back rule of lesson 7 for payments. On the logins, the isolation forest finds all 7 planted sessions with 8 reads and the z-score finds 6, so the forest feeds the night list.
A model evaluation, in the six headings:
- What was tested. Mara's
colleague.py, trained on weeks 1 to 4, tested on week 6 with 1,000 alerts and 61 incidents. Python and library versions frompip freeze. The date. - What Mara reported and what it hides. Accuracy 0.98 and 0.96 are true and tell little. Recall is 0.41 on week 6, and 36 incidents are closed with no reading. On week 5 it was 0.71.
- Where it misses.
oauth-consent-grant: 21 of 36 misses, and 0 alerts of that rule in the training data. Eight closed alerts came from a listed bad address, and 3 of them were real incidents. - Recommendation. Do not go live with auto-close at 0.5. Close alerts below 0.02 only when none of the other three conditions holds, and let a person read the rest: 386 alerts, 7 incidents unread, 38.6 hours. Retrain on a week that holds the new rule, and measure again.
- Other signals. A burst of 26 messages from 9 senders and 6 executive headers. The forest finds 7 of 7 planted sessions with 8 reads and the z-score finds 6. The checkers pass 0 of 4 recorded summaries and 4 of 10 sample claims.
- What remains open. One new week is one sample. Quiet incidents (7 here) are not found by any score. The reviewer was assumed to be right, and a real reviewer is not. Next step: the SOC lead samples 20 closed alerts a week and reports the real incidents among them, starting next Monday.
Mark your report
You mark this yourself, or you ask a colleague or a mentor to mark it. The page checks the numbers above; it cannot read your report.
| Criterion | Not yet | Meets the standard | Strong |
|---|---|---|---|
| Findings | Only accuracy, or misses the new rule. | Recall 0.41, 36 silent misses, oauth-consent-grant with 21, 8 closed bad-address alerts. | Shows that closing every alert scores 0.94, so 0.96 says little. |
| Evidence | A number with no script or data week. | Script, training and test weeks, date, library versions. | Another person reruns the script and gets every number. |
| Judgement | A cause that is a guess, or no assumption named. | Cause shown by code; the perfect reviewer named as an assumption. | Shows from the table why no threshold alone is enough. |
| Recommendation | A threshold only, or a rule over 56 hours. | Threshold and rule with reads, misses, hours; at most 10 unread; mail controls. | A next step with an owner and a start date. |
| Writing | No clear recommendation, or blames Mara. | One page, six headings, dated; the remaining risk in one line. | A manager can sign it in five minutes. |
Meets the standard in every row: you can show this report as a work sample. Remove names and data that are not yours to share.
The report proves your work on this scenario only, done on your own machine without a proctor.