Core / AI security fundamentals
AI security fundamentals cheat sheet
Set up (lesson 0)
| Task | Command or code |
|---|---|
| Make the lab folder | mkdir -p ~/krlab/ai-security-fundamentals |
| Switch on the environment of "How AI works" | source ~/krlab/how-ai-works/.venv/bin/activate |
| Prove that the lab works | python labcheck.py (the last line must say 3 of 3) |
| Which command to use | lessons 2 and 3: python with the environment on; the others: python3 |
Use case and attack surface (lesson 1)
| Task | Command or code |
|---|---|
| The seven answers of a use case | purpose, objective, owner, success (a number), stop when, not for, human |
| Check a use case | python3 check_usecase.py (a file is an optional word) |
| The five places of attack | data, model, application, agent and tools, infrastructure |
| The three groups of writers | trusted, untrusted, third-party |
| Ask who can write to each asset | python3 assets.py (ASK means an untrusted or third-party writer) |
| Check a map for a missing layer | python3 assets.py assets-test.json | tail -2 |
| Read JSON in Python | json.load(f) |
| Print the lines without a word | grep -v word file |
Lifecycle attacks (lesson 2)
| Task | Command or code |
|---|---|
| The stages | use case, collect data, train, evaluate, deploy, operate |
| The floor of the gate | taken from the success answer of the use case |
| The three human points | human-in-the-loop (approves an action), human oversight (watches and may stop), human validation (checks a sample against reality) |
| Recall of each class | recall_score(y, model.predict(X), average=None) |
| Fixed random choice of positions | rng = np.random.default_rng(0), rng.choice(positions, n, replace=False) |
| Gate: fail when a class is weak | weak = [d for d, r in enumerate(recalls) if r < floor] |
| Keep the test set outside the attacker's reach | own storage, own owner, short access list |
Evasion, poisoning, privacy (lesson 3)
| Task | Command or code |
|---|---|
| The three NIST AI 100-2 families (2025 edition) | evasion, poisoning, privacy |
| Slope of the loss for each pixel (linear model) | (model.predict_proba(X) - np.eye(10)[y]) @ model.coef_ |
| Chosen-direction change of a given size | X + size * np.sign(slope) |
| Keep pixels in range | np.clip(images, 0, 16) |
| Confidence of the top answer | model.predict_proba(X).max(axis=1) |
| Cheap controls | return the label only, limit queries for each user, log the queries |
Attack path with ATLAS (lesson 4)
| Task | Command or code |
|---|---|
| Tactic and technique | tactic = why, technique = how |
| Write the path | one line for each step: tactic, technique, asset, control |
| Check the path | python3 check_path.py (a folder is an optional word) |
| Change text into a new file | sed 's/old/new/' file > file.new && mv file.new file |
| Print lines 5 to 8 | sed -n 5,8p |
| Read a CSV in Python | csv.DictReader(open("file.csv")) |
OWASP Top 10 for LLM Applications, 2025 edition (lesson 5)
| Task | Command or code |
|---|---|
| The ten risks | Prompt Injection, Sensitive Information Disclosure, Supply Chain, Data and Model Poisoning, Improper Output Handling, Excessive Agency, System Prompt Leakage, Vector and Embedding Weaknesses, Misinformation, Unbounded Consumption |
| Test a secret in the prompt | python3 test_leak.py |
| Test a planted instruction | python3 test_inject.py |
| Test output put in a page | python3 test_html.py |
| Remove every link from an answer | re.sub(r"https?://\S+", "[link removed]", answer) |
| Escape text for HTML | html.escape(answer) |
| Read a file as a list of lines | open("answers.txt").read().splitlines() |
Threat model (lesson 6)
| Task | Command or code |
|---|---|
| The four questions | what are we building, what can go wrong, what will we do, did we do a good job |
| Four kinds of untrusted text | user input, retrieved documents, tool results, model output |
| The rule for the file | every flow that crosses a boundary has a threat, every threat has a control |
| Check flows and threats | python3 check_threats.py |
| Add a line to a file | echo 'T13,F9,...' >> threats.csv |
Controls (lesson 7)
| Task | Command or code |
|---|---|
| The four kinds | prevent, detect, respond, govern |
| A control must work outside the model | column outside_model is yes; a sentence in the prompt is no |
| Find the gaps | python3 check_controls.py |
| Add text to the end of a line | sed '/^request-log/ s/$/ T2/' controls.csv > controls.new |
Several edits in one sed | sed -e '...' -e '...' file > file.new |
Providers (lesson 8)
| Task | Command or code |
|---|---|
| Score a provider | python3 score.py vendor.json |
| Points | yes = full weight, partial = half, no or unknown = none |
| Verdicts | HOLD, REJECT, ACCEPT WITH CONDITIONS, ACCEPT |
| A critical item that is not yes | HOLD, unless accepted_by names a person |
| Duties that stay with you | decide what data leaves, control access, treat output as untrusted, monitor, respond, prepare to leave |
Public risk resources (lesson 9)
| Task | Command or code |
|---|---|
| MIT AI Risk Repository | Domain Taxonomy: 7 domains, 24 subdomains; Causal Taxonomy: entity, intent, timing; written as 2.2 |
| OWASP Machine Learning Security Top 10 | a draft with 2023 labels, ML01 to ML10; for a model that you train |
| CVE Program record (Common Vulnerabilities and Exposures) | affected product, type or cause, description, public reference |
| CWE | Common Weakness Enumeration: a numbered name for a type of weakness; a record should use one |
| CVE AI Working Group | decides what counts as a CVE-able flaw in AI technology |
| Link threats and name watchers | python3 check_sources.py (a folder is an optional word) |
| Add a line to a file | echo 'A4,component,watcher' >> watch.csv |
The security file (lesson 10)
| Task | Command or code |
|---|---|
| The five documents | assets.json, attack-path.csv, flows.csv and threats.csv, controls.csv, test-plan.csv |
| Check the whole file set | python3 check.py (a folder is an optional word) |
| Exit code of the checker | 0 = all rules pass, 1 = a rule fails |
| Copy several files | cp ../kr-books/{a,b,c} . |
| Prove that a check can fail | break one value on purpose, run it, restore the file |
The NIST AI Risk Management Framework (lessons 4, 6, 8 and 10)
| Function | Question |
|---|---|
| Govern | who decides, who owns, who reviews |
| Map | what is the system, who is involved |
| Measure | how do we know that it works |
| Manage | which controls answer the risks |
Safety habits
- Attack only your own lab and your own model. Never point a test at a system that is not yours.
- Put no secret in a prompt. Treat the system prompt as text that a user can read.
- Treat all model output as untrusted input: escape it, filter it, and check every tool call.
- A rule in the prompt is a request. Count a control only if it works outside the model.
- Test with changes chosen against the model, not only with random noise, and report the attacker that you assumed.
- Measure each class, not only the average. Keep the test set outside the reach of the attacker.
- Report a count with its conditions: model, version, temperature, seeds and runs.
- Name the edition of a standard (OWASP 2025, NIST AI 100-2 2025) and look up identifiers in the source.
- Name the state of a public list too: a draft, a living database, a charter with a version. Take the current text from its owner.
- A passing checker shows that the paper is consistent, not that the system is safe. Say what you did not check.