0

重複の可能性:
すべてのローカルhttpリクエストをハイジャックし、cを使用してURLを抽出する方法は?

この記事に基づいて、すべての着信パケットを取得できます。

/* Callback function invoked by libpcap for every incoming packet */
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;

    /* convert the timestamp to readable format */
    local_tv_sec = header->ts.tv_sec;
    ltime=localtime(&local_tv_sec);
    strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);

    /* print timestamp and length of the packet */
    printf("%s.%.6d len:%d ", timestr, header->ts.tv_usec, header->len);

    /* retireve the position of the ip header */
    ih = (ip_header *) (pkt_data +
        14); //length of ethernet header

    /* retireve the position of the udp header */
    ip_len = (ih->ver_ihl & 0xf) * 4;
    uh = (udp_header *) ((u_char*)ih + ip_len);

    /* convert from network byte order to host byte order */
    sport = ntohs( uh->sport );
    dport = ntohs( uh->dport );

    /* print ip addresses and udp ports */
    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);
}

しかし、どうすればURI情報を抽出できpacket_handlerますか?

4

3 に答える 3

1

あなたは最良の例に従っていません。投稿したURLはUDPパケットを処理する例ですが、HTTPはTCPに基づいています。

于 2010-05-06T07:01:03.510 に答える
0

すべてのパケットに URI があるわけではありません。

http リクエストでは、URI は接続の開始直前に送信されますが、後続のパケットはより大きなリクエストの一部にすぎません。

要求されている URI (およびすべてのデータ) を見つけるには、pkt_data.

于 2010-05-06T06:57:48.930 に答える
0

通常 (Connection: keep-alive、非常に短い最初のパケットなどを無視)、URI は最初の発信 TCP パケットの最初の行の 2 番目の単語になります (単語をスペース区切り、行を CR LF 区切りとして定義します)。

Wiresharkは libpcap に基づいており、オープン ソースであり、これについてはかなりうまく機能しているため、そこから始めることができます。

于 2010-05-06T07:20:10.477 に答える