`
memorymyann
  • 浏览: 266333 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

28.sniffer程序2

阅读更多

对前面27中sniffer中的一个改进。这里只贴出改动地方的代码。

void get_packet(u_char * args, const pcap_pkthdr * header, const u_char * packet){
        char from_ip[24], to_ip[24];
        const struct sniffer_ethernet * ethernet = (struct sniffer_ethernet *) packet;

//packet记载了包的内容,因为是从数据链路层开始抓取,第一头部当然是以太网(当然不是绝对,但大部分局域网是以太网,如果是其它网络,就 用对应的数据结构

//前面我设置的端口是常用的WEB端口80,WEB协议是HTTP,下一层则是TCP,TCP又是构建在IP基础之上,所以这里包的分层解析就很 清楚了
        const struct ip * ip_header = (struct ip *)(packet + SIZE_ETHERNET);

//越过以太网头部,获取IP头部,struct ip是linux中对IP头部的一个数据结构,如果是windows机器,就请使用对应的数据结构

        const struct tcphdr * tcp_header = (struct tcphdr *)(packet + SIZE_ETHERNET + sizeof(struct ip));
//越过以太网和IP头部,获取TCP头部指针,同样道理struct tcphdr是linux下的tcp描述结构,如果是windows机器,换成对应的数据结构
        inet_ntop(AF_INET, &ip_header->ip_src, from_ip, sizeof(from_ip));
        inet_ntop(AF_INET, &ip_header->ip_dst, to_ip, sizeof(to_ip));

//从ip头部获取目的IP地址和源IP地址

        printf("from %s:%d to %s:%d, ip packet len = %d\n", from_ip, ntohs(tcp_header->source), to_ip, ntohs(tcp_header->dest), ip_header->ip_len);

//从TCP头部可以获取目的端口和源端口


        printf("tcp syn = %d, ack = %d\n", tcp_header->syn, tcp_header->ack);

        char * content = (char *)(packet + SIZE_ETHERNET + sizeof(struct ip) + sizeof(struct tcphdr));

//这里就是TCP包所承载的内容所在,80号端口自然是HTTP内容,这里还包括HTTP头部,但HTTP是应用层协议,linux内核中集成了 TCP和UDP协议,并没有关于HTTP描述。不过HTTP比较简单,之后的运行结果可以看到HTTP内容


        string content_str(content);
        int loc = content_str.find("HTTP", 0);
        if(loc != string::npos) { //这个判断后面运行结果会提到
                cout << content_str << endl;
        }
}

 

改动后

void get_packet(u_char * args, const pcap_pkthdr * header, const u_char * packet){
        char from_ip[24], to_ip[24];
        const struct sniffer_ethernet * ethernet = (struct sniffer_ethernet *) packet;
        const struct ip * ip_header = (struct ip *)(packet + SIZE_ETHERNET);
        const struct tcphdr * tcp_header = (struct tcphdr *)(packet + SIZE_ETHERNET + sizeof(struct ip));

        inet_ntop(AF_INET, &ip_header->ip_src, from_ip, sizeof(from_ip));
        inet_ntop(AF_INET, &ip_header->ip_dst, to_ip, sizeof(to_ip));

        printf("from %s:%d to %s:%d, ip packet len = %d, ip head len = %d\n", from_ip, ntohs(tcp_header->source), to_ip, ntohs(tcp_header->dest), ntohs(ip_header->ip_len), ip_header->ip_hl * 4);
        printf("tcp syn = %d, ack = %d, fin = %d, seq = %d, len = %d\n", tcp_header->syn, tcp_header->ack, tcp_header->fin, ntohl(tcp_header->seq), tcp_header->doff * 4);

//一般情况下tcp头部和ip头部的长度都是20个字节,但他们都是可以增加的,因此之前获取TCP承载的数据的头不指针的办法char * content = (char *)(packet + SIZE_ETHERNET + sizeof(struct ip) + sizeof(struct tcphdr));所获取的位置不一定正确,ip头长度记录在ip_hl中,tcp头部长度记录在doff中,至于×4的原因,因为TCP和IP头部都是以4字节为基本单位来增长。

//至于判断,ip_len记录了IP包总长度,如果TCP和IP实际长度之和等于IP包总长,则说明IP包没有承载数据,(比如三路握手的起始的3个包就是不带数据的),如果大于则说明有数据,一般情况下不可能出现小于的情况。
        if(ntohs(ip_header->ip_len) == ip_header->ip_hl * 4 + tcp_header->doff * 4) {
                printf("no data\n");
        } else {
        char * content = (char *)(packet + SIZE_ETHERNET + ip_header->ip_hl * 4 + tcp_header->doff * 4);

// +的意思是向后移动

//承载的数据起始指针,应该就是头指针+以太网包头长度+ip包头长度+tcp包头长度,依次类推UDP换成UDP即可,关键是弄清楚协议所在的层次,和所使用的协议
        printf("data = %s\n", content);
    }   
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics