Code Monkey home page Code Monkey logo

ipset-country's Introduction

ipset-country

Block or allow countries using iptables, ipset and ipdeny.com


This used to be a Gist but was moved here instead
Please do not add Gist comments, but create an issue here


  • Also works with ipverse.com and other providers
  • Supports RH, Debian with iptables and/or firewalld
  • Both ipv4 and ipv6 are supported

Installation

  1. Setup firewall if you have not done so yet, at least INPUT chain is needed
  2. Run this script from cron, e.g. /etc/cron.daily or a systemd timer (see below)
  3. To run on boot you can also add it to e.g. /etc/rc.local or systemd
  4. Use argument "-f" to load unchanged zonefiles instead of skipping
  • To automatically setup a systemd service and daily timer run: ipset-country -i
  • To uninstall run:ipset-country -u

Running this script will insert an iptables 'REJECT' or 'DROP' rule for ipset. Make sure you do not lock yourself out in case of issues on a remote system.

In case of issues check the log file (/var/log/ipset-country.log)

Configuration

All options are set and explained in the script itself: ipset-country

Optionally you can use a seperate config file located in the same directory as the script, "/etc" or "/usr/local/etc". Specify a custom location using ipset-country -c /path/to/conf

The config file will overwrite any options set in script. To create a new conf file run:

sed -n '/# CONFIGURATION:/,/# END OF CONFIG/p' ipset-country > ipset-country.conf

Distro:

If needed change OS using DISTRO setting. Default is "auto" which should be OK.

Options are:

  • "auto", "debian" or "redhat"
  • "manual"
    • confdir="/etc/iptables" (example)
    • rulesfile="${confdir}/myrules" (example)

Countries:

Specify countries to block as "ISOCODE,Name" (same as ipdeny.com), multiple entries should be seperated by semicolon ;

Example:
COUNTRY="CN,China; US,United States; RU,Russia"


Firewalls and options:

Iptables and ipset are used by default to create the chains, rules and ipsets. If firewalld frontend is enabled it will be used instead.

  • Blacklist: block specified Countries, set MODE to "reject" or "drop"
  • Whitelist: allow specified Countries and block all others, set MODE to "accept"

Iptables:

Set target to use when ip matches country: "accept", "drop" or "reject". Default is MODE="reject"

FirewallD:

Set this option to "1" to enable firewalld: FIREWALLD=0

Set FIREWALLD_MODE=0 to use the default Blacklist mode (uses 'drop' zone). Change to "1" for Whitelist ('public' zone). See MODE above for more information

  • NOTE: There are issues with firewalld on CentOS/RHEL 8 which can cause your firewall to break resulting in being locked out. Adding large ipsets apparently can take a VERY long time. To abort you need remote console access and run pkill firewal-cmd; nft flush ruleset

Block list providers:

Set URLs for ipv4 and/or ipv6 block files, you probably do not have to change these.
To use ipverse.net instead of ipdeny.com and for more details see script

  • IPBLOCK_URL_V4="http://www.ipdeny.com/ipblocks/data/aggregated"
  • IPBLOCK_URL_V6="http://www.ipdeny.com/ipv6/ipaddresses/blocks"

Logs:
In case you want to change file location set: LOG="/var/log/ipset-country.log"


IPset

Useful ipset commands:

  • ipset list
  • ipset test setname <ip>
  • ipset flush
  • ipset destroy

Changes

  • [20220227] fixed iptables-legacy paths (pr #16 by mainboarder)
  • [20201212] added config file option, systemd install (pr #14 by srulikuk)
  • [20201108] added flush option, fix restore=0 (pr #13 by srulikuk)
  • [20200927] fixed restore + logips bug (pr #10 by G4bbix)
  • [20200605] added Blacklist/Whitelist mode (#3)
  • [20200129] added option to DROP instead of REJECT (#1)
  • [20191116] added ipverse support, md5check option
  • [20190905] tested on debian 10 and centos 7
  • [20190905] blocking multiple countries should work
  • [20190905] it will check if INPUT chain exists in iptables
  • [20190905] cleaned it up a bit
  • [20190905] using firewalld is also supported now

Alternatives

Also available: github.com/tokiclover/dotfiles/blob/master/bin/ips.bash

ipset-country's People

Contributors

mainboarder avatar mkorthof avatar srulikuk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ipset-country's Issues

Block all incoming and outgoing traffic

  1. I noticed that only incoming TCP connections are blocked but not incoming e.g. UDP packets. If a country is block then no incoming traffic should be a allowed.
  2. To my surprise, outgoing traffic is not blocked. Why? Outgoing traffic to a blocked country should be blocked too.

Warning: iptables-legacy tables present, use ip6tables-legacy to see them

How to deal with this?
Warning: iptables-legacy tables present, use ip6tables-legacy to see them

iptables-legacy -S
-N LOGIPS
-A INPUT -p tcp -m set --match-set ipv4-singapore src -j LOGIPS
-A INPUT -p tcp -m set --match-set ipv4-india src -j LOGIPS
-A INPUT -p tcp -m set --match-set ipv4-russia src -j LOGIPS
-A INPUT -p tcp -m set --match-set ipv4-belarus src -j LOGIPS
-A INPUT -p tcp -m set --match-set ipv4-vietnam src -j LOGIPS
-A INPUT -p tcp -m set --match-set ipv4-iran src -j LOGIPS
-A INPUT -p tcp -m set --match-set ipv4-hong_kong src -j LOGIPS
-A INPUT -p tcp -m set --match-set ipv4-thailand src -j LOGIPS
-A INPUT -p tcp -m set --match-set ipv4-korea src -j LOGIPS
-A INPUT -p tcp -m set --match-set ipv4-china src -j LOGIPS
-A LOGIPS -m limit --limit 10/min -j LOG --log-prefix "IPS: " --log-level 3
-A LOGIPS -j DROP

Limitation of country codes

170.106.0.0/16 is reported as being in Singapore SG but when you look at the WHOIS record near the bottom it says Tencent in Beijing.

Some questions

Thanks for this script, was gonna write my own but found this ;-) I want to understand a few things in the script and will be grateful if you find the time to respond.

  1. Why require to use iptables-restore? furthermore I can only find that iptables-save is used to grep rules, when is the rules.v{4/6} created / written to? - For myself I want to disable the restore function as I rely on other scripts to restore rules but want to understand this before I proceed.

  2. When updating does it only add new IP blocks or does it also remove anything that's not in the new downloaded file? - this is not easily clear from the ipset command. - Also wouldn't it be more efficient to replace the rule with the new file rather then checking for each ipblock?

Thanks

how to setup the opposite, instead block, allow

Hi.

it's a wonderful job that you did!
I was reading and I didnt find a solution for block all (dont need the search on list) and unlock just for me country goal.
Is that this option?

If I use the list for block all countries and leave just my country goal unlocked dont make sense because I'll spend too much processor on it. Is better I wont search in the list but I ll block all and just search my contry in the list for unlock.

Do you think that is possible?

Thanks!

Feature request

I would like to be able to exclude some ip's/subnet from being blocked.

Thanks.

Add Entware as a supported distribution?

Would it be feasible to add Entware as a supported distribution? This script would be really useful on devices such as routers running DD-WRT and Linux-based NASes that support Entware, which provides full versions of ipset and iptables and most standard LInux utilities to supplant the embedded BusyBox versions. The major difference is that everything is installed under /opt, so for example /etc becomes /opt/etc, /sbin/ipset is in /opt/sbin/ipset, etc.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.