Advanced / Red-team your own AI system
Red-team your own AI system cheat sheet
Set up (lesson 0)
| Task | Command or code |
|---|---|
| Make the lab folder | mkdir -p ~/krlab/red-team-your-ai |
| Check the lab (the model is optional) | python3 labcheck.py |
| Start the target with the stand-in model | KR_STUB=1 python3 target.py |
| Start the target with the local model | python3 target.py |
| Switch fixes on for a retest | KR_STUB=1 KR_FIX=owner,path,debug python3 target.py |
Scope and rules (lesson 1)
| Task | Command or code |
|---|---|
| Check planned requests against the scope | python3 rules.py check draft.json |
| Check at a time, with repeats | python3 rules.py check draft.json --at 10:00 --runs 6 |
| Use another scope file | python3 rules.py check draft-b.json --scope scope-b.json |
Send a request through the rules (no redirect, no proxy, http only) | call(scope, "GET", url) |
| A scope has one entry for each port, with its own paths and methods | "in_scope": [{"host": ..., "port": ..., "paths": [...], "methods": [...]}] |
| Show the budget that is spent | python3 rules.py used |
| Start a new budget (the owner gives it) | python3 rules.py reset |
| Ask whether the test must stop | stop_reason(scope, used, errors_in_row, stop_file_present) |
| Stop with the stop word | touch STOP-NOW |
Map the target (lesson 2)
| Task | Command or code |
|---|---|
| Probe the paths inside the scope | python3 mapper.py probe paths.txt |
| Find routes and tools in the source | grep -n "self.path|def do_" target.py |
| Scan a folder or a file for secrets | python3 secretscan.py repo-sample |
| Mask secrets in a text | mask(text) |
| Check the data flows | python3 mapper.py flows flows.txt |
Test cases (lesson 3)
| Task | Command or code |
|---|---|
| Check the table and count the cases for each risk | python3 coverage.py cases.csv not-tested.txt |
| A judge that looks for a text | contains:PINEAPPLE, missing:30 days |
| A judge that looks for a pattern | regex:<img|<script |
| A judge for a tool call | tool_called:order_status:PC-1002, forbidden_path |
The harness (lesson 4)
| Task | Command or code |
|---|---|
| Send every case three times and save the counts | python3 harness.py run -n 3 --summary before.json |
| Send only some cases | python3 harness.py run --only info-1,info-2 |
| Read the exit code | 0 clean, 1 unsafe run, 2 broken: no answer, stopped early, a case refused by the scope, or no case ran |
| Keep the old record | a new run renames an old runs.jsonl to runs.jsonl.1, then .2 |
| Score recorded answers, send nothing | python3 harness.py score recorded-04.jsonl |
| Point the harness at another target | KR_TARGET=http://127.0.0.1:8411 python3 harness.py run |
Manual testing (lesson 5)
| Task | Command or code |
|---|---|
| Send one message and write a note | python3 probe.py "message" --change "word=code" |
Let a harness judge fill auto, and add your verdict | python3 probe.py "message" --judge contains:PINE-STAFF --verdict unsafe |
| Probe as another customer | python3 probe.py "message" --customer u2 |
| Vary one thing at a time | vary(start, axes) |
| Summarise a session of notes | python3 notes.py read session-05.jsonl |
The infrastructure (lesson 6)
| Task | Command or code |
|---|---|
| Show the listening ports | ss -ltn |
| Check the ports of the scope | python3 ports.py |
| Check a saved listing | python3 ports.py ss-sample.txt |
| Check login, debug page, log and body size (only the expected refusal is ok) | python3 infra.py --summary before.json |
Findings (lesson 7)
| Task | Command or code |
|---|---|
| Score impact times likelihood | python3 severity.py 3 4 |
| Turn counts into a likelihood | likelihood_from_rate(hits, runs) |
| Upper bound of the true rate (95 percent) | upper_bound(hits, runs) |
Check a findings file (WEAK: the upper bound allows a higher likelihood) | python3 findcheck.py findings.json |
| Send the evidence again | python3 findcheck.py findings.json --replay |
Report and retest (lesson 8)
| Task | Command or code |
|---|---|
| Generate the report | python3 report.py findings.json before.json after.json not-tested.txt > report.md |
| Report for a live run | python3 report.py ... --live llama3.2:3b > report.md |
| Check the report | python3 reportcheck.py report.md findings.json |
| Compare counts before and after | outcome(before, after) |
The whole assessment (lesson 9)
| Task | Command or code |
|---|---|
| Draft the findings | python3 draftfind.py before.json runs.jsonl judgement.json > findings.json |
Run every stage (exit 0 pass, 1 critical open, 2 broken stage; a broken run leaves report-full.INCOMPLETE.md) | ./assess.sh |
Safety habits
- Test only a system that you own, or that the owner has allowed in writing. Write the scope down before the first request.
- Send every request through one function that checks the scope, the stop word and the budget. It does not follow a redirect, and the budget is kept in a file.
- A run with no answer is an error, never a pass, and a test that sent nothing is not clean. Exit code 2 means that you cannot trust the result. A refusal that nobody catches also ends with exit code 2.
- Run the lab target only on
127.0.0.1, keep no personal files in the lab folder, and stop the target when you do not test. - Write the safe behaviour of a case before the first run, and keep the record of every answer.
- Report a count with its runs, the model and the date. Never write "it always works".
- Mask every secret in notes, evidence and reports. Print four characters at most.
- Retest with the same cases and the same runs. Change only the fix.
- List what you did not test, and why.