ARPSpoofing教程(四) - 捕获并分析数据包
本程序的主要目标是展示如何解析所捕获的数据包的协议首部。
这个程序可以称为UDPdump,打印一些网络上传输的UDP数据的信息。
1: #include"pcap.h"
2: typedef struct ip_address{
3: u_char byte1,byte2,byte3,byte4;
4: }ip_address;
5: typedef struct ip_header{
6: u_char ver_ihl; // 版本 (4 bits) + 首部长度 (4 bits)
7: u_char tos; // 服务类型(Type of service)
8: u_short tlen; // 总长(Total length)
9: u_short idenfication;
10: u_short flags_fo;
11: u_char ttl;
12: u_char proto;
13: u_short crc;
14: ip_address saddr;
15: ip_address daddr;
16: u_int op_pad;
17: }ip_header;
18:
19: //UDP head
20: typedef struct udp_header{
21: u_short sport; // 源端口(Source port)
22: u_short dport; // 目的端口(Destination port)
23: u_short len; // UDP数据包长度(Datagram length)
24: u_short crc; // 校验和(Checksum)
25: }udp_header;
26: //每次捕获到数据包时,libpcap都会自动调用这个回调函数
27: void packet_handler(u_char *param,const pcap_pkthdr*header,const u_char *ptk_data){
28:
29: char timestr[16];
30: //将时间戳转换成可识别的格式
31: time_t local_tv_sec=header->ts.tv_sec;
32: tm *ltime=localtime(&local_tv_sec);
33: strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
34: printf("%s,%.6d len:%d\n",timestr,header->ts.tv_usec,header->len);
35:
36: ip_header *iphead=(ip_header*)(ptk_data+14);
37: u_int ip_len=(iphead->ver_ihl&0xf)*4;
38: udp_header *udphead=(udp_header*)((u_char*)iphead+ip_len);
39: u_short sport=ntohs(udphead->sport);
40: u_short dport=ntohs(udphead->dport);
41: printf("%d.%d.%d.%d.%d -> %d.%d.%d.%d.%d\n",
42: iphead->saddr.byte1,
43: iphead->saddr.byte2,
44: iphead->saddr.byte3,
45: iphead->saddr.byte4,
46: sport,
47: iphead->daddr.byte1,
48: iphead->daddr.byte2,
49: iphead->daddr.byte3,
50: iphead->daddr.byte4,
51: dport);
52:
53: }
54: int main(){
55: pcap_if_t *alldevs;
56: pcap_t *adhandle;
57: char errbuf[PCAP_ERRBUF_SIZE];
58: u_int netmask=0;
59: bpf_program fcode;
60: char packet_filter[]="ip and udp";
61:
62: if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&alldevs,errbuf)==-1){
63: fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
64: exit(1);
65: }
66:
67: //print device list
68: int count=1;
69: for(pcap_if_t *d=alldevs;d!=NULL;d=d->next){
70: printf("%d. %s",count++,d->name);
71: printf("(%s)\n",d->description);
72: }
73: if(count==1) {
74: printf("No interface found! Make sure WinPcap is isntalled\n");
75: return -1;
76: }
77: int num;
78: printf("Enter the interface number:(1-%d): ",count);
79: scanf("%d",&num);
80: if(num<1||num>count){
81: printf("Out Of Range\n");
82: pcap_freealldevs(alldevs);
83: return -1;
84: }
85: pcap_if_t *d=alldevs;
86: // 跳转到选中的适配器
87: for(int i=0;i<num-1;i++,d=d->next);
88: // 设备名
89: // 65535保证能捕获到不同数据链路层上的每个数据包的全部内容
90: // 混杂模式
91: // 读取超时时间
92: // 远程机器验证
93: // 错误缓冲池
94: adhandle=pcap_open(d->name,65536,PCAP_OPENFLAG_PROMISCUOUS,1000,NULL,errbuf);
95: if(adhandle==NULL){
96: fprintf(stderr,"Unable to open the adapter %s",d->name);
97: return -1;
98: }
99: if (pcap_datalink(adhandle)!=DLT_EN10MB){
100: fprintf(stderr,"This program works only on Ethernet network.\n");
101: pcap_freealldevs(alldevs);
102: return -1;
103: }
104:
105:
106: if(d->addresses!=NULL)
107: netmask=((sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
108: else
109: netmask=0xffffff;
110: printf("debug: %d\n",netmask);
111: if(pcap_compile(adhandle,&fcode,packet_filter,1,netmask)<0){
112: fprintf(stderr,"Error setting thr filter\n");
113: pcap_freealldevs(alldevs);
114: return -1;
115: }
116: printf("Listening on %s...\n",d->description);
117:
118: pcap_freealldevs(alldevs);
119: //开始捕获
120: pcap_loop(adhandle,0,packet_handler,NULL);
121:
122:
123: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
试验结果:

ARPSpoofing教程(四) - 捕获并分析数据包的更多相关文章
- Wireshark数据抓包教程之认识捕获分析数据包
Wireshark数据抓包教程之认识捕获分析数据包 认识Wireshark捕获数据包 当我们对Wireshark主窗口各部分作用了解了,学会捕获数据了,接下来就该去认识这些捕获的数据包了.Wiresh ...
- 使用winpcap多线程抓包,以及简单的分析数据包
刚开始使用winpcap数据包的时候,我在抓包的时候使用了 pcap_loop(adhandle, 0, packet_handler, NULL); 这个回调函数进行抓包.同时在回调函数中分析IP地 ...
- Wireshark 如何捕获网络流量数据包
转自:http://www.4hou.com/web/7465.html?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutia ...
- #WEB安全基础 : HTTP协议 | 0x7 学会使用wireshark分析数据包
wireshark是开源,免费,跨平台的抓包分析工具 我们可以通过wireshark学习HTTP报文和进行抓包分析,在CTF中的流量分析需要用到抓包 1.下载和安装 这是wireshark的官网 ht ...
- 使用wireshark在windows平台下捕获HTTP协议数据包中的帐号密码信息
1.打开wireshark软件,从Interface List中选择相应的网卡,例如我的PC机上是“本地连接”,然后选择”Start”启动抓包程序. 2.打开学校主页,输入账号和密码登录校内邮箱. 3 ...
- wireshark1.8捕获无线网卡的数据包——找不到无线网卡!
问题说明:奇怪的是,我线网卡明明有的,是interl的型号,可是wireshark总是找不到,如下: 奇了怪了,没有!原来是如下的: 实际上这块无线网卡是存在的,只不过由于兼容或驱动的原因无法显示型号 ...
- winPcap编程之打开适配器并捕获数据包(四 转)
在贴源码之前先介绍一个将要用到的很重要的函数--pcap_open(),下面是pcap_open()在remote-ex.h中的声明: pcap_t *pcap_open(const char *so ...
- wireshark数据包分析实战 第三、四章
1,wireshark支持的协议上千种,开源的. 2,wireshark需要winpcap驱动支持.winpcap驱动的作用通过操作系统捕捉原始数据包.应用过滤器.将网卡切换为混杂模式. 3,捕获文件 ...
- 详解DHCP工作方法,并用wireshark对DHCP四个数据包抓包分析
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
随机推荐
- 使用 Eclipse 调试 Java 程序的技巧
你应该看过一些如<关于调试的N件事>这类很流行的帖子 .假设我每天花费1小时在调试我的应用程序上的话,那累积起来的话也是很大量的时间.由于这个原因,用这些时间来重视并了解所有使我们调试更方 ...
- C 语言学习的第 02 课:C 语言的开发环境
工欲善其事,必先利其器.不知道还是不是记得上一篇文章中说到的,计算机本身是一个数据输入及输出的设备.所以,为了将你大脑中的各种 idea 输入到电脑,且最终生成能够执行的程序,总是要预备点什么的. 通 ...
- FlashFXP|FTP
经典的FTP传输工具FlashFxp,留作几年吧!看和曾经用的软件代表着岁月的流逝和时间的推进性! 洒脱度过生活中的每一天.每一分钟,Mvpbang追随一生! 压缩包中有秘钥文件-flashfxp.k ...
- wsdl说明书
WSDL文档的结构实例解析 <?xml version="1.0" encoding="UTF-8"?> <definitions xmlns ...
- Jenkins+svn+maven
首先我们在我们的服务器上安装好svn和maven 这里在前两步骤基本上没有啥问题,主要就是在Jenkins的步骤我弄了好长时间,这里记录一下 Jenkins的问题我是在这个网址解决的:http://b ...
- openwrt的环境搭建、定制和编译
参考1:编译openwrt全过程 参考2: ARM9的OpenWRT系统的移植以及 无线视觉操控系统的软件开发 参考3:搭建OpenWrt开发环境(包括编译过程) 参考4:各个openwrt版本的sv ...
- ActiveMQ(七)_伪集群和主从高可用使用
一.本文目的 介绍如何在同一台虚拟机上搭建高可用的Activemq服务,集群数量包含3个Activemq,当Activemq可用数>=2时,整个集群可用. 本 ...
- 【BZOJ 3531】【SDOI 2014】旅行
因为有$10^5$个宗教,需要开$10^5$个线段树. 平时开的线段树是“满”二叉树,但在这个题中代表一个宗教的线段树管辖的区间有很多点都不属于这个宗教,也就不用“把枝叶伸到这个点上”,所以这样用类似 ...
- ios审核要注意的地方(转)
磨刀不误砍柴工.作为手机应用开发者,你需要向应用商店提交应用审核,迅速通过审核可以让你抢占先机.对苹果iOS应用开发者来说尤其如此.苹果应用商店的审核近乎吹毛求疵,下面这些清单可以让你知道苹果会在哪些 ...
- uva10870 矩阵
f(n) = a1f(n − 1) + a2f(n − 2) + a3f(n − 3) + . . . + adf(n − d), for n > d, 可以用矩阵进行优化,直接构造矩阵,然后快 ...