iptables基础一

1. iptables基础知识

2. iptables命令基本用法及实例

??



??

1. iptables基础知识

??
防火墙的实现方式与分类
??防火墙的实现方式分硬件与软件两种
??按防护范围分:
????- 主机防火墙:工作在主机边缘处的出入口,为单台主机提供防范
????- 网络防火墙:工作在网络的边缘处出入口,为整个网络提供防护
??
数据包在本机的流向
??
??报文由传送到本机网卡(PREROUTING链),由内核接收并放到接收缓存区(内存上)中。每当进来一个网络报文,系统就会向内核发送一个中断,由内核来接收、处理报文。若缓冲区满了就开始拒绝收包(也有可能放到消息队列)。
内核拆包,分析报文中的目标IP和源IP,由目标IP分析报文是传给谁,若是本机则传送至INPUT链,开始拆ip首部,tcp/udp首部(得到端口,端口号要在内核中注册过),最后把数据包交给相应进程处理。相关进程处理完后,把响应包发给源IP经由OUTPUT链离开本机。数据包由哪个网络接口流出取决于路由结果。
??若目的IP不是本机,要么丢弃,要么转发(本机为网关)。FORWARD链
??若要转发,继续封装帧首部,物理层首部。转发至下一跳或者目标主机。POSTROUTING链
??防火墙可提高主机的安全性,但对系统性能会有影响。因为数据包每次经由链时,都会由链内的规则匹配一遍,规则条目越多,性能越差。若要优化防火墙,提高性能,可尽量把多条规则合并成一条规则。
??
报文流向:
??1:流入本机的:prerouting --> input --> 用户空间进程
??2:流出本机的:用户空间进程发出 ==> output --> postrouting
??3:由本机转发的:prerouting --> forward --> postrouting
??

iptables与netfilter的关系
??
??netfilter:内核中的代码,编译内核时,若不启用则无法支持防火墙功能
????hook function设置勾子函数
??iptables:用户空间中的应用程序,编写规则的接口
????rules 添加规则
??netfilter的勾子函数:
????刚刚进入网卡,马上要由内核进行处理时(路由前):PREROUTING
????报文即将离开本机时(路由后):POSTROUTING
????到本机内部来的:INPUT
????经由本机发出的:输出队列OUTPUT
????经由本机转发的:FORWARD
??
防火墙的功能
??filter:过滤功能,实现防火墙功能
??mangle:把报文拆解开,拆解完把报文中需要修改的内容按需修改
??nat:network addrees translation网络地址转换,只拆解报文修改地址(ip层地址,传输层地址)的那一部分
??raw:关闭在nat表上启用的连接追踪机制

??
iptables的整体工作方式:
??有多种不同的工作方式,每种工作方式称为一个table,在每一个table上可以有多个链
??内置链:跟勾子有一一对应关系,分别为PREROUTING , INPUT , FORWARD , OUTPUT , POSTROUTING。向哪个链上添加规则就相当于向哪个勾子上添加规则。
??自定义链:名字可以自取,与勾子无关,对内置链的扩展,可实现更灵活的规则组织管理机制。只有把自定义链链接到勾子上才能生效。
??
表和链的对应关系
??filter:实现的链INPUT , FORWARD , OUTPUT
??mangle:在任何位置都可以实现
??nat:对性能影响比较大。可以在PREROUTING,OUTPUT ,POSTROUTING上实现
??raw:对应的链:PREROUTING , OUTPUT
??
同一链上的不同表的规则的应用优先级由高到低依次为:raw-->mangle-->nat-->filter
??
路由功能发生的时刻:
??报文进入本机后:(也取决于本机的路由功能是否开启)
????判断目标主机是否为本机。是-->INPUT;否--> FORWARD
??报文离开本机前:
????判断经由哪一个接口送往下一跳,取决于路由自身的功能。
????路由有两个时刻会发生,1:报文进来之后,2:报文从本机出去之前
??
防火墙规则组成部分
??根据规则的匹配条件尝试匹配报文,对于匹配成功的报文根据规则规定的处理动作作出处理
??匹配条件:
????基本匹配
????扩展匹配
??处理动作(target)
????基本处理动作
????扩展处理动作
????自定义处理动作
??
添加防火墙规则时的考量点:

  1. 要实现哪种功能,判断添加规则至哪个表上
  2. 确定报文流经的位置,判断添加至哪个链上
  3. 确定源、目标IP,源、目标端口
    ??

    2. iptables命令基本用法及实例

    ??

    iptables命令:iptables/ip6tables — administration tool for IPv4/IPv6 packet filtering and NAT
    规则格式:iptables [-t table] SUBCOMMAND chain [matches...] [-j target]
    ??-t table:raw, mangle, nat, [filter]

    ??rule-specification = [matches...] [target]
    ??match = -m matchname [per-match-options]
    ??target = -j targetname [per-target-options]

??
SUBCOMMAND
??
??链管理

?? iptables [-t table] -N chain 新增一条自定义链
??iptables [-t table] -X [chain] 删除自定义的规则链
??iptables [-t table] -P chain target
????设置默认策略。对filter表中的链而言,其默认策略有
????target = -j targetname [per-target-options]
??????ACCEPT:接受
??????DROP:丢弃
??????REJECT:拒绝
??iptables [-t table] -E old-chain-name new-chain-name
????重命名自定义未被引用的链;引用计数不为0的自定义链不能够被重命名,也不能被删除;

??规则管理:

iptables [-t table] -A chain rule-specification 追加
iptables [-t table] -I chain [rulenum] rule-specification 插入,要指明位置,省略时表示第一条
iptables [-t table] -D chain rulenum 删除
??(1) rule number 指明规则序号
??(2) rule specification 规则格式
iptables [-t table] -R chain rulenum rule-specification 替换指定链上的指定规则
iptables [-t table] -F [chain [rulenum]] [options...] 清空指定的规则链
iptables [-t table] -Z [chain [rulenum]] [options...] 置零
??iptables的每条规则都有两个计数器:
????(1) 匹配到的报文的个数;
????(2) 匹配到的所有报文的大小之和;

?? 查看

iptables [-t table] -L [chain [rulenum]] [options...]
??列出指定鏈上的所有规则
????-n:numberic,以数字格式显示地址和端口号;
????-v:verbose,详细信息;
??????-vv, -vvv
????-x:exactly,显示计数器结果的精确值而非单位换算后的结果;
????--line-numbers:显示规则的序号;

??
matches:匹配条件
??
??基本匹配条件:无需加载任何模块,由iptables/netfilter自行提供

[!] -s, --source address[/mask][,...]:检查报文中的源IP地址是否符合此处指定的地址或范围;
[!] -d, --destination address[/mask][,...]:检查报文中的目标IP地址是否符合此处指定的地址或范围;
[!] -p, --protocol protocol :限制协议
??protocol: tcp, udp, udplite, icmp, icmpv6,esp, ah, sctp, mh or "all"
??常用的:{tcp|udp|icmp}
[!] -i, --in-interface name:数据报文流入的接口;只能应用于数据报文流入的环节,只能应用于PREROUTING,INPUT和FORWARD链;
[!] -o, --out-interface name:数据报文流出的接口;只能应用于数据报文流出的环节,只能应用于FORWARD、OUTPUT和POSTROUTING链;

??扩展匹配条件: 需要加载扩展模块,方可生效
??1. 隐式扩展:不需要手动加载扩展模块;因为它们是对协议的扩展,所以,但凡使用-p指明了协议,就表示已经指明了要扩展的模块;

[!] -p, --protocol PROTOCOL PROTOCOL…… : 限制协议
protocol: tcp, udp, udplite, icmp, icmpv6,esp, ah, sctp, mh or "all"
常用的{tcp|udp|icmp}
> man手册中的解释:
The protocol of the rule or of the packet to check. The specified protocol can be one of tcp, udp, udplite, icmp, icmpv6,esp, ah, sctp, mh or the special keyword "all", or it can be a numeric value, representing one of these protocols or a different one. A protocol name from /etc/protocols is also allowed.
A "!" argument before the protocol inverts the test. The number zero is equivalent to all. "all" will match with all protocols and is taken as default when this option is omitted. Note that, in ip6tables, IPv6 extension headers except esp are not allowed. esp and ipv6-nonext can be used with Kernel version 2.6.11 or later. The number zero is equivalent to all, which means that you cannot test the protocol field for the value 0 directly. To match on a HBH header, even if it were the last, you cannot use -p 0, but always need -m hbh.

??
tcp:隐含指明了”-m tcp”,有专用选项:
??[!] --source-port, --sport port[:port]:匹配报文中的tcp首部的源端口;可以是端口范围;
?? [!] --destination-port,--dport port[:port]:匹配报文中的tcp首部的目标端口;可以是端口范围;
??[!] --tcp-flags mask comp:检查报文中mask指明的tcp标志位,要这些标志位comp中必须为1;
man手册中的解释:
mask is the flags which we should examine, written as a comma-separated list,如 SYN,ACK,FIN,RST
comp is a comma-separated list of flags which must be set,如SYN*

??“如:
??--tcp-flags SYN,ACK,FIN,RST SYN”表示,要检查的标志位为SYN,ACK,FIN,RST四个(第一组),其中SYN(第二组)必须为1,余下的必须为0;
??“--tcp-flags syn,fin,ack,rst ack,fin”四次挥手

??[!] --syn:用于匹配第一次握手,相当于”--tcp-flags SYN,ACK,FIN,RST SYN“;

??udp :隐含指明了”-m udp ”,有专用选项:
[!] --source-port, --sport port[:port]:匹配报文中UDP首部的源端口;可以是端口范围;
[!] --destination-port,--dport port[:port]:匹配报文中UDP首部的目标端口;可以是端口范围;

icmp :隐含指明了”-m icmp ”,有专用选项:
??[!] --icmp-type {type[/code]|typename} 对icmp的类型和代码做限定
????type/code:两种类型
????0/0:echo replay Ping应答
????8/0:echo request Ping请求
例:
本机ping

~]# iptables -A OUTPUT -s 192.168.1.7 -d 0/0 -p icmp --icmp-type 8 -j ACCEPT
~]# iptables -A INPUT -s 0/0 -d 192.168.1.7 -p icmp --icmp-type 0 -j ACCEPT

其它主机ping主机放行规则

~]# iptables -A INPUT -s 0/0 -d 192.168.1.7 -p icmp --icmp-type 8 -j ACCEPT
~]# iptables -A OUTPUT -s 192.168.1.7 -d 0/0 -p icmp --icmp-type 0 -j ACCEPT

?? 显式扩展:必须由-m选项专门加载相应模块

multiport:多端口匹配——以离散方式定义多端口匹配,最多可以指定15个端口
??[!] --source-ports,--sports port[,port|,port:port]...匹配源端口
????Match if the source port is one of the given ports. The flag --sports is a convenient alias for this option. Multiple ports or port ranges are separated using a comma, and a port range is specified using a colon. 53,1024:65535 would therefore match ports 53 and all from 1024 through 65535.

??[!] --destination-ports,--dports port[,port|,port:port]...匹配目标端口
????Match if the destination port is one of the given ports. The flag --dports is a convenient alias for this option.
??[!] --ports port[,port|,port:port]...匹配端口
????Match if either the source or destination ports are equal to one of the given ports.
??
例:一次开放本机22和80端口

~]# iptables -I INPUT -s 0/0 -d 192.168.1.7 -p tcp -m multiport --dports 22,80 -j ACCEPT
~]# iptables -I OUTPUT -d 0/0 -s 192.168.1.7 -p tcp -m multiport --sports 22,80 -j ACCEPT

ipange:指明一段连续的IP地址范围作为源或者目标地址匹配
??This matches on a given arbitrary range of IP addresses.
[!] --src-range from[-to]
??Match source IP in the specified range.
[!] --dst-range from[-to]
??Match destination IP in the specified range.
??
例:
~]# iptables -A INPUT -d 192.168.1.7 -p tcp --dport 23 -m iprange --src-range 192.168.1.2-192.168.1.100 -j ACCEPT
~]# iptables -A OUTPUT -s 192.168.1.7 -p tcp --sport 23 -m iprange --dst-range 192.168.1.2-192.168.1.100 -j ACCEPT

string:对报文中的应用层数据做字符串匹配检测,linux内核版本要高于2.6.14

  This modules matches a given string by using some pattern matching strategy. It requires a linux kernel >=2.6.14.
   --algo {bm|kmp} 指明算法,以实现高性能的检测
    Select the pattern matching strategy. (bm = Boyer-Moore, kmp = Knuth-Pratt-Morris)
  --from offset 从哪个偏移位开始检查
    Set the offset from which it starts looking for any matching. If not passed, default is 0.
  --to offset 到哪个偏移位置结束检查
    Set the offset up to which should be scanned. That is, byte offset-1 (counting from 0) is the last one that is scanned. If not passed, default is the packet size.
  [!] --string pattern 给定要检查的字符串的模式
    Matches the given pattern.
  [!] --hex-string pattern 给定要检查的字符串的模式(十六进制字符模式)
    Matches the given pattern in hex notation.
例:

~ ] # iptables -I OUTPUT -s 192.168.1.12 -d 0/0 -p tcp --sport 80 -m string --algo bm --string “old” -j REJECT

time扩展:根据收到报文的时间/日期与指定的时间/日期范围进行匹配

  --datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]] 起始日期时间
  --datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]] 结束日期时间,只有日期没有时间表示从0:0:0开始
     Only match during the given time, which must be in ISO 8601 "T" notation. The possible time range is 1970-01-01T00:00:00 to 2038-01-19T04:17:07.
     If --datestart or --datestop are not specified, it will default to 1970-01-01 and 2038-01-19, respectively.

  --timestart hh:mm[:ss] 起始时间
  --timestop hh:mm[:ss] 结束时间
     Only match during the given daytime. The possible time range is 00:00:00 to 23:59:59. Leading zeroes are allowed (e.g. "06:03") and correctly interpreted as base-10.
  [!] --monthdays day[,day...] 匹配一个月中的某些天
     Only match on the given days of the month. Possible values are 1 to 31. Note that specifying 31 will of course not match on months which do not have a 31st day; the same goes for 28- or 29-day February.
  [!] --weekdays day[,day...] 匹配一个周中的某些天
     Only match on the given weekdays. Possible values are Mon, Tue, Wed, Thu, Fri, Sat, Sun, or values from 1 to 7, respectively. You may also use two-character variants (Mo, Tu, etc.).
  --kerneltz:使用内核上的时区,而非默认的UTC;一般不用(kerneltz,tz表示time zone)
例:

~]# iptables -A INPUT -s 172.16.0.0/16 -d 172.16.100.67 -p tcp --dport 80 -m time --timestart 14:30 --timestop 18:30 --weekdays Sat,Sun --kerneltz -j DROP
~]# iptables -R INPUT 4 -d 192.168.1.12 -p tcp --dport 23 -m iprange --src-range 192.168.2.2-192.168.2.200 -m time --timestart 09:00:00 --timestop 16:00:00 --weekdays 1,2,3,4,5 -j ACCEPT

原文地址:http://blog.51cto.com/11975865/2082818

时间: 2024-08-28 07:24:11

iptables基础一的相关文章

iptables基础概念

1.基础概念. 防火墙,其实说白了讲,用于实现Linux下访问控制的功能的,它分为硬件的或者软件的防火墙两种.无论是在哪个网络中,防火墙工作的地方一定是在网络的边缘.而我们的任务就是需要去定义到底防火墙如何工作,这就是防火墙的策略,规则,以达到让它对出入网络的IP.数据进行检测. 目前市面上比较常见的有3.4层的防火墙,叫网络层的防火墙,还有7层的防火墙,其实是代理层的网关. 对于TCP/IP的七层模型来讲,我们知道第三层是网络层,三层的防火墙会在这层对源地址和目标地址进行检测.但是对于七层的防

Linux防火墙iptables基础

IPtables基础 简介 iptables命令可用于配置Linux的包过滤规则,常用于实现防火墙.NAT. iptables里面有4张表,分别是filter,NAT,mangle,raw表.运维人员的话主要关注的是filter和NAT表. filter:主要是过滤包的,内建三个链INPUT.OUTPUT以及FORWARD.INPUT作用于进入本机的包:OUTPUT作用于本机送出的包:FORWARD作用于那些跟本机无关的包. NAT:主要用户地址转换和端口映射,内建三个链,分别是PREOUTIN

Linux 之 iptables基础(一)

 iptables基础(一) =========================================================================== 概述: iptables介绍 1)Firewall ★Firewall:防火墙系统 是一种隔离工具,Packets Filter Firewall (包过滤型防火墙): 定义:工作于主机或网络的边缘,对经由的报文根据预先定义的规则(匹配条件)进行检测,对于能够被规则匹配到的报文实行某预定义的处理机制的一套组件: 硬件

Linux防火墙工具iptables基础介绍

iptables基础知识说明: 一.规则链:规则链是防火墙规则/策略的集合 INPUT:处理入站数据包 OUTPUT:处理出站数据包 FORWARD:处理转发数据包 POSTROUTING链:在进行路由选择后处理数据包 PREROUTING链:在进行路由选择前处理数据包 二.规则表:规则表是规则链的集合(优先顺序:raw.mangle.nat.filter) raw表:确定是否对该数据包进行状态跟踪(OUTPUT.PREROUTING) mangle表:为数据包设置标记(PREROUNTING.

Iptables基础整理

Iptables基础框架 Iptables基于内核netfilter安全框架运行,主要有过滤数据包和NAT地址转换功能. Iptables基本表链结构 filter INPUT FORWARD OUTPUT     nat     OUTPUT PREROUTING POSTROUTING mangle INPUT FORWARD OUTPUT PREROUTING POSTROUTING raw     OUTPUT PREROUTING   Iptables主要通过规则链的方式进行数据包的过

iptables基础笔记

1.iptables是什么 iptables是Linux上的一支防火墙服务程序,且由内核直接提供 2.iptables的基本工作方式 iptables通过"表"的形式管理进出主机网卡的数据流 默认的"表"有3张,分别为Filter(默认使用),Nat,Manager.我们的需求使用Filter表的情况居多,通过对比数据包头部信息与每张表中的规则,来决定处理数据流是放行还是丢弃. 3.1Filter"表"介绍 Filter是过滤器表,主要与想要进出

linux运维之iptables基础及配置实例

iptables学习基础,目前处于学习阶段!请各位大神勿喷,同时也请多多指教! 图片可能不够清晰,附件已上传,但还在审核.....!!!!! 等审核通过了在后面附加地址!!!!!

iptables基础知识详解

iptables防火墙可以用于创建过滤(filter)与NAT规则.所有Linux发行版都能使用iptables,因此理解如何配置 iptables将会帮助你更有效地管理Linux防火墙.如果你是第一次接触iptables,你会觉得它很复杂,但是一旦你理解iptables的工 作原理,你会发现其实它很简单. 首先介绍iptables的结构:iptables -> Tables -> Chains -> Rules. 简单地讲,tables由chains组成,而chains又由rules组

iptables基础实战练习

目录: 一.基本规则练习 二.SNAT源地址转移 三.DNAT目标地址转移 一.基础规则练习 (1) 放行ssh (端口:22) 1 iptables -A INPUT -d 192.168.42.153 -p tcp --dport 22 -j ACCEPT 2 iptables -A OUTPUT -s 192.168.42.153 -p tcp --sport 22 -j ACCEPT (2)修改默认规则链(关闭所有端口) 1 iptables -P INPUT DROP 2 iptabl