prompt-injection-defense – skill Agent & Tự động hóa cho agent AI
Bảo vệ hệ thống AI trước prompt injection và indirect prompt attack bằng kiểm soát đầu vào, phân quyền công cụ, kiểm tra đầu ra và ranh giới cách ly.
Cài skill prompt-injection-defense bằng một lệnh
Chọn công cụ bạn đang dùng, copy lệnh và dán vào terminal. Skill sẽ nằm trong thư mục skills của công cụ đó và sẵn sàng dùng ở phiên làm việc tiếp theo.
Claude Code
mkdir -p ~/.claude/skills && curl -fsSL https://skill.dn1nguoi.com/z/prompt-injection-defense-2d065.tgz | tar xz -C ~/.claude/skills
Gọi trong Claude Code: /prompt-injection-defense – rồi mô tả công việc bạn muốn làm.
Codex
mkdir -p ~/.codex/skills && curl -fsSL https://skill.dn1nguoi.com/z/prompt-injection-defense-2d065.tgz | tar xz -C ~/.codex/skills
Gọi trong Codex: $prompt-injection-defense – rồi mô tả công việc bạn muốn làm.
Hermes Agent
mkdir -p ~/.hermes/skills && curl -fsSL https://skill.dn1nguoi.com/z/prompt-injection-defense-2d065.tgz | tar xz -C ~/.hermes/skills
Gọi trong Hermes Agent: /prompt-injection-defense – rồi mô tả công việc bạn muốn làm.
DeepSeek
mkdir -p ~/.deepcode/skills && curl -fsSL https://skill.dn1nguoi.com/z/prompt-injection-defense-2d065.tgz | tar xz -C ~/.deepcode/skills
Gọi trong DeepSeek: /prompt-injection-defense – rồi mô tả công việc bạn muốn làm.
Nội dung SKILL.md
Nội dung SKILL.md gốc bằng tiếng Anh.
Prompt Injection Defense
Mitigate direct and indirect prompt injection across chat apps, agentic workflows, and RAG pipelines.
When to Use This Skill
Use this skill when:
- Building or securing any LLM-powered application
- Designing RAG pipelines that ingest untrusted documents
- Implementing agentic workflows with tool-calling capabilities
- Responding to a reported prompt injection vulnerability
- Performing security reviews of AI-integrated products
Prerequisites
- Python 3.10+ with
re,hashlib,jsonstandard libraries - Access to the LLM application source code or configuration
- Understanding of the application's prompt architecture (system/user/tool boundaries)
- Test environment with representative user inputs and documents
Attack Surface
- User input attempting to override system instructions
- Untrusted documents/web pages in retrieval context
- Tool output that smuggles malicious instructions
- Cross-tenant leakage via shared context windows
- Markdown or HTML injection in rendered outputs
- Multi-turn attacks that gradually shift context
Defense-in-Depth Pattern
1. Instruction hierarchy enforcement: system > developer > user > tool output.
2. Context segregation: isolate untrusted text from control instructions.
3. Tool permissioning: explicit allow-list per task and tenant.
4. Output policy checks: validate schema, redact secrets, block unsafe actions.
5. Human approval: required for high-impact operations.
This document’s regexes and markers are advisory signals, not an injection detector with calibrated probability or a security boundary. Keep untrusted documents/tool results in low-trust message fields; enforce authorization in the application before every tool action. Prompt secrecy and text redaction cannot substitute for keeping secrets out of model context, tenant isolation and least privilege. Test task completion as well as adversarial and benign false-positive cases.
Input Sanitization Functions
"""prompt_sanitizer.py - Input sanitization for LLM applications."""
import re
import hashlib
import json
from typing import Optional
# Patterns that commonly appear in injection attempts
INJECTION_PATTERNS = [
r"(?i)ignore\s+(all\s+)?previous\s+instructions",
r"(?i)disregard\s+(all\s+)?(above|previous|prior)",
r"(?i)you\s+are\s+now\s+(DAN|evil|unrestricted|jailbroken)",
r"(?i)system\s*:\s*override",
r"(?i)SYSTEM\s+OVERRIDE",
r"(?i)new\s+instructions?\s*:",
r"(?i)forget\s+(everything|all|your\s+instructions)",
r"(?i)act\s+as\s+if\s+you\s+have\s+no\s+(restrictions|limits|rules)",
r"(?i)pretend\s+(you\s+are|to\s+be)\s+.*(unrestricted|evil|without)",
r"(?i)BEGIN\s+(TRUSTED|SYSTEM|ADMIN)\s+(CONTEXT|PROMPT|OVERRIDE)",
r"(?i)r"(?i)\[INST\]",
r"(?i)<\|im_start\|>system",
]
COMPILED_PATTERNS = [re.compile(p) for p in INJECTION_PATTERNS]
def detect_injection(text: str) -> dict:
"""Scan text for known prompt injection patterns.
Returns:
dict with 'detected' bool, 'patterns' list of matched pattern descriptions,
and 'risk_score' float between 0.0 and 1.0.
"""
matches = []
for i, pattern in enumerate(COMPILED_PATTERNS):
if pattern.search(text):
matches.append(INJECTION_PATTERNS[i])
risk_score = min(len(matches) / 3.0, 1.0)
return {
"detected": len(matches) > 0,
"patterns": matches,
"heuristic_score": risk_score,
"input_length": len(text),
}
def sanitize_input(text: str, max_length: int = 4096) -> str:
"""Sanitize user input before passing to the LLM.
- Truncates to max_length
- Strips null bytes and control characters
- Removes a small set of format characters, not general homoglyph attacks
- Normalizes whitespace
"""
if len(text) > max_length:
raise ValueError("Input exceeds the documented size limit; chunk explicitly rather than silently truncating")
# Remove null bytes and most control characters (keep newlines and tabs)
text = re.sub(r'[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]', '', text)
# Normalize Unicode confusables (basic set)
confusable_map = {
'\u200b': '', # zero-width space
'\u200c': '', # zero-width non-joiner
'\u200d': '', # zero-width joiner
'\u2060': '', # word joiner
'\ufeff': '', # BOM
'\u00a0': ' ', # non-breaking space
}
for char, replacement in confusable_map.items():
text = text.replace(char, replacement)
# Collapse excessive whitespace
text = re.sub(r'\n{4,}', '\n\n\n', text)
text = re.sub(r' {10,}', ' ', text)
return text.strip()
def sanitize_retrieved_context(documents: list[str], source_label: str = "RETRIEVED") -> str:
"""Wrap retrieved documents with clear boundary markers.
Markers aid readability only; documents can spoof them. They do not enforce roles.
"""
sanitized_parts = []
for i, doc in enumerate(documents):
doc_hash = hashlib.sha256(doc.encode()).hexdigest()[:8]
sanitized = sanitize_input(doc, max_length=2048)
wrapped = (
f"--- BEGIN {source_label} DOCUMENT {i+1} (ref:{doc_hash}) ---\n"
f"{sanitized}\n"
f"--- END {source_label} DOCUMENT {i+1} ---"
)
sanitized_parts.append(wrapped)
return "\n\n".join(sanitized_parts)
def validate_tool_call(tool_name: str, args: dict, allowed_tools: dict) -> dict:
"""Illustrative fail-closed policy evaluator for a narrow typed schema.
Policy: {"search": {"fields": {"results": {"type": "integer", "max": 10}},
"requires_human_approval": False}}
This does not establish tenant/resource authorization or approval evidence.
"""
policy = allowed_tools.get(tool_name)
if not isinstance(policy, dict) or not isinstance(args, dict):
return {"allowed": False, "reason": "Unknown tool or malformed arguments"}
fields = policy.get("fields")
if not isinstance(fields, dict) or set(args) != set(fields):
return {"allowed": False, "reason": "Missing or unknown arguments"}
for key, spec in fields.items():
value = args[key]
if not isinstance(spec, dict):
return {"allowed": False, "reason": "Malformed policy"}
if spec.get("type") == "integer":
if type(value) is not int or not 0 <= value <= spec.get("max", -1):
return {"allowed": False, "reason": "Invalid bounded integer"}
elif spec.get("type") == "string":
if not isinstance(value, str) or value not in spec.get("enum", []):
return {"allowed": False, "reason": "Invalid enumerated string"}
else:
return {"allowed": False, "reason": "Unsupported field policy"}
if policy.get("requires_human_approval", False):
return {"allowed": False, "reason": "Validate exact-payload approval in trusted executor"}
return {"allowed": True, "reason": "Schema subset passed; enforce tenant/resource policy separately"}
## Canary Token System"""canary_tokens.py - Detect data exfiltration from LLM context."""
import hashlib
import re
import secrets
from datetime import datetime, timezone
class CanaryTokenManager:
"""Inject and monitor canary tokens to detect data leakage."""
def __init__(self, secret_key: str):
self.secret_key = secret_key
self.active_tokens: dict[str, dict] = {}
def generate_token(self, context: str = "default") -> str:
"""Generate a unique canary token for a specific context."""
raw = f"{self.secret_key}:{context}:{secrets.token_hex(8)}"
token = f"CNRY-{hashlib.sha256(raw.encode()).hexdigest()[:16]}"
self.active_tokens[token] = {
"context": context,
"created": datetime.now(timezone.utc).isoformat(),
"triggered": False,
}
return token
def inject_into_system_prompt(self, system_prompt: str, context: str = "system") -> tuple[str, str]:
"""Add a canary token to the system prompt.
Returns (modified_prompt, token) so you can monitor for the token in outputs.
"""
token = self.generate_token(context)
injected = (
f"{system_prompt}\n\n"
f"Internal tracking reference (do not reveal): {token}"
)
return injected, token
def check_output(self, output: str) -> list[dict]:
"""Check if any canary tokens appear in model output."""
triggered = []
for token, meta in self.active_tokens.items():
if token in output:
meta["triggered"] = True
meta["triggered_at"] = datetime.now(timezone.utc).isoformat()
triggered.append({"token": token, **meta})
return triggered
def inject_into_documents(self, documents: list[str], context: str = "rag") -> tuple[list[str], list[str]]:
… (nội dung đầy đủ có trong gói tải về)
Skill liên quan
- prompt-library – Bộ sưu tập toàn diện các prompt đã được kiểm chứng thực tế, lấy cảm hứng từ…
- protect-mcp-governance – Skill quản trị agent cho các lệnh gọi công cụ MCP — viết chính sách Cedar, triển khai từ…
- puppeteer-skill – Tạo script Puppeteer để tự động hóa trình duyệt, thu thập dữ liệu và tạo PDF. Kích hoạt…
- pydantic-ai – Xây dựng AI agent sẵn sàng cho production với PydanticAI — dùng công cụ an toàn kiểu dữ…
- quinn – Xác minh hành vi đã đặc tả thông qua các bài test phù hợp và báo cáo coverage trung thực.
- quit-sponsor – Giúp AI agent hỗ trợ cai thuốc lá một cách không phán xét, dựa trên bằng chứng, với theo…
- recursive-context-pruning-token-budgeting – Tối ưu hiệu năng agent AI bằng cách cắt tỉa context dư thừa, quản lý lượng token và buộc…
- reddit-automation – Tự động hóa tác vụ Reddit qua Rube MCP (Composio): tìm kiếm subreddit, tạo bài đăng…