iptables

系统+安全 Liemer_Lius 592℃

1、CentOS7 iptables如何保存?

默认情况下,CentOS7使用的是firewalld服务,使用iptables并想保存的话,需要将firewalld服务停掉,并安装iptables-services。

> systemctl stop firewalld
> systemctl disable firewalld
> yum install -y iptables-services
> service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables: [  OK  ]

iptables规则手动管理:

配置文件位置: /etc/sysconfig/iptables-config
规则保存位置: /etc/sysconfig/iptables
手动保存命令: /sbin/iptables-save

2、iptables help

Arguments:
添加:-A, --append
插入: -I, --insert
删除: -D, --delete
替换: -R, --replace
列表: -L, --list
清除: -F, --flush
建链: -N, --new
删链: -X, --delete-chain
重命名链:  -E, --rename-chain, Exp: iptables -E old-chain new-chain

Options:
协议: -p, --protocol, [tcp|udp]
源: -s, --source, Exp: iptables -s 10.10.10.10
目标: -d, --destination, Exp: iptables -p address[/mask]
跳转target: -j, --jump [ACCEPT, DROP, REJECT]
跳转chain: -g, --goto, Exp: iptables ... -g my_chain
行号: --line-numbers
表: -t, --table, table to manipulate (default: `filter', All: filter,nat,mangle,raw,security)

3、常用iptables语句

iptables -A INPUT -p tcp -s ${MY_IP} --dport 8020 -j ACCEPT
iptables -A INPUT -p tcp --dport 8020 -j REJECT
iptables -nvL INPUT --line-numbers |grep 'tcp dpt:8020' |awk '{print $1}' |sort -r |xargs -i iptables -D INPUT {}
# 匹配多ip
iptables -A INPUT -m iprange --src-range 13.32.4.168-13.32.4.176 -j ACCEPT  # 匹配源IP
iptables -A INPUT -m iprange --dest-range 8.8.8.2-8.8.8.22 -j DROP  # 匹配目标IP

# 匹配端口范围
--sport 22:80
-m multiport --dport 21,22,23,80,3306

# 匹配网络接口
-i 匹配包进入的网卡
-o 匹配包流出的网卡

# icmp有很多类型,--icmp-type 8代表ping
# 禁ping
iptables -I INPUT -p icmp --icmp-type 8 -j DROP
iptables -I INPUT -p icmp --icmp-type 8 -s 10.0.0.0/24 -j ACCEPT

# 匹配网络状态 -m state --state
# 允许关联的状态包通过,一般用于ftp服务,比喻:看电影出去接电话或者WC,回来也得允许进去
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

企业级防火墙部署

# 不允许进和转发,只允许出。最安全的
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# 开启信任的IP网段(多网卡)
iptables -A INPUT -s 124.43.62.96/27 -p all -j ACCEPT
iptables -A INPUT -s 192.168.1.0/24 -p all -j ACCEPT
# 允许访问80端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# 允许所有人ping
iptables -A INPUT -p icmp -m icmp --icmp-type any -j ACCEPT
# 允许关联的状态包
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 保存iptables配置,2种保存方式
/etc/init.d/iptables save
iptables-save > /etc/sysconfig/iptables
# iptables维护可以通过直接编辑/etc/sysconfig/iptables
vim /etc/sysconfig/iptables
/etc/init.d/iptables restart|reload # 编辑后重启

共享上网

# 载入内核模块
modprobe ip_tables
modprobe iptables_filter
modprobe iptables_nat
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state
# 局域网共享
# 方法1: 适合于有固定外网IP的,在有固定外网IP的服务器执行:
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to-source 10.0.0.7
# 192.168.1.0/24是内网地址段(办公室或IDC内网段),10.0.0.7是外网IP
# 方法2:适合变化的外网地址(ADSL)
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j MASQUERADE
# MASQUERADE 表示伪装,外网IP地址不稳定的情况即可使用MASQUERADE(动态伪装),能够自动的寻找外网地址并改为当前正确的外网IP地址

端口映射

# 场景:浏览器中访问 13.32.4.61:8088,能够映射转换到13.32.161.250:8088
# 在13.32.4.61上执行
iptables -t nat -A PREROUTING -d 13.32.4.61 -p tcp --dport 8088 -j DNAT --to 13.32.161.250:8088
iptables -t nat -A POSTROUTING -d 13.32.161.250 -p tcp --dport 8088 -j SNAT --to 13.32.4.61

# 还可以使用REDIRECT单独进行端口转换
# 例:将 80 端口的封包转递到 8080端口
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
# 使用 8080 这个 port 来启动 WWW ,但是别人都用80来联机

iptables常用企业案例

linux主机防火墙(表: filter  链: INPUT)
局域网机器共享上网(表: nat  链: POSTROUTING)
外部地址和端口,映射为内部地址和端口(表: nat  链: PREROUTING)

# 实现外网IP 124.42.34.112 一对一映射到内部 10.0.0.8
# 网关IP eth0:124.42.60.109 eth1:10.0.0.254
# 首先在路由网关上绑定VIP 124.42.34.112,可以是别名的方式。
iptables -t nat -A POSTROUTING -s 10.0.0.0/255.255.240.0 -d 124.42.34.112 -j SNAT --to-source 10.0.0.254
iptables -t nat -A PREROUTING -d 124.42.34.112 -j DNAT --to-destination 10.0.0.8
iptables -t nat -A POSTROUTING -s 10.0.0.8 -o eth0 -j SNAT --to-source 124.42.34.112

 

转载请注明:liutianfeng.com » iptables

喜欢 (2)

评论已关闭。