ubuntu iptables

1. Introduction to iptables

iptables is complex and it is integrated into the linux kernel. Through iptables, users can filter packets entering and leaving your computer. Set your rules through the iptables command to guard your computer network - which data is allowed to pass, which cannot pass, and which data is logged (log). Next, I'll show you how to set up your own rules, so start now.

 

2. Initialization work

At the shell prompt # type

iptables -F

iptables -X

iptables -t nat -F

iptables -t nat -X

Each of the above commands has its exact meaning. Generally, before setting up your iptables, you must first clear all previously set rules, which we call initialization. Although in many cases it does nothing, but to be on the safe side, be careful! If you are using redhat or fedora, then you have an easier way

service iptables stop

 

3. Start setting the rules:

Now let's start setting up your rules

iptables -P INPUT DROP

This one command will build you a very "secure" firewall, I can't imagine any hacker that would be able to break into such a machine, since it drops all the data coming into your machine from the network. This is of course over-secure, and your machine will be equivalent to no network at this point. If you ping localhost, you'll find that the screen stays there because the ping doesn't get any response.

 

4. Add rules

Then continue to enter the command above:

iptables -A INPUT -i ! ppp0 -j ACCEPT

This rule means: accept all data from sources other than network interface ppp0.

We assume that you have two network interfaces, eth0 is connected to the LAN, and loop is the loopback network (localhost). ppp0 is the general internet network interface for adsl to access the Internet. If you do not use this way of accessing the Internet, it may be eth1. Here I assume you are using adsl to surf the Internet and your internet interface is ppp0

At this point, you have allowed access to the local area network, and you can also access localhost

此时再输入命令 ping localhost,结果还会和刚才一样吗?

到此我们还不能访问www,也不能mail,接着看吧。

 

5、我想访问www

iptables -A INPUT -i ppp0 -p tcp -sport 80 -j ACCEPT

允许来自网络接口ppp0(internet接口),并且来源端口是80的数据进入你的计算机。

80端口正是www服务所使用的端口。

好了,现在可以看网页了。但是,你能看到吗?

如果你在浏览器的地址中输入www.baidu.com,能看到网页吗?

你得到的结果一定是:找不到主机www.baidu.com

但是,如果你再输入220.181.27.5,你仍然能够访问baidu的网页。

为什么?如果你了解dns的话就一定知道原因了。

因为如果你打入www.baidu.com,你的电脑无法取得www.baidu.com这个名称所能应的ip地址220.181.27.5。如 果你确实记得这个ip,那么你仍然能够访问www,你当然可以只用ip来访问www,如果你想挑战你的记忆的话^ _ ^,当然,我们要打开DNS。

 

6、打开dns端口

打开你的dns端口,输入如下命令:

iptables -A INPUT -i ppp0 -p udp -sport 53 -j ACCEPT

这条命令的含义是,接受所有来自网络接口ppp0,upd协议的53端口的数据。53也就是著名的dns端口。

此时测试一下,你能通过主机名称访问www吗?你能通过ip访问www吗?

当然,都可以!

 

7、查看防火墙

此时可以查看你的防火墙了

iptables -L

如果你只想访问www,那么就可以到此为止,你将只能访问www了。 不过先别急,将上面讲的内容总结一下,写成一个脚本。

#!/bin/bash

# This is a script

# Edit by liwei

# establish static firewall

iptables -F

iptables -X

iptables -t nat -F

iptables -t nat -X

iptables -P INPUT DROP

iptables -A INPUT -i ! ppp0 -j ACCEPT

iptables -A INPUT -i ppp0 -p tcp –sport 80 -j ACCEPT

iptables -A INPUT -i ppp0 -p udp –sport 53 -j ACCEPT

 

8、复杂吗?到此iptables可以 按你的要求进行 包过滤了。你可以再设定一些端口,允许你的机器访问这些端口。这样有可能,你不能访问QQ,也可能不能打网络游戏,是好是坏,还是要看你自己而定了。顺便 说一下,QQ这个东西还真是不好控制,用户与服务器连接使用的好像是8888端口,而QQ上好友互发消息使用的又是udp的4444端口(具体是不是 4444还不太清楚)。而且QQ还可以使用www的80端口进行登录并发消息,看来学无止境,你真的想把这个家伙控制住还不容易呢?还是进入我们的正题 吧。

如果你的机器是服务器,怎么办?

 

9、如果不巧你的机器是服务器,并且要提供www服务。显然,以上的脚本就不能符合我们的要求了。但只要你撑握了规则,稍作修改同样也能很好的工作。在最后面加上一句

iptables -A INPUT -i ppp0 -p tcp –dport 80 -j ACCEPT

这一句也就是将自己机器上的80端口对外开放了,这样internet上的其他人就能访问你的www了。当然,你的www服务器得工作才行。如果 你的机器同时是smtp和pop3服务器,同样的再加上两条语句,将–dport后面的80改成25和110就行了。如果你还有一个ftp服务器,呵 呵,如果你要打开100个端口呢……

我们的工作好像是重复性的打入类似的语句,你可能自己也想到了,我可以用一个循环语句来完成,对,此处可以有效的利用shell脚本的功能,也让你体验到了shell脚本语言的威力。看下文:

 

10、用脚本简化你的工作,阅读下面的脚本

#!/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

iptables -P INPUT DROP #we can use another method to instead it

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

这个脚本有三个部分(最前面的一段是注释,不算在这三部分中)

第一部分是定义一些端口:访问你的机器"Open_ports"端口的数据,允许进入;来源是"Allow_ports"端口的数据,也能够进入。

第二部分是iptables的初始化,第三部分是对定义的端口具体的操作。

如果以后我们的要求发生了一些变化,比如,你给自己的机器加上了一个ftp服务器,那么只要在第一部分"Open_ports"的定义中,将ftp对应的20与21端口加上去就行了。呵呵,到此你也一定体会到了脚本功能的强大的伸缩性,但脚本的能力还远不止这些呢!

 

11、使你的防火墙更加完善

看上面的脚本init部分的倒数第二句

iptables -P INPUT DROP

这是给防火墙设置默认规则。当进入我们计算机的数据,不匹配我们的任何一个条件时,那么就由默认规则来处理这个数据—-drop掉,不给发送方任何应答。

也就是说,如果你从internet另外的一台计算机上ping你的主机的话,ping会一直停在那里,没有回应。

如果黑客用namp工具对你的电脑进行端口扫描,那么它会提示黑客,你的计算机处于防火墙的保护之中。我可不想让黑客对我的计算机了解太多,怎么办,如果我们把drop改成其他的动作,或许能够骗过这位刚出道的黑客呢。

怎么改呢?将刚才的那一句( iptables -P INPUT DROP )去掉,在脚本的最后面加上

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

这样就好多了,黑客虽然能扫描出我们所开放的端口,但是他却很难知道,我们的机器处在防火墙的保护之中。如果你只运行了ftp并且仅仅对局域网内 部访问, 他很难知道你是否运行了ftp。在此我们给不应该进入我们机器的数据,一个欺骗性的回答,而不是丢弃(drop)后就不再理会。这一个功能,在我们设计有 状态的防火墙中(我这里讲的是静态的防火墙)特别有用。

 

#!/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

ptables -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

 

12、端口转发

打开转发开关

要让iptables的端口转发生效,首先需要打开转发开关

方法一:临时打开,重启后失效

$sudo su

#echo 1 >/proc/sys/net/ipv4/ip_forward

 

方法二:永久打开,重启依然有效

编辑/etc/sysctl.conf文件,将net.ipv4.ip_forward=1前面的#注释去掉,保存文件,然后执行sudo sysctl -p使其生效

 

典型使用场景举例

场景一:目标机的22端口外网没有打开,通过本地端口转发实现通过其他端口访问ssh的22端口

案例:125.69.67.213机器的22端口未对外开放,但开放了3000~4000之间的端口,因此通过3000端口转发到22实现ssh登录

sudo iptables -t nat -A PREROUTING -p tcp -i eth0 -d 125.69.67.213 --dport 3000 -j DNAT --to 125.69.67.213:22

这个属于本机端A端口转发到本机的B端口

 

场景二:将内网的22端口映射到外网的一个端口,实现SSH直接登录,不用跳转

案例:192.168.2.61为外网机,192.168.2.70为内网机,如果不做映射,需要先登录到61,再登录到70.做如下映射之后,可直接通过外网机的3003登录到内网机

sudo iptables -t nat -A PREROUTING -d 192.168.2.61 -p tcp --dport 3003 -j DNAT --to-destination 192.168.2.70:22

sudo iptables -t nat -A POSTROUTING -d 192.168.2.70 -p tcp --dport 22 -j SNAT --to 192.168.2.61

注:(1) 本例中也可以通过SecureCRT的自动登录实现。

 

场景三:在外网直接访问内网的MySQL数据库

案例:很多时候数据库在内网机,外网不能直接访问,但做运维的时候可能需要通过图形界面工具直接连上去。做端口映射就可以解决这个问题。例如:将外网机192.168.2.61的3001端口转发到内网机192.168.2.70的MySQL的3306端口

sudo iptables -t nat -A PREROUTING -d 192.168.2.61 -p tcp --dport 3001 -j DNAT --to-destination 192.168.2.70:3306

sudo iptables -t nat -A POSTROUTING -d 192.168.2.70 -p tcp --dport 3306 -j SNAT --to 192.168.2.61

 

iptables其他常见操作

查看当前iptables的所有规则

sudo iptables -L

或者

sudo iptables-save

 

iptables规则保存到文件

sudo sh -c "iptables-save > /etc/iptables.rules"

 

从文件恢复iptables的规则

sudo iptables-restore /etc/iptables.rules

 

开机启动加载iptables规则

注:配置的规则系统默认重启后就失效,因此做开机启动时加载iptables的配置也有必要。

在/etc/network/interfaces的末尾添加如下一行: 

pre-up iptables-restore < /etc/iptables.rules

 

如果想在关机的时候自动保存修改过的iptables规则,可添加如下行

post-down iptables-save > /etc/iptables.up.rules

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326435884&siteId=291194637