- 論壇徽章:
- 0
|
我現(xiàn)在的想法是 用socket添一個(gè)抓包程序 向DNS 125.210.177.185 發(fā)送請(qǐng)求之后,解析所有收到的UDP包,然后過(guò)濾掉125.210.177.XXX之外的包,
解析這個(gè)包的內(nèi)容,得到解析后的地址;
我從網(wǎng)上找到的抓包代碼
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <string.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/if_ether.h>
#include <net/ethernet.h>
void die(char *why, int n)
{
perror(why);
exit(n);
}
int do_promisc(char *nif, int sock )
{
struct ifreq ifr;
strncpy(ifr.ifr_name, nif,strlen(nif)+1);
if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)) //獲得flag
{
die("ioctl", 2);
}
ifr.ifr_flags |= IFF_PROMISC; //重置flag標(biāo)志
if(ioctl(sock, SIOCSIFFLAGS, &ifr) == -1 ) //改變模式
{
die("ioctl", 3);
}
}
//修改網(wǎng)卡成PROMISC(混雜)模式
char buf[40960];
int catch_packet_func()
{
struct sockaddr_in addr;
struct ether_header *peth;
struct iphdr *pip;
struct tcphdr *ptcp;
struct udphdr *pudp;
char mac[16];
int i,sock, r, len;
char *data;
char *ptemp;
char ss[32],dd[32];
if((sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1) //建立socket
//man socket可以看到上面幾個(gè)宏的意思
{
die("socket", 1);
}
do_promisc("eth0", sock); //eth0為網(wǎng)卡名稱(chēng)
system("ifconfig" ;
for(;
{
len = sizeof(addr);
r = recvfrom(sock,(char *)buf,sizeof(buf), 0, (struct sockaddr *)&addr,&len);
buf[r] = 0;
ptemp = buf;
peth = (struct ether_header *)ptemp;
ptemp += sizeof(struct ether_header); //指針后移eth頭的長(zhǎng)度
pip = (struct ip *)ptemp; //pip指向ip層的包頭
ptemp += sizeof(struct ip);//指針后移ip頭的長(zhǎng)度
switch(pip->protocol) //根據(jù)不同協(xié)議判斷指針類(lèi)型
{
case IPPROTO_TCP:
ptcp = (struct tcphdr *)ptemp; //ptcp指向tcp頭部
printf("jcwang||TCP pkt :FORM:[%s]:[%d]\n",inet_ntoa(*(struct in_addr*)&(pip->saddr)),ntohs(ptcp->source));
printf("jcwang||TCP pkt :TO:[%s]:[%d]\n",inet_ntoa(*(struct in_addr*)&(pip->daddr)),ntohs(ptcp->dest));
break;
case IPPROTO_UDP:
pudp = (struct udphdr *)ptemp; //ptcp指向udp頭部
printf("jcwang||UDP pkt:\n len:%d payload len:%d from %s:%d to %s:%d\n", r, ntohs(pudp->len),
inet_ntoa(*(struct in_addr*)&(pip->saddr)),
ntohs(pudp->source), inet_ntoa(*(struct in_addr*)&(pip->daddr)), ntohs(pudp->dest) );
break;
case IPPROTO_ICMP:
printf("jcwang||ICMP pkt:%s\n",inet_ntoa(*(struct in_addr*)&(pip->saddr)));
break;
case IPPROTO_IGMP:
printf("jcwang||IGMP pkt:\n" ;
break;
default:
printf("jcwang||Unkown pkt, protocl:%d\n", pip->protocol);
break;
} //end switch
perror("dump" ;
}
}
但是 我看程序的打印 只有125.210.177.185 :53端口到 125.210.177.185 :XX端口的包
沒(méi)有看到10.240.44.184到125.210.177.185 和125.210.177.146到10.240.44.184的包
是不是底層給過(guò)濾了 這端程序如何修改才能抓到DNS包
在線(xiàn)等啊 有木有大俠來(lái)指教哇 |
|