gpt4 book ai didi

c++ - 在C++中融化POC

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:41:55 25 4
gpt4 key购买 nike

我听说了Meltdown漏洞,并阅读了paper,这很困难,因为我不是母语人士,但是我决定制作一个概念验证的小型c++程序,如下所示,我想知道造成这种情况的原因代码不起作用。
我已经在AMD FX 8350八核和Intel I3处理器上对其进行了测试

/*In this code I wanted to make a PoC for the Meltdown flaw (https://meltdownattack.com/)*/
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <chrono>

int secretvar = 15; //This is our "unreadable" var wich we "cannot" read but we will have the value exposed via cache attack
int x = rand() % 100; //Used later, importent that it's random

int main()
{
int arr[1000]; //We setup our array
for (int i = 0; i < 100; ++i) { //Fill it with random values
arr[i] = rand() % 10 + 1;
};

if (x == 4) { //Here I want to trigger the Out-of-Order execution so that the cpu executes the code below th if case
int c = arr[secretvar]; //before it really checks if x is actually 4. And with this, the CPU would put the cached variable "c" into
//its cache.
};

/*At this point we dont know th value of secretvar but we know that the CPU has cached some index with the
exact value of secretvar.Therefore we can now iterate over the array and how long it takes the cpu to acess the
diffrent array indexes.The cpu will be faster at acessing one specific element because it is taken from cache
and not from the memory.This index is the value of secretvar.*/

double lowest_val = 500;
int lowest_index = 0;

for (int i = 0; i < 100; i++) {
auto start = std::chrono::high_resolution_clock::now(); //start timer
arr[i]; //acess array
auto finish = std::chrono::high_resolution_clock::now();//end timer
std::chrono::duration<double> elapsed = finish - start;//calculate needed time
double e = elapsed.count();
if (e < lowest_val) {
lowest_val = e;
lowest_index = i;
}
std::cout << i << " : " << e << " s\n"; //show it to the screen
}
std::cout << lowest_index << "Was acessed fastest "<< " with a time of "<< lowest_val << "ms" << std::endl;
system("pause");
return 0;
}

最佳答案

好的,因此我意识到,如果我用汇编器对其进行编程,则此测试会更加准确。接下来我会尝试,但是在那之前我必须学习一些东西。我认为问题已经解决,但是仍然欢迎您提出有关c++程序的建议。
谢谢您的帮助

关于c++ - 在C++中融化POC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48138210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com