linux c arp协议分析一 - 捕捉局域网内的arp包

解析ARP包结构与Linux ARP协议实现
本文详细介绍了ARP包的基本格式,并通过提供Linux环境下捕获ARP包的源码,深入探讨了ARP协议的工作原理及其实现细节。旨在帮助读者理解ARP通信过程,为后续的网络编程和协议开发打下坚实基础。

一、实验步骤:

1.假设监听方IP为192.168.1.33

2.假设arp请求发起方的IP为192.168.250

3.在192.168.1.33中开启catcharppacket

4.在192.168.250上使用ping命令:ping 192.168.1.33

5.观察catcharppacket的输出

二、实验结论(捕获的arp包的格式):

1.mac_target:长度6,host byte order

2.mac_source:长度6,host byte order

3.ethertype:长度2,network byte order

4.hw_type:长度2,network byte order

5.proto_type:长度2,network byte order

6.mac_addr_len:长度1,host byte order

7.ip_addr_len:长度1,host byte order

8.operation_code:长度2,network byte order

9.mac_sender:长度6,host byte order

10.ip_sender:长度4,network byte order

11.mac_receiver:长度6,host byte order

12.ip_receiver:长度4,network byte order

三、实验目的

了解arp包的格式,为下一步自制arp包做准备,请见一下节《linux c arp协议分析二 - 打造并发送自己的arp包》

catcharppacket源码:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <net/if.h> #include <arpa/inet.h> #include <netpacket/packet.h> #include <net/ethernet.h> #include <net/if_arp.h> struct arp_packet { //DLC Header //接收方mac unsigned char mac_target[ETH_ALEN]; //发送方mac unsigned char mac_source[ETH_ALEN]; //Ethertype - 0x0806是ARP帧的类型值 unsigned short ethertype; //ARP Frame //硬件类型 - 以太网类型值0x1 unsigned short hw_type; //上层协议类型 - IP协议(0x0800) unsigned short proto_type; //MAC地址长度 unsigned char mac_addr_len; //IP地址长度 unsigned char ip_addr_len; //操作码 - 0x1表示ARP请求包,0x2表示应答包 unsigned short operation_code; //发送方mac unsigned char mac_sender[ETH_ALEN]; //发送方ip unsigned char ip_sender[4]; //接收方mac unsigned char mac_receiver[ETH_ALEN]; //接收方ip unsigned char ip_receiver[4]; //填充数据 unsigned char padding[18]; }; void die(const char*pre); void print_arp_packet(struct arp_packet ap); int main() { int sfd; char buf[1024]; struct sockaddr_ll my_etheraddr; struct arp_packet rcvBuffer; sfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP)); if(-1 == sfd) { die("socket"); } memset(&my_etheraddr, 0, sizeof(sockaddr_ll)); my_etheraddr.sll_family = AF_PACKET; my_etheraddr.sll_protocol = htons(ETH_P_ARP); my_etheraddr.sll_ifindex = IFF_BROADCAST; if(-1 == bind(sfd, (struct sockaddr *)&my_etheraddr, sizeof(my_etheraddr))){ die("bind"); } while(1) { if(-1 == recv(sfd, &rcvBuffer, sizeof(rcvBuffer), 0)) continue; print_arp_packet(rcvBuffer); } return 0; } void die(const char*pre) { perror(pre); exit(1); } void print_arp_packet(struct arp_packet ap) { printf("\n\n-----------------arp package begin--------------------------\n"); printf("mac_target = "); for(int i = 0; i < ETH_ALEN; i++) { printf(i > 0 ? ":0x%.2x" : "0x%.2x", ap.mac_target[i]); } printf("\nmac_source = "); for(int i = 0; i < ETH_ALEN; i++) { printf(i > 0 ? ":0x%.2x" : "0x%.2x", ap.mac_source[i]); } printf("\nethertype = 0x%x", ntohs(ap.ethertype)); printf("\nhw_type = 0x%x", ntohs(ap.hw_type)); printf("\nproto_type = 0x%x", ntohs(ap.proto_type)); printf("\nmac_addr_len = 0x%x", ap.mac_addr_len); printf("\nip_addr_len = 0x%x", ap.ip_addr_len); printf("\noperation_code = 0x%x", ntohs(ap.operation_code)); printf("\nmac_sender = "); for(int i = 0; i < ETH_ALEN; i++) { printf(i > 0 ? ":0x%.2x" : "0x%.2x", ap.mac_sender[i]); } printf("\nip_sender = %s", inet_ntoa(*(struct in_addr*)(ap.ip_sender))); printf("\nmac_receiver = "); for(int i = 0; i < ETH_ALEN; i++) { printf(i > 0 ? ":0x%.2x" : "0x%.2x", ap.mac_receiver[i]); } printf("\nip_receiver = %s", inet_ntoa(*(struct in_addr*)(ap.ip_receiver))); printf("\n-----------------arp package end----------------------------\n"); }

转载请注明出处:http://blog.youkuaiyun.com/xiaodao1986/article/details/6619813

### Linux ARP欺骗检测与防御方法 #### ARP欺骗概述 ARP欺骗是种网络攻击方式,通过发送伪造的ARP报文来误导网络设备,使它们错误地将特定IP地址映射到攻击者的MAC地址上。这种攻击可能导致数据泄露、中间人攻击等问题。 #### 检测ARP欺骗的方法 ##### 使用Arpwatch工具 `Arpwatch` 是种用于监控和记录局域网ARP活动的工具。它可以识别异常的MAC地址变更情况,并向管理员报告可能存在的ARP欺骗行为[^2]。 ```bash sudo apt-get install arpwatch ``` ##### 利用arp-scan扫描网络 `arp-scan` 可以用来快速扫描整个子网内的活跃主机及其对应的MAC地址。通过对同IP不同时间点获取的不同MAC地址进行对比分析,能够有效发现可疑现象。 ```bash sudo apt-get install arp-scan sudo arp-scan --interface=eth0 --localnet ``` ##### 基于Wireshark抓分析 `Wireshark` 提供了强大的网络流量捕捉功能,允许用户深入查看每ARP请求/回应的数据细节。旦观察到重复或不合理的ARP消息,则表明可能存在安全隐患。 ```bash wireshark & ``` #### 实施防御策略 ##### 开启静态ARP条目配置 为关键服务器设置固定的IP-MAC绑定关系,防止其被篡改。此操作需谨慎执行以免影响正常的网络维护工作。 ```bash sudo arp -s 192.168.x.y FF:FF:FF:AA:BB:CC ``` ##### 启用交换机端口安全特性 现代二层交换机通常支持Port Security特性,可限定连接至某物理接口上的合法MAC数量上限;超出限额后自动封锁该端口访问权限直至人工干预恢复为止。 ```shell # Cisco IOS样例命令 switchport port-security mac-address sticky switchport port-security maximum 1 switchport port-security violation protect ``` ##### 应用高级防护软件产品 市面上有许多专门针对此类威胁设计的安全解决方案可供选用,如Cisco Identity Services Engine (ISE), Aruba ClearPass等平台集成的身份验证服务能进步增强抵御能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值