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

//Example 1.
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/in.h>
#include <linux/if_ether.h>
#include <unistd.h>
#include <stdlib.h> int main(int argc, char **argv) {
int sock, n;
char buffer[];
unsigned char *iphead, *ethhead; if ((sock=socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP)))<) {
perror("socket");
exit();
} while () {
printf("----------\n");
n = recvfrom(sock,buffer,,,NULL,NULL);
printf("%d bytes read\n",n); /* Check to see if the packet contains at least
* complete Ethernet (14), IP (20) and TCP/UDP
* (8) headers.
*/
if (n<) {
perror("recvfrom():");
printf("Incomplete packet (errno is %d)\n", errno);
close(sock);
exit();
} ethhead = buffer;
printf("Source MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
ethhead[],ethhead[],ethhead[],ethhead[],ethhead[],ethhead[]);
printf("Destination MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
ethhead[],ethhead[],ethhead[],ethhead[],ethhead[],ethhead[]); iphead = buffer+; /* Skip Ethernet header */
if (*iphead==0x45) { /* Double check for IPv4 and no options present */
printf("Source host: %d.%d.%d.%d\n", iphead[], iphead[], iphead[], iphead[]);
printf("Dest host: %d.%d.%d.%d\n", iphead[], iphead[], iphead[], iphead[]);
printf("Source,Dest ports %d,%d\n", (iphead[]<<)+iphead[], (iphead[]<<)+iphead[]);
printf("Layer-4 protocol %d\n",iphead[]);
}
}
}
//Example 2.

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/in.h>
#include <linux/if_ether.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h> int main(int argc, char **argv) {
int sock, n;
char buffer[];
unsigned char *iphead, *ethhead;
struct ifreq ethreq; if ( (sock=socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP)))<) {
perror("socket");
exit();
} /* Set the network card in promiscuos mode */
strncpy(ethreq.ifr_name,"eth0",IFNAMSIZ);
if (ioctl(sock,SIOCGIFFLAGS,&ethreq)==-) {
perror("ioctl");
close(sock);
exit();
}
ethreq.ifr_flags|=IFF_PROMISC;
if (ioctl(sock,SIOCSIFFLAGS,&ethreq)==-) {
perror("ioctl");
close(sock);
exit();
} while () {
printf("----------\n");
n = recvfrom(sock,buffer,,,NULL,NULL);
printf("%d bytes read\n",n); /* Check to see if the packet contains at least
* complete Ethernet (14), IP (20) and TCP/UDP
* (8) headers.
*/
if (n<) {
perror("recvfrom():");
printf("Incomplete packet (errno is %d)\n", errno);
close(sock);
exit();
} ethhead = buffer;
printf("Source MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
ethhead[],ethhead[],ethhead[],ethhead[],ethhead[],ethhead[]);
printf("Destination MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
ethhead[],ethhead[],ethhead[],ethhead[],ethhead[],ethhead[]); iphead = buffer+; /* Skip Ethernet header */
if (*iphead==0x45) { /* Double check for IPv4 and no options present */
printf("Source host %d.%d.%d.%d\n", iphead[],iphead[],iphead[],iphead[]);
printf("Dest host %d.%d.%d.%d\n",iphead[],iphead[],iphead[],iphead[]);
printf("Source,Dest ports %d,%d\n",(iphead[]<<)+iphead[],(iphead[]<<)+iphead[]);
printf("Layer-4 protocol %d\n",iphead[]);
}
} }

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

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/in.h>
#include <linux/if_ether.h>
#include <net/if.h>
#include <linux/filter.h>
#include <sys/ioctl.h> int main(int argc, char **argv) {
int sock, n;
char buffer[];
unsigned char *iphead, *ethhead;
struct ifreq ethreq; /*my ip: 10.219.119.23 == 0x0adb7716*/
/*
udp and host 192.168.13.41 and src port 5000
(000) ldh [12]
(001) jeq #0x800 jt 2 jf 14
(002) ldb [23]
(003) jeq #0x11 jt 4 jf 14
(004) ld [26]
(005) jeq #0x0adb7716 jt 8 jf 6
(006) ld [30]
(007) jeq #0x0adb7716 jt 8 jf 14
(008) ldh [20]
(009) jset #0x1fff jt 14 jf 10
(010) ldxb 4*([14]&0xf)
(011) ldh [x + 14]
(012) jeq #0x1388 jt 13 jf 14
(013) ret #68
(014) ret #0
*/
struct sock_filter BPF_code[]= {
{ 0x28, , , 0x0000000c },
{ 0x15, , , 0x00000800 },
{ 0x30, , , 0x00000017 },
{ 0x15, , , 0x00000011 },
{ 0x20, , , 0x0000001a },
{ 0x15, , , 0x0adb7716 },
{ 0x20, , , 0x0000001e },
{ 0x15, , , 0x0adb7716 },
{ 0x28, , , 0x00000014 },
{ 0x45, , , 0x00001fff },
{ 0xb1, , , 0x0000000e },
{ 0x48, , , 0x0000000e },
{ 0x15, , , 0x00001388 },
{ 0x6, , , 0x00000044 },
{ 0x6, , , 0x00000000 }
};
struct sock_fprog Filter; Filter.len = ;
Filter.filter = BPF_code; if ( (sock=socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP)))<) {
perror("socket");
return -;
} /* Set the network card in promiscuous mode 设置网卡为混杂模式*/
strncpy(ethreq.ifr_name,"eth3",IFNAMSIZ); //hardcode, please check your computer: $ifconfig
if (ioctl(sock,SIOCGIFFLAGS,&ethreq)==-) {
perror("ioctl");
close(sock);
return -;
}
ethreq.ifr_flags|=IFF_PROMISC;
if (ioctl(sock,SIOCSIFFLAGS,&ethreq)==-) {
perror("ioctl");
close(sock);
return -;
} /* Attach the filter to the socket */
if(setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &Filter, sizeof(Filter))<){
perror("setsockopt");
close(sock);
return -;
} while () {
printf("----------\n");
n = recvfrom(sock,buffer,,,NULL,NULL);
printf("%d bytes read\n",n); /* Check to see if the packet contains at least
* complete Ethernet (14), IP (20) and TCP/UDP
* (8) headers.
*/
if (n<) {
perror("recvfrom():");
printf("Incomplete packet (errno is %d)\n", errno);
close(sock);
return ;
} ethhead = buffer;
printf("Source MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
ethhead[],ethhead[],ethhead[],ethhead[],ethhead[],ethhead[]);
printf("Destination MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
ethhead[],ethhead[],ethhead[],ethhead[],ethhead[],ethhead[]); iphead = buffer+; /* Skip Ethernet header */
if (*iphead==0x45) { /* Double check for IPv4 and no options present */
printf("Source host %d.%d.%d.%d\n", iphead[],iphead[],iphead[],iphead[]);
printf("Dest host %d.%d.%d.%d\n", iphead[],iphead[], iphead[],iphead[]);
printf("Source,Dest ports %d,%d\n", (iphead[]<<)+iphead[], (iphead[]<<)+iphead[]);
printf("Layer-4 protocol %d\n",iphead[]);
}
} }

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. Mail.Ru Cup 2018 Round 2 Solution

    A. Metro Solved. 题意: 有两条铁轨,都是单向的,一条是从左往右,一条是从右往左,Bob要从第一条轨道的第一个位置出发,Alice的位置处于第s个位置,有火车会行驶在铁轨上,一共有n个 ...

  2. animation CSS3动画总结

    最近一个小游戏项目用到了CSS3的动画属性,例如transition.transform.animation.经过三个星期,终于做完了,利用周末好好梳理总结一下. keyframes这个属性用来定义一 ...

  3. python基础学习十 logging模块详细使用【转载】

    很多程序都有记录日志的需求,并且日志中包含的信息既有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,主要用于输出 ...

  4. python中模块导入问题(已解决)

    想在python中导入request包: 无此模块,于是先安装requests包: 但是提示"Requirement already satisfied".在提示的相应目录里,找到 ...

  5. IntelliJ IDEA 常用快捷键,maven依赖图,个性化设置,禁用Search Everywhere

    查看idea 中jar关系图 快捷键: Ctrl+/ 用于注释,取消注释 Ctrl+Shift+F 全文搜索 Ctrl+F 单页面查找 Ctrl+Alt+Shift+L  格式化代码 ======== ...

  6. Java FastJson 介绍

    1.前言 1.1.FastJson的介绍: JSON协议使用方便,越来越流行,JSON的处理器有很多,这里我介绍一下FastJson,FastJson是阿里的开源框架,被不少企业使用,是一个极其优秀的 ...

  7. 如何创建.babelrc文件?

    方法一: 根目录下,创建  .babelrc.  文件名就可以了! 方法二: git进入根目录,输入   type>.babelrc  ,回车即可!

  8. div框选中状态,倒三角样式

    html代码: <html> <head> <style> #triangle-bottomright { width:0; height: 0; display: ...

  9. 炫酷的CSS3抖动样式:CSS Shake

    CSS Shake是一个使用CSS3实现的动画样式,使用SASS编写,利用它我们可以实现多种不同样式的抖动效果(如下面的GIF图像): 炫酷的CSS3抖动样式:CSS Shake 这是一个很微小的动画 ...

  10. 代码注释,神兽护体,代码无bug

    /** * * ━━━━━━神兽出没━━━━━━ * ┏┓ ┏┓ * ┏┛┻━━━┛┻┓ * ┃ ┃ * ┃ ━ ┃ * ┃ ┳┛ ┗┳ ┃ * ┃ ┃ * ┃ ┻ ┃ * ┃ ┃ * ┗━┓ ┏━┛ ...