I am trying to do some test experiment for testing out code security. This is part of executing some arbitrary code into any given memory location.
The detailed experiment is to create a test binary, make a hex dump of the executable, create an array of that hex dump in another source code and then jump into text section of that array of executable binary and see it is able to execute that code or not.
I had created a small testbin -
//**testbin.c:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
void main()
{
printf("
HELLO
");
}
So the text section as follows (removed other section) The offset is 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
With xxd -i, created a array and added to a header file which will be included in the test code.
unsigned char testbin[] = {
0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
// The rest of array
Now in the test code
#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();
}
But I see segmentation fault when I execute. The expected outcome is HELLO being printed when I jump to the code. Any suggestion?
question from:
https://stackoverflow.com/questions/65871645/load-and-execute-executable-code-from-a-memory