Quantcast
Channel: Форум программистов и сисадминов Киберфорум
Viewing all articles
Browse latest Browse all 517205

Как получить доменный адрес и А-записи с pcap файла? - C++

$
0
0
Привет всем. Появилась проблема с pcap файлом. Я получил свой pcap файл с помощью утилиты tcpdump с фильтрами:
-v -i lo
Вот пример того что я получил в текстовом (расшифрованном формате):
11:15:47.803647 IP (tos 0x0, ttl 64, id 45262, offset 0, flags [DF], proto UDP (17), length 132) 127.0.1.1.53 > 127.0.0.1.56698: 445 2/0/0 ess.makedreamprofits.ru. CNAME protimer-env.elasticbeanstalk.com., protimer-env.elasticbeanstalk.com. A 54.229.216.199 (104)

Так вот. Мне нужно вытянуть с моего pcap файла: время, размер пакета, ip отправителя и получателя, ttl, доменное имя и А-записи. Но не выходит получить 2 последних - доменное имя и А-записи. Вот мой код (рабочий):
Код:

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

using namespace std;

#define SIZE_ETHERNET 14
#define ETHER_ADDR_LEN 6

/* 4 bytes IP address */
typedef struct ip_address{
    u_char byte1;
    u_char byte2;
    u_char byte3;
    u_char byte4;
} ip_address;

/* IPv4 header */
typedef struct ip_header{
    u_char  ver_ihl;        // Version (4 bits) + Internet header length (4 bits)
    u_char  tos;            // 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;          // 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 header
typedef struct udp_header{
    u_short sport;          // Source port
    u_short dport;          // Destination port
    u_short len;            // Datagram length
    u_short crc;            // Checksum
} udp_header;
*/

int main()
{
    string file = "log.pcap";
    char errbuff[PCAP_ERRBUF_SIZE];

    //file
    pcap_t *pcap = pcap_open_offline(file.c_str(), errbuff);

    //packet header
    struct pcap_pkthdr *header;

    //packet data
    const u_char *data;

    u_int packetCount = 0;
    ip_header* ip;
    //main loop
    while (pcap_next_ex(pcap, &header, &data) >= 0)
    {
        cout << ++packetCount << ") ";

        cout << "Packet size: " << header->len << " bytes\n";

        if (header->len != header->caplen)
            cout << "Warning! Capture size different than packet size: " << header->len << " bytes\n";

        cout << "Epoch Time: " << header->ts.tv_sec << ":" << header->ts.tv_usec << " seconds\n";

        //ip
        ip = (ip_header*) (data + SIZE_ETHERNET);
        //print ip address
        printf("%d.%d.%d.%d -> %d.%d.%d.%d\n",
            ip->saddr.byte1, ip->saddr.byte2, ip->saddr.byte3, ip->saddr.byte4,
            ip->daddr.byte1, ip->daddr.byte2, ip->daddr.byte3, ip->daddr.byte4
        );

        //ttl
        cout << "TTL: " << (unsigned int) ip->ttl << endl;

        cout << endl;
    }

    system("pause");
    return 0;
}

Использую winpcap, но по-моему winpcap и libpcap ничем не отличаются.
Если кто знает, помогите пожалуйста)

Viewing all articles
Browse latest Browse all 517205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>