Skip to content

Instantly share code, notes, and snippets.

@pjv
Created May 13, 2016 13:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pjv/926ece8549cd45bac4821945f6ad253c to your computer and use it in GitHub Desktop.
Save pjv/926ece8549cd45bac4821945f6ad253c to your computer and use it in GitHub Desktop.
Cloudflare v4 API ban and urban bash scripts
#!/bin/bash
# dependencies: curl
# example usage: cf_ban 192.168.0.1
# append cloudflare email address and API token:
USER=
TOKEN=
curl -sSX POST "https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules" \
-H "X-Auth-Email: $USER" \
-H "X-Auth-Key: $TOKEN" \
-H "Content-Type: application/json" \
--data "{\"mode\":\"block\",\"configuration\":{\"target\":\"ip\",\"value\":\"$1\"},\"notes\":\"Blocked via cf_ban script\"}"
#!/bin/bash
# dependencies: curl, jq (https://stedolan.github.io/jq/)
# example usage: cf_unban 192.168.0.1
# append cloudflare email address and API token:
USER=
TOKEN=
# get the rule ID
JSON=$(curl -sSX GET "https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules?mode=block&configuration_target=ip&configuration_value=$1" \
-H "X-Auth-Email: $USER" \
-H "X-Auth-Key: $TOKEN" \
-H "Content-Type: application/json")
ID=$(echo $JSON | jq -r '.result[].id')
# unban IP by deleting the rule
curl -sSX DELETE "https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules/$ID" \
-H "X-Auth-Email: $USER" \
-H "X-Auth-Key: $TOKEN" \
-H "Content-Type: application/json"
@pjv
Copy link
Author

pjv commented May 13, 2016

You can do a lot more than this with the v4 API. These little scripts are to minimally re-create ban and unban functionality from the v1 API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment