Browse Source

added iptables scripts

master
Dirk Alders 1 year ago
parent
commit
0743f10423
3 changed files with 222 additions and 0 deletions
  1. 1
    0
      ipt
  2. 123
    0
      ipt-static-ruleset
  3. 98
    0
      vlanm

+ 1
- 0
ipt View File

@@ -0,0 +1 @@
1
+sudo iptables -L -n -v --line-numbers $*

+ 123
- 0
ipt-static-ruleset View File

@@ -0,0 +1,123 @@
1
+#######################################################################################################################
2
+# Firewall configuration for this host ################################################################################
3
+#######################################################################################################################
4
+
5
+# Allow minimum for all VLANS (ping, dhcp, dns, ntp)
6
+sudo iptables -A INPUT -j ACCEPT -p icmp --icmp-type 8 -m comment --comment "accept incomming pings"
7
+sudo iptables -A OUTPUT -j ACCEPT -p icmp --icmp-type 8 -m comment --comment "accept outgoing pings"
8
+sudo iptables -A INPUT -j ACCEPT -p udp --dport 67:68 --sport 67:68 -m comment --comment "accept incomming dhcp"
9
+sudo iptables -A OUTPUT -j ACCEPT -p udp --dport 67:68 --sport 67:68 -m comment --comment "accept outgoing dhcp"
10
+sudo iptables -A INPUT -j ACCEPT -p udp --dport 53 -m comment --comment "accept incomming dns"
11
+sudo iptables -A INPUT -j ACCEPT -p tcp --dport 53 -m comment --comment "accept incomming dns"
12
+sudo iptables -A OUTPUT -j ACCEPT -p udp --dport 53 -m comment --comment "accept outgoing dns"
13
+sudo iptables -A OUTPUT -j ACCEPT -p tcp --dport 53 -m comment --comment "accept outgoing dns"
14
+sudo iptables -A INPUT -j ACCEPT -p udp --dport 123 -m comment --comment "accept incomming ntp"
15
+sudo iptables -A OUTPUT -j ACCEPT -p udp --dport 123 -m comment --comment "accept outgoing ntp"
16
+
17
+# Allow some usefull outgoing traffic for all VLANS (ssh)
18
+sudo iptables -A OUTPUT -j ACCEPT -p tcp --dport 22 -m comment --comment "accept outgoing ssh"
19
+
20
+
21
+# Allow established and related trafic
22
+sudo iptables -A INPUT -j ACCEPT -m conntrack --ctstate ESTABLISHED,RELATED -m comment --comment "accept all incomming established and related trafic"
23
+sudo iptables -A OUTPUT -j ACCEPT -m conntrack --ctstate ESTABLISHED,RELATED -m comment --comment "accept all outgoing established and related trafic"
24
+
25
+# Allow everything for trusted networks
26
+sudo iptables -A INPUT -j ACCEPT -i lo -m comment --comment "accept all incoming traffic of loopback device"
27
+sudo iptables -A OUTPUT -j ACCEPT -o lo -m comment --comment "accept all outgoing traffic of the loopback device"
28
+sudo iptables -A INPUT -j ACCEPT -i eth0 -m comment --comment "accept all incoming traffic of eth0.30 device"
29
+sudo iptables -A OUTPUT -j ACCEPT -o eth0 -m comment --comment "accept all outgoing traffic of the eth0.30 device"
30
+
31
+sudo iptables -A OUTPUT -j ACCEPT -o eth0.10 ! -d 192.168.0.0/16 -m comment --comment "accept all outgoing traffic to the internet"
32
+
33
+# Add reject rule for propper feedback
34
+sudo iptables -A INPUT -j REJECT -m comment --comment "Reject all remaining traffic"
35
+
36
+
37
+
38
+#######################################################################################################################
39
+# Cros VLAN forwarding ################################################################################################
40
+#######################################################################################################################
41
+# General
42
+sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
43
+sudo iptables -A FORWARD -j ACCEPT -m conntrack --ctstate ESTABLISHED,RELATED -m comment --comment "forward all established and related traffic"
44
+sudo iptables -A FORWARD -j ACCEPT -p icmp --icmp-type 8 -m comment --comment "forward all pings"
45
+sudo iptables -A FORWARD -j ACCEPT -p udp --dport 123 -m comment --comment "forward all ntp request"
46
+sudo iptables -A FORWARD -j ACCEPT -p udp --sport 123 -m comment --comment "forward all ntp request"
47
+
48
+#---------------------
49
+# -> VLAN10 (Internet)
50
+#---------------------
51
+# Allow VLAN30 full access to the internet
52
+sudo iptables -A FORWARD -j ACCEPT -i eth0 -o eth0.10 ! -d 192.168.0.0/16 -m comment --comment "forward all traffic from vlan30 to the internet"
53
+sudo iptables -A FORWARD -j ACCEPT -i eth0 -o eth0.10 -d 192.168.0.0/24 -m comment --comment "forward all traffic from vlan30 to vlan10"
54
+# Allow VLAN40 access to to http,https
55
+sudo iptables -A FORWARD -j ACCEPT -i eth0.40 -o eth0.10 ! -d 192.168.0.0/16 -p tcp --dport 80:443 -m comment --comment "forward all http and https traffic from vlan40 to the internet"
56
+sudo iptables -A FORWARD -j ACCEPT -i eth0.40 -o eth0.10 ! -d 192.168.0.0/16 -p tcp --dport 8080 -m comment --comment "forward port 8080 to the internet for siemens dishwasher"
57
+# Allow VLAN60 access to to http,https
58
+sudo iptables -A FORWARD -j ACCEPT -i eth0.60 -o eth0.10 ! -d 192.168.0.0/16 -p tcp --dport 80:443 -m comment --comment "forward all http and https traffic from vlan60 to the internet"
59
+
60
+#--------------------
61
+# -> VLAN20 (Network)
62
+#--------------------
63
+# Allow VLAN30 full access
64
+sudo iptables -A FORWARD -j ACCEPT -i eth0 -o eth0.20 -d 192.168.20.0/24 -m comment --comment "forward traffic from vlan30 to VLAN20"
65
+
66
+#--------------------------------------
67
+# -> VLAN30 (Family) - VERY RESTRICTIVE
68
+#--------------------------------------
69
+# Allow VLAN40 and VLAN50 access to the mqtt port
70
+sudo iptables -A FORWARD -j ACCEPT -i eth0.40 -o eth0 -p tcp --dport 1883 -d 192.168.30.0/24 -m comment --comment "forward mqtt traffic from vlan40 to VLAN30"
71
+sudo iptables -A FORWARD -j ACCEPT -i eth0.50 -o eth0 -p tcp --dport 1883 -d 192.168.30.0/24 -m comment --comment "forward mqtt traffic from vlan50 to VLAN30"
72
+sudo iptables -A FORWARD -j ACCEPT -i eth0.50 -o eth0 -p tcp --dport 21 -d 192.168.30.0/24 -m comment --comment "forward ftp traffic from vlan50 to VLAN30 (brother printer)"
73
+
74
+
75
+#-------------------------------
76
+# -> VLAN40 (Untrusted Internet)
77
+#-------------------------------
78
+# Allow VLAN30 full access
79
+sudo iptables -A FORWARD -j ACCEPT -i eth0 -o eth0.40 -d 192.168.40.0/24 -m comment --comment "forward traffic from vlan30 to VLAN40"
80
+
81
+#---------------------------------
82
+# -> VLAN50 (Untrusted no Internet
83
+#---------------------------------
84
+# Allow VLAN30 full access
85
+sudo iptables -A FORWARD -j ACCEPT -i eth0 -o eth0.50 -d 192.168.50.0/24 -m comment --comment "forward traffic from vlan30 to VLAN50"
86
+
87
+#------------------
88
+# -> VLAN60 (Gäste)
89
+#------------------
90
+
91
+
92
+
93
+#----------------------
94
+# Masquerade traffic to
95
+#----------------------
96
+sudo iptables -t nat -A POSTROUTING -o eth0.10 -j MASQUERADE -m comment --comment "masquerade ->eth0.10"
97
+sudo iptables -t nat -A POSTROUTING -o eth0.20 -j MASQUERADE -m comment --comment "masquerade ->eth0.20"
98
+sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE -m comment --comment "masquerade ->eth0.30"
99
+sudo iptables -t nat -A POSTROUTING -o eth0.40 -j MASQUERADE -m comment --comment "masquerade ->eth0.40"
100
+sudo iptables -t nat -A POSTROUTING -o eth0.50 -j MASQUERADE -m comment --comment "masquerade ->eth0.50"
101
+
102
+
103
+
104
+
105
+#######################################################################################################################
106
+# Change policy to drop all remainig ##################################################################################
107
+#######################################################################################################################
108
+sudo iptables -P INPUT DROP
109
+sudo iptables -P OUTPUT DROP
110
+sudo iptables -P FORWARD DROP
111
+
112
+
113
+
114
+#######################################################################################################################
115
+# Show configuration ##################################################################################################
116
+#######################################################################################################################
117
+sudo iptables -L -n -v --line-numbers
118
+
119
+
120
+echo "********************************************************************************************"
121
+echo "* -> Execute sudo sh -c \"iptables-save > /etc/iptables/rules.v4\""
122
+echo "********************************************************************************************"
123
+

+ 98
- 0
vlanm View File

@@ -0,0 +1,98 @@
1
+#!/bin/python3
2
+#
3
+import argparse
4
+from getpass import getpass
5
+import re
6
+import subprocess
7
+
8
+def device_by_vlan(n):
9
+    return 'eth0' if n == 10 else 'eth0.%d' % n
10
+
11
+def iprange_by_vlan(n):
12
+    return '192.168.0.0/24' if n == 10 else '192.168.%d.0/24' % n
13
+
14
+def get_iptables_info():
15
+    global password
16
+    my_cmd = "sudo -S iptables -L -n -v --line-numbers".split()
17
+    cmd = subprocess.run(my_cmd, stdout=subprocess.PIPE, input=password, encoding="ascii",)
18
+    return cmd.stdout
19
+
20
+def get_rule_num(chain, comment):
21
+    required_chain = False
22
+    for line in get_iptables_info().split('\n'):
23
+        if line.strip().lower().startswith('chain'):
24
+            required_chain = line.strip().lower().startswith('chain ' + chain.strip().lower())
25
+        if required_chain:
26
+            rule_num = None
27
+            try:
28
+                rule_num = int(line.split()[0])
29
+            except:
30
+                pass
31
+            else:
32
+                s = re.search(r'\/\*(.*?)\*\/', line)
33
+                if s is not None:
34
+                    if s.group(1).strip() == comment:
35
+                        return rule_num
36
+
37
+
38
+
39
+if __name__ == "__main__":
40
+    global password
41
+
42
+    prog = 'vlan manager'
43
+    description = 'Add and removes iptable rules for a given vlan'
44
+    epilog = 'Text at the bottom of help'
45
+    parser = argparse.ArgumentParser(prog=prog, description=description, epilog=epilog)
46
+    parser.add_argument('-v', '--vlan_number', type=int, required=True, help="Define the vlan to be used")
47
+    parser.add_argument('-o', '--open_to_internet', action='store_true', help="Toggle rule: Open vlan to the internet")
48
+    parser.add_argument('-l', '--log_to_internet', action='store_true', help="Toggle rule: Log vlan to the internet")
49
+
50
+    args = parser.parse_args()
51
+
52
+    UID_O = 'VLANM_RULE__OPEN_TO_INTERNET_%d' % args.vlan_number
53
+    UID_L = 'VLANM_RULE__LOG_TO_INTERNET_%d' % args.vlan_number
54
+
55
+    if args.open_to_internet:
56
+        password = getpass("password: ")
57
+        print()
58
+        rn = get_rule_num('forward', UID_O)
59
+        if rn is None:
60
+            print("Add rule:", UID_O)
61
+        else:
62
+            print("Remove rule:", UID_O)
63
+        if getpass('Proceed? [Y/n]') in ['', 'y', 'Y']:
64
+            if rn is None:
65
+                my_cmd = ['sudo',  '-S', 'iptables', '-A', 'FORWARD', '-j', 'ACCEPT', '-i', device_by_vlan(args.vlan_number), '-o', 'eth0', '!', '-d', '192.168.0.0/16', '-m', 'comment', '--comment', UID_O]
66
+                cmd = subprocess.run(my_cmd, stdout=subprocess.PIPE, input=password, encoding="ascii",)
67
+                print(cmd.stdout)
68
+            else:
69
+                my_cmd = ['sudo', '-S', 'iptables', '-D', 'FORWARD', str(rn)]
70
+                cmd = subprocess.run(my_cmd, stdout=subprocess.PIPE, input=password, encoding="ascii",)
71
+                print(cmd.stdout)
72
+    elif args.log_to_internet:
73
+        password = getpass("password: ")
74
+        print()
75
+        rn = get_rule_num('forward', UID_L)
76
+        if rn is None:
77
+            print("Add rule:", UID_L)
78
+        else:
79
+            print("Remove rule:", UID_L)
80
+        if getpass('Proceed? [Y/n]') in ['', 'y', 'Y']:
81
+            if rn is None:
82
+                my_cmd = ['sudo', '-S', 'iptables', '-A', 'FORWARD', '-j', 'LOG', '-i', device_by_vlan(args.vlan_number), '-o', 'eth0', '!', '-d', '192.168.0.0/16', '-m', 'comment', '--comment', UID_L]
83
+                cmd = subprocess.run(my_cmd, stdout=subprocess.PIPE, input=password, encoding="ascii",)
84
+                print(cmd.stdout)
85
+                print("Use 'sudo ~/bin/ftail /var/log/kern.log' to view the logs.")
86
+            else:
87
+                my_cmd = ['sudo', '-S', 'iptables', '-D', 'FORWARD', str(rn)]
88
+                cmd = subprocess.run(my_cmd, stdout=subprocess.PIPE, input=password, encoding="ascii",)
89
+                print(cmd.stdout)
90
+
91
+
92
+    else:
93
+        print("************************")
94
+        print("* No action specified! *")
95
+        print("************************")
96
+        print()
97
+        parser.print_help()
98
+

Loading…
Cancel
Save