Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3098972
  • 博文数量: 797
  • 博客积分: 10134
  • 博客等级: 上将
  • 技术积分: 9335
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-22 22:57
个人简介

1

文章分类

全部博文(797)

文章存档

2022年(1)

2021年(2)

2017年(2)

2016年(1)

2015年(4)

2014年(1)

2013年(6)

2012年(6)

2011年(10)

2010年(26)

2009年(63)

2008年(61)

2007年(51)

2006年(563)

我的朋友

分类: LINUX

2006-07-23 01:27:58

作者:
 
 
经常看到一些网络中流传的 iptables 脚本在开头的位置大多是如下内容

CODE:
#!/bin/sh
#
modprobe ipt_MASQUERADE
modprobe ip_conntrack_ftp
modprobe  ip_nat_ftp
iptables -F
iptables -t nat -F
iptables -X
iptables -t nat -X
iptables -P INPUT DROP

摘自
有的是这样的

CODE:
# 2.0 载入模组
PATH=/sbin:/bin:/usr/sbin:/usr/bin
export PATH EXTIF INIF INNET
modprobe ip_tables > /dev/null 2>&1
modprobe iptable_nat > /dev/null 2>&1
modprobe ip_nat_ftp > /dev/null 2>&1
modprobe ip_nat_irc > /dev/null 2>&1
modprobe ip_conntrack > /dev/null 2>&1
modprobe ip_conntrack_ftp > /dev/null 2>&1
modprobe ip_conntrack_irc > /dev/null 2>&1

摘自
网上流传的东西未必就都是正确的,针对上面一些误区,我来做一下详细说明
(上面第二个出自鸟哥的脚本,我们只讨论技术,没有得罪的意思 ^_^)



1、modprobe ip_tables
当 iptables 对 filter、nat、mangle 任意一个表进行操作的时候,会自动加载 ip_tables 模块
另外,iptable_filter、iptable_nat、iptable_mangle 模块也会自动加载,情形例如

CODE:
# lsmod
Module                  Size  Used by    Not tainted
iptable_mangle          2136   0  (autoclean) (unused)
iptable_filter          1708   0  (autoclean) (unused)
ip_tables              12832   2  [iptable_mangle iptable_filter]
8139too                13704   1
mii                     2544   0  [8139too]
reiserfs              183376   2  (autoclean)
raid1                  13068   1  (autoclean)
md                     44480   2  [raid1]
因此,脚本里不用写 modprobe ip_tables



2、modprobe ip_conntrack
ip_conntrack 是状态检测机制,state 模块要用到
当 iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 时,ip_conntrack 自动加载,例如

CODE:
# lsmod
Module                  Size  Used by    Not tainted
ipt_state                536   1  (autoclean)
ip_conntrack           25096   0  (autoclean) [ipt_state]
iptable_filter          1708   1  (autoclean)
ip_tables              12832   2  [ipt_state iptable_filter]
8139too                13704   1
mii                     2544   0  [8139too]
reiserfs              183376   2  (autoclean)
raid1                  13068   1  (autoclean)
md                     44480   2  [raid1]
另外,modprobe ip_conntrack_ftp 时也会自动加载 ip_conntrack

因此,脚本里不用写 modprobe ip_conntrack



3、modprobe ip_conntrack_ftp
ip_conntrack_ftp 是本机做 FTP 时用的
ip_nat_ftp 是通过本机的 FTP 需要用到的(若你的系统不需要路由转发,没必要用这个)
当 modprobe ip_nat_ftp 时,系统自动会加载 ip_conntrack_ftp 模块,例如

CODE:
# lsmod
Module                  Size  Used by    Not tainted
ip_nat_ftp              2736   0  (unused)
iptable_nat            18040   4  [ip_nat_ftp]
ip_tables              12544  12  [iptable_filter iptable_nat]
ip_conntrack_ftp        3856   1
ip_conntrack           20268   3  [ip_nat_ftp iptable_nat ip_conntrack_ftp]
8139too                13704   1
mii                     2544   0  [8139too]
reiserfs              183376   2  (autoclean)
raid1                  13068   1  (autoclean)
md                     44480   2  [raid1]
因此,当需要用到 ip_nat_ftp 时,脚本里不用写 modprbe ip_conntrack 和 modprobe ip_conntrack_ftp



4、iptables -P OUTPUT DROP
除非你明白 filter 中 OUTPUT 链的作用,除非你想限制 Linux 本机上网

否则,不要 iptables -P OUTPUT DROP!



5、先设置规则,还是先设置默认策略
不少脚本都是这样写的

CODE:
iptables -F xxx
iptables -X
iptables -P INPUT DROP
modprobe ip_nat_ftp
之后才是具体规则
iptables -P INPUT DROP 是什么意思?
设置默认规则为 DROP,也就是说如果数据包没有被链中规则匹配,则默认按默认规则处理
试想,假如你在远程调试一个脚本,当前 filter 表的 INPUT 链默认规则是 DROP,iptables -F 后意味着什么?
因此,我建议大家

先设置默认规则为 ACCEPT
然后添加具体链的规则
最后设置默认规则为 DROP
若用基于 RedHat(CentOS) 的发行版,可以用 service iptables stop 来卸载内核中与 iptables 和 netfilter 有关的东西
阅读(1422) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~