0
votes

I'm getting a confusing error message. I'm running MinGW on Windows XP 32-bit. When I attempt to compile the following code, I get an error message "./hello.c: line 4: Syntax error near unexpected token '('". Line 4 is at int main(...), I can't figure out what unexpected token is "near '('". I've tried using int main(void), but I get the same message. However, if I compile it without the "char string..." and "data = fputs(...)" and have it read from a given text file, it compiles without issue.

What I'm trying to accomplish is to read from a file where the filename is given by an external source, i.e. php. Eventually I'm going to be working this into an Apache module with a parser that I've made, hence the call from php, but I wanted to fool around and build some template code to work with before I got to that part.

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
    FILE *fp;
    //char string = "JD";    commented out
    char data;
    //printf("Type in your filename:   "); also commented out
    //scanf("%s", &argv);  also commented out

    if(argc >= 2)
    {
        fp = fopen("sample.txt", "r"); //switched to reading a given file
    }
    while((data = getchar()) != EOF)
    {
        fgets(data, sizeof(data), fp);
        // data = fputs(string, fp);
    }

    if (fp==NULL) /* error opening file returns NULL */
    {
        printf("Could not open player file!\n"); /* error message */
        return 1; /* exit with failure */
    }
    /* while we're not at end of file */
    while (fgets(data, sizeof(string), fp) != NULL)
    {
        printf(data); /* print the string */
    }

    fclose(fp); /* close the file */
    return 0; /* success */
}

Okay, I tried writing a simple "Hello World" program, but I'm still getting the same error message with it which makes me think the error message isn't being caused by my code at all.

#include <stdio.h>

int main(void) //still getting a syntax error before unexpected token '('
{
    printf("Hello, world!");
    return 0;
}
3
...so what's in myotherfile?Carl Norum
Did u try compiling it?user1814023
@NishithJainMR Did you try looking at the code?Jim Balter
sh: 1: Syntax error: "(" unexpected means the problem is not with your program (if it would have been true gcc would have produced error instead of sh). The problem is with the command you want to run.Don't You Worry Child
Why are people talking about myotherfile? That's never used by the program ... only the first 9 chars, ./myother are copied.Jim Balter

3 Answers

0
votes

There is problem with your logic . the "exploit" array would contain "./myotherAAAAAAAAAAAAAAAAAAAA:" which you are passing to system ..so problem are bound to happen

0
votes
strncpy(command, "./myotherfile ", 9);

only copies the first 9 chars. Replace that with

strcpy(command, "./myotherfile ");

which should do what you want.

P.S. I suspect that you originally had

strncpy(command, "./myfile ", 9);

which would have worked, and you didn't change the 9 when you changed the length of the file name. There are entire books written on why couplings like this are a bad idea and what to do instead. In this case the simplest solution is to use strcpy so you don't need to mention the length.

0
votes

I think you are trying to run ./motherfile...Then when you concatenate it with "exploit" name becomes "./myotherAAAAAAAAAAAAAAAAAAA " and not "./myotherfile AAAAAAAAAAAAAAAAAAAA", to give space concatenate it with a space first.