This repository provides scripts and systemd services to periodically fetch IP ranges associated with specified Autonomous System Numbers (ASNs) and block them using iptables on a Linux system.
The asn_block project automates blocking IP ranges for specified ASNs. It uses:
- A configuration file (
as-blocklist.yaml) to define ASNs and settings. - A script (
asblock_fetch.py) to fetch IP ranges for those ASNs from an external source. - A script (
asblock_apply.py) to apply the fetched IP ranges toiptables. - Systemd services and timers (
asblock-fetch.service,asblock-fetch.timer,asblock-apply.service,asblock-apply.timer) to run these scripts periodically.
- as-blocklist.yaml: Configuration file listing ASNs to block and related settings.
- asblock_fetch.py: Python script to fetch IP ranges for the specified ASNs.
- asblock_apply.py: Python script to apply the fetched IP ranges to
iptables. - asblock-fetch.service: Systemd service for running the fetch script.
- asblock-fetch.timer: Systemd timer to schedule periodic execution of the fetch script.
- asblock-apply.service: Systemd service for running the apply script.
- asblock-apply.timer: Systemd timer to schedule periodic execution of the apply script.
- Configuration: Define ASNs and settings (e.g., source URL for IP ranges) in
as-blocklist.yaml. - Fetching IP Ranges: The
asblock_fetch.pyscript, triggered byasblock-fetch.timer, fetches IP ranges for the listed ASNs. - Applying Blocks: The
asblock_apply.pyscript, triggered byasblock-apply.timer, updatesiptablesto block the fetched IP ranges. - Automation: Systemd timers ensure the scripts run at specified intervals.
-
Clone the repository:
git clone https://github.com/mrmoghadasi/asn_block.git cd asn_block -
Install system dependencies:
sudo apt update sudo apt install iptables iptables-persistent python3 python3-pip
-
Install Python dependencies:
pip install -r requirements.txt
-
Configure
as-blocklist.yamlwith the desired ASNs and settings. -
Copy the systemd service and timer files:
sudo cp asblock-fetch.service asblock-fetch.timer asblock-apply.service asblock-apply.timer /etc/systemd/system/
-
Reload systemd and enable the timers:
sudo systemctl daemon-reload sudo systemctl enable asblock-fetch.timer sudo systemctl enable asblock-apply.timer sudo systemctl start asblock-fetch.timer sudo systemctl start asblock-apply.timer
sh <(curl -s https://raw.githubusercontent.com/mrmoghadasi/asn_block/main/setup_asn_block.sh)- Edit
as-blocklist.yamlto specify the ASNs to block and any source settings. - The scripts will run automatically based on the timer configurations.
- To manually trigger the scripts:
sudo systemctl start asblock-fetch.service sudo systemctl start asblock-apply.service
- System:
iptables: For manipulating firewall rules.iptables-persistent: To persistiptablesrules across reboots.- Python 3.x: To run the scripts.
- Python Libraries (see
requirements.txt):requests: For fetching IP range data.pyyaml: For parsing the YAML configuration file.
- Ensure root privileges for modifying
iptablesrules and installing system packages. - Verify the fetched IP ranges to avoid blocking unintended networks.
- Adjust timer intervals in
asblock-fetch.timerandasblock-apply.timeras needed. - Ensure the system has internet access to fetch IP range data.
Contributions are welcome! Please submit a pull request or open an issue for suggestions or bug reports.
This project is licensed under the MIT License.