⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arp.c

📁 用libpcap和libnet写arp包
💻 C
字号:
#include <stdio.h>#include <pcap.h>#include <libnet.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>int get_hw_addr(char *, unsigned char *, struct in_addr *);int main(){    char *dev;    char errbuf[PCAP_ERRBUF_SIZE];    unsigned char bcast_mac[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };    unsigned char my_mac[6] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };    unsigned char zero_mac[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };    char *ipptr = "10.1.0.155";    libnet_t *handle;    struct in_addr ipaddr, myaddr;    int i;    inet_aton(ipptr, &ipaddr);    dev = pcap_lookupdev(errbuf);    if (dev == NULL) {        fprintf(stderr, "Couldn't find device %s: %s\n", dev, errbuf);        return 2;    }    if (get_hw_addr(dev, my_mac, &myaddr) == -1) {        return 2;    }    ipaddr.s_addr = myaddr.s_addr & 0x00ffffff;    handle = libnet_init(LIBNET_LINK, dev, errbuf);    if (handle == NULL) {        fprintf(stderr, "Couldn't initiate handle: %s\n", errbuf);        return 2;    }    for (i = 0; i < 256; i++) {        if (libnet_build_arp(ARPHRD_ETHER,                             ETHERTYPE_IP,                             6,                             4,                             ARPOP_REQUEST,                             my_mac,                             (unsigned char *) &myaddr,                             zero_mac,                             (unsigned char *) &ipaddr, NULL, 0, handle,                             0) == -1) {            fprintf(stderr, "Couldn't build arp: %s\n", errbuf);            libnet_destroy(handle);            return 2;        }        if (libnet_build_ethernet            (bcast_mac, my_mac, ETHERTYPE_ARP, NULL, 0, handle, 0) == -1) {            fprintf(stderr, "Couldn't build ethernet: %s\n", errbuf);            libnet_destroy(handle);            return 2;        }        if (libnet_write(handle) == -1) {            fprintf(stderr, "Couldn't send package: %s\n", errbuf);            libnet_destroy(handle);            return 2;        }        libnet_clear_packet(handle);        ipaddr.s_addr += 0x01000000;    }    libnet_destroy(handle);    return 0;}int get_hw_addr(char *dev, unsigned char macbuf[6], struct in_addr *myaddr){    libnet_t *handle;    char errbuf[LIBNET_ERRBUF_SIZE] = "";    struct libnet_ether_addr *mac;    handle = libnet_init(LIBNET_LINK, dev, errbuf);    if (!handle) {        fprintf(stderr, "get_hw_addr(): libnet_init: %s\n", errbuf);        return -1;    }    mac = libnet_get_hwaddr(handle);    if (!mac) {        fprintf(stderr, "get_hw_addr(): libnet_get_hwaddr: %s\n",                libnet_geterror(handle));        libnet_destroy(handle);        return -1;    }    myaddr->s_addr = (unsigned long) libnet_get_ipaddr4(handle);    memcpy(macbuf, mac->ether_addr_octet, 6);    libnet_destroy(handle);    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -