Chinaunix首页 | 论坛 | 博客
  • 博客访问: 452131
  • 博文数量: 42
  • 博客积分: 1325
  • 博客等级: 中尉
  • 技术积分: 1312
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-13 18:00
个人简介

呵~~~呵~~~

文章分类

全部博文(42)

文章存档

2016年(3)

2015年(1)

2014年(2)

2013年(2)

2012年(7)

2011年(11)

2010年(3)

2009年(13)

我的朋友

分类: LINUX

2011-07-08 00:29:45

一、在这里用tcp数据包中的http请求做为分析对象,其它的都可以举一反三.

void dl_ethernet(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
{
        flow_t this_flow//存储从链路层到TCP层信息

        u_char *data = NULL;

        //parse link layer
        u_int caplen = h->caplen;
        u_int length = h->len;
        struct ether_header *eth_header = (struct ether_header *) p;

        if(length != caplen) return;
        if(caplen < sizeof(struct ether_header)) return;
        if(ntohs(eth_header->ether_type) != ETHERTYPE_IP) return;

        //get MAC
        int i = 0;
        for( ; i < ETH_ALEN; i++)
        {
                this_flow.ether_smac[i] = eth_header->ether_shost[i];
                this_flow.ether_dmac[i] = eth_header->ether_dhost[i];
        }

        //end parse link layer,move data point.
        data = (u_char *)(p + sizeof(struct ether_header));
        caplen = caplen - sizeof(struct ether_header);

        //parse IP layer
        struct ip *ip_header = (struct ip *) data;

        if(caplen < sizeof(struct ip)) return;
        if(ip_header->ip_p != IPPROTO_TCP) return;
        if(ntohs(ip_header->ip_off) & 0x1fff) return; //throw fragment


        u_int ip_total_len = ntohs(ip_header->ip_len);
        if(caplen < ip_total_len) return;

        u_int ip_header_len = ip_header->ip_hl * 4;
        if (ip_header_len > ip_total_len) return;

        //get IP
        this_flow.src = (u_int32_t)ntohl(ip_header->ip_src.s_addr);
        this_flow.dst = (u_int32_t)ntohl(ip_header->ip_dst.s_addr);

        //end parse IP layer,move data point
        data = data + ip_header_len;
        length = ip_total_len - ip_header_len;

        //parse TCP layer
        struct tcphdr *tcp_header = (struct tcphdr *)data;

        if (length < sizeof(struct tcphdr)) return;
        u_int tcp_header_len = tcp_header->th_off * 4//编译时可能会报错.参照/usr/include/netinet/tcp.h进行宏定义.或者直接修改源文件.

        //get tcp info
        this_flow.sport = ntohs(tcp_header->th_sport);
        this_flow.dport = ntohs(tcp_header->th_dport);
        this_flow.seq   = ntohl(tcp_header->th_seq);
        this_flow.ack   = ntohl(tcp_header->th_ack);
        this_flow.isfin = tcp_header->th_flags & TH_FIN;

        //end parse TCP layer,move data point
         data += tcp_header_len;
         length -= tcp_header_len;

        //process application layer
        if(length < 12) return;
        if(data[0] != 'G' || data[1] != 'E' ||data[2] != 'T') return;

        char *Get = NULL, *Get_end = NULL;
        char *Refer = NULL, *Refer_end = NULL;
        char *Host = NULL, *Host_end = NULL;
        char *UserAgent =NULL, *UserAgent_end =NULL;

        //get field "Get"
        Get = data + 4;
        Get_end = strchr( Get, 0x20);
        if(Get_end == NULL)
                return;
        else
                *Get_end = 0x0;

        //get field "Referer"
        Refer = strstr( Get_end + 1, "Referer: ");
        if(Refer != NULL)
        {
                Refer += 9;
                Refer_end = strchr( Refer, 0x0D);
                if( Refer_end == NULL)
                        Refer = NULL;
        }

        //get field "Host"
        Host = strstr( Get_end + 1, "Host: ");
        if(Host != NULL)
        {
                Host += 6;
                Host_end = strchr( Host, 0x0D);
                if( Host_end == NULL)
                        return;
        }
        else
                return;

        //get user-agent field
        UserAgent = strstr( Get_end +1 ,"User-Agent: ");
        if(UserAgent !=NULL)
        {
                UserAgent += 12;
                UserAgent_end = strchr( UserAgent , 0x0D);
                if( UserAgent_end == NULL)
                        UserAgent = NULL;
        }

        if(Refer_end != NULL) *Refer_end = 0x0;
        if(Host_end != NULL) *Host_end = 0x0;
        if(UserAgent_end !=NULL) *UserAgent_end = 0x0;

#ifdef DEBUG
        printf("\n/ * * * * * * * * * * * * /\n");
        printf("URL [http://%s%s]\n", Host, Get);
        if(Refer != NULL) printf("refer [%s]\n", Refer);
        if(UserAgent != NULL) printf("UserAgent[%s]\n", UserAgent);
#endif


        //END ALL
}

void dl_ppp(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
{

//略, 参见《TCP/IP详解 卷1:协议》
}


阅读(1103) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~