上一篇博客简单讲述了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. 跟我一起写Makefile(转)

    这是我见过最全的Makefile编写指南:跟我一起写Makefile. PDF版本可以从这里下载得到.

  2. Gradle 1.12用户指南翻译——第三十八章. Eclipse 插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  3. OpenCV——PS滤镜,渐变映射

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

  4. Eclipse修改工程名字

    1:修改项目目录下:.project文件 <?xml version="1.0" encoding="UTF-8"?> <projectDes ...

  5. Leetcode_252_Implement Stack using Queues

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48598773 Implement the followin ...

  6. CDH安装系统环境准备——虚拟机网络配置

    虚拟机网络配置教程如下: 1.修改网络配置文件[root@master ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0配置IP地址.网关.掩码.DNS ...

  7. Solr 新增、更新、删除索引

    solr-admin新增索引 [索引中无则新增,有则更新] 1.在doc标签和field标签中增加权重(boost),增加权重后,可以在搜索的时候做权重过滤. <add> <doc ...

  8. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构建二叉树)

    根据中序和后续遍历构建二叉树. /** * Definition for a binary tree node. * public class TreeNode { * int val; * Tree ...

  9. AngularJs 学习笔记(三)依赖注入

    一个对象可以通过三种方式来获取对依赖对象的控制权: 1.在内部创建依赖的对象 2.通过全局变量引用这个依赖对象 3.通过参数进行传递(在这里是通过函数参数) AngularJs通过$injector注 ...

  10. Day4_装饰器

    装饰器: #模板def auth(func): def wrapper(*args,**kwargs): res=func(*args,**kwargs) return res return wrap ...