基于linux的netfilter处理数据包的过程分析,基于Netfilter的网络数据包分析

前面的几篇文章我已经对Netfilter的大概的机制作了比较详细的介绍,这篇文章我就说一下如何分析网络数据包。我刚刚写了一个程序,程序的功能很简单,就是提取出网络数据包的源地址和改包所使用的网络协议,大家可以看看源代码:

#define __KERNEL__

#define MODULE

#include #include #include #include #include #include #include #include #include #include

static struct nf_hook_ops nfho;

unsigned int hook_func(unsigned int hooknum,

struct sk_buff **skb,

const struct net_device *in,

const struct net_device *out,

int (*okfn)(struct sk_buff *))

{

struct sk_buff *sb = *skb;

unsigned char src_ip[4];

*(unsigned int *)src_ip = sb->nh.iph->saddr;

printk("A packet from:%d.%d.%d.%d Detected!",

src_ip[0],src_ip[1],src_ip[2],src_ip[3]);

switch(sb->nh.iph->protocol)

{

case IPPROTO_TCP:

printk("It's a TCP PACKET\n");break;

case IPPROTO_ICMP:

printk("It's a ICMP PACKET\n");break;

case IPPROTO_UDP:

printk("It's a UDP PACKET\n");break;

}

return NF_ACCEPT;

}

int init_module()

{

nfho.hook = hook_func;

nfho.hooknum  = NF_IP_PRE_ROUTING;

nfho.pf       = PF_INET;

nfho.priority = NF_IP_PRI_FIRST;

nf_register_hook(&nfho);

return 0;

}

void cleanup_module()

{

nf_unregister_hook(&nfho);

}

这实际上是对前面几篇文章的几个小程序的组合,实际上就是对sk_buff 结构体的的两个元素进行了检测,就得到了源地址和协议的信息。上面的这条语句对于那些C不是很熟悉的人可能吃力了一点:

*(unsigned int *)src_ip = sb->nh.iph->saddr;

我稍微的解释一下,网络的源地址是4个子节的int,因此我定义了一个4个子节的数组src_ip,从而每一个子节里面就存储的点分十进制的一个数,为了一次完成赋值,我把src_ip 转成unsigned int指针,就可以一次4个字节一起访问了。

下面是这个程序的测试结果:

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.107.8 Detected!It's a UDP PACKET

A packet from:210.43.106.214 Detected!It's a UDP PACKET

A packet from:210.43.106.246 Detected!It's a UDP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.106.112 Detected!It's a UDP PACKET

A packet from:210.43.107.8 Detected!It's a UDP PACKET

A packet from:210.43.106.214 Detected!It's a UDP PACKET

A packet from:210.43.106.246 Detected!It's a UDP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.106.112 Detected!It's a UDP PACKET

A packet from:210.43.106.214 Detected!It's a UDP PACKET

A packet from:210.43.106.246 Detected!It's a UDP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.106.112 Detected!It's a UDP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.106.254 Detected!It's a UDP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.107.230 Detected!It's a UDP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.106.214 Detected!It's a UDP PACKET

A packet from:210.43.107.230 Detected!It's a UDP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.106.214 Detected!It's a UDP PACKET

A packet from:210.43.107.230 Detected!It's a UDP PACKET

A packet from:210.43.106.96 Detected!It's a UDP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.106.112 Detected!It's a UDP PACKET

A packet from:210.43.106.214 Detected!It's a UDP PACKET

A packet from:210.43.107.230 Detected!It's a UDP PACKET

A packet from:210.43.106.96 Detected!It's a UDP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.106.112 Detected!It's a UDP PACKET

A packet from:210.43.107.230 Detected!It's a UDP PACKET

A packet from:210.43.106.96 Detected!It's a UDP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.106.112 Detected!It's a UDP PACKET

A packet from:210.43.107.230 Detected!It's a UDP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.106.214 Detected!It's a UDP PACKET

A packet from:210.43.106.96 Detected!It's a UDP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:192.168.1.1 Detected!It's a UDP PACKET

A packet from:192.168.1.1 Detected!It's a UDP PACKET

A packet from:192.168.1.1 Detected!It's a UDP PACKET

A packet from:192.168.1.1 Detected!It's a UDP PACKET

A packet from:192.168.1.1 Detected!It's a UDP PACKET

A packet from:192.168.1.1 Detected!It's a UDP PACKET

A packet from:192.168.1.1 Detected!It's a UDP PACKET

A packet from:192.168.1.1 Detected!It's a UDP PACKET

A packet from:192.168.1.1 Detected!It's a UDP PACKET

A packet from:192.168.1.1 Detected!It's a UDP PACKET

A packet from:210.43.106.214 Detected!It's a UDP PACKET

A packet from:210.43.106.96 Detected!It's a UDP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.107.130 Detected!It's a ICMP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.106.214 Detected!It's a UDP PACKET

A packet from:210.43.106.96 Detected!It's a UDP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.107.130 Detected!It's a ICMP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.106.112 Detected!It's a UDP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.107.130 Detected!It's a ICMP PACKET

A packet from:210.43.106.96 Detected!It's a UDP PACKET

A packet from:210.43.106.210 Detected!It's a UDP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.106.112 Detected!It's a UDP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

A packet from:210.43.107.136 Detected!It's a UDP PACKET

A packet from:210.43.107.130 Detected!It's a TCP PACKET

如果需要对包的端口进行分析的话,就要对IP报文的数据段(sb->data)进行分析了(TCP和UDP等包都是作为IP的数据而存在的),大家可以参考一下相应的资料。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/368063.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java EE CDI程序化依赖关系消歧示例–注入点检查

在本教程中,我们将看到在注入Java EE CDI bean时如何避免程序依赖消除歧义。 我们已经在Jave EE依赖关系消除歧义示例中展示了如何避免CDI Bean中的依赖关系歧义消除。 在这里,我们将向您展示如何以动态方式避免依赖消除歧义。 我们将通过检查注入另一个…

机器学习算法整理(四)集成算法—随机森林模型

随机:数据采样随机,特征选择随机 (数据采样,有放回) 转载于:https://www.cnblogs.com/douzujun/p/8386930.html

CSS 布局入门

概述 Web 兴起之后,关于CSS的介绍和学习资料已经铺天盖地。 本文不涉及具体的CSS语法之类的,而是希望从初学者的角度,让没有接触或很少接触CSS的人能快速的了解 CSS 到底是什么以及如何使用。 什么是 CSS 了解一个概念,首先看到…

迷你搜索引擎–使用Neo4j,Crawler4j,Graphstream和Encog的基础知识

继续执行正在实现搜索引擎的Programming Collection Intelligence (PCI)的第4章。 我可能比做一次运动所咬的东西要多。 我认为, 与其一直使用本书中使用的普通关系数据库结构,不如说我一直想看看Neo4J,所以现在是时候…

linux人脸识别视频推流,RTMP推流协议视频智能分析/人脸识别/直播点播平台EasyDSS接口调用注意事项介绍...

TSINGSEE青犀视频目前推出了前端支持不同协议设备接入的视频智能分析平台,包括RTSP协议的EasyNVR、GB28181协议的EasyGBS,RTMP推流协议的EasyDSS,还有能够进行人脸识别、车牌识别的EasyCVR,这些平台均提供了视频转码分发的能力&am…

js高级写法

名称 一般写法优化取整(不四舍五入) parseInt(a,10); //Before Math.floor(a); //Before a>>0; //Before ~~a; //After a|0; //After 取整(四舍五入) Math.round(a); //Beforenum.toFixed(0) a.5|0; //After未定义 undefined; //Before void 0; //After, 快…

IntersectionObserve API使用

why 之前图片懒加载的实现方法大多数为:给window添加scroll事件,滚动时获取元素的offset值,判断元素在viewport内的可见行。这样做的缺点是:频繁的计算dom节点的属性导致性能较差,对scroll绑定的回调函数进行节流能减少…

css小技巧(1)

1、-webkit-overflow-scrolling: touch; 解决ios滑动时无缓冲问题 2、::-webkit-scrollbar 设置ios滑动时是否显示滚动条 3、::selection 选中文字时文字颜色和选中色 <!doctype html> <html> <head> <meta charset"utf-8"> <meta cont…

在基于图论的Java程序中基于DSL的输入图数据的方法

我们大多数人已经编写了一些程序来处理图论算法&#xff0c;例如查找两个顶点之间的最短路径&#xff0c;查找给定图的最小生成树等等。 在这些算法的每一种中&#xff0c;表示图形的编程方式是使用邻接矩阵或邻接表 。 两者都不是定义图形输入的非常直观的方法。 例如&#xf…

linux 暴力删除文件,暴力删除文件

有些时候电脑上会有一些文件删除不了&#xff0c;例如&#xff1a;删除的时候&#xff0c;提示文件不存在&#xff0c;或者请求的安全信息不可用或无法显示。无法删除文件夹的原因:1.当文件夹中存在正在被占用的文件时,删除当然会失败。其中包括有一些病毒程序在运行时,删除文件…

Remmarguts' Date(POJ2449+最短路+A*算法)

题目链接&#xff1a;http://poj.org/problem?id2449 题目&#xff1a; 题意&#xff1a;求有向图两点间的k短路。 思路&#xff1a;最短路A*算法 代码实现如下&#xff1a; 1 #include <set>2 #include <map>3 #include <queue>4 #include <stack>5 …

点击文本框后页面变大

原因&#xff1a;HTML中默认是认为16px的字体&#xff0c;人才能看清楚&#xff0c;所以&#xff0c;当点击文本框的时候&#xff0c;当前文本框会以字体16px的大小显示&#xff08;即字体小于16px页面会变大&#xff09;。解决原理&#xff1a;设置文本框的的字体为16px&#…

通过外部文件覆盖打包的Spring应用程序属性文件

开发Spring应用程序时&#xff0c;最常见的用例是您希望拥有多个版本的配置属性&#xff0c;具体取决于要部署到的位置&#xff0c;例如&#xff0c;数据库URL或功能标志可能是特定于dev&#xff0c;qa&#xff0c;production等环境的。 像大多数Spring开发任务一样&#xff0…

usb3.0 linux无法识别,USB3.0接口不能识别U盘的解决方法

USB接口可以说是电脑的标配&#xff0c;现在基本上所有电脑都会搭载USB接口。而USB标准从1.0发展到现在的3.0&#xff0c;甚至更新的也已出来。不过&#xff0c;如果USB3.0无法识别U盘&#xff0c;那该怎么办呢?USB3.0是一种技术也是一种规范&#xff0c;现在很多笔记本都是默…

table 鼠标移上去改变单元格边框颜色。

表格定义了 border-collapse:collapse;边框会合并为一个单一的边框。会忽略 border-spacing 和 empty-cells 属性。用td:hover,显示不全 搜索了好久&#xff0c;没有找到好的方法&#xff0c;用左右边框也不完美。 于是就在想&#xff0c;移上去的时候给加个伪元素after&#…

PotPlayer安装与配置

目录 1.简介 2.安装 3.设置 基本选项设置&#xff1a; 播放选项设置&#xff1a; PotPlayer皮肤设置&#xff1a; 1.简介 PotPlayer一款小巧简单的视频播放软件&#xff0c;具有于强大的定制能力和个性化功能。 2.安装 官网下载 potplayer http://potplayer.daum.net/?langzh_…

如何使用反射来基于JPA注释记录数据模型

因此&#xff0c;当您仅可以注释Java类时&#xff0c;使用JPA&#xff0c;Hibernate或EBeans会很酷&#xff0c;但是您不是一直希望可以从代码“生成”数据模型的文档吗&#xff1f; 提取JPA / Hibernate和其他验证批注的信息&#xff1f; 假设您的bean中包含所有这些漂亮的注…

Hadoop的目录结构

转载于:https://www.cnblogs.com/wzlbigdata/p/8392162.html

css3制作广告栏效果的疑问?

【整理】原文&#xff1a;https://segmentfault.com/a/1190000007087701 本人新手&#xff0c;国庆苦逼加无用班&#xff0c;那是我在夕阳下的奔跑吗&#xff1f;闲来无聊整理以前学习的资料&#xff0c;关于广告栏的效果制作&#xff0c;详情观看[这里][1]。其中用了一个作者自…

linux 实验 ps,Linux实验室:监控命令iostat与ps_服务器x86服务器-中关村在线

4、iostat与上面的命令相似&#xff0c;很显然&#xff0c;这个linux系统监控命令是属于IO监控系列的&#xff0c;iostat(I/O statistics&#xff0c;输入输出统计)是一个用于收集显示系统存储设备输入和输出状态统计的简单工具。例如命令&#xff1a;iostat -m -x 1 1000。从结…