Linux IPTABLES firewall script for block/allow using DNS

There is 1 reply in this Thread which was already clicked 3,050 times. The last Post () by Iame.

    • Official Post

    Credit to Shalafi for this.


    Here's a small and easy to use iptables based firewall script which works the other way around. Instead of allowing people to connect and block (illegal) users through iptables, you block access completely and only allow hosts to connect by allowing them in the firewall. The only downside is, your client needs to use a DNS or a non-dynamic IP-address so that you can allow him access. The plus is, this means more security since you only allow users with a DNS/ipaddress and your server is default not viewable from the internet. You can also expand your F-line for the client with the same DNS in the case your firewall doesn't start or work for some extra security


    Keep in mind, that I tested this script on a NSLU2 running with the OpenSlug 3.10 Beta firmware. However, this script should work on regular Linux configurations without to many changes (disable the modprobe commands which is needed on the NSLU2). And to make sure that peers with a rotating IP-address can connect, you need to put this script in a cronjob setup to run every 15 minutes (see example at the bottom) during the day.


    It's a regular init.d script which has a start (start firewall), stop (stop firewall) or restart (stop then start firewall) function. Make sure that after you create the firewall script, to chmod it to executable (chmod a+x /etc/init.d/firewall) and make sure it autostarts (depends on your distribution) using the example below.


    Filename: /etc/init.d/firewall



    To make the firewall script start at bootup on a NSLU2, use the following command in your SSH client:


    Code
    ln -s /etc/init.d/firewall /etc/rcS.d/S59firewall


    To create a cronjob, run the command crontab -e and put in the following information:


    Code
    15,30,45,59 * * * * /etc/init.d/firewall restart


    After this, Cron will restart the firewall on 00:15, 00:30, 00:45, 00:59, 01:15, 01:30, etc, etc, etc.


    Offcourse you can edit the script for use with newcs, mbox, etc, etc. Just make sure you use the correct protocols and/or portnumbers. Alternatively you can change the name of the chain (which is now cccam) to your program which you use.

  • Thanks for great post. I am trying to use it in Debian 6 . I get "-bash: ./firewall: /bin/bash^M: bad interpreter: No such file or directory". I have used nano to set it up . I have changed iptables to /sbi/iptables and save file in /etc/init.d. only other change is i have commented out two lines in this section
    case "$1" in
    start)
    echo "Starting iptables firewall"
    # Only needed on NSLU2 - Disable them on regular Linux installations:
    # modprobe ip_tables
    # modprobe iptable_filter
    flush_existing
    create_chains
    populate_chains
    process_input_chain
    process_output_chain
    ;;
    stop)
    echo "Stopping iptables firewall"
    flush_existing
    rmmod ip_tables
    rmmod iptable_filter
    echo "Firewall rules flushed"
    ;;
    reload|force-reload)
    echo "Reloading iptables firewall"
    flush_existing
    create_chains
    populate_chains
    process_input_chain
    process_output_chain
    ;;
    restart)
    echo "Restarting iptables firewall"
    flush_existing
    create_chains
    populate_chains
    process_input_chain
    process_output_chain
    ;;
    *)
    echo "Usage: /etc/init.d/ssh {start|stop|reload|force-reload|restart}"
    exit 1
    Not sure how much of this section should be "comment out"
    Any help very much appreciated and special thanks to Ten Below

  • master G

    Added the Label Article

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!