WinPcap学习(七)分析数据包

这次的主要目标是展示如何解析所捕获的数据饭协议首部,这里选择分析UDP协议而不是其它协议,是因为它比其它的协议更简单。

 

/*
 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
 * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Politecnico di Torino, CACE Technologies
 * nor the names of its contributors may be used to endorse or promote
 * products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#include<iostream>
#include<pcap.h>

// 4字节的IP地址
struct ip_address{
    u_char byte1;
    u_char byte2;
    u_char byte3;
    u_char byte4;
};
//ipv4首部
struct ip_header{
    u_char ver_ihl;             // 版本(4 bits)+首部长度(4 bits)
    u_char tos;                 // 服务类型(Type of service)
    u_short tlen;               // 总长
    u_short identification;     // 标识
    u_short flags_fo;           // 标志位(3 bits)+段偏移(13 bits)
    u_char ttl;                 // 存活时间
    u_char proto;               // 协议
    u_short crc;                // 首部校验和
    ip_address saddr;           // 源地址
    ip_address daddr;           // 目的地址
    u_int op_pad;               // 选项与填充
};
// UDP首部
struct udp_header{
    u_short sport;              // 源端口
    u_short dport;              // 目的端口
    u_short len;                // UDP数据包长度
    u_short crc;                //校验和
};
/* 回调函数原型 */
void packet_handler(u_char *param, const  pcap_pkthdr *header, const u_char *pkt_data);

int main()
{
    pcap_if_t *alldevs;
    pcap_if_t *d;
    char errbuf[PCAP_ERRBUF_SIZE];
    //获取设备列表
    if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&alldevs,errbuf)==-1)
    {
        fprintf(stderr,"Error in pcap_findalldevs:%s\n",errbuf);
        exit(1);
    }

    //打印列表
    int i = 0;
    for(d=alldevs;d;d=d->next)
    {
        printf("%d. %s",++i,d->name);
        if(d->description)
            printf("(%s)\n",d->name);
        else
            printf("(No descriptioin available)\n");
    }
    if(i==0)
    {
        printf("\nNo interfaces found! Make sure WinPcap is installed. \n");
        return -1;
    }
    printf("Enter the interface number (1-%d):",i);
    int inum;
    scanf("%d",&inum);
    if(inum<1||inum>i)
    {
        printf("\nInterface number out of range.\n");
        pcap_freealldevs(alldevs);
        return -1;
    }
    //跳转到已选设备
    for(d=alldevs,i=0;i<inum-1;d=d->next,i++)
        ;
    //打开适配器
    pcap_t *adhandle;
    if((adhandle=pcap_open(d->name,65536,PCAP_OPENFLAG_PROMISCUOUS,1000,NULL,errbuf))==NULL)
    {
        fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n",errbuf);
        pcap_freealldevs(alldevs);
        return -1;
    }
    //检查数据链路层,为了简单,只考虑以太网
    if(pcap_datalink(adhandle)!=DLT_EN10MB)
    {
        fprintf(stderr,"\nThis program workds only on Ethernet networks. \n");
        pcap_freealldevs(alldevs);
        return -1;
    }
    u_int netmask;
    if(d->addresses != NULL)
        netmask = ((sockaddr_in*)(d->addresses->netmask))->sin_addr.S_un.S_addr;
    else
        netmask = 0xffffff;
    char packet_filter[] = "ip and udp";
    bpf_program fcode;
    //编译过滤器
    if(pcap_compile(adhandle,&fcode,packet_filter,1,netmask))
    {
        fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax. \n");
        pcap_freealldevs(alldevs);
        return -1;
    }
    //设置过滤器
    if(pcap_setfilter(adhandle,&fcode)<0)
    {
        fprintf(stderr,"\nError setting the filter.\n");
        pcap_freealldevs(alldevs);
        return -1;
    }
    printf("\nlistening on %s...\n",d->description);
    pcap_freealldevs(alldevs);
    pcap_loop(adhandle,0,packet_handler,NULL);
    return 0;
}
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
    tm *ltime;
    char timestr[16];
    ip_header *ih;
    udp_header *uh;
    u_int ip_len;
    u_short sport,dport;
    time_t local_tv_sec;
    //将时间戳转换为可识别的格式
    local_tv_sec = header->ts.tv_sec;
    ltime = localtime(&local_tv_sec);
    strftime(timestr,sizeof timestr,"%H:%M:%S",ltime);
    //打印数据包的时间戳和长度
    printf("%s.%.6d len:%d",timestr,header->ts.tv_usec,header->len);
    //获取IP数据据包头部的位置
    ih = (ip_header*) (pkt_data+14);//以太网头部长度
    //获取UDP首部位置
    ip_len = (ih->ver_ihl&0xf)*4;
    uh = (udp_header*)((u_char*)ih+ip_len);
    //将网络字节序列转换成主机字节序列
    sport = ntohs(uh->sport);
    dport = ntohs(uh->dport);
    //打印IP地址和UDP端口
    printf("%d.%d.%d.%d.%d -> %d.%d.%d.%d.%d\n",
           ih->saddr.byte1,ih->saddr.byte2,ih->saddr.byte3,ih->saddr.byte4,sport,
           ih->daddr.byte1,ih->daddr.byte2,ih->daddr.byte3,ih->daddr.byte4,dport);
}

首先,将过滤器设置成“ip and udp”。在这种方式下,我们确信packet_handler()只会收到基于IPv4的UDP数据包,这将简化解析过程,提高程序的效率。

 

packet_handler(),尽管只受限于单个协议的解析(比如基于IPV4的UDP),不过它展示了捕捉器(sniffers)是多么的复杂,就像TcpDump和WinDump对网络数据流进行解码那样。在开始捕捉前,使用了pcap_datalink()对MAC层进行检测,以确保我们是在处理一个以太网络。
 

最后欢迎大家访问我的个人网站: 1024s

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LIBRARY ortp EXPORTS ortp_init ortp_scheduler_init ortp_exit ortp_get_scheduler ortp_set_log_file ortp_set_log_level_mask ortp_logv_out ortp_set_log_handler ortp_strdup_printf ortp_logv ortp_get_global_stats ortp_global_stats_display session_set_new session_set_select session_set_destroy rtp_stats_display rtp_session_get_stats rtp_session_init rtp_session_new rtp_session_set_scheduling_mode rtp_session_set_blocking_mode rtp_session_set_profile rtp_session_get_profile rtp_session_set_data rtp_session_signal_connect rtp_session_signal_disconnect_by_callback rtp_session_set_ssrc rtp_session_set_seq_number rtp_session_get_seq_number rtp_session_set_jitter_compensation rtp_session_set_local_addr rtp_session_set_remote_addr rtp_session_enable_adaptive_jitter_compensation rtp_session_set_recv_buf_size rtp_session_send_with_ts rtp_session_sendm_with_ts rtp_session_set_sockets rtp_session_get_rtp_socket rtp_session_get_rtcp_socket rtp_session_set_payload_type rtp_session_set_send_payload_type rtp_session_get_send_payload_type rtp_session_set_recv_payload_type rtp_session_get_recv_payload_type rtp_session_recv_with_ts rtp_session_recvm_with_ts rtp_session_create_packet rtp_session_get_current_send_ts rtp_session_get_current_recv_ts rtp_session_reset rtp_session_uninit rtp_session_destroy rtp_add_csrc rtp_session_send_dtmf rtp_session_set_source_description rtp_session_set_symmetric_rtp rtp_profile_new rtp_profile_set_payload rtp_profile_clone_full rtp_profile_destroy rtp_profile_get_payload_from_rtpmap payload_type_set_send_fmtp payload_type_clone fmtp_get_value ortp_free ortp_malloc ortp_strdup ortp_realloc ortp_malloc0 freemsg dupmsg allocb getq putq msgpullup qinit flushq msgdsize peekq freeb dupb concatb WIN_thread_create WIN_thread_join WIN_cond_init WIN_mutex_init WIN_mutex_unlock WIN_cond_wait WIN_mutex_lock WIN_cond_destroy WIN_mutex_destroy WIN_cond_signal __ortp_log_mask rtp_session_register_event_queue rtp_session_unregister_event_queue ortp_ev_queue_new ortp_ev_queue_flush ortp_ev_queue_get ortp_ev_queue_destroy ortp_event_get_type ortp_event_get_data ortp_event_destroy stunParseHostName stunParseServerName sendMessage stunEncodeMessage stunBuildReqSimple stunParseMessage stunServerProcessMsg stunNatType stunTest stunOpenSocket stunOpenSocketPair getWinSocketError rtp_session_set_dscp esballoc rtp_session_flush_sockets rtp_session_resync rtp_session_set_remote_addr_and_port rtp_session_resync rtp_session_set_time_jump_limit copymsg rtp_session_enable_jitter_buffer rtp_session_compute_recv_bandwidth rtp_session_compute_send_bandwidth rtp_session_set_rtp_socket_recv_buffer_size rtp_session_set_rtp_socket_send_buffer_size rtp_session_set_jitter_buffer_params rtp_get_payload rtp_session_set_remote_addr_full b64_decode rtp_session_send_rtcp_APP copyb
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
影响等。我们可以建立农业知识图谱,整合和共享农业领域的专业知识和经验。我们还可以开发农业数据平台,提供农业数据的存储、管理和分析服务Winpcap是一个Windows平台上的网络数据包捕获库,它可以用于实时捕获和分析网络。通过农业大数据的应用,我们可以提高农业生产的科学性和效率,推动乡数据包。要实现Winpcap数据包协议分析,你可以按照以下步骤进行操作: 1. 安村振兴的可持续发展。 总之,作为信息科学与技术学院的学生,我们应装Winpcap库:首先,你需要下载并安装Winpcap库。可以从Winpcap官方网站该充分发挥自身的专业优势,积极参与乡村振兴的工作。通过应用信息(https://www.winpcap.org/)下载适合你的操作系统版本的Winpcap安装程序。 2. 配技术、运用大数据分析和机器学习算法,我们可以推动农业现代化、促进置开发环境:在你的开发环境中,需要将Winpcap库包含到你的项目中。智慧农村建设、推动农村电商发展、提升农业大数据应用水平。我们的具体的配置方式取决于你使用的开发工具,比如Visual Studio等。 3. 打开网络适配专业知识和技能可以为实现可持续的乡村振兴发展贡献自己的一份力器:使用Winpcap库提供的函数,打开一个网络适配器以便开始捕获数据包。你可以量。让我们紧密关注乡村振兴战略,积极参与实践,为

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值