x86 程序集:创建一个 shellcode 以在缓冲区溢出成功后写入文件

问题描述 投票:0回答:0

嗨 Stack Overflow 社区,

我目前正在应对 CTF 挑战,我需要在 C 程序上执行缓冲区溢出,然后执行 shellcode 来创建和写入文件。给出的C程序如下:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char** argv)
{
    setuid(0);
    char buffer[500];
    strcpy(buffer, argv[1]);

    return 0;
}

我成功执行了使用 GDB 和 NOP sled 生成 shell 的 shellcode。但是,我现在想创建一个写入文件的 shellcode。我开始为它编写一个简单的 x86 汇编代码,编译它,并使用 objdump 检索十六进制表示,但它似乎不起作用。程序正常退出,没有创建文件。

下面是我写的x86汇编代码:

global _start
_start:
shellcode:
      jmp before_string
after_jump:
      mov ebx            ; address of "id.txt" string
      xor ecx, ecx        ; Zeroing ecx
      xor eax, eax       ; Zeroing eax
      xor edx, edx       ; Zeroing edx

      mov al, 0x05       ; Open system call
      mov ecx, 0x41       ; O_WRONLY

      mov [ebx+6], dl    ; NULL terminator for "helloworld.txt"
      int 0x80           ; call open("id.txt", 1, 0). File descriptor is returned in eax

      mov ebx, eax       ; Move file descriptor to ebx
      ;Write system call

shellcode_1:
    jmp before_write
after_jump_write:
      pop ecx            ; address of "hello" string
      xor edx, edx       ; Zeroing edx
      xor eax, eax       ; Zeroing eax
      mov [ebx+9], dl    ; NULL terminator for "helloworld.tx"
      mov edx, 0x09      ; length of "hello" string in bytes
      mov al, 0x04       ; write system call
      int 0x80           ; call write(1, "209041474", 9)

      xor eax, eax
      xor ebx, ebx
      mov al, 0x1        ; exit system call
      int 0x80

before_string:
      call after_jump

string_open:
      db "helloworld.txt"

before_write:
      call after_jump_write

string_write:
      db "hello"

我不确定为什么 shellcode 没有按预期工作。任何人都可以帮助我确定问题或提供有关如何在执行缓冲区溢出后正确创建 shellcode 以写入 x86 程序集中的文件的任何指导吗?任何帮助将非常感激。谢谢!

c assembly buffer-overflow ctf
© www.soinside.com 2019 - 2024. All rights reserved.