gpt4 book ai didi

c++ - 我们可以打乱 C 或 C++ 中的声明顺序吗?

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

是否有适当的方法/插件/插件来忽略以下子句(对于某些 c/c++ 编译器)?要在与预处理器或类似程序相同的阶段对结构中的成员声明重新排序?也许通过在结构声明的前面添加一个关键字,如 volatile 或类似的东西。

我在想:一个编译器选项,一个内置关键字,或者一种编程方法。

C99 §6.7.2.1 clause 13 states:

Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared.

C++ 似乎也有类似的子句,我对此也很感兴趣。这些子句都指定了一个合理的特性,以便在以后的声明中具有更大的内存偏移量。但是,出于接口(interface)目的或其他原因,我通常不需要知道我的结构的声明顺序。写一些像这样的代码会很好:

scrambled struct foo {
int a;
int bar;
};

或者,假设顺序对该结构并不重要。

scrambled struct foo {
int bar;
int a;
};

因此,每次编译时随机交换 ab 的声明。我相信这也适用于预留堆栈内存。

main() {
scrambled int a;
scrambled int foo;
scrambled int bar;
..

我为什么要问?

我很好奇程序机器人是如何创建的。我看到一些人在运行将创建 hack 的程序时分析内存偏移量的变化。

看起来过程是:观察内存偏移量并记下给定偏移量的用途。稍后,黑客程序会将所需的值注入(inject)内存中的这些偏移量。

现在假设每次编译程序时这些内存偏移量都会改变。也许它会阻碍或劝阻个人花时间去了解一些你希望他们不知道的事情。

最佳答案

Run-time foxing 是最好的方法,那么你只需要发布一个单一的版本。如果 struct 有多个相同类型的字段,您可以使用数组代替。第 1 步。使用数组代替具有三个 int 字段的结构

#define foo 0
#define bar 1
#define zee 2

struct abc {
int scramble [3];
};

...
value = abc.scramble[bar];

第 2 步,现在使用每次程序运行时随机化的索引数组。

int abcindex [3];               // index lookup 
int abcpool [3]; // index pool for randomise

for (i=0; i<3; i++) // initialise index pool
abcpool[i] = i;

srand (time(NULL));
for (i=0; i<3; i++) { // initialise lookup array
j = rand()%(3-i);
abcindex[i] = abcpool[j]; // allocate random index from pool
abcpool[j] = abcpool[2-i]; // remove index from pool
}

value = abc.scramble[abcindex[bar]];

另一种试图欺骗黑客的方法是包含诡计变量,这些变量表现得好像与它有关,但如果被篡改则使程序退出。最后,您可以保留某种校验和或 key 变量的加密拷贝,以检查它们是否已被篡改。

关于c++ - 我们可以打乱 C 或 C++ 中的声明顺序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27194738/

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