#include #include #include #include #include #include #include #include #include #include #define SRC_ETHER_ADDR "aa:aa:aa:aa:aa:aa" #define DST_ETHER_ADDR "bb:bb:bb:bb:bb:bb" int CreateRawSocket(int protocol_to_sniff) { int rawsock; if((rawsock = socket(PF_PACKET, SOCK_RAW, htons(protocol_to_sniff)))== -1) { perror("Error creating raw socket: "); exit(-1); } return rawsock; } int BindRawSocketToInterface(char *device, int rawsock, int protocol) { struct sockaddr_ll sll; struct ifreq ifr; bzero(&sll, sizeof(sll)); bzero(&ifr, sizeof(ifr)); /* First Get the Interface Index */ strncpy((char *)ifr.ifr_name, device, IFNAMSIZ); if((ioctl(rawsock, SIOCGIFINDEX, &ifr)) == -1) { printf("Error getting Interface index !\n"); exit(-1); } /* Bind our raw socket to this 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("Error binding raw socket to interface\n"); exit(-1); } return 1; } int SendRawPacket(int rawsock, unsigned char *pkt, int pkt_len) { int sent= 0; printf("Packet len: %d\n", pkt_len); /* A simple write on the socket ..thats all it takes ! */ if((sent = write(rawsock, pkt, pkt_len)) != pkt_len) { /* Error */ printf("Could only send %d bytes of packet of length %d\n", sent, pkt_len); return 0; } return 1; } unsigned char* CreateEthernetHeader(char *src_mac, char *dst_mac, int protocol) { struct ethhdr *ethernet_header; ethernet_header = (struct ethhdr *)malloc(sizeof(struct ethhdr)); /* copy the Src mac addr */ memcpy(ethernet_header->h_source, (void *)ether_aton(src_mac), 6); /* copy the Dst mac addr */ memcpy(ethernet_header->h_dest, (void *)ether_aton(dst_mac), 6); /* copy the protocol */ ethernet_header->h_proto = htons(protocol); /* done ...send the header back */ return ((unsigned char *)ethernet_header); } /* argv[1] is the device e.g. eth0 */ main(int argc, char **argv) { int raw; unsigned char *packet; int ethhdr_len; /* Create the raw socket */ raw = CreateRawSocket(ETH_P_ALL); /* Bind raw socket to interface */ BindRawSocketToInterface(argv[1], raw, ETH_P_ALL); /* create Ethernet header */ packet = CreateEthernetHeader(SRC_ETHER_ADDR, DST_ETHER_ADDR, ETHERTYPE_IP); ethhdr_len = sizeof(struct ethhdr); if(!SendRawPacket(raw, packet, ethhdr_len)) { perror("Error sending packet"); } else printf("Packet sent successfully\n"); /* Free the ethernet_header back to the heavenly heap */ free(packet); close(raw); return 0; }