iptables匹配multiport
multiport匹配帮助信息如下。multiport有三个选项。其中,–source-port(简写–sports)用于指定源端口,多个源端口使用逗号分隔,当指定源端口范围时,使用分号表示区间(port:port)。
选项–destination-ports(简写为–dports)用于指定目的端口。选项–ports可匹配源或者目的端口。
multiport仅支持带有端口号的协议,如tcp,udp,udplite,dccp和sctp。
# iptables -m multiport -hmultiport match options:
[!] --source-ports port[,port:port,port...]--sports ...match source port(s)
[!] --destination-ports port[,port:port,port...]--dports ...match destination port(s)
[!] --ports port[,port:port,port]match both source and destination port(s)
如下策略,丢弃目的端口号:23,24,25以及135,136,137,138,139。在另外的主机上不能访问其中的任何一个端口。
# iptables -A INPUT -p tcp -m multiport --dports 23:25,135:139 -j DROP
#
# iptables -L -n -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target prot opt in out source destination 8 480 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 23:25,135:139
multiport匹配
函数xt_register_matches注册匹配结构multiport_mt_reg。
static struct xt_match multiport_mt_reg[] __read_mostly = {{ .name = "multiport",.family = NFPROTO_IPV4,.revision = 1,.checkentry = multiport_mt_check,.match = multiport_mt, .matchsize = sizeof(struct xt_multiport_v1),.me = THIS_MODULE,},{ .name = "multiport",.family = NFPROTO_IPV6,.revision = 1,.checkentry = multiport_mt6_check,.match = multiport_mt, .matchsize = sizeof(struct xt_multiport_v1),.me = THIS_MODULE,},
};
static int __init multiport_mt_init(void)
{return xt_register_matches(multiport_mt_reg,ARRAY_SIZE(multiport_mt_reg));
如下IPv4策略检测函数,首先协议必须合法;其次端口配置项数量不能超过15个(XT_MULTI_PORTS),注意,配置端口范围时占用2个配置项。
static inline bool
check(u_int16_t proto, u_int8_t ip_invflags, u_int8_t match_flags, u_int8_t count)
{/* Must specify supported protocol, no unknown flags or bad count */return (proto == IPPROTO_TCP || proto == IPPROTO_UDP|| proto == IPPROTO_UDPLITE|| proto == IPPROTO_SCTP || proto == IPPROTO_DCCP)&& !(ip_invflags & XT_INV_PROTO)&& (match_flags == XT_MULTIPORT_SOURCE|| match_flags == XT_MULTIPORT_DESTINATION|| match_flags == XT_MULTIPORT_EITHER)&& count <= XT_MULTI_PORTS;
}
static int multiport_mt_check(const struct xt_mtchk_param *par)
{const struct ipt_ip *ip = par->entryinfo;const struct xt_multiport_v1 *multiinfo = par->matchinfo;return check(ip->proto, ip->invflags, multiinfo->flags,multiinfo->count) ? 0 : -EINVAL;
对于IPv6策略配置,同样使用以上的check判断函数。
static int multiport_mt6_check(const struct xt_mtchk_param *par)
{const struct ip6t_ip6 *ip = par->entryinfo;const struct xt_multiport_v1 *multiinfo = par->matchinfo;return check(ip->proto, ip->invflags, multiinfo->flags,multiinfo->count) ? 0 : -EINVAL;
匹配处理函数multiport_mt,对于非首个分片报文,由于可能没有四层头部信息,认为不匹配。否则,获取四层报文开始位置,有函数ports_match_v1进行处理。
static bool
multiport_mt(const struct sk_buff *skb, struct xt_action_param *par)
{const __be16 *pptr;__be16 _ports[2];const struct xt_multiport_v1 *multiinfo = par->matchinfo;if (par->fragoff != 0)return false;pptr = skb_header_pointer(skb, par->thoff, sizeof(_ports), _ports);if (pptr == NULL) {/* We've been asked to examine this packet, and we* can't. Hence, no choice but to drop.*/pr_debug("Dropping evil offset=0 tinygram.\n");par->hotdrop = true;return false;}return ports_match_v1(multiinfo, ntohs(pptr[0]), ntohs(pptr[1]));
遍历策略中配置的所有端口。
static inline bool
ports_match_v1(const struct xt_multiport_v1 *minfo,u_int16_t src, u_int16_t dst)
{unsigned int i;u_int16_t s, e;for (i = 0; i < minfo->count; i++) {s = minfo->ports[i];
首先处理配置的端口范围项,对于源端口检查,判断是否在配置的源端口范围内;对于目的端口检查,判断是否在配置的目的端口范围内。在不指定源/目的端口的情况下,只要端口位于源端口范围或者目的端口范围之内,都是匹配的。
if (minfo->pflags[i]) {/* range port matching */e = minfo->ports[++i];pr_debug("src or dst matches with %d-%d?\n", s, e);switch (minfo->flags) {case XT_MULTIPORT_SOURCE:if (src >= s && src <= e)return true ^ minfo->invert;break;case XT_MULTIPORT_DESTINATION:if (dst >= s && dst <= e)return true ^ minfo->invert;break;case XT_MULTIPORT_EITHER:if ((dst >= s && dst <= e) ||(src >= s && src <= e))return true ^ minfo->invert;break;default:break;}
以下处理非范围的端口号匹配,判断是否相等即可。
} else {/* exact port matching */pr_debug("src or dst matches with %d?\n", s);switch (minfo->flags) {case XT_MULTIPORT_SOURCE:if (src == s)return true ^ minfo->invert;break;case XT_MULTIPORT_DESTINATION:if (dst == s)return true ^ minfo->invert;break;case XT_MULTIPORT_EITHER:if (src == s || dst == s)return true ^ minfo->invert;break;default:break;}}}return minfo->invert;
内核版本 5.10
iptables匹配multiport相关推荐
- 防火墙 之 iptables 匹配条件讲解
1 概述 iptables命令中,需要根据匹配的条件作出相应的动作,本文将结合例子,讲解匹配条件 匹配条件分为基本和扩展的条件 基本:通用的,PARAMETERS,无需加载模块,由iptables ...
- iptables(四)iptables匹配条件总结之一
经过前文的总结,我们已经能够熟练的管理规则了,但是我们使用过的"匹配条件"少得可怜,之前的示例中,我们只使用过一种匹配条件,就是将"源地址"作为匹配条件. 那么 ...
- Linux多个端口组合,iptables使用multiport 添加多个不连续端口
使用multiport可以添加多个不连接的端口,最多可以添加15组,如下: iptables -A INPUT -p tcp -m multiport --dports 21:25,135:139 - ...
- iptables匹配iprange
iprange匹配帮助信息如下. iprange match options: [!] --src-range ip[-ip] Match source IP in the specified ran ...
- iptables匹配功能length
用于匹配报文特定长度,或者范围,这里的报文长度指的是4层数据的长度,如TCP/UDP/ICMP等.以下仅允许数据包小于100字节的ping请求数据进入. # iptables -I INPUT -p ...
- iptables匹配connlimit限制并发连接数量
以下规则将每个IP的最大并发连接数量控制在5个,使用connlimit-above或者connlimit-upto都可实现. # iptables -I INPUT -p tcp --syn --dp ...
- iptables匹配quota
quota匹配帮助信息如下. # iptables -m quota -h quota match options: [!] --quota quota quota (bytes) 如下,当主机192 ...
- iptables匹配ttl
TTL匹配帮助信息如下,可判断相等,大于和小于三种关系. # iptables --match ttl -h ttl match options: [!] --ttl-eq value Match t ...
- 服务器安全之iptables iptables
服务器安全之iptables 感谢老男孩老师为我们讲解iptables 优化之路 iptables防火墙简介 Netfilter/Iptables(以下简称Iptables)是unix/linux自 ...
最新文章
- Ajax基础讲解 1
- python批量爬取文档
- laravel创建新model数据的两种方法
- shell不允许输入空字符_shell脚本编程之控制脚本
- pop!_os_Pop!幕后花絮_OS Linux
- gif透明背景动画_ppt模板3D卡通GIF动画PPT素材,多种多样的日常动作
- ElasticSearch近似匹配调研
- 通过代理截取并修改非对称密钥加密信息
- 县级外业调查及举证软件_省三调办举办全省第三次全国国土调查统一时点更新省级技术培训会...
- SLAM--Pangolin显示相机位姿
- 【IoT】CC2541 BLE 4.0 底层协议栈广播连接过程解析
- Docker入门-常用指令总结与归纳
- 网赚项目分享:八条可以在线上做的副业兼职
- 仿生毛毛虫机器人源码
- 【校招VIP】前端校招考点之页面转换算法
- Linux学习-redis主从架构
- 档案管理学川大972 | 专门档案专题
- 基于FPGA的GV7600驱动
- WordExcel使用小技巧(1)—— 打对勾或打叉
- PHP 脚本在服务器上执行
热门文章
- linux之shell脚本
- SPP (Spatial Pyramid Pooling)
- Android屏幕适配方案
- js实现轮播图(简单滚动轮播)
- PHP扩展undefined symbol,解决 undefined symbol: php_pdo_register_driver in Unknown on line 0
- Linux内存管理 之 KSM代码实现
- 漫步数理统计三十——依概率收敛
- 图片数据损坏了怎么恢复
- [2023-01 持续更新] 谷歌学术google镜像/Sci-Hub可用网址/Github镜像可用网址总结
- Dubbo2.6.5入门——简单的HelloWorld