Linux下使用混杂模式抓包(2)
来源:互联网 发布:云校软件下载 编辑:程序博客网 时间:2024/06/07 08:29
有时候不光要抓自己的包,还要抓目的地址不是本机地址的包,既是过路包,这时候就要将监听网卡设为混在模式
这里新添了头文件:
net/if.h 定义了ifreq的结构体,既是接口(interface)的信息头文件。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <linux/if_ether.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/ethernet.h>
/**
* Set misc mode for interface
* \param if_name interface name we will set
* \param sockfd the socket id we will set
* */
int set_promisc (char *if_name, int sockfd)
{
struct ifreq ifr;
strcpy (ifr.ifr_name, if_name);
if (0 != ioctl (sockfd, SIOCGIFFLAGS, &ifr))
{
printf ("Get interface flag failed\n");
return -1;
}
/* add the misc mode */
ifr.ifr_flags |= IFF_PROMISC;
if (0 != ioctl (sockfd, SIOCSIFFLAGS, &ifr))
{
printf ("Set interface flag failed\n");
return -1;
}
}
int main (int argc, char *argv[])
{
int sockfd;
int ret = 0;
char buffer[1518] = {0};
char *eth_head = NULL;
if ((sockfd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
{
printf ("create socket failed\n");
return -1;
}
if (0 != set_promisc ("eth0", sockfd))
{
printf ("Failed to set interface promisc mode\n");
}
while (1)
{
memset (buffer, 0x0, sizeof (buffer));
ret = recvfrom (sockfd, buffer, sizeof (buffer), 0, NULL, NULL);
printf ("recview package length : %d\n", ret);
eth_head = buffer;
printf ("PACKAGE START\n");
/* get source and dectination mac address */
printf ("dectination mac:%02x-%02x-%02x-%02x-%02x-%02x,"
"source mac:%02x-%02x-%02x-%02x-%02x-%02x;\n", eth_head[0],
eth_head[1], eth_head[2], eth_head[3], eth_head[4],
eth_head[5], eth_head[6], eth_head[7], eth_head[8],
eth_head[9], eth_head[10], eth_head[11]);
printf ("eth_type:%02x%02x\n", eth_head[12], eth_head[13]);
/* ARP protocol flag */
if (0x08 == eth_head[12] && 0x06 == eth_head[13])
{
printf ("ARP source ip:%d.%d.%d.%d,destination ip:%d.%d.%d.%d;\n",
eth_head[28], eth_head[29], eth_head[30], eth_head[31],
eth_head[38], eth_head[39], eth_head[40], eth_head[41]);
}
/* IPv4 protocol flag */
else if (0x08 == eth_head[12] && 0x00 == eth_head[13])
{
if (0x45 == eth_head[14])
{
printf ("IPv4 source ip:%d.%d.%d.%d,destination ip:%d.%d.%d."
"%d;\n", eth_head[26], eth_head[27], eth_head[28],
eth_head[29], eth_head[30], eth_head[31],
eth_head[32], eth_head[33]);
}
else
{
printf ("p_head:%02x\n", eth_head[14]);
}
}
printf ("PACKAGE END\n");
}
return 0;
}
运行之后就会发现发往其它mac的包了。
- Linux下使用混杂模式抓包(2)
- 使用 网卡混杂模式 编写网络抓包程序
- Linux环境下使用tcpdump抓包
- Win平台高级编程:网卡混杂模式抓包 (RAW SOCKET) -- 多线程IOCP完成端口
- linux 下tcmpdump抓包
- linux下网络抓包命令tcpdump的使用
- Linux下使用socket进行网卡抓包(1)
- linux下混杂设备
- linux 下怎样查看网卡是否支持混杂(promisc)模式
- linux下分别使用lipcap和socket抓包分析包
- Linux下使用Wireshark进行抓包分析(含SIP和RTP包)
- Linux下编写网络抓包程序
- Linux下编写网络抓包程序
- linux 下 libpcap抓包实例程序
- linux下libpcap抓包分析
- Linux下编写网络抓包程序
- linux下libpcap抓包分析
- linux下libpcap抓包分析
- 浅谈文件的创建、读取及写入
- input 输入框 常用属性
- 《Linux内核完全注释》部分习题解答
- Spring 和struts 整合的三种方式
- CSTO沙龙第三期预告:给艺术插上科技的翅膀
- Linux下使用混杂模式抓包(2)
- ZendOptimizer
- Android + NDK + Eclipse 调试
- do_irq中调用do_softirq的原因
- Debug Oracle Procedure
- 25个让Java程序员更高效的Eclipse插件
- 异常在Dos中显示的一些问题……
- J2EE技术规范(八)——JMS(消息,域)
- 设计模式(六)----- 适配器模式(Adapter)----(JAVA版)