-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiptables.sh
executable file
·132 lines (102 loc) · 2.84 KB
/
iptables.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
iptver=$1
rulefile=/etc/iptables/${iptver}.rules
cat ${rulefile}
tableliste="filter nat mangle raw security"
tableliste="nat"
tableliste="filter"
function show_config ()
{
# Show current config
echo "========================================================================"
# ${iptver} -nvL --line-numbers
for i in $tableliste
do
${iptver} -t $i -nvL --line-numbers
done
echo "========================================================================"
}
function clean_config ()
{
# Clean current config
${iptver} -F
${iptver} -X
for i in $tableliste
do
${iptver} -t $i -F
${iptver} -t $i -X
done
echo "========================================================================"
}
init_config ()
{
# keep established
${iptver} -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
${iptver} -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
# drop invalid
${iptver} -A INPUT -m state --state INVALID -j DROP
${iptver} -A FORWARD -m state --state INVALID -j DROP
${iptver} -A OUTPUT -m state --state INVALID -j DROP
# Allow loopback
${iptver} -t filter -A INPUT -i lo -j ACCEPT
${iptver} -t filter -A OUTPUT -o lo -j ACCEPT
# ICMP (Ping)
${iptver} -t filter -A INPUT -p icmp -m limit --limit 1/second -j ACCEPT
${iptver} -t filter -A OUTPUT -p icmp -j ACCEPT
# DNS In/Out
${iptver} -t filter -A OUTPUT -p tcp --dport 53 -j ACCEPT
${iptver} -t filter -A OUTPUT -p udp --dport 53 -j ACCEPT
${iptver} -t filter -A INPUT -p tcp --dport 53 -j ACCEPT
${iptver} -t filter -A INPUT -p udp --dport 53 -j ACCEPT
# NTP Out
${iptver} -t filter -A OUTPUT -p udp --dport 123 -j ACCEPT
}
web_config ()
{
# HTTP + HTTPS Out
${iptver} -t filter -A OUTPUT -p tcp --dport 80 -j ACCEPT
${iptver} -t filter -A OUTPUT -p tcp --dport 443 -j ACCEPT
# HTTP + HTTPS In
${iptver} -t filter -A INPUT -p tcp --dport 80 -j ACCEPT
${iptver} -t filter -A INPUT -p tcp --dport 443 -j ACCEPT
# ddos limit to 10/minutes if 100/minutes reached
${iptver} -A INPUT -p tcp --dport 80 -m limit --limit 10/minute --limit-burst 100 -j ACCEPT
${iptver} -A INPUT -p tcp --dport 443 -m limit --limit 10/minute --limit-burst 100 -j ACCEPT
}
drop_all_other ()
{
# drop all
${iptver} -t filter -P INPUT DROP
${iptver} -t filter -P FORWARD DROP
${iptver} -t filter -P OUTPUT DROP
}
one_port_out ()
{
${iptver} -t filter -A OUTPUT -p tcp --dport $1 -j ACCEPT
}
one_port_in ()
{
${iptver} -t filter -A INPUT -p tcp --dport $1 -j ACCEPT
}
one_port ()
{
one_port_in $1
one_port_out $1
}
clean_config
show_config
init_config
web_config
# 22 for ssh
for i in 22
do
one_port_in $i
done
# for ftp and ssh
for i in 21 22
do
one_port_out $i
done
drop_all_other
show_config
# Save
${iptver}-save > ${rulefile}