2

Fail2Ban just blocked my IP for 3 SSH attempts. It added the iptables rule and I can see it using the "sudo iptables -L -n" command. But I can still access the site and login through SSH! What might be the problem? Is it because im using CloudFlare? I have set Nginx to write the real IPs to the access logs instead of the Cloud Flare IP. Isn't it enough?

Chain fail2ban-ssh (1 references)
 target     prot opt source               destination         
 DROP       all  --  119.235.14.8         0.0.0.0/0           
 RETURN     all  --  0.0.0.0/0            0.0.0.0/0  

The input chain :

Chain INPUT (policy DROP)

    target     prot opt source               destination         
    fail2ban-NoAuthFailures  tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
    fail2ban-nginx-dos  tcp  --  0.0.0.0/0            0.0.0.0/0            multiport dports 80,8090
    fail2ban-postfix  tcp  --  0.0.0.0/0            0.0.0.0/0            multiport dports 25,465
    fail2ban-ssh-ddos  tcp  --  0.0.0.0/0            0.0.0.0/0            multiport dports 22
    fail2ban-ssh  tcp  --  0.0.0.0/0            0.0.0.0/0            multiport dports 22
    ufw-before-logging-input  all  --  0.0.0.0/0            0.0.0.0/0           
    ufw-before-input  all  --  0.0.0.0/0            0.0.0.0/0           
    ufw-after-input  all  --  0.0.0.0/0            0.0.0.0/0           
    ufw-after-logging-input  all  --  0.0.0.0/0            0.0.0.0/0           
    ufw-reject-input  all  --  0.0.0.0/0            0.0.0.0/0           
    ufw-track-input  all  --  0.0.0.0/0            0.0.0.0/0           
    LOG        all  --  0.0.0.0/0            0.0.0.0/0            LOG flags 0 level 4
THpubs
  • 1,695
  • 7
  • 26
  • 43

1 Answers1

4

UPDATE 2019: The API use in origin answer has been deprecated in favor of API v4, use new version instead. Fail2ban unban action fails with Cloudflare thanks @baptx commented

iptables can not get the real ip, so you should use cloudflare's api to blacklist the IP on the cloud.

here is my action config file

# Fail2Ban configuration file
#
# Author: Charles Chou
# Modified: Norman Yee 
#           fix original cloudflare-blacklist.conf

# $Revision$
#

[Definition]

# Option:  actionstart
# Notes.:  command executed once at the start of Fail2Ban.
# Values:  CMD
#
actionstart =

# Option:  actionstop
# Notes.:  command executed once at the end of Fail2Ban
# Values:  CMD
#
actionstop =

# Option:  actioncheck
# Notes.:  command executed once before each actionban command
# Values:  CMD
#
actioncheck =

# Option:  actionban
# Notes.:  command executed when banning an IP. Take care that the
#          command is executed with Fail2Ban user rights.
# Tags:    <ip>  IP address
#          <failures>  number of failures
#          <time>  unix timestamp of the ban time
# Values:  CMD
#
actionban = curl -s "https://www.cloudflare.com/api.html?a=ban&key=<ip>&u=<account>&tkn=<token>"

# Option:  actionunban
# Notes.:  command executed when unbanning an IP. Take care that the
#          command is executed with Fail2Ban user rights.
# Tags:    <ip>  IP address
#          <failures>  number of failures
#          <time>  unix timestamp of the ban time
# Values:  CMD
#
actionunban = curl -s "https://www.cloudflare.com/api.html?a=nul&key=<ip>&u=<account>&tkn=<token>"

[Init]

# Option:  account
# Notes.:  In the actionban and actionunban sections,replace CLOUDFLARE_LOGIN with your CloudFlare login email
# Values:  your CloudFlare account
#
account = example@example.com

# Option:  token
# Notes.:  In the actionban and actionunban sections, replace CLOUDFLARE_API_TOKEN with your API key
# Values:  Your CloudFlare API key 
#
token = Your API key here

Charles Chou
  • 108
  • 7
  • According to https://support.cloudflare.com/hc/en-us/articles/204073570-Can-I-still-use-fail2ban-while-using-CloudFlare-, it should be enough to restore the original visitor IP back to the server logs (e.g. with mod_clouflare if we are using Apache web server). Isn't that correct? – baptx May 24 '19 at 10:16
  • By the way, here is a working solution for Cloudflare API v4: https://technicalramblings.com/blog/cloudflare-fail2ban-integration-with-automated-set_real_ip_from-in-nginx/ Based on this question: https://serverfault.com/questions/910940/fail2ban-unban-action-fails-with-cloudflare/912547 – baptx May 24 '19 at 11:09
  • It is also possible to make the unban command work by using the original config file `/etc/fail2ban/action.d/cloudflare.conf` and adding `| tr -d '\n'` before the cut command. An official fixed will be released in fail2ban: https://github.com/fail2ban/fail2ban/pull/2656 – baptx Apr 27 '20 at 19:19