文章目录
  1. 1. iptables
  2. 2. mac下, 以前用的是ipsw, 现在用的是pf

iptables

iptables的工作流程大体上可以表达为三种

源地址发送数据–> {PREROUTING–>路由规则–>POSTROUTING} –>目的地址接收到数据
源地址发送数据–> {PREROUTING–>INPUT–>本机}
{本机–>OUTPUT–>POSTROUTING} –>目的地址接收到数据
不考虑地址转发的情况下,例如一台独立的主机的网络管理,我们只需要配置INPUT和OUTPUT两条链即可完成对网络的管理
实例
iptables
-L 查看
-F 清除所有规则
-X 清除自定义链
-Z 清除所有链统计
-n的含义是用ip和端口的方式来显示规则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 iptables -t filter -L -n   查看filter表中的所有规则 
iptables -t filter -F 清空filter表中的规则
首先要牢记iptables是有链 有表的结构 , 那么定义每一条链上的每一条表的默认规则的格式为
iptables -t 表 -P 链 动作类型
-t 就不说了 -P 这里要大写 链的名称也是大小写敏感的注意不要写错 动作类型有ACCEPT允许通过 DROP丢弃 也就是禁止 还有一种动作是LOG作为日志记录,目前我还没有用过。
iptables -t filter -P INPUT ACCEPT #设置INPUT链上filter表默认规则

这里要注意的是,默认允许了所有的链接都可以通过,到达服务器。这样的设置在正式生产服务器环境上是十分危险的,不建议这么设置,应当设置为DROP,但是默认允许所有的链接都禁止的后果就是把自己的远程操作也踢掉了,服务器在本地还无所谓,但是在远程这就麻烦了。于是要牢记,默认规则要在设置好自己访问的规则以后再设置,最好是在最后设置。
iptables -A INPUT -i eth0 -s 192.168.1.110 -j DROP #禁止ip为192.168.1.110的计算机访问本机eth0网卡
iptables -A OUTPUT -o eth0 -d 192.168.1.110 -j DROP 禁止本机访问192.168.1.110可以这么写
iptables -A INPUT -i lo -j ACCEPT 允许本机访问本地回环网卡 localhost
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A INPUT -i eth0 -s 192.168.1.0/24 -j DROP 禁止192.168.1.xxx 这个网段所有的IP访问本机,24其实指的是二进制的从左到右 有241 ,在IPV4的ip地址表示方法下 有32位,那么32-24=8 也就是十进制IP最后一段位置代表是0
iptables -A 链 -io 网卡 -p 协议 -s 源ip --sport 源端口 -d 目标ip --dport 目标端口 -j 动作
和IP一样 链的不同(可能是INPUT也可能是OUTPUT) 使用不同的-i或者-o
-p是小写的。 代表协议,类型可以是tcp、udp、icmp或者all,特别是要注意的是我们平时使用的ping命令 其实是icmp协议,而这个协议比较特殊,不使用端口 所以在使用all的时候(包含icmp)同时设置端口可能会出错!
INPUT要有源,OUTPUT要有目标,但是INPUT往往是限制目标端口,OUTPUT往往是限制源端口,注意逻辑不要乱。
iptables -A INPUT -p tcp --dport 80 -j ACCEPT # web服务器添加80服务
iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 3306 -j ACCEPT 允许192.168.1.x网段访问mysql数据库
iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT # 1代表了第一位顺序 也就是最优先的匹配规则

有多条规则匹配一次访问的时候,以最前面的规则为准,当无法匹配到对应规则的时候,使用默认规则
开机自启动
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.

  1. 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
  2. Test the anchor file
    Parse and test your anchor file to make sure there are no errors:
    sudo pfctl -vnf
  3. 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.
  4. 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 -ef on 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
  5. 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

文章目录
  1. 1. iptables
  2. 2. mac下, 以前用的是ipsw, 现在用的是pf