1.防火墙常用规则



systemctl start iptables
systemctl stop iptables
systemctl restart iptables
iptables -nvL


1.屏蔽ip地址和解封ip地址

iptables -A INPUT -s 22.22.22.22 -j DROP

iptables -D INPUT -s 22.22.22.22 -j DROP

iptables -I INPUT -s 117.81.61.88 -j ACCEPT
iptables -I INPUT -s 222.93.243.191 -j ACCEPT

2.阻止端口和同意端口

阻止特定的传出连接:

 iptables -A OUTPUT -p tcp --dport 80 -j DROP

同意特定的传入连接:

iptables -A INPUT -p tcp --dport 80 -j ACCEPT

使用 multiport 我们可以一次性在单条规则中写入多个端口,例如:

iptables -A INPUT  -p tcp -m multiport --dports 22,80,443 -j ACCEPT

iptables -A OUTPUT -p tcp -m multiport --sports 22,80,443 -j ACCEPT

在 IPtables 中 IP 地址范围是可以直接使用 CIDR 进行表示的,例如:

iptables -A OUTPUT -p tcp -d 192.168.100.0/24 --dport 22 -j ACCEPT

3.端口转发

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 25 -j REDIRECT --to-port 2525

iptables -t nat -A PREROUTING -p tcp -m tcp --dport 20022 -j DNAT --to-destination 192.168.1.150:22

端口转出参考:

4.屏蔽HTTP服务Flood攻击

命令会将连接限制到每分钟 100 个,上限设定为 200

 

iptables -A INPUT -p tcp --dport 80 -m limit --limit 100/minute --limit-burst 200 -j ACCEPT

5.禁止ping

iptables -A INPUT -p icmp -i eth0 -j DROP

6.允许访问回环网卡

iptables -A INPUT -i lo -j ACCEPT

iptables -A OUTPUT -o lo -j ACCEPT

12、屏蔽指定MAC地址

iptables -A INPUT -m mac --mac-source 00:00:00:00:00:00 -j DROP

13、限制并发连接数

iptables -A INPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 3 -j REJECT

以上规则限制每客户端不超过 3 个连接

14、清空IPtables规则

iptables -F

要清空特定的表可以使用 -t 参数进行指定,例如:

iptables -t nat –F

15.保存规则

iptables-save > ~/iptables.rules

iptables-restore < ~/iptables.rules

service iptables save

17、允许建立相关连接

随着网络流量的进出分离,要允许建立传入相关连接,可以使用如下规则:

iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

允许建立传出相关连接的规则:

iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT

18、丢弃无效数据包

很多网络攻击都会尝试用黑客自定义的非法数据包进行尝试,我们可以使用如下命令来丢弃无效数据包:

iptables -A INPUT -m conntrack --ctstate INVALID -j DROP

19、IPtables屏蔽邮件发送规则

如果你的系统不会用于邮件发送,我们可以在规则中屏蔽 SMTP 传出端口:

iptables -A OUTPUT -p tcp --dports 25,465,587 -j REJECT

20、阻止连接到某块网卡

如果你的系统有多块网卡,我们可以限制 IP 范围访问某块网卡:

iptables -A INPUT -i eth0 -s xxx.xxx.xxx.xxx -j DROP

21、设置默认规则 

iptables -P INPUT DROP  # 设置 filter 表 INPUT 链的默认规则是 DROP,如果前面没有添加允许的规则,会把自己的xshell连接DROP掉

 22、按行号删除防火墙规则



iptables -nL --line-number
iptables -D INPUT 10
iptables -D INPUT 11

iptables-save > ~/iptables.rules



 

 



failwall 常用规则

http://bjyun.fujieace.com/help/9.html 

防火墙安装

先禁用旧防火墙管理模式

systemctl stop firewalld && systemctl disable firewalld

安装 iptables:

yum -y install iptables-services

systemctl start iptables.service && systemctl enable iptables.service

iptables -F

iptables -A INPUT -i lo -j ACCEPT

iptables -A INPUT -p tcp -m multiport --dport 80,22,25,10051,10050,3306 -j ACCEPT

iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT

iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

iptables -t filter -A INPUT -m state --state INVALID -j DROP

iptables -P INPUT DROP

service iptables save

 中新金盾防火墙简单了解

http://blog.sina.com.cn/s/blog_a90c9f9b0102y815.html

防火墙规则参考链接

 



iptables共享上网限速参考



1



eth0: a.b.c.d(外网的上网地址)
eth1: 172.16.44.1(做为内网的网关)



 

2.
配置iptables nat



#开启ip_forward
echo "1">/proc/sys/net/ipv4/ip_forward #永久生效需要配置/etc/sysctl文件中修改
#清除原来的防火墙规则
iptables -F
iptables -t nat -F
iptables -t mangle -F
#添加nat转发
iptables -t nat -A POSTROUTING -s 172.16.44.0/24 -o eth0 -j MASQUERADE


 

centos防火墙源文件 centos防火墙规则_运维

 

通过执行上面的代码后,局域网内的电脑就可以上网了。
3.
由于我的内网还挂了网站,所以要开启80端口的转发。



iptables -t nat -I PREROUTING -p tcp -d a.b.c.d --dport 80 -j DNAT --to 172.16.44.210:80
iptables -t nat -I POSTROUTING -p tcp -d 172.16.44.210 --dport 80 -j SNAT --to 172.16.44.1



4.下载限速



#删除原来的tc规则队列
tc qdisc del dev eth1 root
#添加tc规则队列
tc qdisc add dev eth1 root handle 10: htb default 256
#生成根类
tc class add dev eth1 parent 10: classid 10:1 htb rate 100mbit ceil 100mbit
#支类列表用于限制速度
#这里的rate指的是保证带宽,ceil是最大带宽。
tc class add dev eth1 parent 10:1 classid 10:10 htb rate 400kbps ceil 400kbps prio 1

#添加支类规则队列
#采用sfq伪随机队列,并且10秒重置一次散列函数。
tc qdisc add dev eth1 parent 10:10 handle 101: sfq perturb 10

#建立网络包过滤器,设置fw。
tc filter add dev eth1 parent 10: protocol ip prio 10 handle 1 fw classid 10:10
#在iptables里面设定mark值,与上面的handle值对应。
iptables -t mangle -A POSTROUTING -d 172.16.44.130 -j MARK --set-mark 1



通过上面的代码就可以限制172.16.44.130这台机子的下载速度到400kbps。

Tip
经过实际测试这里的kbps实际上就是KB/S每秒千字节。另一个单位是kbit,这个才是每秒千比特。这里的172.16.44.130也可以写成一个网段,比如:172.16.44.0/24
5.

上传限速的原理其实跟下载的差不多,只不过限制的网卡不同,要在eth0上过滤来源地址去限制。



#删除原来的tc规则队列
tc qdisc del dev eth0 root

#添加tc规则队列
tc qdisc add dev eth0 root handle 20: htb default 256

#生成根类
tc class add dev eth0 parent 20: classid 20:1 htb rate 100mbit ceil 100mbit

#支类列表用于限制速度
tc class add dev eth0 parent 20:1 classid 20:10 htb rate 40kbps ceil 40kbps prio 1

#添加支类规则队列
tc qdisc add dev eth0 parent 20:10 handle 201: sfq perturb 10

#建立网络包过滤器
tc filter add dev eth0 parent 20: protocol ip prio 100 handle 2 fw classid 20:10
iptables -t mangle -A PREROUTING -s 172.16.44.130 -j MARK --set-mark 2



Tip

跟下载不同的是POSTROUTING要改成PREROUTING,-d改成-s。

观察连接数
通过iptables的nat连接可以通过下面的代码查看。至于统计连接数可以写代码实现,也可以利用awk,grep等工具。反正里面的内容就是文本,处理起来也比较简单。

cat /proc/net/ip_conntrack

参考于

http://outofmemory.cn/code-snippet/90/linux-usage-iptables-tc-limited-liuliang