Harcrack

libc-search.c

Oct 17th, 2018
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.55 KB | None | 0 0
  1. /*
  2.  * $Id: libc-search.c,v 1.5 2003/08/28 17:31:26 raptor Exp $
  3.  *
  4.  * libc-search.c - quick libc symbol/pattern search helper
  5.  * Copyright (c) 2003 Marco Ivaldi <[email protected]>
  6.  *
  7.  * This quick and easily-adaptable program loads its dynamic libraries and
  8.  * tries to find the address of the symbol/pattern specified as command
  9.  * line argument. It can be somewhat useful when writing exploits with the
  10.  * return-into-libc technique.
  11.  *
  12.  * Compile with the same shared libs of the program you want to exploit,
  13.  * plus of course -ldl.
  14.  *
  15.  * $ gcc libc-search.c -o libc-search -lc -ldl
  16.  * $ ./libc-search -s system              
  17.  * The system address is: 0x4005f590
  18.  * $ ./libc-search -s _exit
  19.  * The _exit address is: 0x400bae90
  20.  * $ ./libc-search -p /bin/sh
  21.  * The /bin/sh address is: 0x4012276d
  22.  * $ ./libc-search -p /bin/sh -b 0x4012276e
  23.  * The /bin/sh address is: 0x401254e3
  24.  *
  25.  * NOTE. Don't use it to find functions used in the local program itself
  26.  * (e.g. exit, fprintf, etc.) or you'll end up finding the ones in .plt.
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <dlfcn.h>
  33. #include <signal.h>
  34.  
  35. #define LIBBASE     0x4001a000  // change if needed
  36. #define OPT_SYMBOL  0x01
  37. #define OPT_PATTERN 0x02
  38. #define OPT_LIBBASE 0x04
  39.  
  40. void fault() /* SIGSEGV handler */
  41. {
  42.     fprintf(stderr, "ERROR: sorry, pattern not found\n");
  43.     exit(-1);
  44. }
  45.  
  46. void check_zero(int addr) /* check an address for the presence of a 0x00 */
  47. {
  48.     if ( !(addr & 0xff) || !(addr & 0xff00) || !(addr & 0xff0000) ||
  49.         !(addr & 0xff000000) )
  50.         fprintf(stderr, "WARNING: the address contains a 0x00!\n");
  51. }
  52.  
  53. int search_str(char *pattern, int addr) /* search for a string in libc */
  54. {
  55.     /* install SIGSEGV handler and begin search */
  56.     signal(SIGSEGV, fault);
  57.  
  58.     while ( memcmp((void *)addr, pattern, strlen(pattern) + 1) )
  59.         addr++;
  60.  
  61.     /* uninstall SIGSEGV handler and end search */
  62.     signal(SIGSEGV, SIG_DFL);
  63.  
  64.     check_zero(addr);
  65.     return(addr);
  66. }
  67.  
  68. int search(char *pattern)
  69. {
  70.     void *p;
  71.     int addr;
  72.  
  73.     /* dlopen() the main program */
  74.     if ( !(p = dlopen(NULL, RTLD_LAZY)) ) {
  75.         fprintf(stderr, "%s\n", dlerror());
  76.         exit(-1);
  77.     }
  78.  
  79.     /* search for the pattern */
  80.     if ( !(addr = (int)dlsym(p, pattern)) ) {
  81.         fprintf(stderr, "%s\n", dlerror());
  82.         exit(-1);
  83.     }
  84.  
  85.     dlclose(p);
  86.  
  87.     check_zero(addr);
  88.     return(addr);
  89. }
  90.  
  91. void usage(char *name)
  92. {
  93.     fprintf(stderr, "usage: %s [ -s <symbol> | "
  94.             "-p <pattern> [-b <libbase>] ]\n", name);
  95.     exit(-1);
  96. }
  97.  
  98. int main(int argc, char **argv)
  99. {
  100.     int address, libbase = LIBBASE, opt_line = 0;
  101.     char arg[256], *foo;
  102.  
  103.     if (argc < 2)
  104.         usage(argv[0]);
  105.  
  106.     /* parse command line */
  107.     {
  108.         int c = 0;
  109.         while ( (c = getopt(argc, argv, "s:p:b:h")) != EOF )
  110.  
  111.             switch (c) {
  112.                 case 'h':
  113.                     usage(argv[0]);
  114.                     break;
  115.                 case 's':
  116.                     opt_line |= OPT_SYMBOL;
  117.                     strncpy(arg, optarg, 255);
  118.                     break;
  119.                 case 'p':
  120.                     opt_line |= OPT_PATTERN;
  121.                     strncpy(arg, optarg, 255);
  122.                     break;
  123.                 case 'b':
  124.                     opt_line |= OPT_LIBBASE;
  125.                     libbase = strtoul(optarg, &foo, 16);
  126.                     break;
  127.             }
  128.     }
  129.  
  130.     /* check command line options */
  131.     if ( !(opt_line & OPT_SYMBOL) && !(opt_line & OPT_PATTERN) )
  132.         usage(argv[0]);
  133.     if ( (opt_line & OPT_SYMBOL) && (opt_line & OPT_PATTERN) )
  134.         usage(argv[0]);
  135.     if ( (opt_line & OPT_SYMBOL) && (opt_line & OPT_LIBBASE) )
  136.         usage(argv[0]);
  137.  
  138.     if (opt_line & OPT_SYMBOL)
  139.         /* find symbol */
  140.         address = search(arg);
  141.     else
  142.         /* find pattern */
  143.         address = search_str(arg, libbase);
  144.  
  145.     fprintf(stderr, "The %s address is: %p\n", arg, address);
  146.  
  147.     exit(0);
  148. }
Advertisement
Add Comment
Please, Sign In to add comment