场景需求:
(1)员工在公司内部(192.168.124.0/24 ,192.168.122.0/24 )能访问服务器上任何服务
(2)当员工出差,通过VPN连接到公司
(3)公司门户网站允许公网访问
解:
(2)外网======》VPN服务器======》公司的服务
常见允许访问外网的服务:
网站www: http 80/tcp
https 443/tcp
邮件mail: smtp 25/tcp
smtps 465/tcp
pop3 110/tcp
pop3s 995/tcp
imap 143/tcp
一些常见不允许外网访问的服务:
文件服务器: NFS 123/UDP
SAMBA 137,138,139/tcp,445/tcp
FTP 20/tcp,21/tcp
远程管理: SSH 22/tcp
数据库: MYSQL 3306/tcp
ORACLE 1521/tcp
配置规则基本思路:
ACCEPT规则:1)允许本地访问。2)允许已监听状态数据包通过。3)允许规则中允许的数据包通过。(千万别忘记22端口)
DENY规则:拒绝未被允许的数据包。
iptables规则保存成配置文件
开始。。。。。。。。。
满足条件1:
[[email protected] ~]# iptables -I INPUT -i lo -j ACCEPT [[email protected] ~]# iptables -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT [[email protected] ~]# iptables -A INPUT -s 192.168.124.0/24 -j ACCEPT [[email protected] ~]# iptables -A INPUT -s 192.168.122.0/24 -j ACCEPT [[email protected] ~]# iptables -nL Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ACCEPT all -- 192.168.124.0/24 0.0.0.0/0 ACCEPT all -- 192.168.122.0/24 0.0.0.0/0 Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
满足条件2,3:
[[email protected] ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT [[email protected] ~]# iptables -A INPUT -p tcp --dport 1723 -j ACCEPT(VPN常用端口) [[email protected] ~]# iptables -I INPUT -p icmp -j ACCEPT [[email protected] ~]# iptables -A INPUT -j REJECT [[email protected] ~]# iptables -nL Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ACCEPT all -- 192.168.124.0/24 0.0.0.0/0 ACCEPT all -- 192.168.122.0/24 0.0.0.0/0 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:1723 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
保存到配置文件,设置开机自启动后,自动加载:
[[email protected] ~]# /etc/init.d/iptables save iptables:将防火墙规则保存到 /etc/sysconfig/iptables: [确定] [[email protected] ~]# cat /etc/sysconfig/ip ip6tables ip6tables.old iptables-config iptables.save ip6tables-config iptables iptables.old [[email protected] ~]# cat /etc/sysconfig/iptables # Generated by iptables-save v1.4.7 on Sun Mar 13 00:27:10 2016 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [69:8172] -A INPUT -p icmp -j ACCEPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -s 192.168.124.0/24 -j ACCEPT -A INPUT -s 192.168.122.0/24 -j ACCEPT -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT -A INPUT -p tcp -m tcp --dport 1723 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-port-unreachable COMMIT # Completed on Sun Mar 13 00:27:10 2016
或者将文件保存未shell文件,将shell文件写到rc.local文件中:例如
sh /root/shell/myiptables.sh
写到rc.local中。。。
时间: 2024-10-21 04:39:26