上一篇博客简单讲述了libpcap的工作流程及简单使用,今天我们需要做的是继续使用libpcap抓取我们感兴趣的流量,并进行简单的解析:

测试环境是centos 7

下面贴一张arp帧结构图:

下面我们实现的是通过pcap过滤抓取arp报文,解析其中的Ethernet address 和proctocal address并打印出来

分析是arp request还是reply,前面就不做过多解释,代码比较简单,直接贴:

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <pcap.h>
#include <arpa/inet.h> #define MAXBYTES2CAPTURE 2048
#define ARP_REQUEST 1
#define ARP_REPLY 2 typedef struct arphdr {
u_int16_t htype; //hardware type
u_int16_t ptype; //protocol type
u_char hlen; //hardware address length
u_char plen; //protocol address length
u_int16_t oper; //operation code
u_char sha[]; //sendHardware address
u_char spa[]; //sender ip address
u_char tha[]; //target hardware address
u_char tpa[]; //target ip address
} arphdr_t; int main(int argc, char **argv)
{
int i = ;
bpf_u_int32 net = ;
bpf_u_int32 mask = ;
struct bpf_program filter; /*place to store the filter program*/
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *handle = NULL; /*interface handle*/
struct pcap_pkthdr pkthdr; /**/
const unsigned char *packet = NULL; /*received raw data*/
arphdr_t *arpheader = NULL; /*point to arp header*/ if (argc != ) {
printf("USAGE: arpsniffer <interface>\n");
exit();
} memset(errbuf, , PCAP_ERRBUF_SIZE);
/*open network device for packet capture*/
handle = pcap_open_live(argv[], MAXBYTES2CAPTURE, , , errbuf);
if (handle == NULL) {
fprintf(stderr, "Couldn't open device %s: %s\n", argv[], errbuf);
exit();
} /*look up device network addr and mask*/
if (pcap_lookupnet(argv[], &net, &mask, errbuf) == -) {
fprintf(stderr, "Couldn't get netmask for device %s: %s\n", argv[], errbuf);
exit();
} /*complie the filter expression of filter program*/
pcap_compile(handle, &filter, "arp", , mask); pcap_setfilter(handle, &filter); while() {
/*Get one packet if null continue wait*/
if ((packet = pcap_next(handle, &pkthdr)) == NULL) {
continue;
} arpheader = (struct arphdr *)(packet + ); /*Point to the ARP header*/
printf("\n------------- ARP --------------\n");
printf("Received Packet Size: %d bytes\n", pkthdr.len);
printf("Hardware type: %s\n", (ntohs(arpheader->htype) == )?"Ethernet":"Unknown");
printf("Protocol type: %s\n", (ntohs(arpheader->ptype) == 0x0800)?"IPv4":"Unknown");
printf("Operation : %s\n", (ntohs(arpheader->oper) == ARP_REQUEST)?"ARP_REQUEST":"ARP_REPLY"); /*If is Ethernet and IPv4 print packet contents*/
if (ntohs(arpheader->htype) == && ntohs(arpheader->ptype) == 0x0800) {
printf("\nSoucre MAC:%02x:%02x:%02X:%02x:%02x:%02x\n",
arpheader->sha[], arpheader->sha[],
arpheader->sha[], arpheader->sha[],
arpheader->sha[], arpheader->sha[]);
printf("Soucre IP:%d.%d.%d.%d\n",
arpheader->spa[], arpheader->spa[],
arpheader->spa[], arpheader->spa[]);
printf("\nDestination MAC:%02x:%02x:%02X:%02x:%02x:%02x\n",
arpheader->tha[], arpheader->tha[],
arpheader->tha[], arpheader->tha[],
arpheader->tha[], arpheader->tha[]);
printf("Destination IP:%d.%d.%d.%d\n",
arpheader->tpa[], arpheader->tpa[],
arpheader->tpa[], arpheader->tpa[]);
}
}
return ;
}

下面是运行结果:

 [root@localhost pcap_arp]# ./pcap enp0s3

 ------------- ARP --------------
Received Packet Size: bytes
Hardware type: Ethernet
Ptotocol type: IPv4
Operation : ARP_REQUEST Soucre MAC:b0::FE::5a:5b
Soucre IP:192.168.16.139 Destination MAC:::::e7:
Destination IP:192.168.16.125 ------------- ARP --------------
Received Packet Size: bytes
Hardware type: Ethernet
Ptotocol type: IPv4
Operation : ARP_REPLY Soucre MAC:::::e7:
Soucre IP:192.168.16.125 Destination MAC:b0::FE::5a:5b
Destination IP:192.168.16.139

Makefile:

 #
#design of ARP sniffer
# CFLAGS = -g
LDFLAGS = -lpcap OBJS = test.o
TARGET = pcap RM = rm -f $(TARGET):$(OBJS)
$(CC) $(LDFLAGS) -o $@ $^ %.o:%.c
$(CC) $(CFLAGS) -c -o $@ $< .PHONY:clean clean:
$(RM) $(TARGET) $(OBJS)

使用libpcap过滤arp的更多相关文章

  1. 基于ARP的局域网IP劫持——C语言实现

      我站在 烈烈风中   恨不能 荡尽绵绵心痛   望苍天 四方云动   剑在手   问天下谁是英雄 ——<霸王别姬> 阅读这篇文章之前,请确认已经熟悉ARP报文伪造的方法,可参考< ...

  2. Python黑帽编程 3.2 ARP监控

    Python黑帽编程 3.2 ARP监控 在第3.1节<ARP欺骗>中,我们学习了ARP的基本原理,使用Python实现了我们自己的ARP欺骗工具.在上一节的基础上,我们来实现一个ARP监 ...

  3. Python黑客编程ARP欺骗

    Python灰帽编程 3.1 ARP欺骗 ARP欺骗是一种在局域网中常用的攻击手段,目的是让局域网中指定的(或全部)的目标机器的数据包都通过攻击者主机进行转发,是实现中间人攻击的常用手段,从而实现数据 ...

  4. 图解ARP协议(三)ARP防御篇-如何揪出“内鬼”并“优雅的还手”

    一.ARP防御概述 通过之前的文章,我们已经了解了ARP攻击的危害,黑客采用ARP软件进行扫描并发送欺骗应答,同处一个局域网的普通用户就可能遭受断网攻击.流量被限.账号被窃的危险.由于攻击门槛非常低, ...

  5. Wireshark命令行工具tshark

    Wireshark命令行工具tshark 1.目的 写这篇博客的目的主要是为了方便查阅,使用wireshark可以分析数据包,可以通过编辑过滤表达式来达到对数据的分析:但我的需求是,怎么样把Data部 ...

  6. iptables命令、规则、参数详解

    表    (table)包含4个表:4个表的优先级由高到低:raw-->mangle-->nat-->filterraw---RAW表只使用在PREROUTING链和OUTPUT链上 ...

  7. Wireshark命令行工具tshark详解(含例子)-01

    Wireshark命令行工具tshark使用小记 1.目的 写这篇博客的目的主要是为了方便查阅,使用wireshark可以分析数据包,可以通过编辑过滤表达式来达到对数据的分析:但我的需求是,怎么样把D ...

  8. TCP/IP详解卷1 - wireshark抓包分析

    TCP/IP详解卷1 - 系列文 TCP/IP详解卷1 - 思维导图(1) TCP/IP详解卷1 - wireshark抓包分析 引言 在初学TCP/IP协议时,会觉得协议是一种很抽象的东西,通过wi ...

  9. 结合Wireshark捕获分组深入理解TCP/IP协议栈

    摘要:     本文剖析了浏览器输入URL到整个页面显示的整个过程,以百度首页为例,结合Wireshark俘获分组进行详细分析整个过程,从而更好地了解TCP/IP协议栈.   一.俘获分组 1.1 准 ...

随机推荐

  1. ffdshow 源代码分析 9: 编解码器有关类的总结

    ===================================================== ffdshow源代码分析系列文章列表: ffdshow 源代码分析 1: 整体结构 ffds ...

  2. centos下安装mysql(安装,启动,停止,服务端口查询,用户密码设定)

    http://www.2cto.com/database/201305/208114.html http://smilemonkey.iteye.com/blog/673848 netstat -na ...

  3. 【OpenCV学习】Kmean均值聚类对图片进行减色处理

            #include <cv.h> #include <highgui.h> #include <iostream> #define MAX_CLUST ...

  4. Linux基础正则表达式字符汇整(characters)

    RE 字符 意义与范例 ^word 意义:待搜寻的字串(word)在行首! 范例:搜寻行首为 # 开始的那一行,并列出行号 grep -n '^#' regular_express.txt word$ ...

  5. Linux - Shell变量的配置守则

    变量的配置守则 变量与变量内容以一个等号『=』来连结,如下所示: 『myname=VBird』 等号两边不能直接接空格符,如下所示为错误: 『myname = VBird』或『myname=VBird ...

  6. OpenCV——老照片效果

    // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...

  7. Spring 官网下载zip jar

    第一步:打开官网:http://www.springsource.org/download/community: 第二步:点击图片 第三步:点击图标 第四步:找到如下链接,点击进去 第五步:再找到如下 ...

  8. rails将类常量重构到数据库对应的表中之一

    问题是这样:原来代码.html.erb页面中有一个select元素,其每个item对应的是model中的类常量: <%= f.select :pay_type,Order::PAYMENT_TY ...

  9. ruby:借助第三方类名如何查找第三方gem名称(zlib为例)

    rubygem中含有成千上万的第三方gem,网上书上扩展教程中都有指导如何使用第三方gem的例子.但是如果不幸这些例子都没有提及gem名称的话,如何只凭第三方类名或require名查找gem名称呢?换 ...

  10. Configure the MySQL account associate to the domain user via MySQL Windows Authentication Plugin

    在此记录如何将之前一次做第三发软件在配置的过程. 将AD user通过代理映射到mysql 用户. 在Mysql官网有这样一段话: The server-side Windows authentica ...