CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: printf hacking

  1. #1
    Join Date
    Nov 2007
    Posts
    5

    Post printf hacking

    Hi

    I have a function as below

    void print(char* str)
    {
    printf(str);
    }

    now the user of the function can pass anything as the argument for print(). I am looking got major security issues with this fucntion. I mean can this fucntion be hacked somehow or can we give some input so that we can crash the program. Its a kind of urgent so please help me out with this.

    raghu

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: printf hacking

    printf() function only display the string onto the console. If you allow input from user, the most, it will display random bits on the stack.

    However, if you use function like sprintf(), memory can be overwritten that may lead your program to run spurious code injected from the user's input.
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: printf hacking

    Methinks it should be:
    Code:
    void print(char* str)
    {
        printf("%s", str);
    }
    I mean can this fucntion be hacked somehow or can we give some input so that we can crash the program.
    I am not an expert, but I would think that this is perfectly safe as the problem would be on input, not output.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Nov 2007
    Posts
    5

    Re: printf hacking

    any other input people??

  5. #5
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    Re: printf hacking

    It's very easy for a user to crash the program. All the user has to do is pass something like "%s". If the stack happens to contain zeros or something that translates into an address that the process cannot access, then it is very easy to get a core dump or a memory access violation. To make the attack have a higher chance of success, all he/she would have do is pass "%s%s%s%s%s%s%s%s%s" to the function (or something even longer).

    The correct way to deal with the problem was suggested by laserlight. Try "printf("%s", str);

    - Kevin
    Kevin Hall

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured