Advertisement
enfiskutensykkel

Obscure 64-bit

Jun 14th, 2018
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.75 KB | None | 0 0
  1. #include <stddef.h>
  2. #include <stdio.h>
  3. #include <signal.h>
  4. #include <setjmp.h>
  5. #include <stdlib.h>
  6.  
  7. static jmp_buf env;
  8.  
  9. static const char* shc =
  10.     "\x55\x48\x89\xe5\x48\x89\x7d\xe8\x48\x8b\x45\xe8"
  11.     "\xc7\x00\x08\x00\x00\x00\x48\xc7\x45\xf8\x00\x00"
  12.     "\x00\x00\x48\x8b\x45\x08\x48\x89\x45\xf8\x48\x8b"
  13.     "\x45\xf8\x5d\xc3\x69\x05\x05\x01\xed\x0b\x00\xb4"
  14.     "\x47\xfe\x09\x06\xfb\x00\x9b\xf6\xde\xad\xbe\xef";
  15.  
  16. static int calculate(int a, int b)
  17. {
  18.     do
  19.     {
  20.         a ^= b;
  21.         b = (a ^ b) & b;
  22.         b <<= 1;
  23.     }
  24.     while (b);
  25.  
  26.     return a & 255;
  27. }
  28.  
  29. static int showstr(const char* str)
  30. {
  31.     size_t n = 0;
  32.     char buf[64];
  33.     buf[0] = *str;
  34.    
  35.     for (n = 1; buf[n - 1] != 0; ++n)
  36.     {
  37.         buf[n] = calculate(buf[n - 1], str[n]);
  38.     }
  39.  
  40.     asm volatile (
  41. #ifdef __linux__
  42.             "movq $1, %%rax;"
  43. #else
  44.             "movq $0x2000004, %%rax;"
  45. #endif
  46.             "movq $1, %%rdi;"
  47.             "movq %0, %%rsi;"
  48.             "movq %1, %%rdx;"
  49.             "syscall"
  50.             :: "r" (buf), "r" (n)
  51.             : "rax", "rdi", "rsi", "rdx");
  52.  
  53.     return calculate(n, -n);
  54. }
  55.  
  56. static void dealwithit(int s)
  57. {
  58.     longjmp(env, s);
  59. }
  60.  
  61. int main()
  62. {
  63.     int i = EXIT_SUCCESS;
  64.     int s;
  65.     void (*bad)();
  66.  
  67.     bad = ((void (*(*)())(int*)) shc)(&s);
  68.  
  69.     if (s == 010)
  70.     {
  71.         signal(s, dealwithit);
  72.  
  73.         if (!setjmp(env))
  74.         {
  75.             int a = 2;
  76.             printf("The result of %d / %d is %d\n", a, i, a / i);
  77.             goto leave;
  78.         }
  79.  
  80.         puts("goto considered harmful");
  81.     }
  82.     else
  83.     {
  84.         s = showstr(shc + 40);
  85.         goto leave;
  86.     }
  87.  
  88.     --s;
  89.     bad();
  90.     puts("EWD would not approve!");
  91.  
  92. leave:
  93.     exit(s);
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement