iptables的基本规则

一、相关概念

Iptables 利用的是数据包过滤的机制,所以他会分析数据包的报头数据,根据报头数据与定义的规则来决定该数据包是否可以进入主机或者被丢弃,也就是说,根据数据包的分析资料比对预先定义的规则内容,若数据包数据与规则内容相同则进行动作,否则就继续下一条规则的比对,重点在比对与分析顺序。

Iptables的表格和链

Iptables名字的来由:因为这个防火墙软件里面有多个表格,每个表格都定义出自己的默认策略和规则,且每个表格的用途都不相同,linux中的iptables至少就有3个表格,包括管理本机进出的filter,管理后台主机(防火墙内部的其他计算机)的NAT,管理特殊标志使用的mangle(较少使用)。

Filter :主要跟进入linux本机的数据包有关,是默认的table

INPUT:主要与想要进入linux主机的数据包有关。

OUTPUT :主要与linux主机所要送出的数据包有关。

FORWORD :与linux主机没有关系,他可以传递数据包到后台的计算机中,与NATtable相关性较高。

NAT :是network address translation 的缩写,这个表格主要是用来进行来源与目的地的IPport的转换,与linux本机无关,主要与linux主机后的局域网内计算机相关。

PREROUTING :在进行路由判断之前所要进行的规则(DNAT/REDIRECT

POSTROYTING:在进行路由判断之后所要进行的规则(SNAT/MASQUERADE

OUTPUT :与发送出去的数据包有关。

Mangle :这个表格主要是与特殊的数据包的路由标志有关。

二、相关规则

[[email protected] ~]# iptables -F     ##清楚命令

[[email protected] ~]# iptables -nL    ##查看命令

-n   ##不进行IPHOSTNAME的反查,显示信息的速度会快很多

-v   ##列出更多的信息,包括通过该规则的数据包总位数、相关的网络接口等

 iptables的基本规则

 

##-F只是暂时的清除iptables的信息

[[email protected] ~]# service iptables save

 

iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

 

此中是改变文件的内容,并且是将此文件保存

 

 iptables的基本规则

[[email protected] ~]# iptables -A INPUT -s 172.25.254.73 -p tcp --dport 80 -j ACCEPT   ##在默认的filter表中添加对于172.25.254.73主机开放httpd服务

[[email protected] ~]# iptables -A INPUT -s 172.25.254.73 -p tcp --dport 80 -j REJECT   

 

##在默认的filter表中添加对于172.25.254.73主机拒绝开放httpd服务

[[email protected] ~]# iptables -A INPUT -s 172.25.254.73 -p tcp --dport 80 -j DROP

 

##在默认的filter表中添加对于172.25.254.73主机申请的httpd服务选择丢弃,不做回应。

 

 

 iptables的基本规则

 

 

[[email protected] ~]# iptables -A INPUT -p tcp --dport 21 -j ACCEPT   

 

##对所有安装lftp服务的主机开放lftp服务

 

[[email protected] ~]# iptables -I INPUT 1 -s 172.25.254.73 -p tcp --dport 443 -j DROP         ##插入到第一条策略

 iptables的基本规则

 

[[email protected] 8903]# iptables -N allowed-cjain         ##创建一个新的规则链

 

[[email protected] 8903]# iptables -E allowed-cjain allowed-chain    ##修改规则链的名字

 

[[email protected] ~]# iptables -X allowed-chain   ##删除自定义的链

 

 

 iptables的基本规则

 iptables的基本规则

 

[[email protected] 8903]# iptables -D INPUT 1   ##删除filter表中第一条规则

 iptables的基本规则

[[email protected] 8903]# iptables -D INPUT -s 172.25.254.73 -p tcp --dport 80 -j ACCEPT     ##按照规则的具体内容删除表中的具体规则

 

 

iptables的基本规则 

 

[[email protected] ~]# iptables -R INPUT 2 -j REJECT   ##修改规则中的第二条规则

 

 iptables的基本规则

 

[[email protected] ~]# iptables -t nat -nL    ##查看nat表中的规则

[[email protected] ~]# iptables -t nat -nL PREROUTING  ##仅仅查看nat 表中PREROUTING的规则

 

iptables的基本规则 

 

 

-i eth0:匹配从网络接口 eth0 进来

-o eth0:匹配从网络接口 eth0 出去

##这就是所谓的信任设备,假设主机存在两张网卡,eth0为其中一张网卡,从该网卡出去或者进去的信息均会被统统接受

[[email protected] ~]# iptables -A INPUT -i eth0 -j ACCEPT 

 iptables的基本规则

 

 

##开放本机的lo接口:不管数据包来自何处或去到哪里,只要是来自lo这个接口,就予以接受,在-s -d 参数没有被设置过的。

[[email protected] ~]# iptables -A INPUT -i lo -j ACCEPT 

 

 iptables的基本规则

 

[[email protected] ~]# iptables -A INPUT -s 172.25.254.73 -i eth0 -j ACCEPT 

 

##eth0端口进入的数据包且来源是172.25.254.73主机均接受

 

 iptables的基本规则

 

[[email protected] ~]# iptables -A INPUT -s 172.25.254.73 -j ACCEPT 

[[email protected] ~]# iptables -A INPUT -s 172.25.254.71 -j REJECT 

##此时就会发现一个问题,下面两条规则是相互冲突的,但还是想对71主机进行不拒绝。

此时就可以使用LOG

[[email protected] ~]# iptables -A INPUT -s 172.25.254.71 -j LOG 

##表示只要数据来自172.25.254.71主机时那么该数据包的相关信息会被写到内核日志文件中,然后该数据包会继续进行后续的规则对比。

 iptables的基本规则

 

##通常为了使主机更加的安全,会设置以下策略。先来了解一下-m的使用规则

Iptables -A INPUT [-m state] [--state 状态]

-m :一些iptables的外挂设备,主要常见的有:

State :状态模块

Mac :网卡硬件地址

--state :一些数据包的状态

ESTABLISHED :已经成功连接的连接状态

INVALID : 无效的数据包,例如数据破损的数据包状态

NEW :想要新建立数据包连接的状态

RELATED :这个最常用、表示这个数据包是与主机发送出去的数据包有关。

[[email protected] ~]# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT    ##表示正在连接或者已经连接过的均可以继续进行连接。

[[email protected] ~]# iptables -A INPUT -m state --state NEW -i lo -j ACCEPT 

  ##表示从lo端口接如的数据包即使状态是从未连接的new状态也可以进行连接

[[email protected] ~]# iptables -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT 

[[email protected] ~]# iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT 

##表示常用的httpd服务sshd服务在状态即使是新的数据包也可以接受

[[email protected] ~]# iptables -A INPUT -j REJECT   ##其他均进行拒绝

 

 iptables的基本规则

 

 

Nat表的源地址伪装与目的地地址伪装

需要两台不再同一个网段内的虚拟机,以及一台有两个网卡充当路由器的虚拟机。

Node1172.25.254.73

Desktop: 172.25.71.2 需要将网关设置为路由器与他在同一个网段的ip

路由器:eth0 172.25.254.173

Eth1 172.25.71.1 

##由于filter表中的设置,此时desktop即使将网关设置为172.25.71.1,但也与路由器的172.25.71.1不能进行通信,此时,需要将filter表进行刷新。

源地址伪装:在路由器之后进行

 

 

[[email protected] ~]# iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 172.25.254.173  ##eth0网卡出去的数据包,是SNAT源地址转化,伪装成173主机的数据包。

##但是此时172.25.71.2主机与172.25.254.73仍然不能通信,因为两块网卡并没有实现通信。

[[email protected] ~]# sysctl -a | grep ip_forward

net.ipv4.ip_forward = 0

[[email protected] ~]# vim /etc/sysctl.conf   ##在此文件中加入那行并改为1

[[email protected] ~]# sysctl -p              ##使此策略生效

net.ipv4.ip_forward = 1

[[email protected] ~]# sysctl -a | grep ip_forward   ##查看

net.ipv4.ip_forward = 1

 

 iptables的基本规则

 

测试:不同网段的主机实现了通信

 iptables的基本规则

 

目的地地址转化:

[[email protected] ~]# iptables -t nat -A PREROUTING -i eth0 -j DNAT --to-dest 172.25.254.73  

 

 iptables的基本规则

 

测试:

 

 iptables的基本规则

 

 

[[email protected] ~]# iptables -A INPUT -m limit --limit 300/hour  ##限制每小时通过300个数据包

 iptables的基本规则

 

[[email protected] ~]# iptables -A INPUT -m limit --limit-burst 100 ##一次超过100个包直接丢弃

 

 iptables的基本规则

 

[[email protected] ~]# iptables -A INPUT -p icmp -m limit --limit 5/m --limit-burst 5

##同时设定速率限制与门阀限制

 

 iptables的基本规则

##脚本实现服务端#!/bin/bash

iptables -F

iptables -A INPUT -i lo -j ACCEPT

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

iptables -A INPUT -m state --state NEW -i lo -j ACCEPT

iptables -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT

iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT

service iptables save

##filter表的设定

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf

sysctl -p

iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 172.25.254.173

iptables -t nat -A PREROUTING -i eth0 -j DNAT --to-dest 172.25.254.73

 

 iptables的基本规则