Code:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/ethernet.h>
#include <netinet/ether.h>
#include <linux/if_packet.h>
#include <sys/ioctl.h> // ioctl()
#include <net/if.h> // struct ifre
#include <unistd.h>
#define DONE "done"
char* macOne();
char* macTwo();
int main() {
int sock = 0;
char* sourceHWA = malloc(17 * sizeof(char));
char* destHWA = malloc(17 * sizeof(char));
struct sockaddr_ll sock_addr;
struct ifreq ifr; //Information about NW-Interface
struct ether_header* ether = NULL;
char* dev = "eno1";
int i = 1;
sock = socket(AF_PACKET,SOCK_RAW,IPPROTO_RAW);
strncpy(ifr.ifr_name,dev, sizeof(ifr.ifr_name)); //Index of interface
if (ioctl(sock,SIOCGIFINDEX,&ifr) != 0){
perror("ioctl");
exit(1);
}
memset(&sock_addr,0, sizeof(struct sockaddr_ll));
sock_addr.sll_ifindex = ifr.ifr_ifindex;
ether = (struct ether_header*)malloc(sizeof(struct ether_header));
bzero(ether, sizeof(struct ether_header));
while(strcmp(destHWA = macOne(),DONE) && strcmp(sourceHWA = macTwo(),DONE)){
memcpy(ether->ether_dhost,(u_char*)ether_aton(sourceHWA),ETHER_ADDR_LEN);
memcpy(ether->ether_shost,(u_char*)ether_aton(destHWA),ETHER_ADDR_LEN);
ether->ether_type = htons(0x0806);
sendto(sock, ether, sizeof(struct ether_header), 0, (struct sockaddr *) &sock_addr, sizeof(struct sockaddr_ll));
printf("%i sended\t %s =======> %s\n",i++,destHWA,sourceHWA);
}
return 0;
}
char* macOne(){
static int a[6] = {0,0,0,16,32,254};
static char mac[17];
while (1){
if (a[0] <= 255){
sprintf(mac,"%x:%x:%x:%x:%x:%x",a[5],a[4],a[3],a[2],a[1],a[0]);
a[0]++;
}else{
a[0] = 0;
a[1]++;
}
if (a[1] > 255){
a[1] = 0;
a[2]++;
}
if (a[2] > 255)
break;
return mac;
}
return "done";
}
char* macTwo(){
static int a[6] = {98,22,252,0,0,0};
static char mac[17];
while (1){
if (a[3] <= 255){
sprintf(mac,"%x:%x:%x:%x:%x:%x",a[5],a[4],a[3],a[2],a[1],a[0]);
a[3]++;
}else{
a[3] = 0;
a[4]++;
}
if (a[4] > 255){
a[4] = 0;
a[5]++;
}
if (a[5] > 255)
break;
return mac;
}
return "done";
}
Die gefälschten Pakete werden korrekt weitergeleitet, aber nach dem Durchlauf des Programms arbeitet das Switch als wenn nicht gewesen wäre.... Wireshark erkennt die Pakete korrekt, ich bin gerade ziemlich ratlos und bin für jede Hilfe dankbar!