码迷,mamicode.com
首页 > 其他好文 > 详细

防火墙iptables

时间:2020-10-08 18:45:11      阅读:19      评论:0      收藏:0      [点我收藏+]

标签:缺省   规则   均衡集   执行   缓冲区   lis   tput   server   ade   

linux的防火墙 iptables

netfilter(iptables)

netfilter --工作在内核软件,实现数据包的过滤。

iptables --工作应用层一个软件,用来控制netfilter。

1.netfilter\/iptables包过滤防火墙(tcp\/ip四层)

1)应用层 --通过软件为用户提供接口

2)传输层 --提供可靠或不可靠的数据传输(TCP/UDP)使用端口来标示服务类型 sport dport

3)网络层 --提供路由和选址(icmp) sip dip

4)数据链路层 --传输数据帧(MAC) s_mac

5)物理层 --传输透明比特流

过滤的依据: s_mac\/sip\/dip\/sport\/dport\/状态(三次握手\/四次断开)

netfilter的逻辑架构:

技术图片


netfilter防火墙的元素及关系:

netfilter==>表==>链==>规则


三张表:

filter 防火墙表,允许和拒绝都在这里实现

nat 地址转换

mangle 数据包整形

五条链:

INPUT 本机进站的数据流

OUTPUT 本机出站的数据流

FORWARD 路由的数据流

POSTROUTING 路由后的数据流

PREROUTING 路由前的数据流


表跟链的对应关系:

filter:INPUT,OUTPUT,FORWARD

nat: OUTPUT,PREROUTING,POSTROUTING

mangle:INPUT,OUTPUT,FORWARD,PREROUTING,POSTROUTING


四种数据流:

本机进站的数据流:packet-->ethX-->PREROUTING-->INPUT-->本机

本机出站的数据流:packet-->OUTPUT-->POSTROUTING-->ethX-->destination

路由的数据流:

出去: packet-->eth0-->PREROUTING-->FORWARD-->POSTROUTING-->eth1--destination

回来: packet-->eth1-->PREROUTING-->FORWARD-->POSTROUTING-->eth0--destination

本机访问本机: 本机-->packet-->lo-->PREROUTING-->INPUT-->本机

本机-->packet-->OUTPUT-->POSTROUTING--lo-->本机


表的匹配顺序:

mangle-->nat-->filter


防火墙规则匹配顺序:

1.按顺序匹配,如果第一条匹配到了就直接执行这条规则的动作,不往下匹配其它规则.

2.如果第一条匹配不到,第二条也匹配不到,继续往下匹配直到找到匹配的规则,如果找不到匹配默认规则.


传输层:协议(tcp\/udp)

端口(sport\/dport)

网络层:

IP地址(sip\/dip\/icmp)

数据链路层:

mac地址(--mac-source)

物理层:

从哪个网卡进来 -i

从哪个网卡出去 -o


查看:

# iptables -t nat -L -n -v --line

# iptables -t filter -L -n

# iptables -t filter -L INPUT

# iptables -t filter -L INPUT -v -n

# iptables -t filter -L INPUT -n -v --line

# watch -n 0.1 iptables -L INPUT --line -n -v

追加规则:

# iptables -t filter -A INPUT -i lo -j ACCEPT

插入规则:

# iptables -t filter -I INPUT -i eth0 -j ACCEPT --插入成为第一条

# iptables -t filter -I INPUT 3 -i eth0 -j ACCEPT --插入成为第三条规则

替换规则:

# iptables -t filter -R INPUT 3 -i eth1 -j ACCEPT --替换成为指定规则

删除规则:

# iptables -t filter -D INPUT 2 --删除指定链指定编号的规则

+++++++++++

#iptables -t filter -A INPUT -i lo -j ACCEPT

#iptables -t filter -A OUTPUT -o lo -j ACCEPT

#iptables -t filter -A INPUT -i eth0 -s 192.168.0.0\/24 -d 192.168.0.250 -j ACCEPT

# iptables -t filter -A INPUT -i eth1 -j ACCEPT

# iptables -L INPUT -n --line -v

# iptables -t filer -I INPUT 2 -i eth2 -j ACCEPT 插入默认第二条

# iptables -t filter -I INPUT -i eth3 -j ACCEPT 插入默认第一条

# iptables -t filter -R INPUT 1 -i eth4 -j ACCEPT 替换默认第一条

#iptables -t filter -D INPUT 1 删除默认第一条

# iptables -t filter -F INPUT 清空filter所有INPUT链

#iptables -t filter -F

#iptables -L INPUT -n --line -v

+++++++++++++++++++

清空规则:

1、清空一张表

# iptables -t filter -F

2、清空一条链中的规则

# iptables -t filter -F INPUT

新建\/删除用户自定义的链:

# iptables -t filter -N uplooking 新建

# iptables -t filter -X uplooking 删除

# iptables -t filter -X 清空filter表中所有用户自定义链

+++++++++

iptables -t filter -N TCP

iptables -t filter -N UDP

iptables -t filter -A INPUT -i lo -j ACCEPT

iptables -t filter -A INPUT -p tcp -j TCP

++++++++

更改默认规则:

# iptables -t filter -P INPUT ACCEPT

# iptables -t filter -P INPUT DROP

1、iptables扩展匹配

1) iptables(user space)\/netfilter(kernel space)
2)netfilter组成(表(功能模块)/链/规则)
filter(INPUT\/OUTPUT\/FORWARD) 过滤
nat(PREROUTING\/POSTROUTING\/OUTPUT) 地址转换
mangle(5个链 ) 更封装层数据包结构
-->PREROUTING-->FORWARD-->POSTROUTING-->
INPUT | | OUTPUT
本机

规则的匹原则:

1、自上而下按顺序匹配
2、如果匹配到某条规则,执行这个规则动作,就不往后匹配其它规则
3、如果列表的所有的规则都匹配不到,则匹配默认规则
iptables [-t talbe] -A | -I | -D | -R | -E chian option(-s -d -i -o -p --dport -m) -j action(ACCEPT\/DROP\/REJECT\/SNAT\/DNAT....)
应用层
传输层(tcp\/udp\/sport\/doprt\/ tcp6个控制位匹配)
网络层(-s\/-d\/icmp)
数据链路层(mac)
物理层(-i\/-o)

扩展匹配

需求

只允许172.16.85.14访问本机

不允许任何人访问本机

默认规则 drop

172.16.85.14

1、通用匹配 -i -o -s -d
-i eth0 从这块网卡流入的数据 流入一般用INPUT和PREROUING
-o eth0 从这块网卡流出的数据 流出一般在OUTPUT和PSOTROUTING
-s 源IP
-d 目标IP
2、隐含匹配 tcp udp icmp sport dport
3、扩展匹配 -m mac | iprange | state
-m multiport:表示启用多端口扩展 之后我们就可以启用比如 --dports 21,23,80,8080-8100, 3306
常用的ACTION: -j
DROP:悄悄丢弃 nmap
一般我们多用DROP来隐藏我们的身份,以及隐藏我们的链表
REJECT:明示拒绝
ACCEPT:接受
custom_chain:转向一个自定义的链
DNAT
SNAT
MASQUERADE:源地址伪装 nat实验的时候
REDIRECT:重定向:主要用于实现端口重定向
MARK:打防火墙标记的
RETURN:返回
在自定义链执行完毕后使用返回,来返回原规则链。

练习题1:

只要是来自于172.16.85.14的都允许访问我本机的172.16.100.1的SSHD服务
分析:首先肯定是在允许表中定义的。因为不需要做NAT地址转换之类的,然后查看我们SSHD服务,在22号端口上,处理机制是接受,对于这个表,需要有一来一回两个规则,如果我们允许也好,拒绝也好,对于访问本机服务,我们最好是定义在INPUT链上,而OUTPUT再予以定义就好。(会话的初始端先定义),所以加规则就是:
定义进来的: iptables -t filter -A INPUT -s 172.16.0.0\/16 -d 172.16.100.1 -p tcp --dport 22 -j ACCEPT
定义出去的: iptables -t filter -A OUTPUT -s 172.16.100.1 -d 172.16.0.0\/16 -p tcp --dport 22 -j ACCEPT
将默认策略改成DROP:
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

练习题2:

假如我们允许自己ping别人,但是别人ping自己ping不通如何实现呢?
分析:对于ping这个协议,进来的为8(ping),出去的为0(响应).我们为了达到目的,需要8出去,允许0进来
在出去的端口上:iptables -A OUTPUT -p icmp --icmp-type 8 -j ACCEPT
在进来的端口上:iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT
小扩展:对于127.0.0.1比较特殊,我们需要明确定义它
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
iptables -A OUTPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
\/lib\/modules\/2.6.18-194.el5\/kernel\/net\/ipv4\/netfilter\/:存放模块的位置
-m connlimit:每个IP的并发连接数(TCP) --关注的是新发起的连接(NEW --syn)
mount -o loop rhel55.iso \/mnt
rpm -Uvh \/mnt\/Server\/iptables-1.3.5-5.3.el5_4.1.i386.rpm
iptables -t filter -A INPUT -s 192.168.1.115 -p tcp --dport 22 -m connlimit --connlimit-above 1 -j DROP
iptables -t filter -A INPUT -s 192.168.1.115 -p tcp --dport 22 -m connlimit ! --connlimit-above 1 -j ACCEPT
-m icmp:ping包请求与发送
[root@ ~]# iptables -A INPUT -p icmp -m icmp --icmp-type echo-reply -j ACCEPT
[root@ ~]# iptables -A INPUT -p icmp -m icmp --icmp-type echo-request -j ACCEPT
[root@ ~]# iptables -A INPUT -p icmp -j DROP
[root@ ~]# iptables -A OUTPUT -p icmp -m icmp --icmp-type 0 -j ACCEPT ==> 0 相当于 echo-reply
[root@ ~]# iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT ==> 8 相当于 echo-request
-m ipranges:IP范围
[!] --src-range ip-ip Match source IP in the specified range
[!] --dst-range ip-ip Match destination IP in the specified range
[root@ ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.1.1-192.168.1.100 -j ACCEPT
[root@ ~]# iptables -t filter -A FORWARD -m iprange --dst-range 192.168.1.101-192.168.1.252 -j DROP
-m limit:速率限制
[root@ ~]# watch iptables -L INPUT -nv
[root@ ~]# iptables -A INPUT -s 192.168.1.115 -p icmp -m icmp --icmp-type echo-request -m limit --limit 1\/second --limit-burst 1 -j ACCEPT
[root@ ~]# iptables -A INPUT -s 192.168.1.115 -p icmp -m icmp --icmp-type echo-request -j DROP
-m mac:匹配源地址的 MAC 地址
[root@ ~]# iptables -A INPUT -m mac --mac 00:0C:29:58:01:9A -p icmp -j DROP
-m multiport:多端口
[root@ html]# iptables -A INPUT -s 192.168.1.0\/24 -p tcp -m multiport --dport 22:25,25,110,80,53,21 -j DROP
-m state(NEW\/ESTABLISHED\/RELATED\/INVALID):
NEW --第一个数包,跟TCP状态无关
ESTABLISHED --第二个数据包
RELATED --已经发生关系的数据(FTP)
INVALID --无效数据包

个人简单的防火墙(状态):

ESTABLISHED 不允许其他主机发起的主动访问,只允许本地主机主动发起的发功能文以及lo通讯
[root@stu110 ~]# iptables -P INPUT DROP
[root@stu110 ~]# iptables -A INPUT -i lo -j ACCEPT
[root@stu110 ~]# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@stu110 ~]# iptables -A INPUT -p icmp -m state --state NEW -m limit --limit 1\/second --limit-burst 3 -j ACCEPT
[root@stu110 ~]# iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
RELATED:已发生关系的
(1)vsftpd数据传输(考虑模式,默认是主动模式)

主动模式:

21 --控制端口
20 --数据端口

被动模式:

21 --控制端口
1024+ --数据端口
[root@mail ~]# insmod \/lib\/modules\/2.6.18-164.el5xen\/kernel\/net\/ipv4\/netfilter\/ip_conntrack_ftp.ko 需要模块的支持,内核才能支持 RELATED状态
[root@mail ~]# iptables -t filter -A INPUT -p tcp --dport 21 -j ACCEPT
[root@mail ~]# iptables -t filter -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
被动模式\/主动:
service vsftpd start
iptables -A INPUT -p tcp -m multiport --dport 21 -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
modproble ip_conntrack_ftp --加载FTP状态自动跟踪模块,它可以完成自动切换端口
-m string:字符串内容进行匹配(局限大,效率低,不太用)
[root@ ~]# iptables -A INPUT -s 192.168.1.8 -p tcp --dport 22 -m string --algo bm --string "sex" -j DROP
-m time:时间限制
[root@ linux]# insmod \/lib\/modules\/$(uname -r)\/kernel\/net\/ipv4\/netfilter\/ipt_time.ko
[root@ linux]# iptables -t filter -A INPUT -s 192.168.1.1.0\/24 -m time --timestart 9:00 --timestop 17:00 --days Mon,Tue,Wed,Thu,Fri -j ACCEPT
[root@ linux]# iptables -t filter -A INPUT -s 192.168.1.1.0\/24 -p tcp --dport 80 -j DROP
技术图片
技术图片

DDOS原理,及轻量级别***的防止

# vim \/etc\/sysctl.conf

net.ipv4.tcp_max_syn_backlog = 8096 --超过最大连接数后产生新syn队列的长度
net.ipv4.tcp_synack_retries = 2 --syn确认的重试次数
net.ipv4.ip_local_port_range = 1024 65535--表示用于向外连接的端口范围。缺省情况下很小:32768到61000,改为1024到65000。
net.ipv4.tcp_syncookies = 1 --表示开启SYNCookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN***,默认为0,表示关闭;
net.ipv4.tcp_tw_reuse = 1 --表示开启重用。允许将TIME-WAITsockets重新用于新的TCP连接,默认为0,表示关闭;
net.ipv4.tcp_tw_recycle = 1 --表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。
net.ipv4.tcp_max_tw_buckets=5000--表示系统同时保持TIME_WAIT套接字的最大数量,如果超过这个数字,TIME_WAIT套接字将立刻被清除并打印警告信息。默认为180000,改为5000。

sysctl -p
1、通过iptables防止轻量级别的DDOS
-m limit
-m connlimit
2、DDOS的原理,三次握手(客户端第三次不回应服务端)
3、通过内核增大TCP缓冲区,支持更多的连接。
4、增加更强大的硬件防火墙,用于丢弃(过滤)掉不正常syn请求,搭建负载均衡集群,另外保证你的外网有足够大的带宽

防火墙iptables

标签:缺省   规则   均衡集   执行   缓冲区   lis   tput   server   ade   

原文地址:https://blog.51cto.com/13420391/2540286

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!