🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 命令注入 ### **常见php命令注入函数** eval(),,assert(), system(),preg\_replace(), create\_function, call\_user\_func, call\_user\_func\_array,array\_map(),反引号,ob\_start(),exec(),shell\_exec(),passthru(),escapeshellcmd(),popen(),proc\_open(),pcntl\_exec() <https://www.freebuf.com/column/166385.html> **PHP 执行系统命令可以使用以下几个函数** system、exec、passthru、·· 反引号、shell\_exec、popen、proc\_open、pcntl\_exec **system:** 执行外部程序并显示输出 > string system ( string $command \[, int &$return\_var \] ) ``` <pre class="calibre10">``` system <span class="token3">(</span><span class="token2">"/php7/php.exe test.php"</span><span class="token3">)</span><span class="token3">;</span> ``` ``` **exec:** 执行一个外部命令 > string exec ( string $command \[, array &$output \[, int &$return\_var \]\] ) ``` <pre class="calibre10">``` <span class="token4">exec</span><span class="token3">(</span><span class="token2">"/php7/php.exe test.php"</span><span class="token3">)</span> ``` ``` **passthru:** 执行外部程序并且显示原始输出 > void passthru (string command, int &return\_var) ``` <pre class="calibre10">``` <span class="token4">passthru</span><span class="token3">(</span><span class="token2">'echo $PATH'</span><span class="token3">)</span><span class="token3">;</span> ``` ``` \*\*shell\_exec:\*\*通过shell环境执行命令 ``` <pre class="calibre10">``` <span class="token4">shell_exec</span><span class="token3">(</span><span class="token2">'ls -lart'</span><span class="token3">)</span> ``` ``` > string shell\_exec (string command) > `` 反引号:`反引号号里直接包含shell 命令` 会直接执行 ``` <pre class="calibre10">``` echo `ls <span class="token1">-</span>al`<span class="token3">;</span> $get<span class="token1">=</span><span class="token2">"whoami"</span><span class="token3">;</span> echo `$get` ``` ``` resource popen ( string $command , string $mode ) resource proc\_open ( string $cmd , array $descriptorspec , array &$pipes \[, string $cwd \[, array $env \[, array $other\_options \]\]\] ) void pcntl\_exec ( string $path \[, array $args \[, array $envs \]\] ) 例子: ``` <pre class="calibre10">``` <span class="token5">if</span><span class="token3">(</span> <span class="token4">isset</span><span class="token3">(</span> $_POST<span class="token3">[</span> <span class="token2">'Submit'</span> <span class="token3">]</span> <span class="token3">)</span> <span class="token3">)</span> <span class="token3">{</span> <span class="token">// Get input</span> $target <span class="token1">=</span> $_REQUEST<span class="token3">[</span> <span class="token2">'ip'</span> <span class="token3">]</span><span class="token3">;</span> <span class="token">// Determine OS and execute the ping command.</span> <span class="token5">if</span><span class="token3">(</span> <span class="token4">stristr</span><span class="token3">(</span> <span class="token4">php_uname</span><span class="token3">(</span> <span class="token2">'s'</span> <span class="token3">)</span><span class="token3">,</span> <span class="token2">'Windows NT'</span> <span class="token3">)</span> <span class="token3">)</span> <span class="token3">{</span> <span class="token">// Windows</span> $cmd <span class="token1">=</span> <span class="token4">shell_exec</span><span class="token3">(</span> <span class="token2">'ping '</span> <span class="token3">.</span> $target <span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token5">else</span> <span class="token3">{</span> <span class="token">// *nix</span> $cmd <span class="token1">=</span> <span class="token4">shell_exec</span><span class="token3">(</span> <span class="token2">'ping -c 4 '</span> <span class="token3">.</span> $target <span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token">// Feedback for the end user</span> echo <span class="token2">"{$cmd}"</span><span class="token3">;</span> <span class="token3">}</span> 正常输入: input框输入 www<span class="token3">.</span>baidu<span class="token3">.</span>com或者输入ip 命令注入: input输入 <span class="token6">0</span> <span class="token1">|</span> dir c<span class="token3">:</span>提交测试 常见的危险注入符号:<span class="token1">%</span><span class="token1">&</span><span class="token1">|</span><span class="token1">></span>等 ``` ``` # **防御函数** 当用户提供的数据传入此函数,使用 escapeshellarg() 或 escapeshellcmd() 来确保用户欺骗 系统从而执行任意命令。 ## **escapeshellarg** ( string $arg ) 可以用到 php 的安全中,会过滤掉 arg 中存在的一些特殊字符。在输入的参数中如果包含中 文传递给 escapeshellarg,会被过滤掉。 php.5.6.10之前存在任意命令注入的漏洞,php.5.6.10被修复 ``` <pre class="calibre10">``` $dir <span class="token1">=</span> <span class="token2">"."</span><span class="token3">;</span> <span class="token4">system</span><span class="token3">(</span><span class="token2">'ls '</span><span class="token3">.</span><span class="token4">escapeshellarg</span><span class="token3">(</span>$dir<span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> <span class="token4">escapeshellcmd</span><span class="token3">(</span><span class="token2">'ls $dir'</span><span class="token3">)</span><span class="token3">;</span> ``` ``` ## **escapeshellcmd** ( string $command ) escapeshellcmd()函数会转义命令中的所有 shell 元字符来完成工作。这些元字符包括:# & ; ` , | \* ? ~ < > ^ ( ) \[ \] { } $ \\ ``` <pre class="calibre10">``` 二者分工不同,前者为了防止用户利用shell的一些技巧(如分号、反引号等),执行其他命令;后者是为了防止用户的输入逃逸出“参数值”的位置,变成一个“参数选项”(命令构成:命令 参数选项 参数值) 渗透测试:<span class="token3">[</span>http<span class="token3">:</span><span class="token1">/</span><span class="token1">/</span>www<span class="token3">.</span><span class="token6">52</span>bug<span class="token3">.</span>cn<span class="token1">/</span>hkjs<span class="token1">/</span><span class="token6">4879.</span>html<span class="token3">]</span><span class="token3">(</span>http<span class="token3">:</span><span class="token1">/</span><span class="token1">/</span>www<span class="token3">.</span><span class="token6">52</span>bug<span class="token3">.</span>cn<span class="token1">/</span>hkjs<span class="token1">/</span><span class="token6">4879.</span>html<span class="token3">)</span> localhost<span class="token1">/</span>test<span class="token3">.</span>php<span class="token1">?</span>cmd<span class="token1">=</span>echo <span class="token2">"aaaaaaa"</span> <span class="token1">></span><span class="token1">></span><span class="token6">1.</span>txt 注释: 一、<span class="token1">></span> 是定向输出到文件,如果文件不存在,就创建文件;如果文件存在,就将其清空;在我的一篇清空linux历史命令的文章中,就是这种方法,用echo <span class="token1">></span> <span class="token3">.</span>bash_history,将文件内容清空(文件大小变成<span class="token6">0</span>字节)。 二、<span class="token1">></span><span class="token1">></span> 这个是将输出内容追加到目标文件中。如果文件不存在,就创建文件;如果文件存在,则将新的内容追加到那个文件的末尾,该文件中的原有内容不受影响。 ``` ``` 防御例子: ``` <pre class="calibre10">``` <span class="token5">if</span><span class="token3">(</span> <span class="token4">isset</span><span class="token3">(</span> $_POST<span class="token3">[</span> <span class="token2">'Submit'</span> <span class="token3">]</span> <span class="token3">)</span> <span class="token3">)</span> <span class="token3">{</span> <span class="token">// 检查Anti-CSRF令牌</span> <span class="token4">checkToken</span><span class="token3">(</span> $_REQUEST<span class="token3">[</span> <span class="token2">'user_token'</span> <span class="token3">]</span><span class="token3">,</span> $_SESSION<span class="token3">[</span> <span class="token2">'session_token'</span> <span class="token3">]</span><span class="token3">,</span> <span class="token2">'index.php'</span> <span class="token3">)</span><span class="token3">;</span> <span class="token">// 获取input信息 </span> $target <span class="token1">=</span> $_REQUEST<span class="token3">[</span> <span class="token2">'ip'</span> <span class="token3">]</span><span class="token3">;</span> $target <span class="token1">=</span> <span class="token4">stripslashes</span><span class="token3">(</span> $target <span class="token3">)</span><span class="token3">;</span> <span class="token">// 把IP分成4个字节</span> $octet <span class="token1">=</span> <span class="token4">explode</span><span class="token3">(</span> <span class="token2">"."</span><span class="token3">,</span> $target <span class="token3">)</span><span class="token3">;</span> <span class="token">//检查每八位字节是否为整数</span> <span class="token5">if</span><span class="token3">(</span> <span class="token3">(</span> <span class="token4">is_numeric</span><span class="token3">(</span> $octet<span class="token3">[</span><span class="token6">0</span><span class="token3">]</span> <span class="token3">)</span> <span class="token3">)</span> <span class="token1">&&</span> <span class="token3">(</span> <span class="token4">is_numeric</span><span class="token3">(</span> $octet<span class="token3">[</span><span class="token6">1</span><span class="token3">]</span> <span class="token3">)</span> <span class="token3">)</span> <span class="token1">&&</span> <span class="token3">(</span> <span class="token4">is_numeric</span><span class="token3">(</span> $octet<span class="token3">[</span><span class="token6">2</span><span class="token3">]</span> <span class="token3">)</span> <span class="token3">)</span> <span class="token1">&&</span> <span class="token3">(</span> <span class="token4">is_numeric</span><span class="token3">(</span> $octet<span class="token3">[</span><span class="token6">3</span><span class="token3">]</span> <span class="token3">)</span> <span class="token3">)</span> <span class="token1">&&</span> <span class="token3">(</span> <span class="token4">sizeof</span><span class="token3">(</span> $octet <span class="token3">)</span> <span class="token1">==</span> <span class="token6">4</span> <span class="token3">)</span> <span class="token3">)</span> <span class="token3">{</span> <span class="token">//如果所有的4个字节都是int,把IP放回去。</span> $target <span class="token1">=</span> $octet<span class="token3">[</span><span class="token6">0</span><span class="token3">]</span> <span class="token3">.</span> <span class="token2">'.'</span> <span class="token3">.</span> $octet<span class="token3">[</span><span class="token6">1</span><span class="token3">]</span> <span class="token3">.</span> <span class="token2">'.'</span> <span class="token3">.</span> $octet<span class="token3">[</span><span class="token6">2</span><span class="token3">]</span> <span class="token3">.</span> <span class="token2">'.'</span> <span class="token3">.</span> $octet<span class="token3">[</span><span class="token6">3</span><span class="token3">]</span><span class="token3">;</span> <span class="token">// 确定操作系统并执行ping命令。</span> <span class="token5">if</span><span class="token3">(</span> <span class="token4">stristr</span><span class="token3">(</span> <span class="token4">php_uname</span><span class="token3">(</span> <span class="token2">'s'</span> <span class="token3">)</span><span class="token3">,</span> <span class="token2">'Windows NT'</span> <span class="token3">)</span> <span class="token3">)</span> <span class="token3">{</span> <span class="token">// Windows</span> $cmd <span class="token1">=</span> <span class="token4">shell_exec</span><span class="token3">(</span> <span class="token2">'ping '</span> <span class="token3">.</span> $target <span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token5">else</span> <span class="token3">{</span> <span class="token">// *nix</span> $cmd <span class="token1">=</span> <span class="token4">shell_exec</span><span class="token3">(</span> <span class="token2">'ping -c 4 '</span> <span class="token3">.</span> $target <span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token">// 为最终用户提供结果</span> $html <span class="token3">.</span><span class="token1">=</span> <span class="token2">"{$cmd}"</span><span class="token3">;</span> <span class="token3">}</span> <span class="token5">else</span> <span class="token3">{</span> <span class="token">// 返回错误信息</span> $html <span class="token3">.</span><span class="token1">=</span> <span class="token2">'ERROR: You have entered an invalid IP.'</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">// 生成Anti-CSRF令牌</span> <span class="token4">generateSessionToken</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> ``` ``` ``` <pre class="calibre10">``` <span class="token">// 检查Token</span> <span class="token5">function</span> <span class="token4">checkToken</span><span class="token3">(</span> $user_token<span class="token3">,</span> $session_token<span class="token3">,</span> $returnURL <span class="token3">)</span> <span class="token3">{</span> <span class="token">//检查用户表单传过来的token是否等于当前的token 或者token不存在</span> <span class="token5">if</span><span class="token3">(</span> $user_token <span class="token1">!==</span> $session_token <span class="token1">||</span> <span class="token1">!</span><span class="token4">isset</span><span class="token3">(</span> $session_token <span class="token3">)</span> <span class="token3">)</span> <span class="token3">{</span> <span class="token5">if</span><span class="token3">(</span> <span class="token1">!</span><span class="token4">isset</span><span class="token3">(</span> $_SESSION<span class="token3">[</span> <span class="token2">'dvwa'</span> <span class="token3">]</span> <span class="token3">)</span> <span class="token3">)</span> <span class="token3">{</span> $_SESSION<span class="token3">[</span> <span class="token2">'dvwa'</span> <span class="token3">]</span> <span class="token1">=</span> <span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> $dvwaSession<span class="token1">=</span>$_SESSION<span class="token3">[</span> <span class="token2">'dvwa'</span> <span class="token3">]</span><span class="token3">;</span> <span class="token5">if</span><span class="token3">(</span> <span class="token1">!</span><span class="token4">isset</span><span class="token3">(</span> $dvwaSession<span class="token3">[</span> <span class="token2">'messages'</span> <span class="token3">]</span> <span class="token3">)</span> <span class="token3">)</span> <span class="token3">{</span> $dvwaSession<span class="token3">[</span> <span class="token2">'messages'</span> <span class="token3">]</span> <span class="token1">=</span> <span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> $dvwaSession<span class="token3">[</span> <span class="token2">'messages'</span> <span class="token3">]</span><span class="token3">[</span><span class="token3">]</span> <span class="token1">=</span> <span class="token2">'CSRF token is incorrect'</span><span class="token3">;</span> <span class="token4">session_commit</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token4">header</span><span class="token3">(</span> <span class="token2">"Location: {$returnURL}"</span> <span class="token3">)</span><span class="token3">;</span> exit<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> ``` ``` ``` <pre class="calibre17">``` <span class="token">//生成token</span> <span class="token5">function</span> <span class="token4">generateSessionToken</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> # Generate a brand <span class="token5">new</span> <span class="token3">(</span>CSRF<span class="token3">)</span> token <span class="token5">if</span><span class="token3">(</span> <span class="token4">isset</span><span class="token3">(</span> $_SESSION<span class="token3">[</span> <span class="token2">'session_token'</span> <span class="token3">]</span> <span class="token3">)</span> <span class="token3">)</span> <span class="token3">{</span> <span class="token4">unset</span><span class="token3">(</span> $_SESSION<span class="token3">[</span> <span class="token2">'session_token'</span> <span class="token3">]</span> <span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> $_SESSION<span class="token3">[</span> <span class="token2">'session_token'</span> <span class="token3">]</span> <span class="token1">=</span> <span class="token4">md5</span><span class="token3">(</span> <span class="token4">uniqid</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> ``` ```