尝试使用Shellcode时出现分段错误

我有一个shell代码:

        global _start
        SECTION .text
        _start:
        jmp call_shellcode
        shellcode:
                pop rsi
                xor rax, rax
                mov al, 1
                mov rdi, rax
                mov rdx, rdi
                add rdx, 40 
                syscall         ;write(1, len, msg)

                xor rax, rax
                add rax, 60
                xor rdi, rdi
                syscall         ; exit(0)
       call_shellcode:
               call shellcode
               msg: db 'Testing the code',0xa

然后我得到了机器代码 通过

     for i in $(objdump -D acode.o |grep "^ "|cut -f2); do echo -n '\x'$i; done ; echo

C代码是:

 #include<stdio.h>
 #include<string.h>
 char * code="\xeb\x1e\x5e\x48\x31\xc0\xb0\x01\x48\x89\xc7\x48\x89\xfa\x48\x83\xc2\x28\x0f\x05\x48\x31\xc0\x48\x83\xc0\x3c\x48\x31\xff\x0f\x05\xe8\xdd\xff\xff\xff\x4c\x65\x74\x20\x75\x73\x20\x48\x61\x63\x6b\x20\x50\x6c\x61\x6e\x65\x74\x20\x45\x61\x72\x74\x68\x20\x77\x69\x74\x68\x20\x41\x72\x69\x66\x20\x42\x75\x74\x74\x0a";
 int main(){
        printf("len is %d bytes\n" ,strlen(code));
        int (*foo)()=(int(*)())code;
        foo();
        return 0;
}

我不明白为什么在调用foo()时会产生分段错误

评论