iptables规则书写总结

基本语法 iptables -t (表名) 选项 (链名) 条件 -j 目标操作

4个表 nat地址转换 filter数据过滤 raw 状态跟踪 mangle包标记POSTROUTING路由后
选项 -A 链尾的加规则
-I 链的开头或序号插入一条规则
-L 列出所有的规则条目
-n 以数字形式显示地址 端口等信息
–line-numbers 查看规则 显示规则的序号
-D 删除链内序号或内容的一条规则
-F 清空所有的规则
-p 对应的协议
–dport 对应端口
-m 扩展规则
目标操作
ACCEPT DROP REJECT LOG日志 (必须大写)
基本使用案例
01. [ root@proxy ~] # iptables - t f ilter - A INPUT - p tcp - j ACCEPT
02. //追加规则至f ilter表中的INPUT链的末尾,允许任何人使用TCP协议访问本机
3. 03. [ root@proxy ~] # iptables - I INPUT - p udp - j ACCEPT
04. //插入规则至f ilter表中的INPUT链的开头,允许任何人使用UDP协议访问本机
05. [ root@proxy ~] # iptables - I INPUT 2 - p icmp - j ACCEPT
06. //插入规则至f ilter表中的INPUT链的第2行,允许任何人使用ICMP协议访问本机
查看iptables防火墙规则
01. [ root@proxy ~] # iptables - nL INPUT
02. target
03. ACCEPT
udp - - 0.0.0.0/0
0.0.0.0/0
04. ACCEPT
icmp - - 0.0.0.0/0
0.0.0.0/0
05. ACCEPT
tcp - - 0.0.0.0/0
0.0.0.0/0
06. [ root@proxy ~] # iptables - L INPUT - - line- numbers
07. num target
prot opt source
destination
08. 1 ACCEPT
udp - - any where
any where
09. 2
ACCEPT
icmp - -
any where
any where
10. 3
ACCEPT
tcp - -
any where
any where
prot opt source
//仅查看INPUT链的规则
destination
//查看规则,显示行号
删除规则,清空所有规则
01. [ root@proxy ~] # iptables - D INPUT 3
02. //删除f ilter表中INPUT链的第3条规则
03. [ root@proxy ~] # iptables - nL INPUT
04. [ root@proxy ~] # iptables - F
05. //清空f ilter表中所有链的防火墙规则
06. [ root@proxy ~] # iptables - t nat - F
07. //清空nat表中所有链的防火墙规则
08. [ root@proxy ~] # iptables - t mangle - F
09. //清空mangle表中所有链的防火墙规则
10. [ root@proxy ~] # iptables - t raw - F
11. //清空raw表中所有链的防火墙规则
//查看规则,确认是否删除
设置防火墙默认规则
01. [ root@proxy ~] # iptables - t f ilter - P INPUT DROP
02. [ root@proxy ~] # iptables - nL
03. Chain INPUT ( policy DROP)
禁ping
[ root@proxy ~] # iptables - I INPUT - p icmp - j DROP
02. //设置完上面的规则后,其他主机确实无法ping本机,但本机也无法ping其他主机
03. //当本机ping其他主机,其他主机回应也是使用icmp,对方的回应被丢弃
禁止其他主机ping本机ping其他主机
[ root@proxy ~] # iptables - A INPUT - p icmp \
02. > - - icmp- ty pe echo- request - j DROP
03. //仅禁止入站的ping请求,不拒绝入站的ping回应包
根据mac地址过滤
root@proxy ~] # iptables - A INPUT - p tcp - - dport 22 - m mac - - mac- source 52: 54: 00: 00: 00: 0b - j DROP
多端口过滤规则
[ root@proxy ~] # iptables - A INPUT - p tcp \
02. > - m multiport - - dports 20: 22,25,80,110,143,16501: 16800 - j ACCEPT
03. //一次性开启20,21,22,25,80,110,143,16501到16800所有的端口
根据ip地址范围过滤
允许从 192.168.4.10-192.168.4.20 登录
01. [ root@proxy ~] # iptables - A INPUT - p tcp - - dport 22 \
02. > - m iprange - - src- range 192.168.4.10- 192.168.4.20 - j ACCEPT
注意,这里也可以限制多个目标IP的范围,参数是–dst-range,用法与–src-range一致。
2)禁止从 192.168.4.0/24 网段其他的主机登录
01.
[ root@proxy ~] # iptables - A INPUT - p tcp - - dport 22 - s 192.168.4.0/24 - j DROP
步骤一:搭建内外网案例环境
表-4 实验拓扑
这里,我们设定192.168.2.0/24网络为外部网络,192.168.4.0/24为内部网络。
现在,在外部网络中有一台web服务器192.168.2.100,因为设置了网关,client已经可以访问
此web服务器了。但,如果查看web1的日志就会发现,日志里记录的是192.168.4.100在访问网
页。
我们需要实现的效果是,client可以访问web服务器,但要伪装为192.168.2.5后再访问web服
务器(模拟所有位于公司内部的电脑都使用的是私有IP,希望访问外网,就需要伪装为公司的外网IP
后才可以)。
步骤二:设置防火墙规则,实现IP地址的伪装(SNAT源地址转换)
1)确保proxy主机开启了路由转发功能
01.
[ root@proxy ~] # echo 1 > /proc/sy s/net/ipv 4/ip_f orward
//开启路由转发
2)设置防火墙规则,实现SNAT地址转换
01. [ root@proxy ~] # iptables - t nat - A POSTROUTING \
02. > - s 192.168.4.0/24 - p tcp - - dport 80 - j SNAT - - to- source 192.168.2.5
3)登陆web主机查看日志
01. [ root@proxy ~] # tail /v ar/log/httpd/access_log
02. .. ..
03. 192.168.2.5 - - [ 12/Aug/2018: 17: 57: 10 +0800] “GET / HTTP/1.1” 200 27 “- ” “Mozilla/4.0
通过日志会发现,客户端是先伪装为了192.168.2.5之后再访问的web服务器!
4)扩展知识,对于proxy外网IP不固定的情况可以执行下面的地址伪装,动态伪装IP。
Top
http://tts.tmooc.cn/ttsPage/LINUX/NSDTN201801/SECURITY/DAY04/CASE/01/index.html
9/102018/9/6
CASE
01. [ root@proxy ~] # iptables - t nat - A POSTROUTING \
02. > - s 192.168.4.0/24 - p tcp - - dport 80 - j MASQUERADE
最后,所有iptables规则都是临时规则,如果需要永久保留规则需要执行如下命令:
01.
[ root@proxy ~] # serv ice iptables sav e

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值