192 |
- |
1 |
#
|
|
|
2 |
# Author: Mike Rushton
|
|
|
3 |
#
|
|
|
4 |
# IMPORTANT
|
|
|
5 |
#
|
|
|
6 |
# Please set jail.local's permission to 640 because it contains your CF API key.
|
|
|
7 |
#
|
193 |
- |
8 |
# This action depends on curl (and optionally jq).
|
192 |
- |
9 |
# Referenced from http://www.normyee.net/blog/2012/02/02/adding-cloudflare-support-to-fail2ban by NORM YEE
|
|
|
10 |
#
|
|
|
11 |
# To get your CloudFlare API Key: https://www.cloudflare.com/a/account/my-account
|
|
|
12 |
#
|
|
|
13 |
# CloudFlare API error codes: https://www.cloudflare.com/docs/host-api.html#s4.2
|
|
|
14 |
|
|
|
15 |
[Definition]
|
|
|
16 |
|
|
|
17 |
# Option: actionstart
|
|
|
18 |
# Notes.: command executed on demand at the first ban (or at the start of Fail2Ban if actionstart_on_demand is set to false).
|
|
|
19 |
# Values: CMD
|
|
|
20 |
#
|
|
|
21 |
actionstart =
|
|
|
22 |
|
|
|
23 |
# Option: actionstop
|
|
|
24 |
# Notes.: command executed at the stop of jail (or at the end of Fail2Ban)
|
|
|
25 |
# Values: CMD
|
|
|
26 |
#
|
|
|
27 |
actionstop =
|
|
|
28 |
|
|
|
29 |
# Option: actioncheck
|
|
|
30 |
# Notes.: command executed once before each actionban command
|
|
|
31 |
# Values: CMD
|
|
|
32 |
#
|
|
|
33 |
actioncheck =
|
|
|
34 |
|
|
|
35 |
# Option: actionban
|
|
|
36 |
# Notes.: command executed when banning an IP. Take care that the
|
|
|
37 |
# command is executed with Fail2Ban user rights.
|
|
|
38 |
# Tags: <ip> IP address
|
|
|
39 |
# <failures> number of failures
|
|
|
40 |
# <time> unix timestamp of the ban time
|
|
|
41 |
# Values: CMD
|
|
|
42 |
#
|
|
|
43 |
# API v1
|
|
|
44 |
#actionban = curl -s -o /dev/null https://www.cloudflare.com/api_json.html -d 'a=ban' -d 'tkn=<cftoken>' -d 'email=<cfuser>' -d 'key=<ip>'
|
|
|
45 |
# API v4
|
193 |
- |
46 |
actionban = curl -s -o /dev/null -X POST <_cf_api_prms> \
|
|
|
47 |
-d '{"mode":"block","configuration":{"target":"ip","value":"<ip>"},"notes":"Fail2Ban <name>"}' \
|
|
|
48 |
<_cf_api_url>
|
192 |
- |
49 |
|
|
|
50 |
# Option: actionunban
|
|
|
51 |
# Notes.: command executed when unbanning an IP. Take care that the
|
|
|
52 |
# command is executed with Fail2Ban user rights.
|
|
|
53 |
# Tags: <ip> IP address
|
|
|
54 |
# <failures> number of failures
|
|
|
55 |
# <time> unix timestamp of the ban time
|
|
|
56 |
# Values: CMD
|
|
|
57 |
#
|
|
|
58 |
# API v1
|
|
|
59 |
#actionunban = curl -s -o /dev/null https://www.cloudflare.com/api_json.html -d 'a=nul' -d 'tkn=<cftoken>' -d 'email=<cfuser>' -d 'key=<ip>'
|
|
|
60 |
# API v4
|
193 |
- |
61 |
actionunban = id=$(curl -s -X GET <_cf_api_prms> \
|
|
|
62 |
"<_cf_api_url>?mode=block&configuration_target=ip&configuration_value=<ip>&page=1&per_page=1¬es=Fail2Ban%%20<name>" \
|
|
|
63 |
| { jq -r '.result[0].id' 2>/dev/null || tr -d '\n' | sed -nE 's/^.*"result"\s*:\s*\[\s*\{\s*"id"\s*:\s*"([^"]+)".*$/\1/p'; })
|
|
|
64 |
if [ -z "$id" ]; then echo "<name>: id for <ip> cannot be found"; exit 0; fi;
|
|
|
65 |
curl -s -o /dev/null -X DELETE <_cf_api_prms> "<_cf_api_url>/$id"
|
192 |
- |
66 |
|
193 |
- |
67 |
_cf_api_url = https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules
|
|
|
68 |
_cf_api_prms = -H 'X-Auth-Email: <cfuser>' -H 'X-Auth-Key: <cftoken>' -H 'Content-Type: application/json'
|
|
|
69 |
|
192 |
- |
70 |
[Init]
|
|
|
71 |
|
|
|
72 |
# If you like to use this action with mailing whois lines, you could use the composite action
|
|
|
73 |
# action_cf_mwl predefined in jail.conf, just define in your jail:
|
|
|
74 |
#
|
|
|
75 |
# action = %(action_cf_mwl)s
|
|
|
76 |
# # Your CF account e-mail
|
|
|
77 |
# cfemail =
|
|
|
78 |
# # Your CF API Key
|
|
|
79 |
# cfapikey =
|
|
|
80 |
|
|
|
81 |
cftoken =
|
|
|
82 |
|
|
|
83 |
cfuser =
|