A pre-tool-use hook for Claude Code that intercepts and blocks dangerous bash commands before they are executed.
Blocks destructive commands including:
rm -rf /— filesystem destructiondd if=/dev/zero of=/dev/sda— disk wipingmkfs— filesystem formatting:(){ :|:& };:— fork bombscurl | sh,wget | sh— untrusted remote code execution- Writing to raw disk devices
Safe commands pass through normally.
- Claude Code invokes the hook before every
Bashtool call - The hook reads the tool input JSON from stdin
- It checks the command against a list of dangerous patterns
- If dangerous: exits non-zero with an error message (blocks execution)
- If safe: exits 0 (allows execution)
The included .claude/settings.json configures the hook automatically for this project:
{
"hooks": {
"pre-tool-use": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "python3 $PROJECT_DIR/hook.py"
}
]
}
]
}
}Copy the hook configuration to ~/.claude/settings.json and update the path to hook.py.
python3 test_hook.pyThis runs all test cases, verifying that dangerous commands are blocked and safe commands are allowed.
- Python 3.10+
- Claude Code with hooks support