Project

General

Profile

Bug #252 ยป rawsend_massive.c

Sven Eckelmann, 05/15/2016 01:48 PM

 
#include <stdio.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <stdint.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>

int getmac(const char *src, unsigned char *dst)
{
unsigned int n[6];
int i;
if (sscanf(src, "%x:%x:%x:%x:%x:%x",
&n[0], &n[1], &n[2], &n[3], &n[4], &n[5]) != 6) {
if(sscanf(src, "%2x%2x.%2x%2x.%2x%2x",
&n[0], &n[1], &n[2], &n[3], &n[4], &n[5]) != 6) {
return 1;
}
}

for (i = 0; i < 6; i++) {
if (n[i] <= 255)
dst[i] = (unsigned char)n[i];
else
return 1;
}

return 0;
}

int main(int argc, char *argv[])
{
int rawsock;
uint16_t type = 0x1337;
struct ifreq req;
struct sockaddr_ll addr;
struct iovec vector[2];
char buf[256];
size_t size = 256;
struct ether_header header;
uint32_t x;

if (argc != 2 && argc != 3) {
fprintf(stderr, "Usage: %s DEVICE [DST-MAC]\n", argv[0]);
return 1;
}

if ((rawsock = socket(PF_PACKET, SOCK_RAW, htons(type))) < 0 ) {
fprintf(stderr, "Could not open raw socket\n");
return 1;
}

strncpy(req.ifr_name, argv[1], IFNAMSIZ);
if (ioctl(rawsock, SIOCGIFINDEX, &req) < 0) {
fprintf(stderr, "Could not find device %s\n", argv[1]);
return 1;
}

addr.sll_family = AF_PACKET;
addr.sll_protocol = htons(type);
addr.sll_ifindex = req.ifr_ifindex;

if (bind(rawsock, (struct sockaddr *)&addr, sizeof(addr)) < 0 ) {
fprintf(stderr, "Could not bind to device %s\n", argv[1]);
return 1;
}

strncpy(req.ifr_name, argv[1], IFNAMSIZ);
if (ioctl(rawsock, SIOCGIFHWADDR, &req) < 0) {
fprintf(stderr, "Could not get hwaddr of device %s\n", argv[1]);
return 1;
}

if (argc == 2) {
vector[0].iov_base = &header;
vector[0].iov_len = sizeof(struct ether_header);
vector[1].iov_base = buf;
vector[1].iov_len = size;

if (readv(rawsock, vector, 2) < 0) {
close(rawsock);
fprintf(stderr, "Could not receive message\n");
return 1;
}
printf("Received message\n");
} else if (argc == 3) {
for (x = 1; x < 3000; x++) {
vector[0].iov_base = &header;
vector[0].iov_len = sizeof(struct ether_header);
vector[1].iov_base = buf;
vector[1].iov_len = size;

header.ether_type = htons(type);

header.ether_shost[0] = 0;
header.ether_shost[1] = 0;
header.ether_shost[2] = (x >> 24) & 0xff;
header.ether_shost[3] = (x >> 16) & 0xff;
header.ether_shost[4] = (x >> 8) & 0xff;
header.ether_shost[5] = (x) & 0xff;

if (getmac(argv[2], header.ether_dhost)) {
close(rawsock);
fprintf(stderr, "Could not parse mac address %s\n", argv[2]);
return 1;
}
if (writev(rawsock, vector, 2) < 0) {
close(rawsock);
fprintf(stderr, "Could not send message\n");
return 1;
}
}
}
close(rawsock);
return 0;
}
    (1-1/1)