Advanced / AI for security operations
AI for security operations cheat sheet
Set up the lab (lesson 0)
| Task | Command or code |
|---|---|
| Make the lab folder | mkdir -p ~/krlab/ai-for-secops |
| Make and switch on the environment | python3 -m venv .venv, source .venv/bin/activate |
| Install the pinned libraries | pip install -r requirements.txt |
| Show the size and the fingerprint of the data | python3 gen.py |
| Prove the lab works | python3 labcheck.py (last line lab ready: 9 of 9) |
| Skip the model if your computer cannot run it | export KR_NO_MODEL=1 (last line lab ready: 7 of 7) |
Measure the work and the baseline (lesson 1)
| Task | Command or code |
|---|---|
| Load the fixed week of alerts | from gen import alerts then alerts(5) |
| Print the workload and the baseline | python3 workload.py |
| Count by a field | Counter(a["rule"] for a in week).most_common() |
| Read the first N of each day in a chosen order | reached(week, lambda a: (-a["asset"], a["id"])) |
| Change the capacity of a day | CAPACITY in workload.py |
Triage with a model (lesson 2)
| Task | Command or code |
|---|---|
| Train on weeks 1 to 4 | from triage import train, scores then model = train() |
| Train on other weeks | train([3, 4]) |
| Score a week (a ranking score, not a probability) | scores(model, week) |
| Precision and recall at a cut-off | precision_score(y, pred), recall_score(y, pred) |
| Cost table of auto-closing | python3 threshold.py |
| State the threshold as a rule | "Auto-closing may miss at most N incidents in 10" |
| Choose it on a week that you do not report | choose(week4, score4) |
Summarise a log and check it (lesson 3)
| Task | Command or code |
|---|---|
| Find the key facts by code | facts(case_log()) |
| Rule-based summary | summarise(case_log()) |
| Prompt for the model | prompt(case_log()) |
| Check a summary (empty list is a pass) | check(text, facts(log), log) |
| Test the checker with hand-written summaries | python3 test_check.py |
| Ask the model (temperature 0 and a fixed seed repeat a run) | chat([{"role": "user", "content": prompt(log)}], seed=1, temperature=0) |
| Ask the model once and check | python3 ask_model.py |
Find anomalies (lesson 4)
| Task | Command or code |
|---|---|
| Largest z-score of each session | zscores(X) |
| Isolation forest scores and alerts | score, flag = isolation(X, contamination=0.02) |
| Compare with the same budget | in_top(z.argsort()[::-1], rows, 8) |
| Run both detectors | python3 anomaly.py |
Classify messages (lesson 5)
| Task | Command or code |
|---|---|
| Messages in the old and in the new style | messages(1, 600), messages(3, 200, "new") |
| Three numbers from the message (urgent words is the wording control) | extra(message) gives urgent words, link, mismatch |
| Train with or without the three numbers | predict = build(train, use_extra=True) |
| Precision and recall, rounded | measure(predict, rows) |
| Run the comparison | python3 phish.py |
Use intelligence with sources (lesson 6)
| Task | Command or code |
|---|---|
| Write the reports | python3 gen.py intel |
| Search with sources | python3 intel.py "Which port does Group Alder use?" |
| Keys that two reports state differently | conflicts(base) |
| Stale, conflict or unverified report | warnings(base, "rpt-02.txt", question) |
| Check one claim of an answer (group and whole value) | check_line(base, line) gives ok or the reason |
Recognise AI-enhanced attacks (lesson 7)
| Task | Command or code |
|---|---|
| Find a burst of near-copies | python3 bursts.py |
| Change how similar texts must be | bursts(rows, similar=0.7) |
| Display name of an executive, foreign address | fake_executive(message) |
| Hold a payment that skips the approval | hold(request) returns the reasons |
| Run the payment rule | python3 approvals.py |
See what changes when attackers use AI (lesson 8)
| Task | Command or code |
|---|---|
| Write the web log of the day | python3 weblog.py |
| Signature rule against behaviour rule on samples | python3 detect.py samples.json |
| Hit of the signature rule, hit of the behaviour rule | by_signature(sample), by_behaviour(sample) |
| Measure each client of the log and test four rules | python3 footprint.py |
| Repeat share of a client | 1 - paths / requests (low for a scanner, 0.58 to 0.75 for staff) |
| Minutes of a flood that no client's rate shows | busy_minutes(rows) |
Automate safely (lesson 9)
| Task | Command or code |
|---|---|
| Enrich one alert | python3 enrich.py W5-0003 |
| Dry run of a suggestion (default) | python3 gate.py suggestion.json |
| Apply an action that needs no approval | python3 gate.py suggestion.json --apply |
| Apply with a named approver | python3 gate.py suggestion.json --apply --approved-by sam |
| Exit codes of the gate | 0 done, 1 refused, 2 waiting for approval |
| Verdict for a suggestion | gate.decide(suggestion) gives allow, approval or refuse |
| Prove the gate with a table | python3 test_gate.py |
| Read the audit log | cat audit.log |
Gate a change with scanners, tests and a pipeline (lesson 10)
| Task | Command or code |
|---|---|
| Make the folders of the lesson | cd ~/krlab/ai-for-secops, mkdir -p shop drafts |
| Rule check of a folder (exit code 1 on a security problem) | python3 lint.py shop |
| Composition scan against advisories, and the SBOM | python3 sca.py shop/requirements.txt advisories.json, cat sbom.json |
| Run the tests of the shop code | python3 test_app.py |
| Test the model against a floor | python3 model_test.py |
| Run every check, exit code is the gate | python3 pipeline.py; echo $? |
| See why one step failed | run that step alone, for example python3 lint.py shop |
Check an AI review and control the AI helpers (lesson 11)
| Task | Command or code |
|---|---|
| Check each AI finding against the faulty original | python3 verify.py drafts/app-faulty.py ai-review-stub.json |
| Check the installed helpers against the allowlist | python3 helpers.py installed.json allowlist.json; echo $? |
Check the output of AI tools (lesson 12)
| Task | Command or code |
|---|---|
| Check an AI threat list against the data flows | python3 threats.py flows.txt threats-stub.json |
| Check a penetration-test report against the scope | python3 pentest.py scope.txt pentest-stub.json |
| Check fraud scores against the payee rule | python3 fraud.py payments.json; echo $? |
Run the copilot and the build (lessons 13 and 14)
| Task | Command or code |
|---|---|
| Review three proposals yourself | python3 copilot.py ask 3 |
| Run the fixed week with a simulated reviewer | python3 copilot.py week |
| Which alerts a person must read | REVIEW_FROM (score) in copilot.py |
| Run every part and the table of parts | python3 triage_copilot.py |
| A reviewer who accepts every proposal to close | simulated_reviewer(labels, sees_all=False) |
| Errors against the labels | errors(records, labels) gives missed and false alarms |
Evaluate on a new week (lesson 15)
| Task | Command or code |
|---|---|
| The new week with a shifted pattern | alerts(6, shift=True) |
| Alerts closed and incidents missed | count verdict == "close" and label == 1 |
| Rules that the training weeks never had | {a["rule"] for w in (1, 2, 3, 4) for a in alerts(w)} |
| The day of mail for the burst rule | mail_stream() then bursts(rows) |
| Evaluation of a copilot | evaluation.md: tested, reported, misses, recommendation, open |
Safety habits
| Habit | Why |
|---|---|
| A model proposes, and a person or an approved rule decides | The model has no accountability and can be wrong or misled. |
| Count the silent misses before you auto-close | Accuracy hides them. Report recall and the count of missed incidents. |
| Choose the threshold from a rule about risk, not 0.5 | 0.5 is a default of the library. |
| Choose the threshold on one week and report another | A threshold chosen on the test week makes the report too good. |
| Measure on a new week, not on the training data | A pattern that shifts lowers recall, and no score warns you. |
| Find facts by code and check every summary | A fluent summary can lose or invent a fact. |
| Every claim carries a source, and code checks it | Retrieval lowers invention and does not remove it. Read stale, conflict and unverified warnings. |
| Rely on controls that do not depend on judging the message | Call-back, out-of-band approval, dual control, domain and header checks. |
| Detect what the attacker must do, not the words that they choose | A signature misses a reworded variant. Behaviour, footprint and total load stay. |
| Treat an AI review as leads and a scanner or test as the gate | A review can invent a fault and miss one. Verify each finding against the code. |
| Check what an AI tool lists against your own facts, by code | A threat list can name a flow that does not exist, and a scan can report a host that is not in the scope. |
| Allow-list every AI plug-in and assistant, and say what it reads and sends | Each one reads your data and can act in your name. |
| Allowlist, dry run, approval, audit line | The gate keeps a wrong suggestion from becoming an action. |
| Test every control with a table that has hostile rows | A checker or a gate that nobody attacked has holes that you have not seen. |
| Log the score, the evidence and the decision of every alert | "Why was it closed?" needs an answer. |
| Sample what was closed with no review | It is your live estimate of the silent miss rate. |
| Use invented data and your own lab only | Never put real logs or real mail into a prompt or a lab. |