-
Notifications
You must be signed in to change notification settings - Fork 0
/
firewall.sh
189 lines (162 loc) · 4.71 KB
/
firewall.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
### BEGIN INIT INFO
# Provides: PersonalFirewall
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Personal Firewall
# Description: This can by use for personal computer or web server. It is subject to change, and can be enhanced.
### END INIT INFO
# iptables IPV4 and IPV6
IPT=/sbin/iptables
IP6T=/sbin/ip6tables
# This function starts firewall
do_start() {
# Erase all current rules. -F all. -X users
$IPT -t filter -F
$IPT -t filter -X
$IPT -t nat -F
$IPT -t nat -X
$IPT -t mangle -F
$IPT -t mangle -X
#
$IP6T -t filter -F
$IP6T -t filter -X
# no NAT for IPv6
#$IP6T -t nat -F
#$IP6T -t nat -X
$IP6T -t mangle -F
$IP6T -t mangle -X
# default strategy (-P): drop all trafic (input, output and forward)
$IPT -t filter -P INPUT DROP
$IPT -t filter -P FORWARD DROP
$IPT -t filter -P OUTPUT DROP
#
$IP6T -t filter -P INPUT DROP
$IP6T -t filter -P FORWARD DROP
$IP6T -t filter -P OUTPUT DROP
# Loopback
$IPT -t filter -A INPUT -i lo -j ACCEPT
$IPT -t filter -A OUTPUT -o lo -j ACCEPT
#
$IP6T -t filter -A INPUT -i lo -j ACCEPT
$IP6T -t filter -A OUTPUT -o lo -j ACCEPT
# Allow all established connection to receive trafic
$IPT -t filter -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -t filter -A OUTPUT -m state ! --state INVALID -j ACCEPT
#
$IP6T -t filter -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$IP6T -t filter -A OUTPUT -m state ! --state INVALID -j ACCEPT
## Drop XMAS scans and NULL scans.
$IPT -t filter -A INPUT -p tcp --tcp-flags FIN,URG,PSH FIN,URG,PSH -j DROP
$IPT -t filter -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
$IPT -t filter -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
$IPT -t filter -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
#
$IP6T -t filter -A INPUT -p tcp --tcp-flags FIN,URG,PSH FIN,URG,PSH -j DROP
$IP6T -t filter -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
$IP6T -t filter -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
$IP6T -t filter -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
## Drop all broadcasted packets
$IPT -t filter -A INPUT -m pkttype --pkt-type broadcast -j DROP
#
$IP6T -t filter -A INPUT -m pkttype --pkt-type broadcast -j DROP
## ICMP
$IPT -t filter -A INPUT -p icmp -j ACCEPT
#
#$IP6T -t filter -A INPUT -p icmp -j ACCEPT
#$IP6T -t filter -A OUTPUT -p icmp -j ACCEPT
## Log all packet
$IPT -t filter -A INPUT -j LOG
$IPT -t filter -A FORWARD -j LOG
$IPT -t filter -A OUTPUT -j LOG
#
$IP6T -t filter -A INPUT -j LOG
$IP6T -t filter -A FORWARD -j LOG
$IP6T -t filter -A OUTPUT -j LOG
## DNS
$IPT -t filter -A OUTPUT -p tcp --dport 53 -j ACCEPT
$IPT -t filter -A OUTPUT -p udp --dport 53 -j ACCEPT
#
$IP6T -t filter -A OUTPUT -p tcp --dport 53 -j ACCEPT
$IP6T -t filter -A OUTPUT -p udp --dport 53 -j ACCEPT
## INTPUT
$IPT -t filter -A INPUT -p tcp --dport 443 -j ACCEPT
$IPT -t filter -A INPUT -p tcp --dport 80 -j ACCEPT
## OUTPUT
$IPT -t filter -A OUTPUT -p tcp --dport 80 -j ACCEPT
#
echo "firewall started [OK]"
}
# This function stops firewall
do_stop() {
# Erase all rules
$IPT -t filter -F
$IPT -t filter -X
$IPT -t nat -F
$IPT -t nat -X
$IPT -t mangle -F
$IPT -t mangle -X
#
$IP6T -t filter -F
$IP6T -t filter -X
#$IP6T -t nat -F
#$IP6T -t nat -X
$IP6T -t mangle -F
$IP6T -t mangle -X
# delete stategy
$IPT -t filter -P INPUT ACCEPT
$IPT -t filter -P OUTPUT ACCEPT
$IPT -t filter -P FORWARD ACCEPT
#
$IP6T -t filter -P INPUT ACCEPT
$IP6T -t filter -P OUTPUT ACCEPT
$IP6T -t filter -P FORWARD ACCEPT
#
echo "firewall stopped [OK]"
}
# This function show firewall status
do_status() {
# Show current rules
clear
echo Status IPV4
echo -----------------------------------------------
$IPT -L -n -v
echo
echo -----------------------------------------------
echo
echo status IPV6
echo -----------------------------------------------
$IP6T -L -n -v
echo
}
case "$1" in
start)
do_start
# exit without errors
exit 0
;;
stop)
do_stop
# exit without errors
exit 0
;;
restart)
do_stop
do_start
# exit without errors
exit 0
;;
status)
do_status
# exit without errors
exit 0
;;
*)
echo "Usage: firewall.sh {start|stop|restart|status}"
# exit with error
exit 1
;;
esac
exit 0