iptables默认安全规则脚本

什么是iptables

iptables或netfilter(网络过滤器) 是一个用户态的防火墙应用软件,允许系统管理员可以调整设定X表(Xtables)提供相关的系统表格(目前主要是在 iptables/netfilter 底下)以及相关的“链”与“规则”来管理网络封包的流动与转送的动作。因为相关动作上的需要,所以 iptables 的操作需要用到系统管理员的权限,不然就无法运作下去。在大部份的 Linux 系统上面, iptables 是使用/usr/sbin/iptables 来操作,文件则放置在手册页(Man page)(参考: [1])底下,也可以透过 man iptables 的方式来取得这份文件。通常 iptables 都需要核心层级(kernel)的模组来配合运作,Xtables 是主要在核心层级里面 iptables API 运作功能的模组。

目前 iptables 系在 2.4 及 2.6 的核心底下运作,旧版的 Linux 核心 (2.2) 使用 ipchains 及 ipwadm (linux 2.0) 来达成类似的功能。[来自:维基百科]

保存在这的目的

上次把iptables弄坏了,然后就内网都无法连接了.把这个脚本保存在这里,备用.

14
image-1888

具体操作[转载自网络]

下面的命令以root用户在终端执行:

1
vim default_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
#!/bin/bash
#########################################################################
#
# File:         default_firewall.sh
# Description:
# Language:     GNU Bourne-Again SHell
# Version: 1.0
# Date: 2010-6-23
# Corp.: c1gstudio.com
# Author: c1g
# WWW: http://blog.c1gstudio.com
### END INIT INFO
###############################################################################
 
IPTABLES=/sbin/iptables
 
# start by flushing the rules
$IPTABLES -P INPUT DROP
$IPTABLES -P FORWARD ACCEPT
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -t nat -P PREROUTING ACCEPT
$IPTABLES -t nat -P POSTROUTING ACCEPT
$IPTABLES -t nat -P OUTPUT ACCEPT
$IPTABLES -t mangle -P PREROUTING ACCEPT
$IPTABLES -t mangle -P OUTPUT ACCEPT
 
$IPTABLES -F
$IPTABLES -X
$IPTABLES -Z
$IPTABLES -t nat -F
$IPTABLES -t mangle -F
$IPTABLES -t nat -X
$IPTABLES -t mangle -X
$IPTABLES -t nat -Z
 
## allow packets coming from the machine
$IPTABLES -A INPUT -i lo -j ACCEPT
$IPTABLES -A OUTPUT -o lo -j ACCEPT
 
# allow outgoing traffic
$IPTABLES -A OUTPUT -o eth0 -j ACCEPT
 
# block spoofing
$IPTABLES -A INPUT -s 127.0.0.0/8 -i ! lo -j DROP
 
$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPTABLES -A INPUT -p icmp -j ACCEPT
 
 
# stop bad packets
$IPTABLES -A INPUT -m state --state INVALID -j DROP
 
# NMAP FIN/URG/PSH
$IPTABLES -A INPUT -i eth0 -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
# stop Xmas Tree type scanning
$IPTABLES -A INPUT -i eth0 -p tcp --tcp-flags ALL ALL -j DROP
$IPTABLES -A INPUT -i eth0 -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
# stop null scanning
$IPTABLES -A INPUT -i eth0 -p tcp --tcp-flags ALL NONE -j DROP
# SYN/RST
$IPTABLES -A INPUT -i eth0 -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
# SYN/FIN
$IPTABLES -A INPUT -i eth0 -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
# stop sync flood
$IPTABLES -N SYNFLOOD
$IPTABLES -A SYNFLOOD -p tcp --syn -m limit --limit 1/s -j RETURN
$IPTABLES -A SYNFLOOD -p tcp -j REJECT --reject-with tcp-reset
$IPTABLES -A INPUT -p tcp -m state --state NEW -j SYNFLOOD
# stop ping flood attack
$IPTABLES -N PING
$IPTABLES -A PING -p icmp --icmp-type echo-request -m limit --limit 1/second -j RETURN
$IPTABLES -A PING -p icmp -j REJECT
$IPTABLES -I INPUT -p icmp --icmp-type echo-request -m state --state NEW -j PING
 
 
#################################
## What we allow
#################################
 
# tcp ports
 
# smtp
$IPTABLES -A INPUT -p tcp -m tcp --dport 25 -j ACCEPT
# http
$IPTABLES -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
# pop3
$IPTABLES -A INPUT -p tcp -m tcp --dport 110 -j ACCEPT
# imap
$IPTABLES -A INPUT -p tcp -m tcp --dport 143 -j ACCEPT
# ldap
$IPTABLES -A INPUT -p tcp -m tcp --dport 389 -j ACCEPT
# https
$IPTABLES -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
# smtp over SSL
$IPTABLES -A INPUT -p tcp -m tcp --dport 465 -j ACCEPT
# line printer spooler
$IPTABLES -A INPUT -p tcp -m tcp --dport 515 -j ACCEPT
# cups
$IPTABLES -A INPUT -p tcp -m tcp --dport 631 -j ACCEPT
# mysql
$IPTABLES -A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT
# tomcat
$IPTABLES -A INPUT -p tcp -m tcp --dport 8080 -j ACCEPT
# squid
$IPTABLES -A INPUT -p tcp -m tcp --dport 81 -j ACCEPT
# nrpe
$IPTABLES -A INPUT -p tcp -m tcp --dport 15666 -j ACCEPT
 
## restrict some tcp things ##
 
# ssh
$IPTABLES -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
$IPTABLES -A INPUT -p tcp -m tcp --dport 6022 -j ACCEPT
# samba (netbios)
$IPTABLES -A INPUT -p tcp -m tcp -s 192.168.0.0/16 --dport 137:139 -j ACCEPT
# ntop
$IPTABLES -A INPUT -p tcp -m tcp -s 192.168.0.0/16 --dport 3000  -j ACCEPT
# Hylafax
$IPTABLES -A INPUT -p tcp -m tcp -s 192.168.0.0/16 --dport 4558:4559 -j ACCEPT
# webmin
$IPTABLES -A INPUT -p tcp -m tcp -s 192.168.0.0/16 --dport 10000  -j ACCEPT
 
# udp ports
# DNS
$IPTABLES -A INPUT -p udp -m udp --dport 53 -j ACCEPT
# DHCP
$IPTABLES -A INPUT -p udp -m udp --dport 67:68 -j ACCEPT
# NTP
$IPTABLES -A INPUT -p udp -m udp --dport 123 -j ACCEPT
# SNMP
$IPTABLES -A INPUT -p udp -m udp --dport 161:162 -j ACCEPT
 
## restrict some udp things ##
 
# Samba (Netbios)

$IPTABLES -A INPUT -p udp -m udp -s 192.168.0.0/16 --dport 137:139  -j ACCEPT
$IPTABLES -A INPUT -p udp -m udp --sport 137:138 -j ACCEPT
 
# finally - drop the rest
 
$IPTABLES -A INPUT -p tcp --syn -j DROP

//-end
//:wq! 保存退出

下面在Linux终端[使用root用户]进行操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//设置权限
chmod u+x ./default_firewall.sh

//运行脚本
./default_firewall.sh

//查看iptables
/sbin/iptables -nL

//保存iptables
 /sbin/iptables-save > /etc/sysconfig/iptables

//重启iptables
/etc/init.d/iptables restart