linux iptables:安全应用,防火墙

iptables:安全应用,防火墙
windows和linux都有防火墙,企业的边缘会部署防火墙保证企业内部的局域网是安全的。针对个人电脑会有防火墙保证系统是安全的。

防火墙是唯一通道。

防火墙分类(这里的分类只是一个简单的分类,还有很多种):
1.包过滤防火墙,速度快,但是只检查包头,不检查数据区,不建立连接状态表,安全性能低。
2.应用代理防火墙。安全高,性能高。只检查包头,不检查数据区,建立连接状态表。

防火墙还可以分为硬件和软件防火墙。硬件防火墙就是一个硬件设备,软件防火墙比如windows的360和linux的开源的iptables。

iptableslinux自带的小型的防火墙。

 

table有4张表,filter过滤表,写的是防火墙的策略。nat表做网络相关的。mangle和raw是内核部分,不用管。
command:过滤表的策略。不允许哪个网卡进去或者出来,
match:匹配条件。匹配数据包来做。
target:匹配的数据包进行操作。就2种可能性,通过和不通过。如果没问题就通过,有问题就不通过。
表
filter表:针对本机,itables的默认表有:input,forward,output链。
nat表:转换数据包的源地址和目的地址。包括:prerouting,postrouting,out链。

https://www.linuxidc.com/Linux/2017-02/140556.htm
以下是centos的:
(ubuntu没有这些命令)
service iptables stop
service iptables start
service iptables restart
root@ubuntu:/# iptables -L  (查看iptables防火墙的默认策略)
Chain INPUT (policy DROP)
target     prot opt source               destination         
ufw-before-logging-input  all  --  anywhere             anywhere            
ufw-before-input  all  --  anywhere             anywhere            
ufw-after-input  all  --  anywhere             anywhere            
ufw-after-logging-input  all  --  anywhere             anywhere            
ufw-reject-input  all  --  anywhere             anywhere            
ufw-track-input  all  --  anywhere             anywhere            
Chain FORWARD (policy DROP)
target     prot opt source               destination         
DOCKER-ISOLATION  all  --  anywhere             anywhere            
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere    
以下是centos的:
vim sysconfig/iptables :防火墙主配置文件,这里面存储的都是防火墙的策略,如果要添加策略,建议通过命令行来添加,而不是修改这个文件来添加。
*filter  : 表示是一个过滤表
:INPUT  ACCEPT  [0:0]   :这3个链都是开的
:FORWARD  ACCEPT  [0:0]
:OUTPUT  ACCEPT  [0:0]
: RH_Firewall-1-INPUT - [0:0]   : RH_Firewall-1-INPUT是防火墙当前策略的一个名字
-A INPUT -j RH_Firewall-1-INPUT
-A FORWARD -j RH_Firewall-1-INPUT
-A RH_Firewall-1-INPUT -p lo -j ACCEPT  (lo同意)
-A RH_Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT  (icmp同意)
-A RH_Firewall-1-INPUT -p 50 -j ACCEPT  (50.51端口可以访问)
-A RH_Firewall-1-INPUT -p 51 -j ACCEPT
-A RH_Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0251 -j ACCEPT  (udp可以访问)
写策略:
查看规则-L:iptables -L ,iptables  -L -n ,iptables -L -n --line-number
修改默认规则-P:iptables -P INPUT DROP (备注:为链设置默认的target可用的是DROP和ACCEPT,作为最后一条规则被执行)
追加规则-A:作为最后一条规则,iptables -A INPUT -p tcp --dport 22 -j ACCEPT
删除规则-D:iptables -D INPUT 3 ,iptables -D INPUT -s 192.168.0.1 DROP
修改规则-R:iptables -R INPUT 3 -s 192.168.0.1 -j ACCEPT
清空规则-F:iptables -F ,iptables -F INPUT ,iptables -t nat -F
root@ubuntu:/# iptables -L  (默认策略,和iptables配置文件的信息是一致的)
Chain INPUT (policy DROP)
target     prot opt source   destination         
ufw-before-logging-input  all  --  anywhere  anywhere            
ufw-before-input  all  --  anywhere  anywhere            
ufw-after-input  all  --  anywhere   anywhere            

root@ubuntu:/# iptables -L -n (会把目标信息以地址的形式显示出来)

iptables -L -n --line-number  (显示行号,删除规则是基于行号删除的)
INPUT属于进入的概念,iptables -P INPUT DROP-P是修改规则,规则只有3种inout,output,forward, -P表示针对inout或output或forward进行调整。vim sysconfig/iptables文件里面的:INPUT  ACCEPT  [0:0] :FORWARD  ACCEPT  [0:0]  : OUTPUT  ACCEPT  [0:0] 默认都是ACCEPT的,iptables -P  INPUT  DROP,iptables-save(修改了要保存),这样INPUT就是DROP了。  
root@ubuntu:/# iptables -L
Chain INPUT (policy DROP)  :input链,都进不来
target     prot opt source               destination         
ufw-before-logging-input  all  --  anywhere             anywhere    

Chain FORWARD (policy DROP) :forward链,都不能跳转
target     prot opt source               destination         
DOCKER-ISOLATION  all  --  anywhere             anywhere            
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ufw-before-logging-forward  all  --  anywhere             anywhere 

Chain OUTPUT (policy ACCEPT) :output链,都出得去
target     prot opt source               destination         
ufw-before-logging-output  all  --  anywhere             anywhere            
ufw-before-output  all  --  anywhere             anywhere     

Chain DOCKER (1 references)
target     prot opt source               destination         

Chain DOCKER-ISOLATION (1 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             anywhere            

Chain ufw-after-input (1 references)
target     prot opt source               destination         
ufw-skip-to-policy-input  udp  --  anywhere  anywhere   udp dpt:netbios-ns
ufw-skip-to-policy-input  udp  --  anywhere   anywhere  udp dpt:netbios-dgm
ufw-skip-to-policy-input  tcp  --  anywhere    anywhere   tcp dpt:netbios-ssn
Chain是链,input和output是输入和输出,input链是从外面进来到主机这里来,针对进来的信息做什么策略,output链是从主机出去,针对出去的做什么策略,forward是指当要访问资源的时候必须通过我这台计算机转向,A要访问C必须经过B主机,就要通过B主机转向,B直接重定向到C上面去。

input和output是针对当前主机而言的,forward是针对其他电脑的。 iptables -L是打印当前3个链的策略信息。INPUT是DROP表示任何人访问这台电脑都是禁止的,OUTPUT是DROP表示当前电脑不能访问外部。
root@ubuntu:/# iptables -P  INPUT  ACCEPT  (-P修改input链)
root@ubuntu:/# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ufw-before-logging-input  all  --  anywhere             anywhere            
ufw-before-input  all  --  anywhere             anywhere            
追加规则:要往input链或者output链或者forward链里面来追加规则。
root@ubuntu:/# iptables -A INPUT -p tcp --dport 22 -j ACCEPT (-p表示要使用tcp相关的协议,)
root@ubuntu:/# iptables -L --line-number
Chain INPUT (policy DROP)
num  target     prot opt source               destination         
1    ufw-before-logging-input  all  --  anywhere             anywhere            
7    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh  (通过tcp协议在任意地方连接ssh都是同意)
修改规则-R:iptables -R INPUT 3 -s 192.168.0.1 -j ACCEPT
root@ubuntu:/# iptables -R INPUT  7   -s  192.168.0.1     -j  DROP (7表示input链里面的第7行,-s表示不是原来的任意地址了,192.168.0.1进来的要拒绝)
root@ubuntu:/# iptables -L --line-number
Chain INPUT (policy DROP)
num  target     prot opt source               destination  
7    DROP       all  --  192.168.0.1          anywhere 
删除规则-D:iptables -D INPUT 3 ,iptables -D INPUT -s 192.168.0.1 DROP
root@ubuntu:/# iptables -D INPUT 7  (根据行号删除)
root@ubuntu:/# iptables -L --line-number
Chain INPUT (policy DROP)   :就没有7了
num  target     prot opt source               destination  
root@ubuntu:/#  iptables -A INPUT -p  SFTP --dport 22 -j ACCEPT
root@ubuntu:/#  iptables -A INPUT -p  FTP  --dport 21 -j ACCEPT
root@ubuntu:/# iptables -L --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination   
7    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
8    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ftp

清空规则-F:iptables -F (清空所有链),iptables -F INPUT (清空input链),iptables -t nat -F
匹配条件:是从最大的范围到最小范围划分的。计算机上网是要通过网卡上网的,网卡连接的是网线,出入接口是从网卡出去进来,对应的是input和output链。源地址和目的地址是不允许某个ip进来或者访问某个ip。

-A追加规则,output链,将从网卡eth0出去和进来的都拒绝。
root@ubuntu:/# iptables -A OUTPUT -o ens33 -j DROP
root@ubuntu:/# iptables -L --line-number
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination   
7    DROP       all  --  anywhere             anywhere    
root@ubuntu:/# ping www.baidu.com (出不去了)(删除后就可以ping了)
PING www.a.shifen.com (163.177.151.110) 56(84) bytes of data.
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted

root@ubuntu:/# iptables -A INPUT -s 192.168.2.114 -j DROP
root@ubuntu:/# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination  
DROP       all  --  192.168.2.114        anywhere

root@ubuntu:/# iptables  -A  OUTPUT  -d  www.baidu.com  -j  DROP
root@ubuntu:/# iptables -L
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination      
7    DROP       all  --  anywhere             163.177.151.110     
8  DROP  all  --  anywhere  163.177.151.109    root@ubuntu:/# ping www.baidu.com
PING www.a.shifen.com (163.177.151.109) 56(84) bytes of data.
ping: sendmsg: Operation not permitted
root@ubuntu:/# iptables -D OUTPUT 7
root@ubuntu:/# iptables -D OUTPUT 8
iptables: Index of deletion too big.
root@ubuntu:/# iptables -D OUTPUT 7
root@ubuntu:/# ping www.baidu.com
PING www.a.shifen.com (163.177.151.110) 56(84) bytes of data.
64 bytes from 163.177.151.110: icmp_seq=1 ttl=128 time=13.7 ms

-A追加一个规则,来自于192.168.0.1网段的,tcp协议的22端口的,都禁止。

 

按照ip地址加端口加协议用的比较多。禁网卡用的少。

 

 

iptables的匹配规则:

 

不建议修改配置文件:iptables命令方式改的规则,如果不保存到配置文件,则重启后就没有了,service iptables save就是将命令行方式改的规则保存到配置文件。

linux:如果既有配置文件又有命令参数,如果通过命令方式执行的都是临时的,通过配置文件都是永久的。

 

原文地址:https://www.cnblogs.com/yaowen/p/8452408.html