可能重复:
How to hijack all local http request and extract the url using c?
基于this article我可以获取所有传入的数据包。
/* 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);
}
但是如何在packet_handler
中提取URI信息?
答案 0 :(得分:1)
你没有关注最好的例子。您发布的URL是处理UDP数据包但HTTP基于TCP的示例。
答案 1 :(得分:0)
并非每个数据包都有URI。
在http请求中,URI将在连接开始时传输,但后续数据包只是较大请求的一部分。
要查找所请求的URI(以及所有数据),请查看pkt_data
。
答案 2 :(得分:0)
通常(忽略Connection:keep-alive,非常短的第一个数据包等),URI将是第一个传出TCP数据包第一行的第二个字(将字定义为空格分隔,行为CR LF分隔)。
由于wireshark基于libpcap,是开源的并且做得很好,你可以从那里开始。