抓获了端口号为80的tcp包,怎么分析出url呢//回调函数,当收到每一个数据包时会被winpcap所调用
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;
tcp_header *th;
u_int ip_len;
u_short sport,dport;
u_int32_t sn;
time_t local_tv_sec;
//将时间戳转换成可识别的格式
local_tv_sec = header->ts.tv_sec;
ltime=localtime(&local_tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
//获得IP数据包头部的位置
ih = (ip_header *) (pkt_data +
14); //以太网头部长度
ip_len = (ih->ver_ihl & 0xf) * 4;
//获得tcp首部的位置
th = (tcp_header *)((u_char*)ih + ip_len);
//将网络字节序列转换成主机字节序列
sport = ntohs( th->sport );
dport = ntohs( th->dport );
sn = ntohs( th->sn );
//打印数据包的时间戳和长度
printf("%s.%.6d len:%d ", timestr, header->ts.tv_usec, header->len);
//打印IP源IP地址和tcp端口
printf("%d.%d.%d.%d--%d--%d--%d\n",
ih->saddr.byte1,
ih->saddr.byte2,
ih->saddr.byte3,
ih->saddr.byte4,
sport,
dport,
sn);
}
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;
tcp_header *th;
u_int ip_len;
u_short sport,dport;
u_int32_t sn;
time_t local_tv_sec;
//将时间戳转换成可识别的格式
local_tv_sec = header->ts.tv_sec;
ltime=localtime(&local_tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
//获得IP数据包头部的位置
ih = (ip_header *) (pkt_data +
14); //以太网头部长度
ip_len = (ih->ver_ihl & 0xf) * 4;
//获得tcp首部的位置
th = (tcp_header *)((u_char*)ih + ip_len);
//将网络字节序列转换成主机字节序列
sport = ntohs( th->sport );
dport = ntohs( th->dport );
sn = ntohs( th->sn );
//打印数据包的时间戳和长度
printf("%s.%.6d len:%d ", timestr, header->ts.tv_usec, header->len);
//打印IP源IP地址和tcp端口
printf("%d.%d.%d.%d--%d--%d--%d\n",
ih->saddr.byte1,
ih->saddr.byte2,
ih->saddr.byte3,
ih->saddr.byte4,
sport,
dport,
sn);
}
cout<<httpdata<<endl;
}
得出的结果内容很多,只有host的值才是url,怎么获取这个url呢?要进行语法分析吗
if((httpdata[0] == 'G') && (httpdata[1] == 'E') && (httpdata[2] == 'T') && (httpdata[3] == ' ')){
do{
m=3;
++m;//4,m
}while((httpdata[m] == 'H') && (httpdata[m+1]=='o') && (httpdata[m+2]=='s') && (httpdata[m+3] =='t'));
do{
l=m+4;
++l; //m+5,l
}while((httpdata[l]=='C')&&(httpdata[l+1]=='o')&&(httpdata[l+2]=='n')&&(httpdata[l+3]=='n')&&(httpdata[l+4]=='e')&&(httpdata[l+5]=='c')&&(httpdata[l+6]=='t')&&(httpdata[l+7]=='i')&&(httpdata[l+8]=='o')&&(httpdata[l+9]=='n'));
for(n=m+5;n<l;n++){
cout<<httpdata[n]<<endl;
}
}为什么这个不能实现功能?
参考参考这个试试:http://www.pudn.com/downloads30/sourcecode/windows/network/detail95523.html