汇编代码到 shell 代码:section .data 和 section .text 以什么顺序?

问题描述

对于一个作业,我编写了以下汇编代码 shell_exec.asm,它应该在 Linux 中执行一个 shell:

section .data ; declare stuff
  arg0 db "/bin/sh",0 ; 1st arg
  align 4
  argv dd arg0,0 ; 2nd arg
  envp dd 0 ; 3rd arg

section .text
global _start
_start:
  mov eax,0x0b ; execve
  mov ebx,arg0 ; 1st arg
  mov ecx,argv ; 2nd arg
  mov edx,envp ; 3rd arg
  int 0x80 ; kernel

我使用 nasm -f elf32 shell_exec.asm 进行编译,使用 ld -m elf_i386 -o shell_exec shell_exec.o 进行链接。到目前为止一切正常,如果我运行 ./shell_exec,shell 会按照我想要的方式生成。

现在我想从这个程序中提取 shell 代码(如 \12\34\ab\cd\ef...)。我使用 objdump -D -z shell_exec 来显示代码的所有部分,包括 section .data 和全零。输出如下:

shell_exec:     file format elf32-i386


Disassembly of section .text:

08049000 <_start>:
 8049000:       b8 0b 00 00 00          mov    $0xb,%eax
 8049005:       bb 00 a0 04 08          mov    $0x804a000,%ebx
 804900a:       b9 08 a0 04 08          mov    $0x804a008,%ecx
 804900f:       ba 10 a0 04 08          mov    $0x804a010,%edx
 8049014:       cd 80                   int    $0x80

Disassembly of section .data:

0804a000 <arg0>:
 804a000:       2f                      das
 804a001:       62 69 6e                bound  %ebp,0x6e(%ecx)
 804a004:       2f                      das
 804a005:       73 68                   jae    804a06f <__bss_start+0x5b>
 804a007:       00                      add    %al,(%eax)

0804a008 <argv>:
 804a008:       00 a0 04 08 00 00       add    %ah,0x804(%eax)
 804a00e:       00 00                   add    %al,(%eax)

0804a010 <envp>:
 804a010:       00 00                   add    %al,(%eax)
 804a012:       00 00                   add    %al,(%eax)

如果我的汇编代码中只有一个 section .text,我通常可以复制所有给定的值并将它们用作我的 shell 代码。但是,如果我有这两个部分,即 .data.text,顺序如何?

编辑 1

所以,我的第二次尝试是像这样编写汇编代码:

section .text
global _start

_start:
mov ebp,esp

xor eax,eax
push eax ; -4
push "/sh " ; -8
push "/bin" ; -12

xor eax,eax
push eax
lea ebx,[ebp-12] 
push ebx ; 1st arg

mov ecx,esp ; 2nd arg
lea edx,[ebp-4] ; 3rd arg

mov eax,0x0b ; execve
int 0x80 ; kernel

这避免了使用多个部分,但遗憾的是会导致分段错误。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...