1

I wrote a small program in assembly which is supposed to print "AAAA". It works fine when I run it directly, but when I run it as a shellcode in a c program, it doesn't work. Please help.

Here is the assembly code: enter image description here

When I compile and execute the following code:

char shellcode[] = {0x31,0xc0,0xb0,0x04,0x31,0xdb,0xb3,0x01,0x68,0x41,0x41,0x41,0x41,0x89,0xe1,0x31,0xd2,0xb2,0x04,0xcd,0x80,0x31,0xc0,0xb0,0x01,0x31,0xdb,0xb3,0x01,0xcd,0x80};
int main(){
        (*(void(*)())shellcode)();
        return 0;
}

Compiled as:

$gcc -g -Wall -fno-stack-protector -z execstack code.c -o code

Execution:

$./code
$

1 Answer 1

5

You're showing x86 (32bit) shellcode, but are not compiling your program for that architecture, so gcc most likely creates an amd64 (64bit) executable instead. This can be fixed by adding the -m32 switch:

gcc -g -Wall -fno-stack-protector -z execstack -m32 code.c -o code

You can verify this by running file on the resulting file:

code: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), […]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.