Advertisement
Madmouse

adventures in no libc hell, now with library functions xD

Jun 6th, 2015
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | None | 0 0
  1.  
  2. // ----------------------------------------------------------------------------
  3. // "THE BEER-WARE LICENSE" (Revision 43):
  4. // <aaronryool@gmail.com> wrote this file. As long as you retain this notice you
  5. // can do whatever you want with this stuff. If we meet some day, and you think
  6. // this stuff is worth it, you can buy me a beer in return Aaron R. Yool
  7. // ----------------------------------------------------------------------------
  8. // the no libc thing is refering to the assembly project this is a probe for, obviously this uses libc :P
  9.  
  10.  
  11. #include <dirent.h>
  12. #include <fcntl.h>
  13. #include <stdio.h>
  14. #include <unistd.h>
  15. #include <stdlib.h>
  16. #include <sys/stat.h>
  17. #include <sys/syscall.h>
  18.  
  19. #include <stddef.h>
  20. #include <stdio.h>
  21. #include <stdint.h>
  22.  
  23. #define handle_error(msg) \
  24.     do { perror(msg); exit(EXIT_FAILURE); } while (0)
  25.  
  26.  
  27. typedef struct linux_dirent {
  28.     long           d_ino;
  29.     off_t          d_off;
  30.     unsigned short d_reclen;
  31.     char           d_name[];
  32. } dent;
  33.  
  34. print_dent_offsets()
  35. {
  36.     printf("ino: %i\n", offsetof(dent, d_ino));
  37.     printf("off: %i\n", offsetof(dent, d_off));
  38.     printf("reclen: %i\n", offsetof(dent, d_reclen));
  39.     printf("name: %i\n", offsetof(dent, d_name));
  40. }
  41.  
  42.  
  43. int Mopendir(const char* dirstr)
  44. {
  45.     int fd;
  46.     asm(
  47.         ".intel_syntax noprefix\n"
  48.         "xor rax, rax\n"
  49.         "mov al, 2\n"
  50.         "mov rdi, %1\n"
  51.         "mov rsi, 0x10000\n"
  52.         "xor rdx, rdx\n"
  53.         "syscall\n"
  54.         :"+a"(fd) : "r"(dirstr)
  55.     );
  56.     return fd;
  57. }
  58.  
  59. void* Mmalloc(uint64_t size)
  60. {
  61.     void* ptr;
  62.     asm(
  63.         "xor rax, rax\n"    //; char* buf=(char*)sbrk(1024);
  64.         "mov al, 12\n"
  65.         "xor rdi, rdi\n"    //;         //end=brk(0) <- get the current end value
  66.         "syscall\n"
  67.         "lea r9, [rax]\n"
  68.         "xor rax, rax\n"
  69.         "mov al, 12\n"
  70.         "add r9, %1\n"
  71.         "mov rdi, r9\n"     //;         //brk(end+1024) <- get the current end value
  72.         "syscall\n"
  73.         :"+a"(ptr) : "r"(size)
  74.     );
  75.     return ptr;
  76. }
  77.  
  78. Mgetdents(uint64_t fd, void* buf, uint64_t size)
  79. {
  80.     int nread;
  81.     asm(
  82.         "xor rax, rax\n"    //;     nread = syscall(SYS_getdents, fd, buf, sizeof(buf));
  83.         "mov al, 78\n"
  84.         "mov rdi, %1\n"
  85.         "mov rsi, %2\n"
  86.         "mov rdx, %3\n"
  87.         "syscall\n"
  88.         :"+a"(nread) :"r"(fd), "r"(buf),  "r"(size)
  89.     );
  90.     return nread;
  91. }
  92.  
  93. main()
  94. {
  95.     int fd, nread;
  96.     // opendir
  97.     fd = Mopendir(".");
  98.     if (fd == -1)
  99.         handle_error("open");
  100.    
  101.     char* buf = Mmalloc(1024);
  102.    
  103.     // list dir
  104.     nread = Mgetdents(fd, buf, 1024);
  105.     if (nread == -1)
  106.         handle_error("getdents");
  107.    
  108.    
  109.     int bpos = 0;
  110.     for (bpos = 0; bpos < nread;)
  111.     {
  112.         dent* d ;//= (struct linux_dirent *) (buf + bpos);
  113.         asm("lea %0, [%1+%2]" :"=r"(d) : "r"(buf), "r"((uint64_t)bpos));
  114.        
  115.         int d_type = *(buf + bpos + d->d_reclen - 1);
  116.         if(d_type == DT_REG)
  117.             printf("%8d %s\n", d->d_reclen, d->d_name);
  118.        
  119.         bpos += d->d_reclen;
  120.     }
  121.     // closedir
  122.     close(fd);
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement