本文可任意转载,但请保留作者及出处
作者:rainfish
出处:http://blog.csdn.net/bat603/
经过几天的突击,终于明白了怎样在局域网内抓包,这可是我多年来的梦想。首先说说我的学习过程,一开始从网上搜索了关于sniffer大量资料,大致学会了,可是仔细分析结果发现,都是本机上的数据包,而不是整个局域网的。于是又查资料,在 linuxsir上有高人指点,说,现在局域网内都是交换机联接,而不是以前的Hub所以,如果要抓整个局域网的数据包,必须用libpcap,于是又查了许多关于Libpcap的资料,经过一天的努力,总算稍微有点眉目了。总结手里的资料,它们都在讲怎样用libpcap抓包,而没有讲怎样去分析包,所以在下就写了一个小小的例子,去分析数据包里的具体信息,如果有不正确的地方,敬请指正。
关于libpcap的使用方法,请参考我收集的的资料
http://blog.csdn.net/bat603/archive/2006/09/04/1175729.aspx
http://blog.csdn.net/bat603/archive/2006/09/04/1176251.aspx
下边这个比较深奥
http://blog.csdn.net/bat603/archive/2006/09/04/1175271.aspx

源代码及解释
/**************************************************************
********************rainfish**********************************
***************http://blog.csdn.net/bat603/**********************
**********本文可任意转载,但请保留作者及出处*****************/

//该程序使用方法:./exe_your_file  numpacket
#include <stdio.h>
#include <pcap.h>
/* if this gives you an error try pcap/pcap.h 里面有相应的数据结构一般在/usr/include/中*/
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>
#include <linux/ip.h>
#include <linux/tcp.h>//注意使用的ip、tcp数据结构,至于它们和 <netinet/ip.h | tcp.h>的区别,我也弄不清楚。

/*回调函数,int pcap_loop(pcap_t *p, int cnt,
          pcap_handler callback, u_char *user)调用的
这里需要说明的是,关于参数 pkthdr、packet的说明,好多资料都没有进行解释,在这里我只能尝试的去解释
当执行pcap_loop,会自动调用回调函数,pcap_t *p是调用者传递的,参看下面例子,而pkthdr(libpcap 自定义数据包头部),packet(捕获的书据包)就会相应得到,而不用用户操作。下面的例子也能证明这一点。我实在不敢确定,因为没有找到相应的文档
*/
void my_callback(u_char *userless, const struct pcap_pkthdr *pkthdr, 
                    const u_char *packet)
{
    struct in_addr addr;
    struct iphdr *ipptr;
    struct tcphdr *tcpptr;//太次片,,ip,tcp数据结构
    char *data;
        
    pcap_t *descr = (pcap_t*)userless;//捕获网络数据包的数据包捕获描述字
    //const u_char *packet;
    struct pcap_pkthdr hdr = *pkthdr;//(libpcap 自定义数据包头部),
    struct ether_header *eptr;//以太网字头
    u_char *ptr;
    int i;
 
    if (packet == NULL)//packet里面有内容,可以证明上面的猜想,
    {
        printf ("Didn't grab packet!/n");
        exit (1);
    }
    printf ("/n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$/n");
    printf ("Grabbed packet of length %d/n", hdr.len);
    printf ("Received at : %s/n", ctime((const time_t*)&hdr.ts.tv_sec));
    printf ("Ethernet address length is %d/n", ETHER_HDR_LEN);
    
    eptr = (struct ether_header*)packet;//得到以太网字头
    
    if (ntohs(eptr->ether_type) == ETHERTYPE_IP)
    {
        printf ("Ethernet type hex:%x dec:%d is an IP packet/n",
                    ntohs(eptr->ether_type), ntohs(eptr->ether_type));
    }
    else 
    {
        if (ntohs(eptr->ether_type) == ETHERTYPE_ARP)
        {
            printf ("Ethernet type hex:%x dec:%d is an ARP packet/n",
                        ntohs(eptr->ether_type), ntohs(eptr->ether_type));
        }
        else
        {
            printf ("Ethernet type %x not IP/n", ntohs(eptr->ether_type));
            exit (1);
        }
    }
        
    ptr = eptr->ether_dhost;
    i = ETHER_ADDR_LEN;
    printf ("i=%d/n", i);
    printf ("Destination Address: ");
    do
    {
        printf ("%s%x", (i == ETHER_ADDR_LEN)?"":":", *ptr++);
    }while(--i>0);
    printf ("/n");
    //printf ("%x/n",ptr);
    
    ptr = eptr->ether_shost;
    i = ETHER_ADDR_LEN;
    printf ("Source Address: ");
    do
    {
        printf ("%s%x", (i == ETHER_ADDR_LEN)?"":":", *ptr++);
    }while(--i>0);
    printf ("/n");
    printf ("Now decoding the IP packet./n");
    ipptr = (struct iphdr*)    (packet+sizeof(struct ether_header));//得到ip包头
    
    printf ("the IP packets total_length is :%d/n", ipptr->tot_len);
    printf ("the IP protocol is %d/n", ipptr->protocol);
    addr.s_addr = ipptr->daddr;
    printf ("Destination IP: %s/n", inet_ntoa(addr));    
    addr.s_addr = ipptr->saddr;
    printf ("Source IP: %s/n", inet_ntoa(addr));
    
    printf ("Now decoding the TCP packet./n");
    tcpptr = (struct iphdr*)(packet+sizeof(struct ether_header)
                                    +sizeof(struct iphdr));//得到tcp包头
    printf ("Destination port : %d/n", tcpptr->dest);
    printf ("Source port : %d/n", tcpptr->source);
    printf ("the seq of packet is %d/n", tcpptr->seq);
//以上关于ip、tcp的结构信息请查询/usr/include/linux/ip.h | tcp.h
    
    data = (char*)(packet+sizeof(struct ether_header)+sizeof(struct iphdr)
                                    +sizeof(struct tcphdr));//得到数据包里内容,不过一般为乱码。
    
    printf ("the content of packets is /n%s/n",data);
}
int main(int argc, char **argv)
{
    int i;
    char *dev;
    char errbuf[PCAP_ERRBUF_SIZE];
    pcap_t *descr;
    const u_char *packet;
    struct pcap_pkthdr hdr;
    struct ether_header *eptr;
    
    if (argc != 2)
    {
        fprintf (stdout, "Usage: %s numpackets/n", argv[0]);
        return 0;
    }
    
    dev = pcap_lookupdev (errbuf);
    if (dev == NULL)
    {
        printf ("%s/n", errbuf);
        exit (1);
    }
    
    descr = pcap_open_live (dev, BUFSIZ, 1, -1, errbuf);
    //第三个参数,1为混杂模式;0为非混杂模式
    //BUFSIZ同PCAP_ERRBUF_SIZE一样,均为库文件已经定义好的,不推荐使用
    if (descr == NULL)
    {
        printf ("pcap_open_live(): %s/n", errbuf);
        exit (1);
    }
    pcap_loop (descr, atoi(argv[1]), my_callback, NULL);//调用回调函数
    
 
        
    printf("Hello world/n");
    return (0);
}
关于过滤机制,以后再写

利用libpcap分析网络上的数据包(入门级)的更多相关文章

  1. 利用wireshark抓取远程linux上的数据包

    原文发表在我的博客主页,转载请注明出处. 前言 因为出差,前后准备总结了一周多,所以博客有所搁置.出差真是累人的活计,不过确实可以学习到很多东西,跟着老板学习做人,学习交流的技巧.入正题~ wires ...

  2. Linux内核--网络栈实现分析(七)--数据包的传递过程(下)

    本文分析基于Linux Kernel 1.2.13 原创作品,转载请标明http://blog.csdn.net/yming0221/article/details/7545855 更多请查看专栏,地 ...

  3. Linux内核--网络栈实现分析(二)--数据包的传递过程--转

    转载地址http://blog.csdn.net/yming0221/article/details/7492423 作者:闫明 本文分析基于Linux Kernel 1.2.13 注:标题中的”(上 ...

  4. Snail—iOS网络学习之得到网络上的数据

    在开发项目project中,尤其是手机APP,一般都是先把界面给搭建出来.然后再从网上down数据 来填充 那么网上的数据是怎么得来的呢,网络上的数据无非就经常使用的两种JSON和XML 如今 大部分 ...

  5. Linux内核--网络栈实现分析(二)--数据包的传递过程(上)

    本文分析基于Linux Kernel 1.2.13 原创作品,转载请标明http://blog.csdn.net/yming0221/article/details/7492423 更多请看专栏,地址 ...

  6. IM通信协议逆向分析、Wireshark自定义数据包格式解析插件编程学习

    相关学习资料 http://hi.baidu.com/hucyuansheng/item/bf2bfddefd1ee70ad68ed04d http://en.wikipedia.org/wiki/I ...

  7. 使用异步任务加载网络上json数据并加载到ListView中

    Android中使用网络访问来加载网上的内容,并将其解析出来加载到控件中,是一种很常见的操作.但是Android的UI线程(也就是主线程)中是不允许进行耗时操作的,因为耗时操作会阻塞主线程,影响用户体 ...

  8. 网络编程-pcap数据包格式

    Libpcap的官方网站是http://www.tcpdump.org/,该项目和Tcpdump项目是同一个团队维护.Libpcap是一个平台独立的 数据包捕获开发包,制定了数据包离线存储的事实标准. ...

  9. 利用Fiddler抓取手机APP数据包

    Fiddler是一个调试代理,下载地址http://www.telerik.com/download/fiddler 下载安装运行后,查出运行机器的IP,手机连接同一网域内的WIFI,手机WIFI连接 ...

随机推荐

  1. 虚拟机oracle virtualbox 上安装centos6.5 网络设置

    上篇文章写到,在虚拟机上安装centos6.5,结果依照文章非常顺利的安装了,可是用yum安装软件的时候.报错,源有问题,不能下载,然后ping一下摆渡.非常悲催 dns解析不了,cat /etc/r ...

  2. Python 类的特殊成员方法详解

    class doges(object): """类的描述信息""" def __init__(self,name,food): self.n ...

  3. Java基础知识介绍

    数组的定义及初始化方式 数组对象创建没有() 一维数组 静态初始化: String[] books = {"Thinking in Java","Effective Ja ...

  4. ios 自动布局水平跟垂直居中

    [view addConstraint:[NSLayoutConstraint constraintWithItem:segment attribute:NSLayoutAttributeCenter ...

  5. FZU2171:防守阵地 II(线段树)

     Problem Description 部队中总共同拥有N个士兵,每一个士兵有各自的能力指数Xi.在一次演练中,指挥部确定了M个须要防守的地点,指挥部将选择M个士兵依次进入指定地点进行防守任务.获得 ...

  6. 如何將 MySQL 資料庫轉移到 Microsoft SQL Server 與 Azure SQL Database

    MySQL 是相當常用之資料庫伺服器,而微軟雲端服務 Microsoft Azure 上 Azure SQL Database 是一個功能強大且經濟實惠的選擇,透過本篇文章,使用 SQL Server ...

  7. Atitit.attilax的 case list 项目经验 案例列表

    Atitit.attilax的 case list 项目经验 案例列表 1. Atian inputmethod 输入法3 2. Ati desktop engine桌面引擎3 3. Acc资金账户系 ...

  8. Atitit.编程语言原理---方法重载的实现与设计 调用方法的原理

    Atitit.编程语言原理---方法重载的实现与设计 调用方法的原理 1. 重载包括:普通方法的重载和构造方法的重载 1 1.1. 横向重载”和“纵向重载”1 1.2. 方法签名通过  方法名称,参数 ...

  9. Java获取网卡的mac地址

    为了项目的安全,有时候需要得到电脑的唯一码,比如:网卡的mac地址.和大家分享一下,下面是项目中用到的工具类: import java.io.BufferedReader;import java.io ...

  10. redhat samba安装配置

    突然想要用,好久没配过这玩意了,再次配置一次,记录一下过程. 1.挂载镜像      mount -o loop ~/Desktop/RHEL_5.5\ i386\ DV.iso /mnt       ...