转发网址:https://blog.csdn.net/eqiang8271/article/details/8489769

  1. //Example 1.
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <sys/types.h>
  7. #include <linux/in.h>
  8. #include <linux/if_ether.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11.  
  12. int main(int argc, char **argv) {
  13. int sock, n;
  14. char buffer[];
  15. unsigned char *iphead, *ethhead;
  16.  
  17. if ((sock=socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP)))<) {
  18. perror("socket");
  19. exit();
  20. }
  21.  
  22. while () {
  23. printf("----------\n");
  24. n = recvfrom(sock,buffer,,,NULL,NULL);
  25. printf("%d bytes read\n",n);
  26.  
  27. /* Check to see if the packet contains at least
  28. * complete Ethernet (14), IP (20) and TCP/UDP
  29. * (8) headers.
  30. */
  31. if (n<) {
  32. perror("recvfrom():");
  33. printf("Incomplete packet (errno is %d)\n", errno);
  34. close(sock);
  35. exit();
  36. }
  37.  
  38. ethhead = buffer;
  39. printf("Source MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
  40. ethhead[],ethhead[],ethhead[],ethhead[],ethhead[],ethhead[]);
  41. printf("Destination MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
  42. ethhead[],ethhead[],ethhead[],ethhead[],ethhead[],ethhead[]);
  43.  
  44. iphead = buffer+; /* Skip Ethernet header */
  45. if (*iphead==0x45) { /* Double check for IPv4 and no options present */
  46. printf("Source host: %d.%d.%d.%d\n", iphead[], iphead[], iphead[], iphead[]);
  47. printf("Dest host: %d.%d.%d.%d\n", iphead[], iphead[], iphead[], iphead[]);
  48. printf("Source,Dest ports %d,%d\n", (iphead[]<<)+iphead[], (iphead[]<<)+iphead[]);
  49. printf("Layer-4 protocol %d\n",iphead[]);
  50. }
  51. }
  52. }
  1. //Example 2.
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <unistd.h>
  7. #include <sys/socket.h>
  8. #include <sys/types.h>
  9. #include <linux/in.h>
  10. #include <linux/if_ether.h>
  11. #include <net/if.h>
  12. #include <sys/ioctl.h>
  13. #include <unistd.h>
  14. #include <stdlib.h>
  15.  
  16. int main(int argc, char **argv) {
  17. int sock, n;
  18. char buffer[];
  19. unsigned char *iphead, *ethhead;
  20. struct ifreq ethreq;
  21.  
  22. if ( (sock=socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP)))<) {
  23. perror("socket");
  24. exit();
  25. }
  26.  
  27. /* Set the network card in promiscuos mode */
  28. strncpy(ethreq.ifr_name,"eth0",IFNAMSIZ);
  29. if (ioctl(sock,SIOCGIFFLAGS,&ethreq)==-) {
  30. perror("ioctl");
  31. close(sock);
  32. exit();
  33. }
  34. ethreq.ifr_flags|=IFF_PROMISC;
  35. if (ioctl(sock,SIOCSIFFLAGS,&ethreq)==-) {
  36. perror("ioctl");
  37. close(sock);
  38. exit();
  39. }
  40.  
  41. while () {
  42. printf("----------\n");
  43. n = recvfrom(sock,buffer,,,NULL,NULL);
  44. printf("%d bytes read\n",n);
  45.  
  46. /* Check to see if the packet contains at least
  47. * complete Ethernet (14), IP (20) and TCP/UDP
  48. * (8) headers.
  49. */
  50. if (n<) {
  51. perror("recvfrom():");
  52. printf("Incomplete packet (errno is %d)\n", errno);
  53. close(sock);
  54. exit();
  55. }
  56.  
  57. ethhead = buffer;
  58. printf("Source MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
  59. ethhead[],ethhead[],ethhead[],ethhead[],ethhead[],ethhead[]);
  60. printf("Destination MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
  61. ethhead[],ethhead[],ethhead[],ethhead[],ethhead[],ethhead[]);
  62.  
  63. iphead = buffer+; /* Skip Ethernet header */
  64. if (*iphead==0x45) { /* Double check for IPv4 and no options present */
  65. printf("Source host %d.%d.%d.%d\n", iphead[],iphead[],iphead[],iphead[]);
  66. printf("Dest host %d.%d.%d.%d\n",iphead[],iphead[],iphead[],iphead[]);
  67. printf("Source,Dest ports %d,%d\n",(iphead[]<<)+iphead[],(iphead[]<<)+iphead[]);
  68. printf("Layer-4 protocol %d\n",iphead[]);
  69. }
  70. }
  71.  
  72. }

使用BPF的这个可能有问题:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <errno.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <sys/types.h>
  7. #include <linux/in.h>
  8. #include <linux/if_ether.h>
  9. #include <net/if.h>
  10. #include <linux/filter.h>
  11. #include <sys/ioctl.h>
  12.  
  13. int main(int argc, char **argv) {
  14. int sock, n;
  15. char buffer[];
  16. unsigned char *iphead, *ethhead;
  17. struct ifreq ethreq;
  18.  
  19. /*my ip: 10.219.119.23 == 0x0adb7716*/
  20. /*
  21. udp and host 192.168.13.41 and src port 5000
  22. (000) ldh [12]
  23. (001) jeq #0x800 jt 2 jf 14
  24. (002) ldb [23]
  25. (003) jeq #0x11 jt 4 jf 14
  26. (004) ld [26]
  27. (005) jeq #0x0adb7716 jt 8 jf 6
  28. (006) ld [30]
  29. (007) jeq #0x0adb7716 jt 8 jf 14
  30. (008) ldh [20]
  31. (009) jset #0x1fff jt 14 jf 10
  32. (010) ldxb 4*([14]&0xf)
  33. (011) ldh [x + 14]
  34. (012) jeq #0x1388 jt 13 jf 14
  35. (013) ret #68
  36. (014) ret #0
  37. */
  38. struct sock_filter BPF_code[]= {
  39. { 0x28, , , 0x0000000c },
  40. { 0x15, , , 0x00000800 },
  41. { 0x30, , , 0x00000017 },
  42. { 0x15, , , 0x00000011 },
  43. { 0x20, , , 0x0000001a },
  44. { 0x15, , , 0x0adb7716 },
  45. { 0x20, , , 0x0000001e },
  46. { 0x15, , , 0x0adb7716 },
  47. { 0x28, , , 0x00000014 },
  48. { 0x45, , , 0x00001fff },
  49. { 0xb1, , , 0x0000000e },
  50. { 0x48, , , 0x0000000e },
  51. { 0x15, , , 0x00001388 },
  52. { 0x6, , , 0x00000044 },
  53. { 0x6, , , 0x00000000 }
  54. };
  55. struct sock_fprog Filter;
  56.  
  57. Filter.len = ;
  58. Filter.filter = BPF_code;
  59.  
  60. if ( (sock=socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP)))<) {
  61. perror("socket");
  62. return -;
  63. }
  64.  
  65. /* Set the network card in promiscuous mode 设置网卡为混杂模式*/
  66. strncpy(ethreq.ifr_name,"eth3",IFNAMSIZ); //hardcode, please check your computer: $ifconfig
  67. if (ioctl(sock,SIOCGIFFLAGS,&ethreq)==-) {
  68. perror("ioctl");
  69. close(sock);
  70. return -;
  71. }
  72. ethreq.ifr_flags|=IFF_PROMISC;
  73. if (ioctl(sock,SIOCSIFFLAGS,&ethreq)==-) {
  74. perror("ioctl");
  75. close(sock);
  76. return -;
  77. }
  78.  
  79. /* Attach the filter to the socket */
  80. if(setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &Filter, sizeof(Filter))<){
  81. perror("setsockopt");
  82. close(sock);
  83. return -;
  84. }
  85.  
  86. while () {
  87. printf("----------\n");
  88. n = recvfrom(sock,buffer,,,NULL,NULL);
  89. printf("%d bytes read\n",n);
  90.  
  91. /* Check to see if the packet contains at least
  92. * complete Ethernet (14), IP (20) and TCP/UDP
  93. * (8) headers.
  94. */
  95. if (n<) {
  96. perror("recvfrom():");
  97. printf("Incomplete packet (errno is %d)\n", errno);
  98. close(sock);
  99. return ;
  100. }
  101.  
  102. ethhead = buffer;
  103. printf("Source MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
  104. ethhead[],ethhead[],ethhead[],ethhead[],ethhead[],ethhead[]);
  105. printf("Destination MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
  106. ethhead[],ethhead[],ethhead[],ethhead[],ethhead[],ethhead[]);
  107.  
  108. iphead = buffer+; /* Skip Ethernet header */
  109. if (*iphead==0x45) { /* Double check for IPv4 and no options present */
  110. printf("Source host %d.%d.%d.%d\n", iphead[],iphead[],iphead[],iphead[]);
  111. printf("Dest host %d.%d.%d.%d\n", iphead[],iphead[], iphead[],iphead[]);
  112. printf("Source,Dest ports %d,%d\n", (iphead[]<<)+iphead[], (iphead[]<<)+iphead[]);
  113. printf("Layer-4 protocol %d\n",iphead[]);
  114. }
  115. }
  116.  
  117. }

Linux下sniffer实现(转)的更多相关文章

  1. Linux下的sniffer工具--TcpDump的安装和使用

    在如今众多的黑客技术中,嗅探器(sniffer)是最常见,也是最重要的技术之一. 用过windows平台上的sniffer工具(例如,netxray和sniffer pro软件)的朋友可能都知道,在共 ...

  2. Linux下一个简单sniffer的实现

    Sniffer(嗅探器)是一种基于被动侦听原理的网络分析方式.将网络接口设置在监听模式,便可以将网上传输的源源不断的信息截获.对于网络监听的基本原理我们不在赘述,我们也不开启网卡的混杂模式,因为现在的 ...

  3. Linux下不同服务器间数据传输--转载

    因为工作原因,需要经常在不同的服务器见进行文件传输,特别是大文件的传输,因此对linux下不同服务器间数据传输命令和工具进行了研究和总结.主要是rcp,scp,rsync,ftp,sftp,lftp, ...

  4. Linux下不同服务器间数据传输

    因为工作原因,需要经常在不同的服务器见进行文件传输,特别是大文件的传输,因此对linux下不同服务器间数据传输命令和工具进行了研究和总结.主要是rcp,scp,rsync,ftp,sftp,lftp, ...

  5. linux下不同服务器间数据传输(rcp,scp,rsync,ftp,sftp,lftp,wget,curl)(zz)

    linux下不同服务器间数据传输(rcp,scp,rsync,ftp,sftp,lftp,wget,curl) 分类: linux2011-10-10 13:21 8773人阅读 评论(1) 收藏 举 ...

  6. linux下常用文件传输命令 (转)

    因为工作原因,需要经常在不同的服务器见进行文件传输,特别是大文件的传输,因此对linux下不同服务器间数据传输命令和工具进行了研究和总结.主要是rcp,scp,rsync,ftp,sftp,lftp, ...

  7. linux如何ARP嗅探 Linux下嗅探工具Dsniff安装记录

      先来下载依赖包 和一些必须要用到的工具 我这里用的是 dsniff-2.3 的版本 wget http://www.monkey.org/~dugsong/dsniff/dsniff-2.3.ta ...

  8. linux下常用文件传输命令(转)

    因为工作原因,需要经常在不同的服务器见进行文件传输,特别是大文件的传输,因此对linux下不同服务器间数据传输命令和工具进行了研究和总结.主要是rcp,scp,rsync,ftp,sftp,lftp, ...

  9. linux下不同服务器间数据传输(wget,scp)

    一.wget是Linux下最常用的http/ftp文件下载工具1.wget断点续传,只需要加上-c参数即可,例如:代码:wget-chttp://www.abc.com/abc.zip-Oabc.zi ...

随机推荐

  1. hdu5141 线段树

    这题说的是给了一串然后计算出这个串的最长递增子序列的长度,然后计算出有过少个子串[i,j] 他们的最长递增子序列和这整个子串的最长递增子序列相同,我们对于每个j最长递增子序列找出他在序列中的使成为最长 ...

  2. EF Code First 学习笔记:表映射(转)

      多个实体映射到一张表 Code First允许将多个实体映射到同一张表上,实体必须遵循如下规则: 实体必须是一对一关系 实体必须共享一个公共键 观察下面两个实体: public class Per ...

  3. 独家揭秘,106岁的IBM靠什么完成了世纪大转型|钛度专访

    IBM大中华区董事长陈黎明 到2017年2月,陈黎明就担任IBM大中华区董事长整整两年了. 五年前,IBM历史上首位女CEO也是第9位CEO罗睿兰上任,三年前,IBM在罗睿兰的带领下以数据与分析.云. ...

  4. 【转载】IEEE754 学习总结

    以下是参考<IEEE754 学习总结>并结合自己学习总结 一:前言二:预备知识 三:浮点数的表示范围四:将浮点格式转换成十进制数 一:前言 前不久在分析一个程序的过程中遇到了浮点运算,也就 ...

  5. Mindoc搭建流程

    1,安装MySql 2,官网下载:https://github.com/lifei6671/mindoc/releases mindoc_windows_amd64.7z 3,如果下载的压缩包中不存在 ...

  6. win2008R2 bitnami 安装 wamp

    下载官方版本:bitnami-wampstack-5.6.30-1-windows-x64-installer.exe 用管理员权限安装,不然mysql服务可能会安装不上.

  7. usb_submit_urb

    hub_irq() --> usb_submit_urb() usb_start_wait_urb() --> usb_submit_urb() 一旦urb被USB驱动程序正确地创建和初始 ...

  8. hexo + Github 搭建问题综述

    1.Mac下安装hexo Error: Cannot find module './build/Release/DTraceProviderBindings 解决: solution 2.node s ...

  9. spring boot2 基于百度云apiface实现人脸检测与认证2

    接上一篇,上篇只实现了人脸的认证,接下来实现人脸的检测. 原理介绍: 把摄像头抓拍的图像上传到服务器,服务器把图像上传到百度云,百度云返回识别出的人脸的数量和位置,前端根据服务端的返回,在图像中画出人 ...

  10. git submodule临时分支;以及git reset使用

    submodule 已经建立好了一个gitlab submodule形式的repo: 在repo A下面有一个submodule B, A --> B. clone -b branch [rep ...