我正在通过pcap和无线进行一个项目。以下示例针对我先前的问题进行了解答,我试图从无线帧中提取mac地址。我已经为radiotap标头和基本管理框架创建了结构。由于某些原因,当尝试输出mac地址时,我正在打印错误的数据。当我与Wireshark进行比较时,我看不到为什么无线电分接头数据可以正确打印,但是mac地址却不能正确打印。当我查看数据包并比较捕获的数据包时,wireshark显示的十六进制转储中没有任何其他填充。我有点熟悉c,但不是专家,所以也许我没有正确使用指针和结构,有人可以帮助我看看我在做什么错吗?

谢谢,
昆汀

//  main.c
//  MacSniffer
//


#include <pcap.h>
#include <string.h>
#include <stdlib.h>

#define MAXBYTES2CAPTURE 65535

#ifdef WORDS_BIGENDIAN
typedef struct frame_control
{
    unsigned int subtype:4; /*frame subtype field*/
    unsigned int protoVer:2; /*frame type field*/
    unsigned int version:2; /*protocol version*/

    unsigned int order:1;
    unsigned int protected:1;
    unsigned int moreDate:1;
    unsigned int power_management:1;

    unsigned int retry:1;
    unsigned int moreFrag:1;
    unsigned int fromDS:1;
    unsigned int toDS:1;
}frame_control;

struct ieee80211_radiotap_header{
    u_int8_t it_version;
    u_int8_t it_pad;
    u_int16_t it_len;
    u_int32_t it_present;
    u_int64_t MAC_timestamp;
    u_int8_t flags;
    u_int8_t dataRate;
    u_int16_t channelfrequency;
    u_int16_t channFreq_pad;
    u_int16_t channelType;
    u_int16_t channType_pad;
    u_int8_t ssiSignal;
    u_int8_t ssiNoise;
    u_int8_t antenna;
};

#else
typedef struct frame_control
{
    unsigned int protoVer:2; /* protocol version*/
    unsigned int type:2; /*frame type field (Management,Control,Data)*/
    unsigned int subtype:4; /* frame subtype*/

    unsigned int toDS:1; /* frame coming from Distribution system */
    unsigned int fromDS:1; /*frame coming from Distribution system */
    unsigned int moreFrag:1; /* More fragments?*/
    unsigned int retry:1; /*was this frame retransmitted*/

    unsigned int powMgt:1; /*Power Management*/
    unsigned int moreDate:1; /*More Date*/
    unsigned int protectedData:1; /*Protected Data*/
    unsigned int order:1; /*Order*/
}frame_control;

struct ieee80211_radiotap_header{
    u_int8_t it_version;
    u_int8_t it_pad;
    u_int16_t it_len;
    u_int32_t it_present;
    u_int64_t MAC_timestamp;
    u_int8_t flags;
    u_int8_t dataRate;
    u_int16_t channelfrequency;
    u_int16_t channelType;
    int ssiSignal:8;
    int ssiNoise:8;
};
#endif
struct wi_frame {
    u_int16_t fc;
    u_int16_t wi_duration;
    u_int8_t wi_add1[6];
    u_int8_t wi_add2[6];
    u_int8_t wi_add3[6];
    u_int16_t wi_sequenceControl;
    // u_int8_t wi_add4[6];
    //unsigned int qosControl:2;
    //unsigned int frameBody[23124];
};

void processPacket(u_char *arg, const struct pcap_pkthdr* pkthdr, const u_char* packet)
{
    int i= 0, *counter = (int *) arg;
    struct ieee80211_radiotap_header *rh =(struct ieee80211_radiotap_header *)packet;
    struct wi_frame *fr= (struct wi_frame *)(packet + rh->it_len);
    u_char *ptr;
    //printf("Frame Type: %d",fr->wi_fC->type);
    printf("Packet count: %d\n", ++(*counter));
    printf("Received Packet Size: %d\n", pkthdr->len);
    if(rh->it_version != NULL)
    {
      printf("Radiotap Version: %d\n",rh->it_version);
    }
    if(rh->it_pad!=NULL)
    {
      printf("Radiotap Pad: %d\n",rh->it_pad);
    }
    if(rh->it_len != NULL)
    {
        printf("Radiotap Length: %d\n",rh->it_len);
    }
    if(rh->it_present != NULL)
    {
        printf("Radiotap Present: %c\n",rh->it_present);
    }
    if(rh->MAC_timestamp != NULL)
    {
        printf("Radiotap Timestamp: %u\n",rh->MAC_timestamp);
    }

    if(rh->dataRate != NULL)
    {
        printf("Radiotap Data Rate: %u\n",rh->dataRate);
    }
    if(rh->channelfrequency != NULL)
    {
        printf("Radiotap Channel Freq: %u\n",rh->channelfrequency);
    }
    if(rh->channelType != NULL)
    {
    printf("Radiotap Channel Type: %06x\n",rh->channelType);
    }
    if(rh->ssiSignal != NULL)
    {
        printf("Radiotap SSI signal: %d\n",rh->ssiSignal);
    }
    if(rh->ssiNoise != NULL)
    {
        printf("Radiotap SSI Noise: %d\n",rh->ssiNoise);
    }

ptr = fr->wi_add1;
int k= 6;
printf("Destination Address:");
do{
    printf("%s%X",(k==6)?" ":":",*ptr++);
}
while(--k>0);
printf("\n");

ptr = fr->wi_add2;
k=0;
printf("Source Address:");
do{
    printf("%s%X",(k==6)?" ":":",*ptr++);
}while(--k>0);
printf("\n");

ptr = fr->wi_add3;
k=0;
do{
    printf("%s%X",(k==6)?" ":":",*ptr++);
}
while(--k>0);
printf("\n");
/* for(int j = 0; j < 23124;j++)
 {
 if(fr->frameBody[j]!= NULL)
 {
 printf("%x",fr->frameBody[j]);
 }
 }
 */
for (i = 0;i<pkthdr->len;i++)
{

    if(isprint(packet[i +rh->it_len]))
    {
        printf("%c",packet[i + rh->it_len]);
    }

    else{printf(".");}



    //print newline after each section of the packet
    if((i%16 ==0 && i!=0) ||(i==pkthdr->len-1))
    {
        printf("\n");
    }

}
return;
}
int main(int argc, char** argv)
{

int count = 0;
pcap_t* descr = NULL;
char errbuf[PCAP_ERRBUF_SIZE], *device = NULL;
struct bpf_program fp;
char filter[]="wlan broadcast";
const u_char* packet;
memset(errbuf,0,PCAP_ERRBUF_SIZE);
device = argv[1];

if(device == NULL)
{
    fprintf(stdout,"Supply a device name ");
}

descr = pcap_create(device,errbuf);
pcap_set_rfmon(descr,1);
pcap_set_promisc(descr,1);
pcap_set_snaplen(descr,30);
pcap_set_timeout(descr,10000);

pcap_activate(descr);
int dl =pcap_datalink(descr);
printf("The Data Link type is %s",pcap_datalink_val_to_name(dl));
//pcap_dispatch(descr,MAXBYTES2CAPTURE,1,512,errbuf);
//Open device in promiscuous mode
//descr = pcap_open_live(device,MAXBYTES2CAPTURE,1,512,errbuf);

/* if(pcap_compile(descr,&fp,filter,0,PCAP_NETMASK_UNKNOWN)==-1)
 {
 fprintf(stderr,"Error compiling filter\n");
 exit(1);
 }

 if(pcap_setfilter(descr,&fp)==-1)
 {
 fprintf(stderr,"Error setting filter\n");
 exit(1);
 }
 */
pcap_loop(descr,0, processPacket, (u_char *) &count);

return 0;
}

最佳答案

您做错了几件事。

您做错的第一件事是将radiotap标头声明为具有比it_versionit_padit_lenit_present更多字段的结构。绝对不能保证在任意radiotap头中的MAC_timestamp字段之后将有一个64位it_present字段。您必须查看it_present字段,以查看标题中实际存在的字段。有关如何处理Radiotap标头的详细信息,请参见the radiotap Web site

将字段的值与0(或NULL)进行比较不起作用-如果不存在字段,则根本不存在该字段。

您的代码可能恰好可以与特定OS上的特定网络适配器的特定版本的驱动程序一起使用,但是如果更改了驱动程序或在具有不同类型适配器的计算机上运行(例如,Atheros与Broadcom适配器),则代码可能会失败在Mac上),或者您尝试在其他操作系统(例如Linux)上运行它。

如果您希望此代码在big-endian机器上运行,则还需要更仔细地从radiotap标头中获取字段,因为它们都是little-endian。 (您的代码中的#define不足以实现这一点。)

除了字节顺序问题(仅当在PowerPC Mac上运行时才会在Mac上显示),您才正确跳过了radiotap标头,所以这不是问题。

另外,MAC时间戳是64位整数,在32位计算机上,它必须使用%llu而不是%u打印。

您还应该检查错误。如果您看到数据包,pcap_create()pcap_activate()可能不会失败,因此这可能不是直接的问题,但是无论如何您都应该检查是否失败。 pcap_set_例程也可能不会失败,至少在Wi-Fi设备上不会失败,但是仍然应该进行检查。

如果要假设数据包是802.11 + radiotap数据包,则可能应该至少检查一下以确保pcap_datalink()的返回值为DLT_IEEE802_11_RADIO,否则返回失败。在使用它时,请在为链接层类型打印的消息末尾添加换行符。

但是主要认为您做错了,每个数据包最多捕获30个字节!当您执行pcap_set_snaplen(descr,30);时,您说的是“捕获的字节数不要超过30个字节”; radiotap标头可能比那更长,因此您甚至都不会获得所有radiotap标头,更不用说获得任何802.11标头了。

如果您想捕获整个数据包,只需忽略pcap_set_snaplen()调用即可。

哦,如果您要非常小心,请确保在查看radiotap和802.11标头时,不要超过pkthdr->caplen

这也意味着您检查pkthdr->len的循环应检查pkthdr->caplen并应以packet [0]开头或应从rh->it_len中减去pkthdr->caplen(因为您应确保在解析Radiotap时或之前,rh->it_len大于或等于pkthdr->caplen。标头,则该减法的结果将为正)。快照长度包括所有伪标头,例如radiotap标头。

关于c - 使用pcap处理802.11帧中不正确的mac地址,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8203419/

10-10 23:56