1.概述
通常在同一个网段的所有网络接口都有访问在物理媒体上传输的所有数据的能力,而每个网络接口都还应该有一个硬件地址,该硬件地址不同于网络中存在的其他网络接口的硬件地址,同时,每个网络至少还要一个广播地址。(代表所有的接口地址),在正常情况下,一个合法的网络接口应该只响应这样的两种数据帧:
1、帧的目标区域具有和本地网络接口相匹配的硬件地址。
2、帧的目标区域具有"广播地址"。
在接受到上面两种情况的数据包时,nc通过cpu产生一个硬件中断,该中断能引起操作系统注意,然后将帧中所包含的数据传送给系统进一步处理。
而sniffer就是一种能将本地nc状态设成(promiscuous)状态的软件,当nc处于这种"混杂"方式时,该nc具备"广播地址",它对所有遭遇到的每一个帧都产生一个硬件中断以便提醒操作系统处理流经该物理媒体上的每一个报文包。(绝大多数的nc具备置成 promiscuous方式的能力)
可见,sniffer工作在网络环境中的底层,它会拦截所有的正在网络上传送的数据,并且通过相应的软件处理,可以实时分析这些数据的内容,进而分析所处的网络状态和整体布局。值得注意的是:sniffer是极其安静的,它是一种消极的安全攻击。
2. sniffer的简单实现
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <sys/types.h>
#include <asm/types.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ip_icmp.h>
#include <features.h> /* 需要里面的 glibc 版本号 */
#if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
#include <netpacket/packet.h>
#include <net/ethernet.h> /* 链路层(L2)协议 */
#else
#include <asm/types.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h> /* 链路层协议 */
#endif
#include <netinet/if_ether.h>
/**
接收发往本地的arp,ip,rarp所有数据帧,并将网卡设置为混杂模式,用于接收非发往本地的MAC帧
以太网的头部结构:
struct ether_header
{
u_int8_t ether_dhost[ETH_ALEN]; // destination eth addr
u_int8_t ether_shost[ETH_ALEN]; // source ether addr
u_int16_t ether_type; // packet type ID field
} __attribute__ ((__packed__));