Advertisement
badeip

Solution to the Australian Department of Defense challenge

Dec 4th, 2011
1,211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.86 KB | None | 0 0
  1. // By petter wahlman, http://www.twitter.com/badeip
  2. // IA-32 Solution for the Australian Government's Department of Defense job application challenge:
  3. // https://plus.google.com/103685227755333384561/posts/VasNhJpVFA4
  4. // Thanks to: @alfiejohn_ for sending me the image.
  5.  
  6. #include <stdio.h>
  7. #include <stdint.h>
  8. #include <malloc.h>
  9. #include <stdlib.h>
  10. #include <errno.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include <sys/types.h>
  14. #include <sys/mman.h>
  15. #include <sys/utsname.h>
  16.  
  17. static uint8_t dsd[] = {
  18.      0xe8, 0x00, 0x00, 0x00, 0x00, 0x5b, 0x8b, 0xcb, 0x83, 0xc3, 0x1e, 0x33, 0xc0, 0x33, 0xd2, 0x8a,
  19.      0x03, 0x8a, 0x11, 0x32, 0xc2, 0x88, 0x03, 0x3c, 0x00, 0x74, 0x2b, 0x83, 0xc1, 0x01, 0x83, 0xc3,
  20.      0x01, 0xeb, 0xec, 0x33, 0xff, 0xbf, 0xf3, 0xf9, 0x31, 0x1c, 0xb7, 0x44, 0xa5, 0xa4, 0x67, 0xf9,
  21.      0x75, 0x1c, 0xa5, 0xe7, 0x75, 0x12, 0x61, 0x01, 0x04, 0xe7, 0xa4, 0x62, 0xec, 0xa7, 0x64, 0x8f,
  22.      0xc2, 0x00, 0x00, 0x19, 0x1c, 0x3a, 0xcc
  23. };
  24.  
  25. static const uint8_t dump_mem[] = {
  26.     0x8d, 0x79, 0xfc,                       // lea    edi,[ecx-0x4]
  27.     0x57,                                   // push   edi
  28.     0x31, 0xc9,                             // xor    ecx,ecx
  29.     0x30, 0xc0,                             // xor    al,al
  30.     0xf7, 0xd1,                             // not    ecx
  31.     0xfc,                                   // cld
  32.     0xf2, 0xae,                             // repne scasb
  33.     0xf7, 0xd1,                             // not    ecx
  34.     0x49,                                   // dec    ecx
  35.     0x66, 0xc7, 0x47, 0xff, 0x0a, 0x00,     // mov    WORD PTR [edi-1],0xa00
  36.     0x41,                                   // inc    ecx
  37.     0x89, 0xca,                             // mov    edx,ecx
  38.     0x59,                                   // pop    ecx
  39.     0xb8, 0x04, 0x00, 0x00, 0x00,           // mov    eax,0x4
  40.     0x31, 0xdb,                             // xor    ebx,ebx
  41.     0xfe, 0xc3,                             // inc    bl
  42.     0xcd, 0x80,                             // int    0x80
  43.     0x89, 0xd3,                             // mov    ebx,edx
  44.     0x31, 0xc0,                             // xor    eax,eax
  45.     0xfe, 0xc0,                             // inc    al
  46.     0xcd, 0x80                              // int    0x80
  47. };
  48.  
  49.  
  50. //#define UDIS86
  51. #ifdef UDIS86
  52.     #include <udis86.h>
  53.  
  54.     uint32_t btox(uint8_t *buf, uint32_t size, uint8_t *out)
  55.     {
  56.         uint32_t i;
  57.  
  58.         for (i = 0; i < size; i++)
  59.             snprintf(&out[i * 3], 4, "%02x ", buf[i]);
  60.         return i;
  61.     }
  62.  
  63.     uint32_t disass(uint8_t *mem, uint32_t size)
  64.     {
  65.         ud_t u;
  66.         ud_init(&u);
  67.         ud_set_input_buffer(&u, mem, size);
  68.         ud_set_mode(&u, 64);
  69.         ud_set_syntax(&u, UD_SYN_INTEL);
  70.         while (ud_disassemble(&u)) {
  71.             uint64_t offset;
  72.             uint32_t len;
  73.             uint8_t hex[64];
  74.  
  75.             len =  ud_insn_len(&u);
  76.             offset =  ud_insn_off(&u);
  77.             btox(&dsd[offset], len, hex);
  78.             printf("\t%-16s: %s\n", hex, ud_insn_asm(&u));
  79.         }
  80.         printf("\n");
  81.  
  82.     }
  83. #else
  84.     #define disass
  85. #endif
  86.  
  87. int main(int argc, char **argv)
  88. {
  89.     uint8_t *mem;
  90.  
  91.     disass(dsd, sizeof(dsd));
  92.  
  93.     printf("[*] allocating page aligned memory\n");
  94.     mem = memalign(4096, 4096);
  95.     if (!mem) {
  96.         printf("[-] error: %s\n", strerror(errno));
  97.         return 1;
  98.     }
  99.     memset(mem, 0, 4096);
  100.  
  101.     printf("[*] setting page permissions\n");
  102.     if (mprotect(mem, 4096, PROT_READ | PROT_WRITE | PROT_EXEC)) {
  103.         printf("[-] error: %s\n", strerror(errno));
  104.         return 1;
  105.     }
  106.  
  107.     printf("[*] copying payload\n");
  108.     memcpy(mem, dsd, sizeof(dsd));
  109.  
  110.     printf("[*] patching payload\n");
  111.     memcpy(mem + sizeof(dsd) -1, dump_mem, sizeof(dump_mem));
  112.  
  113.     printf("[*] executing payload..\n\n");
  114.     ((int(*)(void))mem)();
  115.  
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement