【发布时间】:2021-04-28 11:36:37
【问题描述】:
我正在尝试做一些测试实验来测试代码安全性。这是将一些任意代码执行到任何给定内存位置的一部分。 详细的实验是创建一个测试二进制文件,制作可执行文件的十六进制转储,在另一个源代码中创建该十六进制转储的数组,然后跳转到该可执行二进制数组的文本部分并查看它是否能够执行该代码与否。
我创建了一个小测试箱 -
//**testbin.c:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
void main()
{
printf("\nHELLO\n");
}
所以文本部分如下(去掉其他部分)偏移量为0x1060
objdump --headers testbin
testbin: file format elf64-x86-64
Sections:
Idx Name Size VMA LMA File off Algn
11 .init 0000001b 0000000000001000 0000000000001000 00001000 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
12 .plt 00000020 0000000000001020 0000000000001020 00001020 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
13 .plt.got 00000010 0000000000001040 0000000000001040 00001040 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
14 .plt.sec 00000010 0000000000001050 0000000000001050 00001050 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
15 .text 00000175 0000000000001060 0000000000001060 00001060 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
16 .fini 0000000d 00000000000011d8 00000000000011d8 000011d8 2**2
使用 xxd -i,创建一个数组并添加到将包含在测试代码中的头文件中。
unsigned char testbin[] = {
0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
// The rest of array
现在在测试代码中
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "test_code.h"
void main()
{
uint32_t *ptr = NULL;
ptr = (uint32_t *)testbin;
void (*foo)(void) = (void (*)())(ptr + 0x1060);
foo();
}
但我在执行时看到分段错误。当我跳转到代码时,预期的结果是打印 HELLO。有什么建议吗?
【问题讨论】: