小编Cha*_*une的帖子

将MachineCode从文件加载到内存并执行C - mprotect失败

嗨,我正在尝试将原始机器代码加载到内存中并在C程序中运行它,现在当程序执行时,它试图在内存上运行mprotect使其可执行时中断.我也不完全确定如果内存设置正确,它将执行.我目前在Ubuntu Linux x86上运行它(也许问题是Ubuntu的过度保护?)

我目前拥有以下内容:

#include <memory.h>
#include <sys/mman.h>
#include <stdio.h>

int main ( int argc, char **argv )
{
 FILE *fp;
 int sz = 0;
 char *membuf;
 int output = 0;

 fp = fopen(argv[1],"rb");

 if(fp == NULL)
 {
  printf("Failed to open file, aborting!\n");
  exit(1);
 }

 fseek(fp, 0L, SEEK_END);
 sz = ftell(fp);
 fseek(fp, 0L, SEEK_SET);


 membuf = (char *)malloc(sz*sizeof(char));
 if(membuf == NULL)
 {
  printf("Failed to allocate memory, aborting!\n");
  exit(1);
 }

  memset(membuf, 0x90, sz*sizeof(char));

 if( mprotect(membuf, sz*sizeof(char), PROT_EXEC | PROT_READ | PROT_WRITE) …
Run Code Online (Sandbox Code Playgroud)

c memory machine-code mprotect

5
推荐指数
1
解决办法
1801
查看次数

标签 统计

c ×1

machine-code ×1

memory ×1

mprotect ×1