views:

257

answers:

1

Greetings, SO.

I have some code which I've made attempts at compiling using gcc, but my attempts have been thwarted. Could anyone more versed assist me with the subject, perhaps there's something I'm missing.

I'm compiling this code on Linux Kitchen 2.6.28-15-generic #49-Ubuntu SMP Tue Aug 18 19:25:34 UTC 2009 x86_64 GNU/Linux.

int
main(void)
{
    __asm__(
            "xorq %rdx,%rdx"
            "movq $0x68732f6e69622fff, %rdx"
            "shr $0x8, %rbx"
            "push %rbx"
            "movq %rsp,%rdi"
            "xorq %rax,%rax"
            "pushq %rax"
            "pushq %rdi"
            "movq %rsp,%rsi"
            "mov $0x3b, %al"
            "syscall"
            "pushq $0x1"
            "pop %rdi"
            "pushq $0x3c"
            "pop %rax"
            "syscall"
    );

    return 0;
}

The error that is returned is:

$ gcc -o shellcode shellcode.c
shellcode.c: Assembler messages:
shellcode.c:4: Error: bad register name `%rdxmovq $0x68732f6e69622fff'

Thanks, everyone.

+7  A: 

You need to put newlines (\n) into your quoted inline assembly. Otherwise, it thinks that

xorq %rdx,%rdx
movq $0x68732f6e69622fff, %rdx

is really

xorq %rdx,%rdxmovq $0x68732f6e69622fff, %rdx

So the first two lines (and so on) should be more like this:

"xorq %rdx,%rdx\n"
"movq $0x68732f6e69622fff, %rdx\n"
jgottula