【发布时间】:2017-03-26 18:50:46
【问题描述】:
我在 C 中使用 pcap 库编写了一个数据包嗅探器,我注意到我的一些数据包的有效负载长度为负。
IP、MAC 和校验和以及所有这些似乎都没有问题,但某些数据包(不是全部)似乎具有负负载。
我这样做:我有一个指向数据包开头的指针(即 u_char)。我将指针向右移动(以通过 L2、3、4 标题)。在此过程中,我还打印了标头(源 MAC、目标 MAC、源 IP 等)。标头似乎是有序的,但我不知道为什么有时会收到负有效载荷。
如果需要,我可以提供代码。
谢谢!
void got_packet(u_char* args, struct pcap_pkthdr* hdr, const u_char* packet) {
FILE* f = fopen("/home/bogdan/C_Stuff/log.txt", "w+");
static int packet_counter = 1;
printf("\n\n");
printf("PACKET NUMBER %i\n", packet_counter);
int ethernet_header_length = 14;
int ip_header_length;
int tcp_header_length;
int payload_length;
const u_char* ip_header;
const u_char* tcp_header;
const u_char* payload;
ip_header = packet + ethernet_header_length;
/* The second part of the IP Header contains
the length of the IP Header */
ip_header_length = ((*ip_header) & 0x0F);
ip_header_length = ip_header_length * 4;
printf("IP Header Length = %i\n", ip_header_length);
/* At the 10th byte we find info about TCP/UDP stuff */
if(*(ip_header + 9) == IPPROTO_TCP) {
printf("TCP Packet\n");
}
tcp_header = ip_header + ip_header_length;
tcp_header_length = ((*(tcp_header) + 12) & 0xF0) >> 4;
tcp_header_length *= 4;
printf("TCP Header Length = %i\n", tcp_header_length);
// Total header size
int total_header_size = ethernet_header_length + ip_header_length + tcp_header_length;
payload = packet + total_header_size;
payload_length = hdr->caplen - total_header_size;
printf("Payload Length = %i\n", payload_length);
if(payload_length > 0) {
int i;
printf("\n**************************************************");
printf("\nPAYLOAD:\n");
for(i = 0; i < payload_length; i++) {
printf("%i ", payload[i]);
if(i % 10 == 0) {
printf("\n");
}
}
printf;
printf("\n");
}
struct ether_header* eptr;
eptr = (struct ether_header*)packet;
if(ntohs(eptr->ether_type) == ETHERTYPE_IP) {
printf("IP PACKET\n");
} else if(ntohs(eptr->ether_type) == ETHERTYPE_ARP) {
printf("ARP PACKET\n");
}
struct ip* ip;
ip = (struct ip*)(packet + sizeof(struct ether_header));
printf("Source MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", eptr->ether_shost[0], eptr->ether_shost[1], eptr->ether_shost[2], eptr->ether_shost[3], eptr->ether_shost[4], eptr->ether_shost[5]);
printf("Destination MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", eptr->ether_dhost[0], eptr->ether_dhost[1], eptr->ether_dhost[2], eptr->ether_dhost[3], eptr->ether_dhost[4], eptr->ether_dhost[5]);
printf("Source IP: %s (IPv%i) | Total length = %i |\nTTL = %i | Checksum = %i\n", inet_ntoa(ip->ip_src), ip->ip_v, ip->ip_len, ip->ip_ttl, ip->ip_sum);
printf("Destination IP: %s (IPv%i) | Total length = %i |\nTTL= %i | Checksum = %i\n", inet_ntoa(ip->ip_dst), ip->ip_v, ip->ip_len, ip->ip_ttl, ip->ip_sum);
printf("\n====================================================\n");
fprintf(f, "%s", inet_ntoa(ip->ip_dst));
fflush(stdout);
packet_counter++;
fclose(f);
}
这是一个示例捕获(一切似乎都很好,除了有效负载长度):
数据包编号 9 IP 标头长度 = 20 TCP 数据包 TCP 标头长度 = 52 有效载荷长度 = -20 IP PACKET 源 MAC:
24:0a:64:22:a2:39 目标 MAC:78:d7:52:29:06:db 源 IP: 192.168.100.10 (IPv4) |总长度 = 13312 | TTL = 64 |校验和 = 11794 目标 IP:216.58.209.165 (IPv4) |总长度 = 13312 | TTL= 64 |校验和 = 11794
【问题讨论】:
-
是的,请提供代码!
-
更有用的是具有负负载长度的数据包示例。
-
我已经上传了代码。另外,我上传了一个带有负有效负载的示例捕获数据包。谢谢!
-
可能是 hdr->caplen 小于实际数据包长度,见:stackoverflow.com/questions/1491660/…