NFTables IP Control

Back  
 Next

This is an old revision of the document!


I primarily set this feature up to act as a form of parental control on my home internet access.

Disable Range of IP addresses, with count-down timer

The DHCP assigned addresses in the range 100 - 254 were to be disabled (dropped) in the evening. The addresses below 100 were assigned a specific IP address in the DHCP based upon MAC.

I added a NFtables named set with ipV4 address and timeout function:

     set controllist {
         type ipv4_addr
         flags interval
         flags timeout
     }

In the forward chain the controllist IP set was dropped for all the source and destination packets.

       ip daddr @controllist counter drop
       ip saddr @controllist counter drop

Router NFtable Setup - with named set IP filtering

Some important NFtables Commands with named sets:

  • sudo nft list table inet firewall : will list all the active firewall table
  • sudo nft add element inet firewall controllist { 192.168.1.130 timeout 5m} : will add the IPv4 address 192.168.1.130 to the named set controllist for 5 minutes
    • where timeout values are: h m s and can be used in tandem as such: 5h30m for 5 hours 30 minutes.
  • sudo nft list set inet firewall controllist : will list the current elements in named set controllist
  • sudo nft flush set inet firewall controllist : will flush the current named set controllist
  • sudo vim /etc/nftables.conf : to edit the current NFTables configuration
  • sudo systemctl restart nftables to restart with the current nftables configuration
    • status to list current running status of nftables
    • start to start nftables with the current configuration
    • stop to stop nftables
    • journalctl -b0 -xe -u nftables to list the end of all the nftables journal entries since the last boot

I made a couple of bash scripts to assist with the use of these controllist name sets.

  • sudo vim controllist.sh : This script automates the entry of a a range of IP address with timeout limits in the the named set. (The timeout option does not seem to function with the interval option.)

controllist.sh

  • A key change to this script to improve functionality would be to allow an input of the timer duration.
  • sudo vim deletecontrol.sh : This is a simple script to flush the control list.

deletecontrol.sh

  • Do not forget to run sudo chmod 774 controllist.sh on the script files to make them executable

I then created a cron job to run the script as required every evening

  • sudo crontab -l : to list current cron jobs (use sudo for root)
  • sudo crontab -e : to edit the cornjobs
  • journalctl -b0 -u cron | grep control : to list all the related cron jobs since last boot

crontab is finicky! crontab does not necessary use BASH and the full path to the command must be given for reliable performance. Further to this cron error messages are sent to the system mail server, so if this is not setup or otherwise not working the error message go nowhere. Systemd has a service to redirect output of cron jobs to systemd's journal: /usr/bin/systemd-cat -t controllist, again the full path is given. The command path can be found using which, e.g. which nft. In any case the final crontab command entry would look like: /usr/bin/systemd-cat -t controllist /home/baumkp/controllist.sh, remembering everything after the 5th space is passed to the system shell command interpreter.

Limit Rate on IP Adress Range

sudo nft add rule inet firewall forward handle 29 iifname “ppp1” ip daddr { 192.168.1.100-192.168.1.253 } limit rate 1200kbytes/second burst 9000kbytes counter accept

Some other tools

  • sudo nmap -sn 192.168.1.0/24
    • -sP lists all IP addresses with any open port (with sudo will also list mac addresses)
    • -sL lists all IP addresses even not active
    • -sT lists all IP addresses with all open ports (with sudo will also list mac addresses) Note this can take a long time so better to limit IP address range.
    • -sn Ping scan, does not scan for port and is hence much faster (with sudo will also list mac addresses)

/app/www/public/data/attic/linux_router/nftables_control.1593227937.txt.gz · Last modified: 2023-04-30 Sun wk17 17:44
CC Attribution-Share Alike 4.0 International Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International