免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3414 | 回复: 10
打印 上一主题 下一主题

用iptables写了个防火墙脚本,把主要部分贴上来请大家给看看 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-12-31 16:32 |只看该作者 |倒序浏览
LINUX#vi /etc/init.d/iptablesvc

#!/bin/bash
# 基于伪装(相当于端口转换吧PAT)的仿火墙Do iptables based masquerading
and firewalling.
#modified by atom 12/31/2003
# Set default PATH
export PATH=/sbin:/usr/sbin:/bin:/usr/bin
# Load NAT modules
modprobe iptable_nat
modprobe ip_nat_ftp
modprobe ip_nat_irc
# Load connection-tracking modules
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_conntrack_irc
#the other mod。。。。。。。。。。。
/sbin/modprobe ip_tables
/sbin/modprobe iptable_filter
/sbin/modprobe ipt_log
/sbin/modprobe ipt_limit
/sbin/modprobe ipt_state
# Disable response to broadcasts.
echo 1 >; /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
# Don't accept source routed packets.
echo 0 >; /proc/sys/net/ipv4/conf/all/accept_source_route
# Disable ICMP redirect acceptance.
echo 0 >; /proc/sys/net/ipv4/conf/all/accept_redirects
# Enable bad error message protection
echo 1 >; /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
# Log spoofed packets, source routed packets, redirect packets
echo 1 >; /proc/sys/net/ipv4/conf/all/log_martians
# Turn on IP forwarding
echo 1 >; /proc/sys/net/ipv4/ip_forward

start () {
  # Clean old iptables  
    iptables -F  
    iptables -X  
    iptables -Z
#1.首先设置默认过虑所有包
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
   # 2.允许所有内部网的转发包和状态为ESTABLISHED和RELATED的包
Allow forwarding through the internal interface
   iptables -A FORWARD -i eth1 -j ACCEPT
   iptables -A FORWARD -o eth1 -j ACCEPT  
   iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j
ACCEPT  
# 3防火墙规则Firewall Rules
   # 3.1允许本地的回环Loopback - Allow unlimited traffic
   iptables -A INPUT -i lo -j ACCEPT
   iptables -A OUTPUT -o lo -j ACCEPT
   #3.2允许外网口输入状态为ESTABLISHED和RELATED的
1024以上端口
   iptables -A INPUT -i eth0 -p tcp -s 0/0 --sport 1024:65535 --dport  
1024:65535 -m state --state ESTABLISHED,RELATED -j ACCEPT
   # 3.3 DNS
   # 允许DNS端口(UDP53)Allow UDP packets in for DNS client from
nameservers
   iptables -A INPUT -i eth0 -p udp -s 0/0 --sport 53 -m state --state   
ESTABLISHED -j ACCEPT
   # 3.4允许HTTP/HTTPS端口(TCP80、443)
   # allow all http/https incoming/return connections
   iptables -A INPUT -i eth0 -p tcp -s 0/0 --sport 80 -m state --state   
ESTABLISHED,RELATED -j ACCEPT
   iptables -A INPUT -i eth0 -p tcp -s 0/0 --sport 443 -m state --state   
ESTABLISHED,RELATED -j ACCEPT
   # 允许FTP端口(TCP20,21)
   # 3.5主动FTP模式Enable active ftp transfers  
   iptables -A INPUT -i eth0 -p tcp -s 0/0 --sport 20 -m state --state
ESTABLISHED,RELATED -j ACCEPT
   # 3.6允许返回的ICMP包Allow ICMP in if it is related to other connections
   iptables -A INPUT -i eth0 -p icmp -m state --state  
ESTABLISHED,RELATED -j ACCEPT
   # 3.7允许内网发起的SMTP和POP3包
  iptables  -A   input –i  eth0  –p tcp –s 0/0 –sport 25 –m state --state
ESTABLISHED,RELATED –j ACCEPT
    iptables  -A  input  -i eth0 –p tcp  –s 0/0  --sport 110 –m state --state
ESTABLISHED,RELATED –j ACCEPT
            #4.设置NAT
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j SNAT --to
222.20.xxx.xxx
##或做MAXQUERADE,即PAT,效果应该一样吧?Do masquerading through
eth0
##iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
# 5. 访问控制
        #此功能与Cisco IOS基本上是相似的,也是Firewall的主要部分,如:只允许
访问主机:222.20.16.254的www,端口为:80
iptables –A FORWARD –o eth0 –p TCP –d 222.20.16.254  --dport !80 –j DENY

#6. 静态端口重定向
# 将外部IP地址端口影射到内部的IP和端口Port Forwarding
##iptables -t nat -A PREROUTING -i eth0 -p tcp -m tcp --dport 2222 -j   
DNAT --to-destination 192.168.100.2:22
#!----外部用户直接访问地址222.20.xxx.xxx telnet端口,通过iptables重定向
到内部主机192.168.0.10的telnet端口(23)。
iptables –A INPUT –i eth0 –p tcp –-dport 23 –j ACCEPT
iptables –t nat –A PREROUTING –i eth0 –p tcp –-dport 23 –j DNAT –-to
192.168.0.10
#!----外部用户直接访问地址222.20.xxx.xxx FTP,通过PIX重定向到内部
192.168.0.10的FTP Server。
iptables –A INPUT –i eth0 –p tcp –-dport 20,21 –j ACCEPT
iptables –t nat –A PREROUTING –i eth0 –p tcp –-dport 20,21 –j DNAT –-to
192.168.0.10
            #!----外部用户直接访问地址222.20.xxx.xxx www(即80端口),通过PIX重定
向到内部192.168.0.10的主机的www(即80端口)。
iptables –A INPUT –i eth0 –p tcp –-dport 80 –j ACCEPT
iptables –t nat –A PREROUTING –i eth0 –p tcp –-dport 80 –j DNAT –-to
192.168.0.10
            #!----外部用户直接访问地址222.20.16.201 HTTP(8080端口),通过PIX重定向
到内部192.168.0.10的主机的www(即80端口)。
iptables –A INPUT –i eth0 –p tcp –-dport 80 –j ACCEPT
iptables –t nat –A PREROUTING –i eth0 –p tcp –-dport 8080 –j DNAT –-to
192.168.0.10
iptables –t nat –A PREROUTING –i eth0 –p tcp –-dport 8080 –j REDIRECT –-to
80
            #!----外部用户直接访问地址222.20.xxx.xxx smtp(25端口),通过PIX重定向到
内部192.168.0.10的邮件主机的smtp(即25端口)
iptables –A INPUT –i eth0 –p tcp –-dport 25 –j ACCEPT
iptables –t nat –A PREROUTING –i eth0 –p tcp –-dport 25 –j DNAT –-to
192.168.0.10
}
stop() {
$IPTABLES -F
$IPTABLES -X
$IPTABLES -P INPUT ACCEPT
$IPTABLES -P FORWARD ACCEPT
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -t nat -F
$IPTABLES -t nat -P PREROUTING ACCEPT
$IPTABLES -t nat -P POSTROUTING ACCEPT
}

case "$1" in
        start)
        start
        ;;
        stop)
        stop
        ;;
        restart)
        stop
        start
        ;;
        *)
        echo "usage wrong!"
        ;;
esac

论坛徽章:
0
2 [报告]
发表于 2003-12-31 16:54 |只看该作者

用iptables写了个防火墙脚本,把主要部分贴上来请大家给看看

自己UP一下先

论坛徽章:
7
荣誉版主
日期:2011-11-23 16:44:17子鼠
日期:2014-07-24 15:38:07狮子座
日期:2014-07-24 11:00:54巨蟹座
日期:2014-07-21 19:03:10双子座
日期:2014-05-22 12:00:09卯兔
日期:2014-05-08 19:43:17卯兔
日期:2014-08-22 13:39:09
3 [报告]
发表于 2004-01-01 14:58 |只看该作者

用iptables写了个防火墙脚本,把主要部分贴上来请大家给看看

如果再把网络拓扑图贴出来就更好了!
BTW:
#3.2允许外网口输入状态为ESTABLISHED和RELATED的
1024以上端口
iptables -A INPUT -i eth0 -p tcp -s 0/0 --sport 1024:65535 --dport
1024:65535 -m state --state ESTABLISHED,RELATED -j ACCEPT
什么情况这一段才能用到?

论坛徽章:
0
4 [报告]
发表于 2004-01-01 16:56 |只看该作者

用iptables写了个防火墙脚本,把主要部分贴上来请大家给看看

我想是这样:
  当连接是由内网用户发起的,回来的包状态为ESTABLISHED或RELATED;

论坛徽章:
0
5 [报告]
发表于 2004-01-01 17:00 |只看该作者

用iptables写了个防火墙脚本,把主要部分贴上来请大家给看看

把分析过程贴一下:

1.基础
1.1iptables简介
        iptables [-t table] command [chain] [match] [target/jump]
-t table: nat、filter(默认)、mangle
command:  -A -D -P -F -L
chain:    INPUT OUTPUT FORWARD PREROUTING POSTROUTING
match:    -p <tcp|udp|icmp>; -s <IP>; -d <IP>; --sport <port>; --dport <port>;
          -i <interface>; -o <interface>;
-j target:ACCEPT DROP REJECT LOG SNAT --to <ip>; DNAT --to <ip>;
          REDIRECT --to <ORT>;
option:   -v -n --modporbe=<command>;
            对于这个句法没什么可说的,但注意target指令必须在最后。为了易读,我们一般用这种语法。总之,你将见到的大部分规则都是按这种语法写的。因此,如果你看到别人写的规则,你很可能会发现用的也是这种语法,当然就很容易理解那些规则了。
            如果你不想用标准的表,就要在[table]处指定表名。一般情况下没有必要指定使用的表,因为iptables 默认使用filter表来执行所有的命令。也没有必要非得在这里指定表名,实际上几乎可在规则的任何地方。当然,把表名在开始处已经是约定俗成的标准。 尽管命令总是放在开头,或者是直接放在表名后面,我们也要考虑考虑到底放在哪儿易读。
            command告诉程序该做什么,比如:插入一个规则,还是在链的末尾增加一个规则,还是删除一个规则,下面会仔细地介绍。
            match细致地描述了包的某个特点,以使这个包区别于其它所有的包。在这里,我们可以指定包的来源IP 地址,网络接口,端口,协议类型,或者其他什么。下面我们将会看到许多不同的match。
            最后是数据包的目标所在。若数据包符合所有的match,内核就用target来处理它,或者说把包发往 target。比如,我们可以让内核把包发送到当前表中的其他链(可能是我们自己建立的),或者只是丢弃这个包而没有什么处理,或者向发送者返回某个特殊的应答。下面有详细的讨论。
---------------------------------------------------------------------------
1.2table说明
            nat nat表的主要用处是网络地址转换,即Network Address Translation,缩写为NAT。做过NAT操作的数据包的地址就被改变了,当然这种改变是根据我们的规则进行的。属于一个流的包只会经过这个表一次。如果第一个包被允许做NAT或Masqueraded,那么余下的包都会自动地被做相同的操作。也就是说,余下的包不会再通过这个表,一个一个的被NAT,而是自动地完成。这就是我们为什么不应该在这个表中做任何过滤的主要原因。
    PREROUTING 链的作用是在包刚刚到达防火墙时改变它的目的地址,如果需要的话。      
    OUTPUT链改变本地产生的包的目的地址。
    POSTROUTING链在包就要离开防火墙之前改变其源地址。
    对NAT表的操作(即-j target)主要是对内网包(即POSTROUTING链)改变源IP(SNAT)、对外网包(即PREROUTING链)改变目的地址(DNAT)或目的端口(REDIRECT)。
            filter表是专门过滤包的,内建三个链,可以毫无问题地对包进行DROP、LOG、ACCEPT和REJECT等操作。
    FORWARD链过滤所有不是本地产生的并且目的地不是本地(所谓本地就是防火墙了)的包
    INPUT链恰恰针对那些目的地是本地的包。
    OUTPUT链用来过滤所有本地生成的包的。
-----------------------------------------------------------------------------
1.3四种有效状态,名称分别为
            ESTABLISHED、INVALID、NEW 和 RELATED。
            状态 ESTABLISHED 指出该信息包属于已建立的连接,该连接一直用于发送和接收信息包并且完全有效。
            INVALID 状态指出该信息包与任何已知的流或连接都不相关联,它可能包含错误的数据或头。
            状态 NEW 意味着该信息包已经或将启动新的连接,或者它与尚未用于发送和接收信息包的连接相关联。
            最后,RELATED 表示该信息包正在启动新连接,以及它与已建立的连接相关联。
-----------------------------------------------------------------------------
1.4几个例子
            Example iptables -A INPUT -d 192.168.1.1
            Explanation 以IP目的地址匹配包。地址的形式和 -- source完全一样。
            Match -i, --in-interface
            Example iptables -A INPUT -i eth0
            Explanation 以包进入本地所使用的网络接口来匹配包。
            注意:这个匹配操作只能用于INPUT,FORWARD和 PREROUTING这三个链,用在其他任何地方都会提示错误信息。
------------------------------------------------------------------------------
1.5防火墙规则分析
1。5。1与PIX防火墙的不同
1。5。2往来包的分析
内部发起的连接:先到内卡口(FORWARD链应ACCEPT),
                SNAT修改源IP,
                到达外卡口(FORWARD链应允许内网向外的转发包);
                返回的包到达外网口(INPUT状态为ESTABLISHED和RELATED的包应接受);
                DNAT修改目的IP或端口;
                到达内网口(FORWARD链打开)
外部发起的连接:先到外卡口(INPUT链打开要开放的端口),
                DNAT修改目的IP或端口,
                到达内卡口(FORWARD链打开)
                返回的包到达内网口(FORWARD链ESTABLISHED和RELATED的包)
                SNAT修改源IP;
                到达外卡口(FORWARD链ESTABLISHED和RELATED的包打开)
本身的回环连接:应ACCEPT local接口的INPUT,OUTPUT链的全部包;
对于内网接口:  应打开FORWARD链-i -o两个方向;
对于外网接口:  应打开INPUT链相应端口;
                FORWARD链的端口1024以上的(状态为ESTABLISHED和RELATED)的包;

论坛徽章:
0
6 [报告]
发表于 2004-01-01 20:16 |只看该作者

用iptables写了个防火墙脚本,把主要部分贴上来请大家给看看

所有使用iptables的同仁们啊!
强烈建议你们到iptables的官方站(www.netfilter.org)下载Linux内核补丁并升级iptables到1.2.9。
肯定会发现无数精彩绝伦的功能!!
比如:
指定某个IP的并发连接数
指定某个规则的有效时间段
指定某个规则的平均匹配几率
……

论坛徽章:
7
荣誉版主
日期:2011-11-23 16:44:17子鼠
日期:2014-07-24 15:38:07狮子座
日期:2014-07-24 11:00:54巨蟹座
日期:2014-07-21 19:03:10双子座
日期:2014-05-22 12:00:09卯兔
日期:2014-05-08 19:43:17卯兔
日期:2014-08-22 13:39:09
7 [报告]
发表于 2004-01-01 23:33 |只看该作者

用iptables写了个防火墙脚本,把主要部分贴上来请大家给看看

To: atom
请恕我直言(纯技术方面的讨论),楼主对iptables的理解有许多错误的地方,这篇文章 http://iptables-tutorial.frozentux.net/iptables-tutorial.html 对iptables的工作原理有相当详细的介绍,值得深入钻研。
楼主对iptables的语法相当娴熟,只是对packets到达firewall后的处理过程的理解有偏差。仅举一例,由内网经SNAT转换到达外网及外网响应的包,是不经过 filter表中的INPUT和OUTPUT链的。
本贴只是涉及技术探讨,希望楼主不要误解,有不对之处欢迎斧正。

论坛徽章:
0
8 [报告]
发表于 2004-01-02 00:09 |只看该作者

用iptables写了个防火墙脚本,把主要部分贴上来请大家给看看

非常感谢,只看了1。19的中文翻译,因为懒;
现在去看原版的。。。

论坛徽章:
0
9 [报告]
发表于 2004-01-02 00:24 |只看该作者

用iptables写了个防火墙脚本,把主要部分贴上来请大家给看看

To clarify this image, consider this. If we get a packet into the first routing decision that is not destined for the local machine itself, it will be routed through the FORWARD chain. If the packet is, on the other hand, destined for an IP address that the local machine is listening to, we would send the packet through the INPUT chain and to the local machine.

Also worth a note, is the fact that packets may be destined for the local machine, but the destination address may be changed within the PREROUTING chain by doing NAT. Since this takes place before the first routing decision, the packet will be looked upon after this change. Because of this, the routing may be changed before the routing decision is done. Do note, that all packets will be going through one or the other path in this image. If you DNAT a packet back to the same network that it came from, it will still travel through the rest of the chains until it is back out on the network.

果然如此,感谢上二层的朋友;
接着把这篇看完

tables_traverse.jpg (31.67 KB, 下载次数: 80)

tables_traverse.jpg

论坛徽章:
0
10 [报告]
发表于 2004-01-02 01:54 |只看该作者

用iptables写了个防火墙脚本,把主要部分贴上来请大家给看看

好,GO ON
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP