溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

分析數(shù)據(jù)包(Microsoft Visual Studio 2010)

發(fā)布時間:2020-07-24 14:45:09 來源:網(wǎng)絡(luò) 閱讀:488 作者:飛飛飛jjs 欄目:網(wǎng)絡(luò)安全

// Fenxi1.cpp : 定義控制臺應(yīng)用程序的入口點。
//
代碼如下:
#include "stdafx.h"
#include "pcap.h"
#include "bittypes.h"
#pragma comment(lib,"ws2_32.lib")
typedef struct ip_address{
    u_char byte1;
    u_char byte2;
    u_char byte3;
    u_char byte4;
}ip_address;

/* IPv4 首部 */
typedef struct ip_header{
    u_char  ver_ihl;        // 版本 (4 bits) + 首部長度 (4 bits)
    u_char  tos;            // 服務(wù)類型(Type of service)
    u_short tlen;           // 總長(Total length)
    u_short identification; // 標識(Identification)
    u_short flags_fo;       // 標志位(Flags) (3 bits) + 段偏移量(Fragment offset) (13 bits)
    u_char  ttl;            // 存活時間(Time to live)
    u_char  proto;          // 協(xié)議(Protocol)
    u_short crc;            // 首部校驗和(Header checksum)
    ip_address  saddr;      // 源地址(Source address)
    ip_address  daddr;      // 目的地址(Destination address)
    u_int   op_pad;         // 選項與填充(Option + Padding)
}ip_header;

/* UDP 首部*/
typedef struct udp_header{
    u_short sport;          // 源端口(Source port)
    u_short dport;          // 目的端口(Destination port)
    u_short len;            // UDP數(shù)據(jù)包長度(Datagram length)
    u_short crc;            // 校驗和(Checksum)
}udp_header;

/* 回調(diào)函數(shù)原型 */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);

int _tmain(int argc, _TCHAR* argv[])
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
u_int netmask;
char packet_filter[] = "ip and udp";
struct bpf_program fcode;

    /* 獲得設(shè)備列表 */
    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
    {
        fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
        exit(1);
    }
    
    /* 打印列表 */
    for(d=alldevs; d; d=d->next)
    {
        printf("%d. %s", ++i, d->name);
        if (d->description)
            printf(" (%s)\n", d->description);
        else
            printf(" (No description 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);
    scanf("%d", &inum);
    
    if(inum < 1 || inum > i)
    {
        printf("\nInterface number out of range.\n");
        /* 釋放設(shè)備列表 */
        pcap_freealldevs(alldevs);
        return -1;
    }

    /* 跳轉(zhuǎn)到已選設(shè)備 */
    for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
    
    /* 打開適配器 */
    if ( (adhandle= pcap_open(d->name,  // 設(shè)備名
                             65536,     // 要捕捉的數(shù)據(jù)包的部分
                                        // 65535保證能捕獲到不同數(shù)據(jù)鏈路層上的每個數(shù)據(jù)包的全部內(nèi)容
                             PCAP_OPENFLAG_PROMISCUOUS,         // 混雜模式
                             1000,      // 讀取超時時間
                             NULL,      // 遠程機器驗證
                             errbuf     // 錯誤緩沖池
                             ) ) == NULL)
    {
        fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
        /* 釋放設(shè)備列表 */
        pcap_freealldevs(alldevs);
        return -1;
    }
    
    /* 檢查數(shù)據(jù)鏈路層,為了簡單,我們只考慮以太網(wǎng) */
    if(pcap_datalink(adhandle) != DLT_EN10MB)
    {
        fprintf(stderr,"\nThis program works only on Ethernet networks.\n");
        /* 釋放設(shè)備列表 */
        pcap_freealldevs(alldevs);
        return -1;
    }
    
    if(d->addresses != NULL)
        /* 獲得接口第一個地址的掩碼 */
        netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
    else
        /* 如果接口沒有地址,那么我們假設(shè)一個C類的掩碼 */
        netmask=0xffffff;


    //編譯過濾器
    if (pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) <0 )
    {
        fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
        /* 釋放設(shè)備列表 */
        pcap_freealldevs(alldevs);
        return -1;
    }
    
    //設(shè)置過濾器
    if (pcap_setfilter(adhandle, &fcode)<0)
    {
        fprintf(stderr,"\nError setting the filter.\n");
        /* 釋放設(shè)備列表 */
        pcap_freealldevs(alldevs);
        return -1;
    }
    printf("蔣教壽:1306404018");
    printf("\nlistening on %s...\n", d->description);
    
    /* 釋放設(shè)備列表 */
    pcap_freealldevs(alldevs);
    
    /* 開始捕捉 */
    pcap_loop(adhandle, 0, packet_handler, NULL);
    
    return 0;
}

/* 回調(diào)函數(shù),當收到每一個數(shù)據(jù)包時會被libpcap所調(diào)用 */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
    struct tm *ltime;
    char timestr[16];
    ip_header *ih;
    udp_header *uh;
    u_int ip_len;
    u_short sport,dport;
    time_t local_tv_sec;

    /* 將時間戳轉(zhuǎn)換成可識別的格式 */
    local_tv_sec = header->ts.tv_sec;
    ltime=localtime(&local_tv_sec);
    strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);

    /* 打印數(shù)據(jù)包的時間戳和長度 */
    printf("%s.%.6d len:%d ", timestr, header->ts.tv_usec, header->len);

    /* 獲得IP數(shù)據(jù)包頭部的位置 */
    ih = (ip_header *) (pkt_data +
        14); //以太網(wǎng)頭部長度

    /* 獲得UDP首部的位置 */
    ip_len = (ih->ver_ihl & 0xf) * 4;
    uh = (udp_header *) ((u_char*)ih + ip_len);

    /* 將網(wǎng)絡(luò)字節(jié)序列轉(zhuǎn)換成主機字節(jié)序列 */
    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);
}

項目-->**屬性(alt+F7)
配置屬性-->C/C++-->常規(guī)-->附加包含目錄-->(是把頭文件所在的文件路徑(Include)添加到附加目錄中C:\WpdPack\Include)


項目-->**屬性(alt+F7)
配置屬性-->鏈接器-->常規(guī)-->附加庫目錄-->(是把Packet.lib;wpcap.lib所在目錄(Lib)添加到附加庫目錄中C:\WpdPack\Lib)


項目-->**屬性(alt+F7)
配置屬性-->鏈接器-->輸入-->附加依賴項-->補充“;Packet.lib;wpcap.lib”

項目-->**屬性(alt+F7)
配置屬性-->C/C++-->預(yù)處理器-->預(yù)處理器定義-->補充“;HAVE_REMOTE”

加頭部如下

#include "pcap.h"
#include "bittypes.h"
#pragma comment(lib,"ws2_32.lib")

下面是獲取的結(jié)果圖:

分析數(shù)據(jù)包(Microsoft Visual Studio 2010)


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI