有时候不光要抓自己的包,还要抓目的地址不是本机地址的包,既是过路包,这时候就要将监听网卡设为混在模式

这里新添了头文件:

net/if.h  定义了ifreq的结构体,既是接口(interface)的信息头文件。

#include

#include

#include

#include

#include

#include

#include

#include

/**

* 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 混杂模式 收包,Linux下使用混杂模式抓包(2)相关推荐

  1. linux怎么抓sip包,Ubuntu下使用Wireshark进行抓包分析(含SIP和RTP包)

    遇到需要在Linux下抓包分析的问题,便用到了wireshark,非常强大的抓包分析软件,直接在系统里面安装,然后使用明亮抓包即可! 我这里用的是Ubuntuserver版,执行安装: 1.apt-g ...

  2. java socket 抓包_linux下用socket的抓包程序

    void die(char *why, int n) { perror(why); exit(n); } int do_promisc(char *nif, int sock ) { struct i ...

  3. linux下wireshark使用笔记本网卡抓包配置

    linux下wireshark使用笔记本网卡抓包命令: 可以先用ifconfig查看本地无线网卡,这里为wlp0s20f3 sudo killall -9 wpa_supplicant sudo if ...

  4. 安卓抓包工具 linux,Android 下使用tcpdump网络抓包方法

    Android 下使用tcpdump网络抓包方法 抓包需要tcpdump以及Root权限,tcpdump在本文后有下载. 首先把tcpdump传进手机,用adb命令(放SD卡有时会有问题,我一次可以用 ...

  5. kali Linux宿主代理、v2ray代理、proxychain4代理和抓包环境Charles、burp的安装配置

    目录 1.v2rayN内网配置 2.宿主代理配置 2.1配置proxychains4代理 2.2配置火狐浏览器代理 3.v2ray安装配置和使用 3.1 修改国内源 3.2 v2ray安装配置 3.3 ...

  6. 硬干货!一张图弄清楚在ESXi下如何进行网络抓包

    对于网络问题,抓包一直是一个比较简单方便的"大招".由于在esxi环境下可以在不同的层面下抓包,因此在什么地方抓包,怎么抓包一直让我觉得很复杂,于是每次操作之前我都要再检查一次手册 ...

  7. iOS 10.3下解决Fiddler代理抓包ssl证书信任问题

    iOS 10.3下解决Fiddler代理抓包ssl证书信任问题 参考文章: (1)iOS 10.3下解决Fiddler代理抓包ssl证书信任问题 (2)https://www.cnblogs.com/ ...

  8. esxi vsphere的端口_硬干货!一张图弄清楚在ESXi下如何进行网络抓包

    对于网络问题,抓包一直是一个比较简单方便的"大招".由于在esxi环境下可以在不同的层面下抓包,因此在什么地方抓包,怎么抓包一直让我觉得很复杂,于是每次操作之前我都要再检查一次手册 ...

  9. mac下HTTP与HTTPS抓包

    mac下HTTP与HTTPS抓包 HTTP抓包流程 1. 在mac 电脑 上下载代理软件,charlse或者fiddler均可以: 下载方法:http://www.charlesproxy.com/可 ...

  10. 安利一个Mac下好用的抓包工具-Charles

    Charles:让天底下没有难抓的包. 前言 今天给大家推荐一个我所使用过的抓包工具中最好用的抓包工具-Charles(音译:查尔斯),它可以抓各端发起的网络请求,我主要用来抓移动设备上的请求,上次小 ...

最新文章

  1. ML之SVM:调用(sklearn的lfw_people函数在线下载55个外国人图片文件夹数据集)来精确实现人脸识别并提取人脸特征向量
  2. [PowerShell]人人都值得学一点PowerShell实现自动化(2)有哪些可用的场景及方式?
  3. akshare写etf动量滚动策略
  4. url中隐藏php后缀,url中如何隐藏.php
  5. a4988 脉宽要求_A4982/A4984/A4985和A4988设备主要针对办公室自动化市场
  6. 【C语言笔记结构体】
  7. Singularity将本地SIF文件,转成sandbox
  8. PE+windows系统+苹果网站整理
  9. 进阿里一年中最好的机会
  10. OpenGL三维图形编程技术(转)
  11. JVAV中常见的十大异常
  12. 4K秒开,稀缺宝藏影视APP!
  13. 几款H桥电机驱动芯片直流有刷电机驱动芯片性能分析
  14. 一个蚂蚁程序员,曾经的辛酸面试历程
  15. bzoj1455罗马游戏*
  16. Java 提供的默认排序算法
  17. 网游限时器 v2.0 官方
  18. thymeleaf 数字格式化
  19. 手动管理采购订单周期的挑战以及如何应对
  20. c语言数据结构校园导游系统,校园导游系统课程设计报告

热门文章

  1. jeesite如何配置swagger_Spring Boot 2.x基础教程:Swagger接口分类与各元素排序问题详解...
  2. Autograd看这一篇就够了!
  3. socket.io简介
  4. [转]HTML DIV+CSS 命名规范大全
  5. [转]史上最全最强SpringMVC详细示例实战教程
  6. leetcode949. Largest Time for Given Digits
  7. 【WCF】无法加载协定为“ServiceReference1.xxxxxx”的终结点配置部分,因为找到了该协定的多个终结点配置。请按名称指示首选的终结点配置部分.
  8. 理解Java多线程高并发Executor框架的使用
  9. Flex(try-catch-finally)机制
  10. 【100题】第三十六 比赛淘汰问题(谷歌笔试)