Dancing in the Aether net

This is just a bit of code to see how the ethernet interface works in Linux. The package packETH in debian has a GUI interface and allows sending all kinds of packets on on the CSMA wire. There are also some python versions and this is just to look deeper into the methods and see what is required and what is not. On Linux at least, you can send what ever you want on the interface as root. This code does work and I have traced it with wireshark and received it at the remote interface. This continues my work to do PXE and have several machines controlled by Alice Infinity, where she can run code and reset the machines ( through a USB interface to power ) if nothing good happens. It allows the creation of code that would be dangerous to run locally for the sake of test. It also gets around the issue of code interaction when tested as threads.

/* * packETH - ethernet packet generator * By Miha Jemec <jemcek@gmail.com> * Copyright 2003 Miha Jemec, Iskratel * Additions- Copyright 2011 Paul Mohr GPL * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * function_send.c - routines for sending packet was the basis for this example * compile with this command * gcc -Wall filname.c -o executablename */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <net/if.h> #include <netpacket/packet.h> #include <net/ethernet.h> #include <netinet/in.h> #include <sys/ioctl.h> #include <netdb.h> /* Add values from the MAC ID here if you really want it to be received :) */ void set_ethernet_address2(unsigned char *pkt){ /* first 6 are the destination This is "3COM" something*/ pkt[0]=0x00; pkt[1]=0x01; pkt[2]=0x02; pkt[3]=0x03; pkt[4]=0x04; pkt[5]=0x05; /* second 6 are the source */ pkt[6]=0x00; pkt[7]=0x01; pkt[8]=0x02; pkt[9]=0x03; pkt[10]=0x04; pkt[11]=0x05; /* these following are the packet type bytes*/ pkt[12]=223; } /* send test packet */ int test_packet_link(unsigned char *pkt, int nr) { int c, fd,i,j; struct sockaddr_ll sa; struct ifreq ifr; char iftext[20]="eth1\0"; /* open socket in raw mode */ fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); if (fd == -1) {printf("Error: Could not open socket!\n");return -2;} printf("Open socket!\n"); /* set interface name to eth1 or "whatever" and add a zero terminator just for luck*/ memset(&ifr, 0, sizeof(ifr)); strncpy (ifr.ifr_name, iftext, sizeof(ifr.ifr_name) - 1); ifr.ifr_name[sizeof(ifr.ifr_name)-1] = '\0'; if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) { printf("No such interface: %s", iftext); if (close(fd) == 0) { printf("Closed socket\n"); return(-3); }else{ printf("Could not close socket!\n"); return(-4); } return -5; } /* is the interface up? */ ioctl(fd, SIOCGIFFLAGS, &ifr); if( (ifr.ifr_flags & IFF_UP) == 0) { printf("Suggest that you try [sudo ifup %s] Interface: %s is not UP.\n", iftext,iftext); if (close(fd) == 0) { printf("Closed socket\n"); return(-6); }else{ printf("Could not close socket!\n"); return(-7); } return -8; } printf("Interface: %s is UP.\n", iftext); /* just write in the structure again */ ioctl(fd, SIOCGIFINDEX, &ifr); /* well we need this to work */ memset(&sa, 0, sizeof (sa)); sa.sll_family = AF_PACKET; sa.sll_ifindex = ifr.ifr_ifindex; sa.sll_protocol = htons(ETH_P_ALL); printf("Interface: %s is acceptable.\n", iftext); printf("%d elements\n",nr); printf("%d sll_family\n",sa.sll_family); printf("%d sll_ifindex\n",sa.sll_ifindex); printf("%d sll_protocol\n",sa.sll_protocol); printf("%p pkt\n",pkt); printf("%d fd\n",fd); for (i=0;i<nr;i++){pkt[i]=0xdd;} set_ethernet_address2(pkt); for (i=0;i<nr;i++){ for (j=0;j<16;j++){ printf("%u ",pkt[i]);i++; if ( j>nr){break;} } printf("\n"); } c = sendto(fd, pkt, nr, 0, (struct sockaddr *)&sa, sizeof (sa)); if ( c != -1){ printf("There were %d bytes sent on the wire. \n", c); if (close(fd) == 0) { printf("Closed socket\n"); return (c); }else{ return(-10); } } else { printf("There were an error , no bytes were sent on the wire. %d \n", c); if (close(fd) == 0) {return -11;} else {printf("Warning! close(fd) returned -1!");return -12; } } } int main (int argc, char *argv[]) { unsigned char packd[1600]; int nr=134; /* get user rights before we start as it requires root access. */ /* must be run as "sudo test" */ if (getuid() && geteuid()) { printf("Without root privileges , none of these interfaces are accessible.\n"); return -1; } else{ printf("Interface is accessible.\n"); system("ifconfig"); test_packet_link(packd,nr); } return(0); }

Just for the sake of knowing, I copied the code and pasted it into a file and compiled and ran it, and it does work at least for Linux, your mileage may vary, and use at your own risk , which is implied with any code that runs as root.


Automated Intelligence

Automated Intelligence
Auftrag der unendlichen LOL katzen