Best way to sanitize exec command with user inserted variables

前端 未结 1 1119
醉话见心
醉话见心 2020-12-15 22:02

I\'m coding a web interface to a horrible piece of propitiatory software our company uses. The software has no real UI and requires us giving putty access to our system for

相关标签:
1条回答
  • 2020-12-15 23:00

    Use the function that PHP has for this purpose:

    $cmd = 
         "/usr/bin/do-something " . 
         escapeshellarg($arg1) . 
         ' ' . 
         escapeshellarg($arg2);
    

    You can also use escapeshellcmd()

    What's the difference?

    escapeshellarg() ONLY adds ' around the string and then \ before any other ' characters. http://www.php.net/escapeshellarg

    escapeshellcmd() escapes all shell-sensitive characters ($, \, etc..) but does not add quotes. http://www.php.net/manual/en/function.escapeshellcmd.php

    The gotcha is in the case that you use escapeshellarg() as PART OF A QUOTED parameter. Then it is rendered useless (actually adding quotes to the mix).

    Generally speaking, we prefer to use escapeshellcmd() with our own quotes added.

    $cmd = 
        "/usr/bin/do-something '" . 
        escapeshellcmd($arg1) . 
        "' '" . 
        escapeshellcmd($arg2) . 
        "'";
    

    Be safe!

    0 讨论(0)
提交回复
热议问题