Skip to content

Instantly share code, notes, and snippets.

@jigangkim
Last active September 14, 2023 06:26
Show Gist options
  • Save jigangkim/c89d7a6c395fc8f0a57821d507070b10 to your computer and use it in GitHub Desktop.
Save jigangkim/c89d7a6c395fc8f0a57821d507070b10 to your computer and use it in GitHub Desktop.
fail2ban v0.11.1 setup for xrdp, code-server

testing custom regex

fail2ban-regex LOG REGEX -d DATEPATTERN -v

xrdp

Only works on xrdp version v0.9.18 or newer: https://github.com/neutrinolabs/xrdp/tree/v0.9.18.

Detects '[20230628-05:33:09] [INFO ] AUTHFAIL: user=testuser ip=192.168.0.2 time=1687930389' line from xrdp sesman log.

Regex reference: https://stackoverflow.com/questions/75915624/xrdp-filter-setting-for-fail2ban

code-server

Verified on code-server v4.16.1 behind a cloudflare proxy.

Detects 'Sep 14 01:13:29 NAME_OF_YOUR_MACHINE code-server[PID]: Failed login attempt {"xForwardedFor":"IP_ADDRESS_TO_BLOCK, 172.71.151.75","remoteAddress":"127.0.0.1","userAgent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36","timestamp":1694654009}' line from syslog.

Regex reference: coder/code-server#1177

cloudflare proxy

For web services behind a cloudflare proxy, send banned ips to cloudflare.

You can check banned ip entries on cloudflare in Dashboard -> Security -> WAF -> Tools.

Reference: https://niksec.com/using-fail2ban-with-cloudflare/

#
# Author: Mike Rushton
#
# IMPORTANT
#
# Please set jail.local's permission to 640 because it contains your CF API key.
#
# This action depends on curl (and optionally jq).
# Referenced from http://www.normyee.net/blog/2012/02/02/adding-cloudflare-support-to-fail2ban by NORM YEE
#
# To get your CloudFlare API Key: https://www.cloudflare.com/a/account/my-account
#
# CloudFlare API error codes: https://www.cloudflare.com/docs/host-api.html#s4.2
[Definition]
# Option: actionstart
# Notes.: command executed on demand at the first ban (or at the start of Fail2Ban if actionstart_on_demand is set to false).
# Values: CMD
#
actionstart =
# Option: actionstop
# Notes.: command executed at the stop of jail (or 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
#
# API v1
#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>'
# API v4
actionban = curl -s -o /dev/null -X POST <_cf_api_prms> \
-d '{"mode":"block","configuration":{"target":"<cftarget>","value":"<ip>"},"notes":"Fail2Ban <name>"}' \
<_cf_api_url>
# 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
#
# API v1
#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>'
# API v4
actionunban = id=$(curl -s -X GET <_cf_api_prms> \
"<_cf_api_url>?mode=block&configuration_target=<cftarget>&configuration_value=<ip>&page=1&per_page=1&notes=Fail2Ban%%20<name>" \
| { jq -r '.result[0].id' 2>/dev/null || tr -d '\n' | sed -nE 's/^.*"result"\s*:\s*\[\s*\{\s*"id"\s*:\s*"([^"]+)".*$/\1/p'; })
if [ -z "$id" ]; then echo "<name>: id for <ip> cannot be found"; exit 0; fi;
curl -s -o /dev/null -X DELETE <_cf_api_prms> "<_cf_api_url>/$id"
_cf_api_url = https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules
_cf_api_prms = -H 'X-Auth-Email: <cfuser>' -H 'X-Auth-Key: <cftoken>' -H 'Content-Type: application/json'
[Init]
# If you like to use this action with mailing whois lines, you could use the composite action
# action_cf_mwl predefined in jail.conf, just define in your jail:
#
# action = %(action_cf_mwl)s
# # Your CF account e-mail
# cfemail =
# # Your CF API Key
# cfapikey =
cftoken = YOUR_CF_GLOBAL_API_KEY
cfuser = YOUR_CF_ACCOUNT_EMAIL
cftarget = ip
[Init?family=inet6]
cftarget = ip6
[Definition]
failregex = Failed login attempt {\"xForwardedFor\":\"<HOST>,
ignoreregex =
datepattern = "timestamp":{EPOCH}}$
[xrdp]
enabled = true
port = 3389
filter = xrdp
logpath = /var/log/xrdp-sesman.log
maxretry = 5
bantime = 3600
[code-server]
enabled = true
port = https
filter = code-server
logpath = /var/log/syslog
maxretry = 5
bantime = 3600
action = cloudflare
iptables-allports
[Definition]
failregex = AUTHFAIL: .* ip=<HOST>
ignoreregex =
datepattern = time={EPOCH}$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment