security config
iptables
iptables的工作流程大体上可以表达为三种
源地址发送数据–> {PREROUTING–>路由规则–>POSTROUTING} –>目的地址接收到数据
源地址发送数据–> {PREROUTING–>INPUT–>本机}
{本机–>OUTPUT–>POSTROUTING} –>目的地址接收到数据
不考虑地址转发的情况下,例如一台独立的主机的网络管理,我们只需要配置INPUT和OUTPUT两条链即可完成对网络的管理
实例
iptables
-L 查看
-F 清除所有规则
-X 清除自定义链
-Z 清除所有链统计
-n的含义是用ip和端口的方式来显示规则
1 | iptables -t filter -L -n 查看filter表中的所有规则 |
有多条规则匹配一次访问的时候,以最前面的规则为准,当无法匹配到对应规则的时候,使用默认规则
开机自启动
chkconfig iptables on
service iptables save
上面的命令其实是把规则保存在/etc/sysconfig/iptables文件中,重启会自动读取
如果是业务相对固定的服务器这样做无所谓,但如果需要经常切换规则,那么讲规则都写在这一个文件中并不是十分容易管理
可以把规则写成单独的文件,在开机的时候自动载入(/etc/rc.local中添加),但是要注意权限设置(755),这样做的好处是便于管理,缺点是只有开机的时候有效
如果是重启防火墙服务,则无法载入对应规则。
mac下, 以前用的是ipsw, 现在用的是pf
Port Forwarding in Mavericks
Since Mavericks stopped using the deprecated ipfw (as of Mountain Lion), we’ll be using pf to allow port forwarding.
- anchor file
Create an anchor file under /etc/pf.anchors/with your redirection rule like:
rdr pass on lo0 inet proto tcp from any to 127.0.0.2 port 80 -> 127.0.0.1 port 40070 - Test the anchor file
Parse and test your anchor file to make sure there are no errors:
sudo pfctl -vnf - Reference the anchor in pf.conf
/etc/pf.conf is the main configuration file that pf loads at boot. We’ll need to load the anchor file we previously created:
rdr-anchor “forwarding”
load anchor “forwarding” from “/etc/pf.anchors/“
Make sure to add these entries to the appropriate spot. - Load and enabling pf
pf is not enabled by default in Mavericks, few ways to enable this:
Manually load and enable from a pf.conf file via sudo pfctl -ef
Auto enable by creating a launch daemon via this doc to run pfctl -efon boot.
Auto enable by adding an -e(enable) to the pfctl ProgramArgument in /System/Library/LaunchDaemons/com.apple.pfctl.plist like this:ProgramArguments pfctl -e -f /etc/pf.conf - Forwarding across interfaces
By default, pf does not forward between interfaces. Here’s a snippet from man for pfctl with help from 2sidedfigure:
The packet filter does not itself forward packets between interfaces. Forwarding can be enabled by setting the sysctl(8) variables net.inet.ip.forwarding and/or net.inet6.ip6.forwarding to 1. Set them permanently in sysctl.conf(5).
We’ll need to enable this by adding to /etc/sysctl.conf:
net.inet.ip.forwarding=1
net.inet6.ip6.forwarding=1
There is the possibility that pf.conf will be overriden with updates to the OS. It might be best to create your own pf config file and load them in additon to the main pf.conf to prevent this.
ref:
https://gist.github.com/kujohn/7209628
https://gist.github.com/zhoutong/8adca7038639f0f5fb0e
http://blog.csdn.net/dog250/article/details/8944111