jeudi 7 juillet 2016

Desiging Shellcode gives incorrect results

I made this simple assembly program:

.text
    .globl _start
    _start:
        mov %20, %rbx
        mov %1, %rax
        int $0x80

This is obviously running on a 64 bit OS (Linux). I then compiled it as follows:

as -o ExitShellcode.o ExitShellcode.s
ld -o ExitShellcode ExitShellcode.o

And finally after running the program, It exits with a status of 20

echo $?
20

Using objdump to dump the shellcode for the file gives:

objdump -d ExitShellcode

ExitShellcode:     file format elf64-x86-64


Disassembly of section .text:

0000000000400078 <_start>:
400078: 48 c7 c3 14 00 00 00    mov    $0x14,%rbx
40007f: 48 c7 c0 01 00 00 00    mov    $0x1,%rax
400086: cd 80                   int    $0x80

However, after putting the shellcode in this program:

#include <stdio.h>

char shellcode[] = "x48xc7xc3x14x00x00x00"
                   "x48xc7xc0x01x00x00x00"
                   "xcdx80";

int main()
{
    int *ret;

    ret = (int *)&ret +2;

    *ret = (int)shellcode;

}

and compiling:

gcc -g -o Shellcode Shellcode.c
Shellcode.c: In function ‘main’:
Shellcode.c:13:9: warning: cast from pointer to integer of different        size     [-Wpointer-to-int-cast]
*ret = (int)shellcode;

and running, the program exits with a 0 status:

echo $?
0

What's the proplem? Shouldn't it exit with a 20?

Aucun commentaire:

Enregistrer un commentaire