Thank you for the guide!
I'd like to share a script I found for Linux, which I’ve modified to work with Unraid.
This script is designed to solve an issue where CrowdSec may mistakenly block your dynamic external IP address (a false positive).
It works by comparing your current external IP with the one listed in the whitelist. If the IP has changed,
the script updates the Whitelist.yaml file with the new IP address,
then restarts the CrowdSec container to apply the changes properly.
This helps prevent CrowdSec from blocking your own IP
Prerequisites:
- CrowdSec Docker container is up and running
- CrowdSec whitelist parser installed:
cscli parsers install crowdsecurity/whitelists
- User Scripts plugin installed
- Python 3 installed
- Python modules: requests, textwrap, and docker installed
Setup :
Copy the script below. Insert your container ID under the # Fixed container ID line, and update the whitelistsFilePath if needed.
Add the script to the User Scripts plugin and schedule it using a custom cron job. I run it hourly with: (*/60 * * * *)
#!/usr/bin/python3
import requests
import docker
import textwrap
from datetime import datetime
whitelistsFilePath = "/mnt/cache/appdata/crowdsec/parsers/s02-enrich/mywhitelists.yaml"
curPubIpFilePath = "./currentIP"
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# Fixed container ID
container_id = 'Insert container id here'
def get_external_ip():
try:
response = requests.get('https://api.ipify.org')
response.raise_for_status()
return response.text
except requests.RequestException as e:
print(f"{timestamp}: Error fetching external IP: {e}")
return None
def read_from_file(filename):
try:
with open(filename, 'r') as file:
content = file.read()
return content
except Exception as e:
print(f"{timestamp}: Error while reading the file ({filename}): {e}")
return None
def write_to_file(filename, content):
try:
with open(filename, 'w') as file:
file.write(content)
print(f"{timestamp}: File '{filename}' has been written successfully.")
except Exception as e:
print(f"{timestamp}: Error while writing the file ({filename}): {e}")
def restart_container(container_id):
client = docker.from_env()
try:
container = client.containers.get(container_id)
print(f"{timestamp}: Stopping container '{container_id}'...")
container.stop()
print(f"{timestamp}: Container '{container_id}' stopped.")
print(f"{timestamp}: Starting container '{container_id}'...")
container.start()
print(f"{timestamp}: Container '{container_id}' started.")
except docker.errors.NotFound:
print(f"{timestamp}: Error: Container '{container_id}' not found.")
except Exception as e:
print(f"{timestamp}: Error while restarting container '{container_id}': {e}")
if __name__ == "__main__":
extIP = get_external_ip()
if extIP is None:
print(f"{timestamp}: Could not retrieve external IP. Exiting.")
exit(1)
# Read the current IP from file
current_ip = read_from_file(curPubIpFilePath)
# Check if the IP has changed
if current_ip != extIP:
print(f"{timestamp}: Public IP has changed. Setting new IP to whitelist.")
# Update the IP file and the whitelist configuration
write_to_file(curPubIpFilePath, extIP)
whitelistsFileContent = textwrap.dedent(f"""\
name: crowdsecurity/mywhitelists
description: "Whitelist events from public IPv4 address"
whitelist:
reason: "My public IP"
ip:
- "{extIP}"
""")
write_to_file(whitelistsFilePath, whitelistsFileContent)
# Stop and start the container if the IP changed
restart_container(container_id)
else:
print(f"{timestamp}: IP didn't change, no restart needed.")
Source and credit to phipzzz