home *** CD-ROM | disk | FTP | other *** search
/ Hackers Toolkit 2.0 / Hackers_Toolkit_v2.0.iso / HTML / archive / Unix / c-src / changemac.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-04  |  15.6 KB  |  382 lines

  1. /*
  2.  *  In P51-02 someone mentioned Ethernet spoofing.  Here you go.
  3.  *  This tiny program can be used to trick some smart / switching hubs.
  4.  *             
  5.  *  AWL production: (General Public License v2)
  6.  *
  7.  *      changemac  version 1.0  (2.20.1998)
  8.  *
  9.  *  changemac  --   change MAC address of your ethernet card.
  10.  *
  11.  *  changemac [-l] | [-d number ] [ -r | -a address ]
  12.  *
  13.  *      -d number       number of ethernet device, 0 for eth0, 1 for eth1 ...
  14.  *                      if -d option is not specify default value is 0 (eth0)
  15.  *
  16.  *      -h              help for changemac command
  17.  *
  18.  *      -a address      address format is xx:xx:xx:xx:xx:xx
  19.  *
  20.  *      -r              set random MAC address for ethernet card
  21.  *
  22.  *      -l              list first three MAC bytes of known ethernet vendors
  23.  *                      (this list is not compleet, anyone who know some more
  24.  *                      information about MAC addresses can mail me)
  25.  *
  26.  *  changemac does not change hardware address, it just change data in
  27.  *  structure of kernel driver for your card.  Next boot on your computer will
  28.  *  read real MAC form your hardware.
  29.  *
  30.  *  The changed MAC stays as long as your box is running, (or as long as next
  31.  *  successful changemac).
  32.  *
  33.  *  It will not work if kernel is already using that ethernet device.  In that
  34.  *  case you have to turn off that device (ifconfig eth0 down).
  35.  *
  36.  *  I use changemac in /etc/rc.d/rc.inet1 (slackware, or redhat) just line
  37.  *  before ifconfig for ethernet device (/sbin/ifconfig eth0 ...)
  38.  *
  39.  *  The author will be very pleased if you can learn something form this code.
  40.  *
  41.  *  Updates of this code can be found on:
  42.  *  http://galeb.etf.bg.ac.yu/~azdaja/changemac.html
  43.  *
  44.  *  Sugestions and comments can be sent to author:
  45.  *  Milos Prodanovic <azdaja@galeb.etf.bg.ac.yu>
  46.  */
  47.  
  48. #include <string.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <errno.h>
  52. #include <sys/socket.h>
  53. #include <sys/ioctl.h>
  54. #include <net/if.h>
  55. #include <unistd.h>
  56.  
  57.  
  58. struct LIST
  59. {
  60.     char name[50];
  61.     u_char mac[3];
  62. };
  63.  
  64. /*
  65.  *  This list was obtainted from vyncke@csl.sni.be, created on 01.7.93.
  66.  */
  67.  
  68. struct LIST vendors[] = {
  69.                 {"OS/9 Network                         ",'\x00','\x00','\x00'},
  70.                 {"BBN                                  ",'\x00','\x00','\x02'},
  71.                 {"Cisco                                ",'\x00','\x00','\x0C'},
  72.                 {"Fujitsu                              ",'\x00','\x00','\x0E'},
  73.                 {"NeXT                                 ",'\x00','\x00','\x0F'},
  74.                 {"Sytek/Hughes LAN Systems             ",'\x00','\x00','\x10'},
  75.                 {"Tektronics                           ",'\x00','\x00','\x11'},
  76.                 {"Datapoint                            ",'\x00','\x00','\x15'},
  77.                 {"Webster                              ",'\x00','\x00','\x18'},
  78.                 {"AMD ?                                ",'\x00','\x00','\x1A'},
  79.                 {"Novell/Eagle Technology              ",'\x00','\x00','\x1B'},
  80.                 {"Cabletron                            ",'\x00','\x00','\x1D'},
  81.                 {"Data Industrier AB                   ",'\x00','\x00','\x20'},
  82.                 {"SC&C                                 ",'\x00','\x00','\x21'},
  83.                 {"Visual Technology                    ",'\x00','\x00','\x22'},
  84.                 {"ABB                                  ",'\x00','\x00','\x23'},
  85.                 {"IMC                                  ",'\x00','\x00','\x29'},
  86.                 {"TRW                                  ",'\x00','\x00','\x2A'},
  87.                 {"Auspex                               ",'\x00','\x00','\x3C'},
  88.                 {"ATT                                  ",'\x00','\x00','\x3D'},
  89.                 {"Castelle                             ",'\x00','\x00','\x44'},
  90.                 {"Bunker Ramo                          ",'\x00','\x00','\x46'},
  91.                 {"Apricot                              ",'\x00','\x00','\x49'},
  92.                 {"APT                                  ",'\x00','\x00','\x4B'},
  93.                 {"Logicraft                            ",'\x00','\x00','\x4F'},
  94.                 {"Hob Electronic                       ",'\x00','\x00','\x51'},
  95.                 {"ODS                                  ",'\x00','\x00','\x52'},
  96.                 {"AT&T                                 ",'\x00','\x00','\x55'},
  97.                 {"SK/Xerox                             ",'\x00','\x00','\x5A'},
  98.                 {"RCE                                  ",'\x00','\x00','\x5D'},
  99.                 {"IANA                                 ",'\x00','\x00','\x5E'},
  100.                 {"Gateway                              ",'\x00','\x00','\x61'},
  101.                 {"Honeywell                            ",'\x00','\x00','\x62'},
  102.                 {"Network General                      ",'\x00','\x00','\x65'},
  103.                 {"Silicon Graphics                     ",'\x00','\x00','\x69'},
  104.                 {"MIPS                                 ",'\x00','\x00','\x6B'},
  105.                 {"Madge                                ",'\x00','\x00','\x6F'},
  106.                 {"Artisoft                             ",'\x00','\x00','\x6E'},
  107.                 {"MIPS/Interphase                      ",'\x00','\x00','\x77'},
  108.                 {"Labtam                               ",'\x00','\x00','\x78'},
  109.                 {"Ardent                               ",'\x00','\x00','\x7A'},
  110.                 {"Research Machines                    ",'\x00','\x00','\x7B'},
  111.                 {"Cray Research/Harris                 ",'\x00','\x00','\x7D'},
  112.                 {"Linotronic                           ",'\x00','\x00','\x7F'},
  113.                 {"Dowty Network Services               ",'\x00','\x00','\x80'},
  114.                 {"Synoptics                            ",'\x00','\x00','\x81'},
  115.                 {"Aquila                               ",'\x00','\x00','\x84'},
  116.                 {"Gateway                              ",'\x00','\x00','\x86'},
  117.                 {"Cayman Systems                       ",'\x00','\x00','\x89'},
  118.                 {"Datahouse Information Systems        ",'\x00','\x00','\x8A'},
  119.                 {"Jupiter ? Solbourne                  ",'\x00','\x00','\x8E'},
  120.                 {"Proteon                              ",'\x00','\x00','\x93'},
  121.                 {"Asante                               ",'\x00','\x00','\x94'},
  122.                 {"Sony/Tektronics                      ",'\x00','\x00','\x95'},
  123.                 {"Epoch                                ",'\x00','\x00','\x97'},
  124.                 {"CrossCom                             ",'\x00','\x00','\x98'},
  125.                 {"Ameristar Technology                 ",'\x00','\x00','\x9F'},
  126.                 {"Sanyo Electronics                    ",'\x00','\x00','\xA0'},
  127.                 {"Wellfleet                            ",'\x00','\x00','\xA2'},
  128.                 {"NAT                                  ",'\x00','\x00','\xA3'},
  129.                 {"Acorn                                ",'\x00','\x00','\xA4'},
  130.                 {"Compatible Systems Corporation       ",'\x00','\x00','\xA5'},
  131.                 {"Network General                      ",'\x00','\x00','\xA6'},
  132.                 {"NCD                                  ",'\x00','\x00','\xA7'},
  133.                 {"Stratus                              ",'\x00','\x00','\xA8'},
  134.                 {"Network Systems                      ",'\x00','\x00','\xA9'},
  135.                 {"Xerox                                ",'\x00','\x00','\xAA'},
  136.                 {"Western Digital/SMC                  ",'\x00','\x00','\xC0'},
  137.                 {"Eon Systems (HP)                     ",'\x00','\x00','\xC6'},
  138.                 {"Altos                                ",'\x00','\x00','\xC8'},
  139.                 {"Emulex                               ",'\x00','\x00','\xC9'},
  140.                 {"Darthmouth College                   ",'\x00','\x00','\xD7'},
  141.                 {"3Com ? Novell ? [PS/2]               ",'\x00','\x00','\xD8'},
  142.                 {"Gould                                ",'\x00','\x00','\xDD'},
  143.                 {"Unigraph                             ",'\x00','\x00','\xDE'},
  144.                 {"Acer Counterpoint                    ",'\x00','\x00','\xE2'},
  145.                 {"Atlantec                             ",'\x00','\x00','\xEF'},
  146.                 {"High Level Hardware (Orion, UK)      ",'\x00','\x00','\xFD'},
  147.                 {"BBN                                  ",'\x00','\x01','\x02'},
  148.                 {"Kabel                                ",'\x00','\x17','\x00'},
  149.                 {"Xylogics, Inc.-Annex terminal servers",'\x00','\x08','\x2D'},
  150.                 {"Frontier Software Development        ",'\x00','\x08','\x8C'},
  151.                 {"Intel                                ",'\x00','\xAA','\x00'},
  152.                 {"Ungermann-Bass                       ",'\x00','\xDD','\x00'},
  153.                 {"Ungermann-Bass                       ",'\x00','\xDD','\x01'},
  154.                 {"MICOM/Interlan [Unibus, Qbus, Apollo]",'\x02','\x07','\x01'},
  155.                 {"Satelcom MegaPac                     ",'\x02','\x60','\x86'},
  156.                 {"3Com [IBM PC, Imagen, Valid, Cisco]  ",'\x02','\x60','\x8C'},
  157.                 {"CMC [Masscomp, SGI, Prime EXL]       ",'\x02','\xCF','\x1F'},
  158.                 {"3Com (ex Bridge)                     ",'\x08','\x00','\x02'},
  159.                 {"Symbolics                            ",'\x08','\x00','\x05'},
  160.                 {"Siemens Nixdorf                      ",'\x08','\x00','\x06'},
  161.                 {"Apple                                ",'\x08','\x00','\x07'},
  162.                 {"HP                                   ",'\x08','\x00','\x09'},
  163.                 {"Nestar Systems                       ",'\x08','\x00','\x0A'},
  164.                 {"Unisys                               ",'\x08','\x00','\x0B'},
  165.                 {"AT&T                                 ",'\x08','\x00','\x10'},
  166.                 {"Tektronics                           ",'\x08','\x00','\x11'},
  167.                 {"Excelan                              ",'\x08','\x00','\x14'},
  168.                 {"NSC                                  ",'\x08','\x00','\x17'},
  169.                 {"Data General                         ",'\x08','\x00','\x1A'},
  170.                 {"Data General                         ",'\x08','\x00','\x1B'},
  171.                 {"Apollo                               ",'\x08','\x00','\x1E'},
  172.                 {"Sun                                  ",'\x08','\x00','\x20'},
  173.                 {"Norsk Data                           ",'\x08','\x00','\x26'},
  174.                 {"DEC                                  ",'\x08','\x00','\x2B'},
  175.                 {"Bull                                 ",'\x08','\x00','\x38'},
  176.                 {"Spider                               ",'\x08','\x00','\x39'},
  177.                 {"Sony                                 ",'\x08','\x00','\x46'},
  178.                 {"BICC                                 ",'\x08','\x00','\x4E'},
  179.                 {"IBM                                  ",'\x08','\x00','\x5A'},
  180.                 {"Silicon Graphics                     ",'\x08','\x00','\x69'},
  181.                 {"Excelan                              ",'\x08','\x00','\x6E'},
  182.                 {"Vitalink                             ",'\x08','\x00','\x7C'},
  183.                 {"XIOS                                 ",'\x08','\x00','\x80'},
  184.                 {"Imagen                               ",'\x80','\x00','\x86'}, 
  185.                 {"Xyplex                               ",'\x80','\x00','\x87'},
  186.                 {"Kinetics                             ",'\x80','\x00','\x89'},
  187.                 {"Pyramid                              ",'\x80','\x00','\x8B'},
  188.                 {"Retix                                ",'\x80','\x00','\x90'},
  189.                 {'\x0','\x0','\x0','\x0'}
  190.                      };
  191.  
  192. void change_MAC(u_char *,int);
  193. void list();
  194. void random_mac(u_char *);
  195. void help();
  196. void addr_scan(char *,u_char *);
  197.  
  198. int
  199. main(int argc, char ** argv)
  200. {
  201.     char c;
  202.     u_char mac[6] = "\0\0\0\0\0\0";
  203.     int nr = 0,eth_num = 0,nr2 = 0;
  204.     extern char *optarg;
  205.  
  206.     if (argc == 1)
  207.     {
  208.         printf("for help: changemac -h\n");
  209.         exit(1);
  210.     }
  211.  
  212.     while ((c = getopt(argc, argv, "-la:rd:")) != EOF)
  213.     {
  214.         switch(c)
  215.         {
  216.             case 'l' :
  217.                 list();
  218.                 exit(1);
  219.             case 'r' :
  220.                 nr++;
  221.                 random_mac(mac);
  222.                 break;
  223.             case 'a' :
  224.                 nr++;
  225.                 addr_scan(optarg,mac);
  226.                 break;
  227.             case 'd' :
  228.                 nr2++;
  229.                 eth_num = atoi(optarg);
  230.                 break;
  231.             default:
  232.                 help();
  233.                 exit(1);
  234.         }
  235.         if (nr2 > 1 || nr > 1)
  236.         {
  237.             printf("too many options\n");
  238.             exit(1);
  239.         }
  240.     }
  241.     change_MAC(mac,eth_num);
  242.     return (0);
  243. }
  244.  
  245. void
  246. change_MAC(u_char *p, int ether)
  247. {
  248.     struct  ifreq  devea;
  249.     int s, i;
  250.  
  251.     s = socket(AF_INET, SOCK_DGRAM, 0);
  252.     if (s < 0)
  253.     {
  254.         perror("socket");
  255.         exit(1);
  256.     }
  257.  
  258.     sprintf(devea.ifr_name, "eth%d", ether);
  259.     if (ioctl(s, SIOCGIFHWADDR, &devea) < 0)
  260.     {
  261.         perror(devea.ifr_name);
  262.         exit(1);
  263.     }
  264.  
  265.     printf("Current MAC is\t");
  266.     for (i = 0; i < 6; i++)
  267.     {
  268.         printf("%2.2x ", i[devea.ifr_hwaddr.sa_data] & 0xff);
  269.     }
  270.     printf("\n");
  271.  
  272. /* an ANSI C  ?? --> just testing your compiler */
  273.     for(i = 0; i < 6; i++) i[devea.ifr_hwaddr.sa_data] = i[p];
  274.  
  275.     printf("Changing MAC to\t");
  276.  
  277. /* right here i am showing how interesting is programing in C */
  278.  
  279.     printf("%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n", 
  280.         0[p],
  281.         1[p],
  282.         2[p],
  283.         3[p],
  284.         4[p],
  285.         5[p]);
  286.  
  287.  
  288.     if (ioctl(s,SIOCSIFHWADDR,&devea) < 0)
  289.     {
  290.         printf("Unable to change MAC -- Is eth%d device is up?\n", ether);
  291.         perror(devea.ifr_name);
  292.         exit(1);
  293.     }
  294.     printf("MAC changed\n");
  295.  
  296.     /* just to be sure ... */
  297.  
  298.     if (ioctl(s, SIOCGIFHWADDR, &devea) < 0)
  299.     {
  300.         perror(devea.ifr_name);
  301.         exit(1);
  302.     }
  303.  
  304.     printf("Current MAC is: ");
  305.  
  306.     for (i = 0; i < 6; i++) printf("%X ", i[devea.ifr_hwaddr.sa_data] & 0xff);
  307.     printf("\n");
  308.  
  309.     close(s);
  310. }
  311.  
  312. void
  313. list()
  314. {
  315.     int i = 0;
  316.     struct LIST *ptr;
  317.  
  318.     printf("\nNumber\t MAC addr \t vendor\n");
  319.     while (0[i[vendors].name])
  320.     {
  321.         ptr = vendors + i;
  322.         printf("%d\t=> %2.2x:%2.2x:%2.2x \t%s \n",
  323.             i++,
  324.             0[ptr->mac],
  325.             1[ptr->mac],
  326.             2[ptr->mac],
  327.             ptr->name);
  328.         if (!(i % 15))
  329.         {
  330.             printf("\n press enter to continue\n");
  331.             getchar();
  332.         }
  333.     }
  334. }
  335.  
  336. void
  337. random_mac(u_char *p)
  338. {
  339.     srandom(getpid());
  340.  
  341.     0[p] = random() % 256;
  342.     1[p] = random() % 256;
  343.     2[p] = random() % 256;
  344.     3[p] = random() % 256;
  345.     4[p] = random() % 256;
  346.     5[p] = random() % 256;
  347. }
  348.  
  349. void
  350. addr_scan(char *arg, u_char *mac)
  351. {
  352.     int i;
  353.  
  354.     if (!(2[arg] == ':' && 
  355.             5[arg] == ':' &&
  356.             8[arg] == ':' &&
  357.             11[arg] == ':' &&
  358.             14[arg] == ':' &&
  359.             strlen(arg) == 17 ))
  360.     {
  361.         printf("address is not in spacified format\n");
  362.         exit(0);
  363.     }
  364.     for(i = 0; i < 6; i++) i[mac] = (char)(strtoul(arg + i*3, 0, 16) & 0xff);
  365. }
  366.  
  367. void
  368. help()
  369. {
  370.     printf(" changemac - soft change MAC address of your ethernet card \n");
  371.     printf(" changemac -l | [-d number ] [ -r | -a address ] \n");
  372.     printf("   before you try to use it just turn ethernet card off, ifconfig ethX down\n");
  373.     printf(" -d number    number of ethernet device \n");
  374.     printf(" -h           this help \n");
  375.     printf(" -a address   address format is xx:xx:xx:xx:xx:xx \n");
  376.     printf(" -r           set random generated address \n");
  377.     printf(" -l           list first three MAC bytes of known ethernet vendors\n");
  378.     printf(" example: changemac -d 1 -a 12:34:56:78:9a:bc\n");
  379. }                                                           
  380.  
  381. /* EOF */
  382.