【js9905com金沙网站】iptables深入解析-应用层插件
对王宛平规的iptables match只怕target扩大料定不能够满意大家的急需,并且暗许iptables也只辨认到五元组,在深切识别已经很费力了.显著在骨子里的急需眼下,大家不会止步于此.下边就讲讲iptables作用扩充的插件,协助Layer7.
在Linux的防火墙种类Netfilter下有二个独自的模块L7 filter 。从字面上看Netfilter是对网络数据的过滤,L7 filter是根据数据流应用层内容的过滤。可是事实上 L7 filter的本职工作不是对数据流举办过滤而是对数码流举行分拣。它选取情势匹配算法把步入设备的数据包应用层内容与初期定义好的说道法则实行比对,即便协作成功就印证那么些数量包属于某种左券。
L7 filter是依照数据流职业的,创立在Netfilter connstrack成效之上。因为二个数据流也许说贰个连连的持有数据都以属于同二个利用的,所以L7 filter没有要求对由此的数据包进行方式相称,而只杰出一个流的前面几个数据包 (比如13个数据包)。当三个流的前方多少个数据手袋含了某种应用层左券的特征码时 (比方QQ),则这一个数据流被L7 filter识别;当后面几个数据包的剧情并未有富含某种应用层合同的特征码时,则L7 filter放弃承袭做形式相称,这几个数据流也就从未有过艺术被识别.
预备干活,需求下载netfilter-layer7-v2.22和l7-protocols 网址:
它的做事和以前注册match流程是同一的 供给顾客空间注册match 和水源的注册.
先看看顾客空间:
static struct xtables_match layer7 = {
.family = AF_INET,
- .name = "layer7",
- .version = XTABLES_VERSION,
- .size = XT_ALIGN(sizeof(struct xt_layer7_info)),
- .userspacesize = XT_ALIGN(sizeof(struct xt_layer7_info)),
- .help = &help,
- .parse = &parse,
- .final_check = &final_check,
- .print = &print,
- .save = &save,
- .extra_opts = opts
- };
再看看opts:
static const struct option opts[] = {
{ .name = "l7proto", .has_arg = 1, .val = 'p' },
- { .name = "l7dir", .has_arg = 1, .val = 'd' },
- { .name = NULL }
- };
主要照旧看parse函数:
/* Function which parses command options; returns true if it ate an option */
static int parse(int c, char **argv, int invert, unsigned int *flags,
- const void *entry, struct xt_entry_match **match)
- {
- struct xt_layer7_info *layer7info =
- (struct xt_layer7_info *)(*match)->data;
- switch (c) {
- case 'p':
- parse_layer7_protocol(argv[optind-1], layer7info);
- if (invert)
- layer7info->invert = true;
- *flags = 1;
- break;
- case 'd':
- if(strlen(argv[optind-1]) >= MAX_FN_LEN)
- xtables_error(PARAMETER_PROBLEM, "directory name too longn");
- strncpy(l7dir, argv[optind-1], MAX_FN_LEN);
- *flags = 1;
- break;
- js9905com金沙网站, default:
- return 0;
- }
- return 1;
- }
由于它是永葆到iptable1.4.3
和基础2.6.28的,假如明日可比新的本子供给自身对应修改部分代码.
此处须求登记的是struct xt_layer7_info :
#define MAX_PATTERN_LEN 8192
#define MAX_PROTOCOL_LEN 256
- struct xt_layer7_info {
- char protocol[MAX_PROTOCOL_LEN];
- char pattern[MAX_PATTERN_LEN];
- u_int8_t invert;
- };
大家拿五个事实上的事例表明:
#iptables -t nat -A POSTROUTING -m layer7 --17proto qq -j DROP
parse函数的机能就是分析参数后,读取特征码pat文件新闻卓越,然后赋值给xt_layer7_info
通过parse_layer7_protocol:默许pat文件放在/etc/l7-protocols
,也得以协和钦赐.
看qq.pat:
#...
# that."
# So now the pattern allows any of the first three bytes to be 02.
Delete
# one of the ".?" to restore to the old behaviour.
# pattern written by www.routerclub.com wsgtrsys
qq
^.?.?x02.+x03$
即:protocol
=“qq”;pattern=“^.?.?x02.+x03$”
我们能够看来公约识别的消息是一串正则表明式鲜明无法一贯用.
对此layer7它只协助ip报文,公约只帮衬tcp、udp和icmp:
static int can_handle(const struct sk_buff *skb)
{
- if(!ip_hdr(skb)) /* not IP */
- return 0;
- if(ip_hdr(skb)->protocol != IPPROTO_TCP &&
- ip_hdr(skb)->protocol != IPPROTO_UDP &&
- ip_hdr(skb)->protocol != IPPROTO_ICMP)
- return 0;
- return 1;
- }
下边看看内核部分:
static struct xt_match xt_layer7_match[] __read_mostly = {
{
- .name = "layer7",
- .family = AF_INET,
- .checkentry = check,
- .match = match,
- .destroy = destroy,
- .matchsize = sizeof(struct xt_layer7_info),
- .me = THIS_MODULE
- }
基础中的工作就是调用match深入分析正则表明式.
struct xt_action_param {
union {
- const struct xt_match *match;
- const struct xt_target *target;
- };
- union {
- const void *matchinfo, *targinfo;
- };
- const struct net_device *in, *out;
- int fragoff;
- unsigned int thoff;
- unsigned int hooknum;
- u_int8_t family;
- bool hotdrop;
- };
1.先是得到客户传的信息:const struct xt_layer7_info * info
=par->matchinfo;
2.决断是不是是补助的说道项目(ip
---> tcp/udp/icmp)
3.收获当前连日ct和master ct消息
4. 确定skb是不是线性
5.找到应用数据的位置:
4. /* now that the skb is linearized, it's safe to set these. */
5. app_data = skb->data + app_data_offset(skb);
- 6. appdatalen = skb_tail_pointer(skb) - app_data;
6.使用regcomp编写翻译传递的字符串正则表明式到regexp结构体中
#define NSUBEXP 10
typedef struct regexp {
- char *startp[NSUBEXP];
- char *endp[NSUBEXP];
- char regstart; /* Internal use only. */
- char reganch; /* Internal use only. */
- char *regmust; /* Internal use only. */
- int regmlen; /* Internal use only. */
- char program[1]; /* Unwarranted chumminess with compiler. */
- } regexp;
7.利用total_acct_packets总计是还是不是为率先个来的报文,假设是申请app_data空间给ct里的layer7字段
8.论断skb->cb
是还是不是为null,为空则附加data到主ct的layer7.app_data,假使有下个报文管理则追加data.(追加的尺度是何许吧?)
,(当skb附加data后会设置skb->cb[0]=1)
9.应用regexec推断相称
10.设置skb->cb[0]=1;然后回到
那边我们特意表达下第多个:
有关ct->master的难点 这里涉及expt
即希望连接的标题,稍微回看一下,在新建贰个ct的时候会查询expect
hlist看看是还是不是是某贰个ct期待的连接.
exp = nf_ct_find_expectation(net, zone, tuple);
if (exp) {
- pr_debug("conntrack: expectation arrives ct=%p exp=%pn",
- ct, exp);
- /* Welcome, Mr. Bond. We've been expecting you... */
- __set_bit(IPS_EXPECTED_BIT, &ct->status);
- ct->master = exp->master;
还或许有一个从前说的只万分前多少个包的主题材料(算是贰个隐式bug):
/* if we've classified it or seen too many packets */
if(total_acct_packets(master_conntrack) > num_packets ||
- master_conntrack->layer7.app_proto) {
具体的编写翻译和装置测量试验这里不再表达.当然大家会意识这些layer7并不圆满,还应该有多数标题亟待去消除.
--------------------------------------分割线
CentOS 7.0关闭私下认可防火墙启用iptables防火墙 http://www.linuxidc.com/Linux/2015-05/117473.htm
iptables使用轨范详解 http://www.linuxidc.com/Linux/2014-03/99159.htm
Linux防火墙iptables详细教程 http://www.linuxidc.com/Linux/2013-07/87045.htm
iptables的备份、苏醒及防火墙脚本的宗旨选用 http://www.linuxidc.com/Linux/2013-08/88535.htm
Linux下防火墙iptables用法律则详解 http://www.linuxidc.com/Linux/2012-08/67952.htm
--------------------------------------分割线
本文永世更新链接地址:http://www.linuxidc.com/Linux/2015-08/121706.htm
本文由js9905com金沙网站-金沙澳门手机版网址发布于计算机,转载请注明出处:【js9905com金沙网站】iptables深入解析-应用层插件