抱歉,我之前给出的代码只是一个简单的示例,它仅仅在本地修改了捕获到的报文中的源MAC地址,并没有直接发送到网络上。因此,当服务端接收到报文时,并不会看到修改后的MAC地址。
要实现修改后的MAC地址被服务端正确接收,需要将修改后的报文重新发送到网络上。你可以使用Raw Socket技术来发送自定义构造的以太网帧报文。
以下是一个简单的示例,演示了如何使用Raw Socket发送以太网帧报文并修改源MAC地址:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>
#include <net/if_arp.h>
#include <arpa/inet.h>
int main() {
int sockfd;
struct sockaddr_ll sa;
char interface[] = "eth0";
// 创建原始套接字
sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (sockfd == -1) {
perror("无法创建套接字");
return 1;
}
// 获取本地网卡接口索引
memset(&sa, 0, sizeof(struct sockaddr_ll));
sa.sll_family = AF_PACKET;
if (ioctl(sockfd, SIOCGIFINDEX, &sa) == -1) {
perror("获取网卡索引失败");
close(sockfd);
return 1;
}
// 设置目标接口和MAC地址
strncpy(sa.sll_ifr.ifr_name, interface, IFNAMSIZ);
// 获取本地网卡的MAC地址
if (ioctl(sockfd, SIOCGIFHWADDR, &sa) == -1) {
perror("获取MAC地址失败");
close(sockfd);
return 1;
}
unsigned char srcMac[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}; // 原始源MAC地址
unsigned char dstMac[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; // 新的目标MAC地址
struct ethhdr *eth;
char packet[sizeof(struct ethhdr)];
// 构造以太网帧报文
eth = (struct ethhdr *)packet;
memcpy(eth->h_dest, dstMac, ETH_ALEN); // 目标MAC地址
memcpy(eth->h_source,s sa.sll_addr , ETH_ALEN); // 源MAC地址为本地网卡的物理地址(通过ioctl获取)
// 填写以太网类型字段,例如IPV4是htons(ETH_P_IP),ARP是htons(ETH_P_ARP)
eth->h_proto = htons(ETH_P_IP);
// 发送报文
if (sendto(sockfd,(char*)packet,sizeof(packet),0,(struct sockaddr*)&sa,sizeof(struct sockaddr_ll))==-1){
perror("发送报文失败");
close(sockfd);
return -1;
}
printf("已成功发送报文\n");
close(sockfd);
return 0;
}
请注意,上述代码只是一个简单的示例,并假设你已经知道要发送报文的目标接口。你需要根据实际情况进行修改和适配。
使用Raw Socket需要root权限或者相应的权限设置,同时在使用时请确保遵守法律规定并遵循适当的使用规范。
内容由零声教学AI助手提供,问题来源于学员提问