[分享]献给根据“iptables入门教程--设置静态防火墙”设置不成功的新手(8.04版)

Web、Mail、Ftp、DNS、Proxy、VPN、Samba、LDAP 等基础网络服务
回复
xjm1285
帖子: 15
注册时间: 2008-04-11 18:20

[分享]献给根据“iptables入门教程--设置静态防火墙”设置不成功的新手(8.04版)

#1

帖子 xjm1285 » 2008-04-17 13:11

本人刚用linux不到一月,看了“iptables入门教程--设置静态防火墙”(viewtopic.php?t=90255&highlight=iptables后试了一下,发现总是语法错误,后来自己修改了几处地方的符号就行了,估计新版的iptables有些符号功能改变了吧
原帖: for Port in "$Open_ports" ;do
这行要把"$Open_ports"的引号去掉 改为
for Port in $Open_ports ; do

还有 iptables -A INPUT -i ppp0 -p tcp -dport $Port -j ACCEPT 中“-dport $Port ”改为“--dport $Port
当然不止这行要改,所有这个位置的符号都要改。
类出该过后代码:
#!/bin/bash

# This is a script

# Edit by liwei

# establish a static firewall



# define const here

Open_ports="80 25 110 10" # 自己机器对外开放的端口,不是服务器的可以关掉,我自己的机器关掉了这一行(行首加#)

Allow_ports="53 80 20 21" # internet的数据可以进入自己机器的端口

#init

iptables -F

iptables -X

iptables -t nat -F

iptables -t nat -X

# The follow is comment , for make it better
# iptables -P INPUT DROP #我建议开启这行,不然还是有很多端口开放的

iptables -A INPUT -i ! ppp0 -j ACCEPT

# define ruler so that some data can come in.

for Port in $Allow_ports ; do
iptables -A INPUT -i ppp0 -p tcp --sport $Port -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --sport $Port -j ACCEPT
done

for Port in $Open_ports ; do
iptables -A INPUT -i ppp0 -p tcp --dport $Port -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --dport $Port -j ACCEPT
done

# This is the last ruler , it can make you firewall better
iptables -A INPUT -i ppp0 -p tcp -j REJECT --reject-with tcp-reset #建议关闭这两行
iptables -A INPUT -i ppp0 -p udp -j REJECT --reject-with icmp-port-unreachable #建议关闭这两行
希望对跟我一样的linux新手起到一些帮助作用
上次由 xjm1285 在 2008-04-17 14:26,总共编辑 2 次。
头像
tigerdoo
帖子: 327
注册时间: 2006-11-17 23:02

#2

帖子 tigerdoo » 2008-04-17 13:38

无效,我启用防火墙后,进入http://www.pcflank.com/test.htm测试,依然提示警告,说我的机器开放了很多端口。

我的规则如下:
#!/bin/bash
# This is a script
# Edit by liwei
# establish a static firewall

# define const here
Open_ports="" # 自己机器对外开放的端口
Allow_ports="53 80" # internet的数据可以进入自己机器的端口

#init
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X

# The follow is comment , for make it better
# iptables -P INPUT DROP

iptables -A INPUT -i ! eth1 -j ACCEPT

# define ruler so that some data can come in.

for Port in $Allow_ports ; do
iptables -A INPUT -i eth1 -p tcp --sport $Port -j ACCEPT
iptables -A INPUT -i eth1 -p udp --sport $Port -j ACCEPT
done

for Port in $Open_ports ; do
iptables -A INPUT -i eth1 -p tcp --dport $Port -j ACCEPT
iptables -A INPUT -i eth1 -p udp --dport $Port -j ACCEPT
done

# This is the last ruler , it can make you firewall better
iptables -A INPUT -i eth1 -p tcp -j REJECT --reject-with tcp-reset
iptables -A INPUT -i eth1 -p udp -j REJECT --reject-with icmp-port-unreachable
IBM T41-P4M 1.5G/1GB/40GB HDD/DVD/14.1TFT/IPW2100 & 10-100M
OS:Xubuntu 11.10
生活就是折腾
活在成都
使用update-rc.d管理Linux服务
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#3

帖子 BigSnake.NET » 2008-04-17 13:42

tigerdoo 写了:无效,我启用防火墙后,进入http://www.pcflank.com/test.htm测试,依然提示警告,说我的机器开放了很多端口。

我的规则如下:
#!/bin/bash
# This is a script
# Edit by liwei
# establish a static firewall

# define const here
Open_ports="" # 自己机器对外开放的端口
Allow_ports="53 80" # internet的数据可以进入自己机器的端口

#init
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X

# The follow is comment , for make it better
# iptables -P INPUT DROP

iptables -A INPUT -i ! eth1 -j ACCEPT

# define ruler so that some data can come in.

for Port in $Allow_ports ; do
iptables -A INPUT -i eth1 -p tcp --sport $Port -j ACCEPT
iptables -A INPUT -i eth1 -p udp --sport $Port -j ACCEPT
done

for Port in $Open_ports ; do
iptables -A INPUT -i eth1 -p tcp --dport $Port -j ACCEPT
iptables -A INPUT -i eth1 -p udp --dport $Port -j ACCEPT
done

# This is the last ruler , it can make you firewall better
iptables -A INPUT -i eth1 -p tcp -j REJECT --reject-with tcp-reset
iptables -A INPUT -i eth1 -p udp -j REJECT --reject-with icmp-port-unreachable
局域网, 代理?
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
xjm1285
帖子: 15
注册时间: 2008-04-11 18:20

#4

帖子 xjm1285 » 2008-04-17 13:47

你这行“iptables -A INPUT -i ! eth1 -j ACCEPT”意思是接受所有的来源不是网络接口eth1的数据,当然开放了很多接口了,改成ppp0(根据你用的网络接口而定)
xjm1285
帖子: 15
注册时间: 2008-04-11 18:20

#5

帖子 xjm1285 » 2008-04-17 14:06

# iptables -P INPUT DROP 这行开了(把#去掉)
然后把
iptables -A INPUT -i eth1 -p tcp -j REJECT --reject-with tcp-reset
iptables -A INPUT -i eth1 -p udp -j REJECT --reject-with icmp-port-unreachable
这两行屏蔽掉就行了
头像
tigerdoo
帖子: 327
注册时间: 2006-11-17 23:02

#6

帖子 tigerdoo » 2008-04-17 19:25

xjm1285 写了:# iptables -P INPUT DROP 这行开了(把#去掉)
然后把
iptables -A INPUT -i eth1 -p tcp -j REJECT --reject-with tcp-reset
iptables -A INPUT -i eth1 -p udp -j REJECT --reject-with icmp-port-unreachable
这两行屏蔽掉就行了
不行,问题依旧。始终提示23, 137, 138, 1080, 3128,27374, 12345, 1243, 31337, 12348.这几个端口为开放状态。
我将iptables -P INPUT DROP启用,并将iptables -A INPUT -i eth1 -j ACCEPT屏蔽了。问题依旧。
另:我是在局域网中上网
IBM T41-P4M 1.5G/1GB/40GB HDD/DVD/14.1TFT/IPW2100 & 10-100M
OS:Xubuntu 11.10
生活就是折腾
活在成都
使用update-rc.d管理Linux服务
xjm1285
帖子: 15
注册时间: 2008-04-11 18:20

#7

帖子 xjm1285 » 2008-04-17 20:44

我宽带上网的代码是:
Allow_ports="53 80 21"

iptables -F

iptables -X

iptables -t nat -F

iptables -t nat -X

iptables -P INPUT DROP

for Port in $Allow_ports ; do
iptables -A INPUT -i ppp0 -p tcp --sport $Port -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --sport $Port -j ACCEPT
done
测试过很安全
你是局域网共享上网吗?
头像
tigerdoo
帖子: 327
注册时间: 2006-11-17 23:02

#8

帖子 tigerdoo » 2008-04-17 20:52

另外,现在iptables不支持-p参数的协议列表吗?
比如写成这样
iptables -A INPUT -p tcp,udp

再就是当源端口与目的端口相同时,不能写成--port吗?

以上两种写法我在redhat里面一直在用,不过是早期的版本了(好象是iptables1.2)
上次由 tigerdoo 在 2008-04-17 20:57,总共编辑 1 次。
IBM T41-P4M 1.5G/1GB/40GB HDD/DVD/14.1TFT/IPW2100 & 10-100M
OS:Xubuntu 11.10
生活就是折腾
活在成都
使用update-rc.d管理Linux服务
xjm1285
帖子: 15
注册时间: 2008-04-11 18:20

#9

帖子 xjm1285 » 2008-04-17 20:53

tigerdoo 写了:
xjm1285 写了:# iptables -P INPUT DROP 这行开了(把#去掉)
然后把
iptables -A INPUT -i eth1 -p tcp -j REJECT --reject-with tcp-reset
iptables -A INPUT -i eth1 -p udp -j REJECT --reject-with icmp-port-unreachable
这两行屏蔽掉就行了
不行,问题依旧。始终提示23, 137, 138, 1080, 3128,27374, 12345, 1243, 31337, 12348.这几个端口为开放状态。
我将iptables -P INPUT DROP启用,并将iptables -A INPUT -i eth1 -j ACCEPT屏蔽了。问题依旧。
另:我是在局域网中上网
你可以用下面的代码试试
Allow_ports="53 80 21" #哪些端口自己看着办

iptables -F

iptables -X

iptables -t nat -F

iptables -t nat -X

iptables -P INPUT DROP

for Port in $Allow_ports ; do
iptables -A INPUT -i eth1 -p tcp --sport $Port -j ACCEPT
iptables -A INPUT -i eth1 -p udp --sport $Port -j ACCEPT
done
头像
tigerdoo
帖子: 327
注册时间: 2006-11-17 23:02

#10

帖子 tigerdoo » 2008-04-17 20:54

xjm1285 写了:我宽带上网的代码是:
Allow_ports="53 80 21"

iptables -F

iptables -X

iptables -t nat -F

iptables -t nat -X

iptables -P INPUT DROP

for Port in $Allow_ports ; do
iptables -A INPUT -i ppp0 -p tcp --sport $Port -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --sport $Port -j ACCEPT
done
测试过很安全
你是局域网共享上网吗?
是,通过路由器上网的。莫非它检测的实际是路由器的设置?
IBM T41-P4M 1.5G/1GB/40GB HDD/DVD/14.1TFT/IPW2100 & 10-100M
OS:Xubuntu 11.10
生活就是折腾
活在成都
使用update-rc.d管理Linux服务
xjm1285
帖子: 15
注册时间: 2008-04-11 18:20

#11

帖子 xjm1285 » 2008-04-17 21:02

tigerdoo 写了:另外,现在iptables不支持-p参数的协议列表吗?
比如写成这样
iptables -A INPUT -p tcp,udp

再就是当源端口与目的端口相同时,不能写成--port吗?

以上两种写法我在redhat里面一直在用,不过是早期的版本了(好象是iptables1.2)
我也不是很清楚这个,如果英文好具体细节你可以去iptablesde官方网站看看
xjm1285
帖子: 15
注册时间: 2008-04-11 18:20

#12

帖子 xjm1285 » 2008-04-17 21:15

tigerdoo 写了:
xjm1285 写了:我宽带上网的代码是:
Allow_ports="53 80 21"

iptables -F

iptables -X

iptables -t nat -F

iptables -t nat -X

iptables -P INPUT DROP

for Port in $Allow_ports ; do
iptables -A INPUT -i ppp0 -p tcp --sport $Port -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --sport $Port -j ACCEPT
done
测试过很安全
你是局域网共享上网吗?
是,通过路由器上网的。莫非它检测的实际是路由器的设置?
iptables中有个NAT功能好象是关于路由的,但是我不清楚如何设置 :cry:
头像
hcym
帖子: 15634
注册时间: 2007-05-06 2:46

#13

帖子 hcym » 2008-04-17 22:03

:lol:

顶就一个字,我只说一遍

:em42
回复