2

I'm asm/shellcode newbie and I have trouble with compile example shellcode included as template in ParrotOS.

I looked through following topics, but It couldn't solve my problem:

Assembly error: invalid character '$' in mnemonic

Invalid character (0xe2) in mnemonic

I tried use as command instead of gcc, however I got this same error.

char code[] = 

    "\xe9\x1e\x00\x00\x00"  //          jmp    8048083 <MESSAGE>
    "\xb8\x04\x00\x00\x00"  //          mov    $0x4,%eax
    "\xbb\x01\x00\x00\x00"  //          mov    $0x1,%ebx
    "\x59"                  //          pop    %ecx
    "\xba\x0f\x00\x00\x00"  //          mov    $0xf,%edx
    "\xcd\x80"              //          int    $0x80
    "\xb8\x01\x00\x00\x00"  //          mov    $0x1,%eax
    "\xbb\x00\x00\x00\x00"  //          mov    $0x0,%ebx
    "\xcd\x80"              //          int    $0x80
    "\xe8\xdd\xff\xff\xff"  //          call   8048065 <GOBACK>
    "give me a bottle of rum!\r\n";     // OR       "\x48\x65\x6c\x6c\x6f\x2c\x20\x57"
                            //          "\x6f\x72\x6c\x64\x21\x0d\x0a"


int main(int argc, char **argv)
{
    (*(void(*)())code)();

    return 0;
}
gcc shellcode.s

shellcode.s: Assembler messages:
shellcode.s:1: Error: no such instruction: `char code[]='
shellcode.s:3: Error: invalid character '\' in mnemonic
shellcode.s:4: Error: invalid character '\' in mnemonic
shellcode.s:5: Error: invalid character '\' in mnemonic
shellcode.s:6: Error: invalid character '\' in mnemonic
shellcode.s:7: Error: invalid character '\' in mnemonic
shellcode.s:8: Error: invalid character '\' in mnemonic
shellcode.s:9: Error: invalid character '\' in mnemonic
shellcode.s:10: Error: invalid character '\' in mnemonic
shellcode.s:11: Error: invalid character '\' in mnemonic
shellcode.s:12: Error: invalid character '\' in mnemonic
shellcode.s:13: Error: no such instruction: `give me a bottle of rum!\r\n"'
shellcode.s:17: Error: junk `(int argc,char **argv)' after expression
shellcode.s:17: Error: operand size mismatch for `int'
shellcode.s:18: Error: no such instruction: `{'
shellcode.s:19: Error: junk at end of line, first unrecognized character is `('
shellcode.s:21: Error: no such instruction: `return 0'
shellcode.s:22: Error: junk at end of line, first unrecognized character is `}'
Innewit
  • 37
  • 10

2 Answers2

7

This is C code, but it has a .s extension which indicates assembly. Rename your file to shellcode.c.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Thank you, that was a little tricky to debug. I didn't expect that gcc recognize extensions, because I never felt that during programming in C/C++. I'm having segmentation fault currently, but I'll try solve that myself. – Innewit Feb 25 '20 at 12:33
6

By default, gcc decides which language to compile the file in according to its extension.

Renaming the file properly according to its contents is the right thing to do (as already suggested)

If for some reason you can't rename the file, you can also force gcc to compile a file with any extension in a given (supported) language using -x option:

gcc -x c shellcode.s

This switch sets the language but doesn't set the default link options. Trying gcc on a C++ file to create an executable fails because gcc link driver doesn't add the C++ libraries by default.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219