c - 执行的shellcode终止主程序

标签 c assembly execute terminate shellcode

我正在尝试在内存区域中执行 shellcode。虽然到目前为止它有效,但我现在面临另一个问题:在调用 shellcode 程序后,main-c 程序退出。除了使用线程之外,还有其他(简单的)方法可以解决这个问题吗?

我认为这与mov rax, 60和后面的syscall退出程序有关。对吗?

主 C 代码

#include <string.h>
#include <sys/mman.h>

const char shellcode[] = "\xeb\x1e\xb8\x01\x00\x00\x00\xbf\x01\x00\x00\x00\x5e\xba\x0d\x00\x00\x00\x0f\x05\xb8\x3c\x00\x00\x00\xbf\x00\x00\x00\x00\x0f\x05\xe8\xdd\xff\xff\xff\x48\x65\x6c\x6c\x6f\x2c\x20\x57\x6f\x72\x6c\x64\x21";

// Error checking omitted for expository purposes
int main(int argc, char **argv)
{
  // Allocate some read-write memory
  void *mem = mmap(0, sizeof(shellcode), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);

  // Copy the shellcode into the new memory
  memcpy(mem, shellcode, sizeof(shellcode));

  // Make the memory read-execute
  mprotect(mem, sizeof(shellcode), PROT_READ|PROT_WRITE|PROT_EXEC);

  // Call the shellcode
  void (*func)();
  func = (void (*)())mem;
  (void)(*func)();

  // This text will never appear
  printf("This text never appears");

  // Now, if we managed to return here, it would be prudent to clean up the memory:
  // (I think that this line of code is also never reached)
  munmap(mem, sizeof(shellcode));

  return 0;
}

Shellcode 基础(汇编程序(英特尔))

global _start

_start:
    jmp message

code:
    mov     rax, 1
    mov     rdi, 1
    pop     rsi
    mov     rdx, 13
    syscall

    mov    rax, 60
    mov    rdi, 0
    syscall

message:
    call code
    db "Hello, World!"

最佳答案

imo 最简单的方法是创建一个二进制文件,然后 exec() 。如果您需要输出,请设置管道。

关于c - 执行的shellcode终止主程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43912394/

相关文章:

c - 如果两者在 C 中具有相同的名称而不使用第三个变量,如何将局部变量复制到全局变量?

c - 像 C++ constexpr 一样的 ANSI-C 常量表达式函数?

c - 何时以及如何创建进程控制 block

c++ - FLD浮点指令加载常数

python - 从 python 打开文件

html - 如何将c文件转换为cgi文件?

python - 使用 ctypes 在 Python 中解码 C const char*

linux - eax 是否始终且仅存储系统调用?

sql - 在postgres动态sql中使用数组作为参数

php - 警告:PDO::prepare() 期望参数 1 为字符串,给定对象