Linux NASM - 重写存储在.data段中的字符 - 段错误

时间:2012-04-21 11:32:54

标签: linux assembly segmentation-fault nasm

可能是一个非常初学者的问题,但我真的很感兴趣如何让它发挥作用。

我有以下汇编代码(受here启发,重命名()示例):

[SECTION .text]
global _start

_start:
    mov             esi, msg        ; saves pointer to string to ESI
    xor             eax, eax
    mov byte        [esi+6], al     ; terminates first string with NULL char
    mov byte        [esi+13], al    ; terminates second string with NULL char
    mov byte        al, 38          ; syscall number (38 = rename)
    lea             ebx, [esi]      ; put the adress of /tmp/a in EBX
    lea             ecx, [esi+7]    ; put the adress of /tmp/b in ECX
    int             0x80            ; syscall execution

    mov             al, 0x01        ; prepare to exit!
    xor             ebx, ebx
    int             0x80            ; exit!

[SECTION .data]
msg:            db '/tmp/a#/tmp/b#'

让我解释一下:此程序调用syscall rename将文件/tmp/a重命名为/tmp/b .data中的字符串包含源文件的名称和目标文件的名称。

因为我想避免使用NULL,所以我决定使用#而不是NULL并在运行时更改它。但是,程序以SEGFAULT终止。看起来在.data段中重写#字符确实存在问题。我的问题是 - 我该如何处理它并让它发挥作用?我知道这是一个初学者的问题,也许我错过了一些非常重要的事情。

感谢您的任何建议。

EDIT - 用于组装和链接的命令

这是针对NASM的:

nasm -f elf -o ThisWorks.o ThisWorks.asm

这对于链接器(注意我将其构建为32位,尽管我有64位Phenom II)。

ld -melf_i386 -o ThisWorks.aout ThisWorks.o

比我执行它:

./ThisWorks.aout

结果是:

Segmentation fault

拆卸

这是objdump -D ThisWorks.aout

的反汇编
ThisWorks.aout:     file format elf32-i386


Disassembly of section .text:

08048080 <_start>:
 8048080:   be 9c 90 04 08          mov    $0x804909c,%esi
 8048085:   31 c0                   xor    %eax,%eax
 8048087:   88 46 06                mov    %al,0x6(%esi)
 804808a:   88 46 0d                mov    %al,0xd(%esi)
 804808d:   b0 26                   mov    $0x26,%al
 804808f:   8d 1e                   lea    (%esi),%ebx
 8048091:   8d 4e 07                lea    0x7(%esi),%ecx
 8048094:   cd 80                   int    $0x80
 8048096:   b0 01                   mov    $0x1,%al
 8048098:   31 db                   xor    %ebx,%ebx
 804809a:   cd 80                   int    $0x80

Disassembly of section .data:

0804909c <msg>:
 804909c:   2f                      das    
 804909d:   74 6d                   je     804910c <_end+0x60>
 804909f:   70 2f                   jo     80490d0 <_end+0x24>
 80490a1:   61                      popa   
 80490a2:   23 2f                   and    (%edi),%ebp
 80490a4:   74 6d                   je     8049113 <_end+0x67>
 80490a6:   70 2f                   jo     80490d7 <_end+0x2b>
 80490a8:   62 23                   bound  %esp,(%ebx)

调试显示,该程序工作正常,但在没有要重命名的文件时会陷入段错误。否则我的代码按预期工作。对不起。

2 个答案:

答案 0 :(得分:2)

在strace show下运行你的probram:

execve("./a.out", ["./a.out"], [/* 67 vars */]) = 0
[ Process PID=7054 runs in 32 bit mode. ]
rename("/tmp/a", "/tmp/b")              = -1 ENOENT (No such file or directory)
syscall_4294967041(0, 0x80490a3, 0, 0x804909c, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) = -1 (errno 38)
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV +++
Segmentation fault

结论:

  1. 您的问题与重写.data部分无关 - 重命名系统调用按您的预期执行。
  2. exit的设置不正确。

    要解决此问题,请将mov al, 0x01更改为mov eax, 0x01

答案 1 :(得分:1)

假设您正在编写shellcode:您无法从shellcode中访问.data段中的任何内容,因为您的shellcode将在被利用的软件进程中执行,并且那里不会有数据段(或更多)确切地说,只有被利用过程的数据段)。 Shellcode没有分段结构,它通常只是一个字节的线性缓冲区。无论如何,您必须将数据收集到shellcode的末尾。