Firewall: automatic update of blocklists (that block IP ranges from certain countries)
Section titled “Firewall: automatic update of blocklists (that block IP ranges from certain countries)”Background
Section titled “Background”Cloudron has a feature to block connections:
Using the blocklist configuration, one or more IP addresses and/or networks can be blocked from connecting to Cloudron.
Based on the discussion here, @imc67 came up with a great script to update such blocklists, whereas I only changed the script a little bit to meet my needs. Below you’ll find the script with all comments to guide you through.
The basic idea:
- Save code as as script
- Modify blocklist urls to your liking
- Modify
cloudron_api_endpointandapi_keywith your values - run the script daily via
cron(for information on cron and crontab, see here). - The script pulls updated lists from https://iplists.firehol.org/ and https://www.ipdeny.com/ipblocks/ and
- uploads the blocklists to Cloudron’s firewall via the API
Caveats
Section titled “Caveats”Note: Blocking certain countries is only one small part of securing Cloudron but it can have a significant impact on the amount of spam and mail server abuse.
Note: If you block certain country IP ranges this way, it also means, you might not be able to receive mails from servers / vendors located in these countries! So blocking the US is probably a bad idea; if you do regular business e.g. with China, the same applies.
Improvements are welcome as there are certainly people out there with better bash skills!
Script
Section titled “Script”#!/bin/bash# Save script in CD ~/urlfilter/, make executable, and run via cron e.g. daily, some time at night## Variable for current date and timecurrent_datetime=$(date +"%Y%m%d_%H%M%S")## Location of script and the blocklist archivecd ~/urlfilter/## Array containing the urls of the IP lists and their descriptions# The first two from firehol.org are Standard blocklists# The next four are an example how to block connections from afghanistan and azerbaijan - first two are for IPv4 addresses, the next two for IPv6 addresses.# In order to add countries, Go to https://www.ipdeny.com/ipblocks/ to find out the relevant country names and abbreviations and add urls accordingly on this scheme:# IPv4: "https://www.ipdeny.com/ipblocks/data/aggregated/[abbreviations, small caps]-aggregated.zone,[abbreviations, all caps] - [COUNTRY NAME]”# IPv6: "https://www.ipdeny.com/ipv6/ipaddresses/aggregated/[abbreviations, small caps]-aggregated.zone,[abbreviations, all caps] - [COUNTRY NAME]”# So for brunei it would be:# Abbreviation: bn / BN# Country name: BRUNEI DARUSSALAM# I.e. "https://www.ipdeny.com/ipblocks/data/aggregated/bn-aggregated.zone,bn - brunei darussalam"#declare -a urls=( "https://iplists.firehol.org/files/spamhaus_drop.netset,Spamhaus - Drop" "https://iplists.firehol.org/files/spamhaus_edrop.netset,Spamhaus - eDrop" "https://www.ipdeny.com/ipblocks/data/aggregated/af-aggregated.zone,AF - Afganistan" "https://www.ipdeny.com/ipblocks/data/aggregated/az-aggregated.zone,AZ - Azerbaijan" "https://www.ipdeny.com/ipv6/ipaddresses/aggregated/af-aggregated.zone,AF - Afganistan" "https://www.ipdeny.com/ipv6/ipaddresses/aggregated/az-aggregated.zone,AZ - Azerbaijan" )
# File name with the current date and timeoutput_file="merged_list_${current_datetime}.txt"
# Download and merge the IP listsfor url_info in "${urls[@]}"do # Splitting the URL information IFS=',' read -r url description <<< "$url_info" # Add comment with the URL and description echo "# URL: $url" >> "$output_file" echo "# Description: $description" >> "$output_file" echo "Download IP list from $url" # Download the IP list and add it to the file curl -sS "$url" >> "$output_file"doneecho "Merge completed! The merged list is stored in $output_file"# Formatting the file for the Cloudron blocklist APIformatted_file="formatted_$output_file"
# Add "\n" to the end of each lineawk '{printf "%s\\n",$0}' "$output_file" > "$formatted_file"# Cloudron blocklist API endpointcloudron_api_endpoint="https://my.yourdomain.com/api/v1/network/blocklist"
# API key for authentication (replace 'your-api-key' with your API key)api_key="#######"
# Upload to Cloudron blocklist API with wgetecho "# Upload to Cloudron Blocklist API with wget..."
# The data in the required format for the APIdata="{\"blocklist\":\"$(cat "$formatted_file" | tr '\n' '\\n')\"}"
# Send the file with a POST request via wgetecho "$data"> temp_data.txtcurl -X POST "$cloudron_api_endpoint" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $api_key" \ --data-binary "@temp_data.txt" \ --verbose \ -o output.txt# Delete temporary filesrm temp_data.txt# Compress lists and create an archive of the last 10 updates7z a -mx9 "${current_datetime}.7z" "formatted_$output_file"rm "formatted_$output_file"rm "$output_file"ls -td *.7z | grep -v '/$' | tail -n +10 | while IFS= read -r f; do rm -f "$f"; doneAuthors:
- @necrevistonnezr
- @imc67