前言
这个CMS非常适合入门代码审计的人去学习,因为代码简单且漏洞成因经典,对一些新手有学习价值,
前台注入
从入口开始:/semcms/Templete/default/Include/index.php

跟进web_inc.php,首先包含
1)db_conn.php:建立与数据库的连接,代码量很少也很简单。

2)contorl.php:对$_GET进行全局过滤危险的SQL函数。

这个过滤从最简单的角度来说,即mysql<8的情况下,把select禁用了,其实就没办法进行跨表查询,SQL利用造成危害的可能性会大大降低,当然这是一种直接且无需考虑用户体验为原则的暴力做法,点到为止吧。
回到web_inc.php,继续阅读,后面吸引我的地方,在于 89 line一处SQL语句的地方。

可以看到$Language没有单引号,直接拼接到语句中,且值由POST方式传递,不过这里经过了verify_str函数,导致我没有办法利用select进行子查询,获取到sc_user表的后台管理员用户密码,那么事实真的如此么?
$Language=test_input(verify_str($_POST["languageID"]));
经过verify_str函数处理后,会传入test_input函数,其返回值将会拼接进SQL语句中进行查询。

test_input里面有个有趣的函数stripslashes,函数的作用就是用于去除反斜杠,举个如图例子

那么绕过verify_str<typo id="typo-660" data-origin="思路" ignoretag="true">思路</typo>就水到渠成了。

分析下payload的原理
languageID=-1 uni\on sel\ect 1,concat(user_admin,0x2d,user_ps),3,4,5,6,7,8,9,10,11,12,13,14 from sc_user
un\ion&&sel\ect绕过了verify_str函数的正则匹配,经过test_input的stripslashes去掉反斜杠,最终拼接到数据库中执行的语句,实际上

返回的后台管理员的账号密码信息到$tag_indexmetatit变量中。

并经过if判断传递给$indextitle变量,最终直接被echo到返回包。
if (empty($tag_indexmetatit)){$indextitle=$tag_indexkey;}else{$indextitle=$tag_indexmetatit;}
if (empty($tag_prometatit)){$protitle=$tag_prokey;}else{$protitle=$tag_prometatit;}
if (empty($tag_newmetatit)){$newstitle=$tag_newkey;}else{$newstitle=$tag_newmetatit;}


小结
由于web_inc.php是所有前台文件都会包含的,所以说这个注入点在任意前台文件中都可以无条件触发,唯一的区别就是其他文件可能没有回显的地方。当然,同样地基于此<typo id="typo-1327" data-origin="绕过" ignoretag="true">绕过</typo>原理,还可以找到很多处类似的注入或者其他更为简单且直接的注入点,这些就留给读者们自己探索。
【相关技术文档】
1、网络安全学习路线
2、电子书籍(白帽子)
3、安全大厂内部视频
4、100份src文档
5、常见安全面试题
6、ctf大赛经典题目解析
7、全套工具包
8、应急响应笔记
寻找后台
虽然在0x01中挖掘到了前台无限制回显的SQL注入漏洞,但因为查询数据库用的是mysqli的query函数而不是multi_query函数,故注入点并不支持堆叠注入,这直接导致我们少了一条SQLGetSHell的道路。值得开心一点的是,我们目前可以通过注入点获取到管理员的账号密码,不过这个CMS的后台地址安装时是随机生成的,所以找到后台地址很困难,下面是自己尝试寻找后台的失败过程,很可惜没有突破。
失败的过程
semcms/install/index.php安装文件有后台地址的生成代码

那么我的思路,就是全局定位$ht_filename变量,看看有没有对此进行操作并存储的代码。

很遗憾,并没有找到对此变量引用的代码。还没到放弃的时候,一般这个时候,我还会额外找找一些其他的办法。
比如搜索scandir函数,该函数作用是列出指定路径中的文件和目录,目的是通过找到类似目录遍历漏洞的点,从而找到后台地址。

继续回溯TemplateDir

可惜的是,发现传入的第一个参数是固定的,故这个思路也断了,暂时没有想到其他的好办法了。
GetShell思路
目标CMS的代码量并不高,故寻找GetShell的思路,可以采用危险函数定位的方法来进行快速排除并在存在漏洞的可疑的地方再进行回溯分析。
定位思路
文件包含函数:流程控制
- require
- include
- require_once
- include_once
文件操作函数: 文件系统函数
- copy — 拷贝文件
- delete — 参见 unlink 或 unset
- fflush — 将缓冲内容输出到文件
- file_get_contents — 将整个文件读入一个字符串
- file_put_contents — 将一个字符串写入文件
- fputcsv — 将行格式化为 CSV 并写入文件指针
- fputs — fwrite 的别名
- fread — 读取文件(可安全用于二进制文件)
- fscanf — 从文件中格式化输入
- fwrite — 写入文件(可安全用于二进制文件)
- move_uploaded_file — 将上传的文件移动到新位置
- readfile — 输出文件
- rename — 重<typo id="typo-2457" data-origin="命名" ignoretag="true">命名</typo>一个文件或目录
- rmdir — 删除目录
- unlink — 删除文件
代码注入函数:
- eval — 把字符串作为PHP代码执行
- assert — 检查一个断言是否为 false
- preg_replace — 执行一个正则表达式的搜索和替换
命令执行函数:程序执行函数
- escapeshellarg — 把字符串转码为可以在 shell 命令里使用的参数
- escapeshellcmd — shell 元字符转义
- exec — 执行一个外部程序
- passthru — 执行外部程序并且显示原始输出
- proc_close — 关闭由 proc_open 打开的进程并且返回进程退出码
- proc_get_status — 获取由 proc_open 函数打开<typo id="typo-2804" data-origin="的" ignoretag="true">的</typo>进程的信息
- proc_nice — 修改当前进程的优先级
- proc_open — 执行一个命令,并且打开用来输入/输出的文件指针。
- proc_terminate — 杀除由 proc_open 打开的进程
- shell_exec — 通过 shell 环境执行命令,并且将完整<typo id="typo-2946" data-origin="的" ignoretag="true">的</typo>输出以字符串的方式返回。
- system — 执行外部程序,并且显示输出
变量覆盖:
- extract — 从数组中将变量导入到当前的符号表
- parse_str — 将字符串解析成多个变量
后台GetShell
搜索file_put_contents函数,只有两个结果,一个是参数写死,故放弃,故只剩这个分析。

写入的文件$templateUrl得到的值是固定两种类型。
../index.php 根目录
../.htaccess 根目录
function Mbapp($mb,$lujin,$mblujin,$dirpaths,$htmlopen){
if ($htmlopen==1){$ml="j";}else{$ml="d";}
$template="index.php,hta/".$ml."/.htaccess"; //开始应用模版
// 1.$template=index.php,hta/j/.htaccess
// 2.$template=index.php,hta/d/.htaccess
$template_mb=explode(",",$template);
//$template_mb 根据,分割为index.php和hta/d/.htaccess的数组
for($i=0;$i<count($template_mb);$i++){
// 获取路径的内容
$template_o = file_get_contents($mblujin.'Templete/'.$mb.'/Include/'.$template_mb[$i]);
// ../拼接$template_mb[$i]中的"hta/".$ml."/"字符串替换为空的结果
// 即得到../.htacess 或者 ../.index.php
$templateUrl = $lujin.str_replace("hta/".$ml."/","", $template_mb[$i]);
// 修改$template_o的'<{Template}>'标记为$mb的值
$output = str_replace('<{Template}>', $mb, $template_o);
$output = str_replace('<{dirpaths}>', $dirpaths, $output);
// 将替换的内容写入到$templateUrl指向的文件
file_put_contents($templateUrl, $output);
}
}
那么这个函数如果$mb可控的话,会发生什么问题?
问题一
能够修改semcms/Templete/default/Include/index.php中的<{Template}>的内容

那么可以尝试如下的形式构造payload:
/semcms/N8D3ch_Admin/SEMCMS_Template.php?CF=template&mb=default/'.phpinfo():.'/..
最终的话会在semcms/Templete/default/Include/index.php写入如下图所示。

问题2
能够修改根目录.htacess的内容
与 .htaccess 相关的奇淫技巧
SetHandler application/x-httpd-php
此时当前目录及其子目录下所有文件都会被当做 php 解析。
那么可以尝试如下的形式构造payload:
/semcms/N8D3ch_Admin/SEMCMS_Template.php?CF=template&mb=default/%0aSetHandler%20application/x-httpd-php%0a%23/../..
//这里因为application/x-httpd-php中带有/,所以多需要一个../进行跳转

最终写入的内容:

那么我们随意上传一个文件,即可当作PHP来解析。
那么_GET['mb']来控制。

不过因为文件引进/semcms/Include/contorl.php,会调用verify_str对$_GET变量进行过滤。

很不凑巧,过滤了单引号,导致我们问题1覆盖的index.php的思路直接断了,因为根本没办法逃逸出单引号。
不过问题2的话,倒是可以成功,因为传入的内容并不在inject_check_sql的黑名单中,可以成功地覆盖.htaccess文件,不过这种方式也是有局限性的,需要Apahce是通过module的形式加载PHP的文件来执行才可以,并且需要在Linux环境,因为window不支持跨越不存在的路径。
任意文件删除
最后还想额外提一下关于后台的漏洞,便是其中一个任意文件删除漏洞,这个删除点不是直接的点,而是先通过构造需要删除的文件路径存进数据库,再通过触发其他点进行获取,传入unlink中进行删除,这种类型笔者称之为二次任意文件删除漏洞,很是经典。
漏洞演示:
1)传入../rmme.txt作为图片的路径

2)选择删除图片后,会删除文件网站根目录下的rmme.txt文件

成因:
(1) 添加URL入库的时候,只是做了test_input,并没有过滤..。

(2) 直接入库


(3) 删除图片的时候,传入AID,获取到images_url字段的值../rmme.txt传入Delfile函数进行删除。

Delfile函数先判断文件是否存在,再使用unlink删掉文件,全程没有一丁点的过滤,送分题!

总结
本文直接从一个入口的注入点展开,想找到一条合适的链路到GetShell的完整过程,但是遗憾的是,没能解决6位随机后台地址的问题,故实际利用起来的话,局限性还是有的,姑且称之为一次分享式的尝试性代码审计体验录吧。
网友评论