Back to Hooks
Prompt Injection Defense
PostToolUseScans tool outputs for prompt injection patterns including instruction overrides, role-playing attempts, and encoding obfuscation
securityprompt-injectiondefensescanning
Hook Script
#!/bin/bash
# Prompt Injection Defense Hook
# Scans tool output for common prompt injection patterns
OUTPUT="$1"
# Injection patterns to detect
PATTERNS=(
"ignore (all |any )?(previous|prior|above) (instructions|prompts|rules)"
"you are now (a |an )?[A-Za-z]+"
"new instructions:"
"system prompt:"
"\\x[0-9a-fA-F]{2}"
"IMPORTANT:.*override"
"forget (everything|all|your)"
"<system>"
"\[INST\]"
"Human:|Assistant:"
)
for pattern in "${PATTERNS[@]}"; do
if echo "$OUTPUT" | grep -qiE "$pattern"; then
echo "WARNING: Potential prompt injection detected in tool output."
echo "Pattern matched: $pattern"
echo "Review the output carefully before proceeding."
exit 2
fi
done
exit 0
Settings Configuration
{
"hooks": {
"PostToolUse": [
{
"matcher": "Read|Bash|WebFetch",
"command": "./hooks/prompt-injection-defense.sh"
}
]
}
}How to use
- Create a hooks directory in your project: mkdir hooks
- Save the hook script as hooks/prompt-injection-defense.sh
- Make it executable: chmod +x hooks/prompt-injection-defense.sh
- Add the configuration to your Claude Code settings
- Restart Claude Code to apply changes