上一篇博客简单讲述了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. SpriteBuilder中如何固定两个互不接触的物理物体?

    如下图: 这个弹簧装置由3部分组成(从上到下): 弹板 弹簧 弹簧金属底座 其中弹板将固定在弹簧上(但并没有接触,如上图),这就引出一个有趣的问题:怎么样才能将两个独立的物理物体(注意:是物理物体)固 ...

  2. windows下追踪路由

    追踪路由 tracert   目标ip/域名 测试两个ip是否畅通 ping    目标ip 在windows查看ip情况 ipconfig linux/unix下查看ip情况的使用 ifconfig

  3. 如何在Android上编写高效的Java代码

    转自:http://www.ituring.com.cn/article/177180 作者/ Erik Hellman Factor10咨询公司资深移动开发顾问,曾任索尼公司Android团队首席架 ...

  4. Linux常用命令(第二版) --文件搜索命令

    文件搜索命令 1.which /usr/bin/which #显示系统命令所在目录,绝对目录,不能查找文件 格式:which [系统命令] e.g. which ls 附-whereis:也可以查找到 ...

  5. vim多行增加缩进

    http://blog.163.com/clevertanglei900@126/blog/static/11135225920116891750734/ 在Normal Mode下,命令>&g ...

  6. "《算法导论》之‘字符串’":字符串匹配

    本文主要叙述用于字符串匹配的KMP算法. 阮一峰的博文“字符串匹配的KMP算法"将该算法讲述得非常形象,可参考之. 字符串‘部分匹配值’计算 KMP算法重要的一步在于部分匹配值的计算.模仿& ...

  7. SharePoint 2007 图片库视图不可用、页面标题不显示

    描述: 问题1:SharePoint新建图片库,想选择"视图"-"所有图片",选择"详细信息.幻灯片.缩略图"等视图,均没有反应.如图1. ...

  8. 面试之路(18)-java的函数参数传递类型之值传递还是引用传递

    关于这个问题争论了很久,最近也是偶然发现这个问题 经典名言: O'Reilly's Java in a Nutshell by David Flanagan (see Resources) puts ...

  9. mybatis 开发环境搭建

    不说废话直接上代码,首先看下我的目录机构: 红色部分,表明你所需的jar包,已经配置文件. 创建用户表,以及插入数据. create table books(id int (11) not null ...

  10. App 被拒 -- App Store Review Guidelines (2015)中英文对照

    Introduction(简介) We're pleased that you want to invest your talents and time to develop applications ...