Firewall with iptables
[Zurück zur aktuellen Version][Diese Version wiederherstellen]

Scope#

iptables#

Commands#

> ls -la /usr/sbin/ip*  # list all commands, there are many
> iptables*             # for ipv4 (in debian busters = nft)
> ip6tables*            # for ipv6 (in debian busters = nft)
> iptables-legacy       # former ones, not nft
> iptables-nft          # directly use nft
Notes:

Syntax#

see docu
> sudo iptables -L -v 
> sudo iptables -n -v --line-numbers -L 
> sudo service iptables start		# activate firewalling
> sudo service ip6tables start
> sudo chkconfig iptables on		# enable after reboot
> sudo chkconfig ip6tables on
iptables [-t <table-name>] <command> <chain-name> <parameter-1>  <option-1> <parameter-n> <option-n>

my general ruleset#

#!/bin/bash

# MARKUS

SOURCE=<any ipv6 address or host>

# ipv4 ------------------------------------
# Flush all rules and delete all chains for a clean startup
iptables -F
iptables -X
iptables -Z		# Zero out all counters

# drop all ipv4 traffic, we wanmt to use ipv6 only
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A INPUT -j DROP
iptables -A FORWARD -j DROP
iptables -A OUTPUT -j DROP

# ipv6 ------------------------------------
# Flush all rules and delete all chains for a clean startup
ip6tables -F
ip6tables -X
ip6tables -Z

# drop all ipv6 traffic, except your ip ($SOURCE)
# do not limit to --dport 22, because the source has random ports (?)
# add first to enabel replacing 1. rule later
ip6tables -A INPUT  -s $SOURCE -j ACCEPT
ip6tables -A OUTPUT -d $SOURCE -j ACCEPT

# allow ipv6 local loopback
ip6tables -A INPUT -i lo -j ACCEPT
ip6tables -A OUTPUT -o lo -j ACCEPT

# drop everthing else
ip6tables -P FORWARD DROP 		# as policy generally
ip6tables -A INPUT  -j DROP
ip6tables -A OUTPUT -j DROP 

Python#

subprocess#

import socket	    # determine ipv6
import subprocess   # run system commands

#ip = get_ip()     # you have to create the code to fetch IP in this variable
ip = socket.getaddrinfo('any.host.com', 443, socket.AF_INET6)
# example result=[(<AddressFamily.AF_INET6: 23>, 0, 0, '', ('2a02:3b4:d876:f321:1234:a123:b456:ff', 443, 0, 0))]
# replace first rules
subprocess.run(["/usr/sbin/ip6tables", "-R", "INPUT", "1", "-s", ip, "-j", "ACCEPT"])
subprocess.run(["/usr/sbin/ip6tables", "-R", "OUTPUT", "1", "-d", ip, "-j", "ACCEPT"])