Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

c - Load and execute executable code from a memory

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The data part of test_code is placed in part of memory that is marked "not for execution". Operating systems checks that you don't use it that way. Google: process memory layout, virtual memory for thorough explanation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...