应急响应靶机训练-Linux2
  6bCuaRVyYM9s 2024年03月23日 50 0

靶机来源: 知攻善防实验室公众号 https://mp.weixin.qq.com/s/xf2FgkrjZg-yWlB9-pRXvw
我是在另一台主机上通过ssh连接到靶机进行解题的,我的ip为192.168.1.103,以下为个人解题记录,写的比较粗糙,有不对的地方希望各位大佬指正。

背景

前景需要:看监控的时候发现webshell告警,领导让你上机检查你可以救救安服仔吗!!
挑战内容:
(1)提交攻击者IP
(2)提交攻击者修改的管理员密码(明文)
(3)提交第一次Webshell的连接URL(http://xxx.xxx.xxx.xx/abcdefg?abcdefg只需要提交abcdefg?abcdefg)
(4)提交Webshell连接密码
(5)提交数据包的flag1
(6)提交攻击者使用的后续上传的木马文件名称
(7)提交攻击者隐藏的flag2
(8)提交攻击者隐藏的flag3

解题

首先查看当前主机最近的用户登录情况,如何攻击者成功登录,那么可以查到成功登录的记录。

[root@web-server ~]# grep "Accepted " /var/log/secure* | awk '{print $1,$2,$3,$9,$11}'
/var/log/secure:Mar 20 10:30:25 root 127.0.0.1
/var/log/secure:Mar 20 14:30:21 root 192.168.20.1
/var/log/secure:Mar 20 15:04:22 root 192.168.20.1
/var/log/secure:Mar 20 15:36:28 root 192.168.20.1
/var/log/secure:Mar 23 00:43:50 root 192.168.1.103
/var/log/secure:Mar 23 00:50:26 root 192.168.1.103
/var/log/secure-20240320:Mar 4 09:48:23 root 192.168.20.1
/var/log/secure-20240320:Mar 7 11:37:01 root 192.168.20.1
/var/log/secure-20240320:Mar 7 14:07:42 root 192.168.20.1
/var/log/secure-20240320:Mar 7 14:39:51 root 192.168.20.1
/var/log/secure-20240320:Mar 7 15:25:23 root 192.168.20.1
/var/log/secure-20240320:Mar 7 15:36:49 root 192.168.20.1
/var/log/secure-20240320:Mar 20 07:59:13 root 192.168.20.1

除了我的ip地址192.168.1.103登录外,ip地址192.168.20.1也登录了,那么这个可能就是攻击者的ip,提交判题程序得知正确。
接着查看一下机器上有没有什么可疑的程序。发现该机器上运行着nginxmysql,那么第二题中说的管理员密码,应该是运行在该机器上的某个系统的管理员密码。

[root@web-server ~]# netstat -anltup
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:888             0.0.0.0:*               LISTEN      1140/nginx: master  
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1505/master         
tcp        0      0 0.0.0.0:12485           0.0.0.0:*               LISTEN      2140/python3        
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1140/nginx: master  
tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN      1058/pure-ftpd (SER 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1024/sshd           
tcp        0      0 192.168.1.106:22        192.168.1.103:56462     ESTABLISHED 2523/sshd: root@pts 
tcp        0      0 192.168.1.106:22        192.168.1.103:55898     ESTABLISHED 2473/sshd: root@pts 
tcp6       0      0 ::1:25                  :::*                    LISTEN      1505/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      2132/mysqld         
tcp6       0      0 :::21                   :::*                    LISTEN      1058/pure-ftpd (SER 
tcp6       0      0 :::22                   :::*                    LISTEN      1024/sshd           
udp        0      0 0.0.0.0:68              0.0.0.0:*                           836/dhclient 

然后,发现默认的文件夹下给了一个流量数据包,下载这个数据包使用wireshark进行分析。

[root@web-server ~]# ls
anaconda-ks.cfg  wp  数据包1.pcapng

首先,过滤一下http报文,可以看到这些报文都是攻击者192.168.20.1在访问Linux主机。

先浏览一下过滤出来的报文,发现攻击者访问了/flag1路径,追踪http流。

发现了一个第一个flag:flag1{Network@_2020_Hack},提交判题程序得知正确。

除此之外,访问的URI就是index.php?user-app-registerversion2.php
接着看一下过滤出来的第一个http报文,同样追踪http流

在返回包中看到了路径,还有内核版本还有当前用户名

那么,攻击者应该是通过这个请求执行了某些命令,看看请求包中的内容是什么。

可以看到请求体中,有一个ini_set()函数,这是蚁剑的特征函数,可以得知,攻击者使用的是蚁剑管理Webshell。那么,前面的Network2020就是这个Webshell的连接密码,提交判题程序得知正确。
这是通过http过滤出来的第一个数据包,那么攻击者第一次Webshell的连接URL就是index.php?user-app-register,提交判题程序得知正确。
接着,来看看攻击者通过这个Webshell做了什么操作。
先将请求体中的内容进行URL解码

从解码后的内容中可以看出这是PHP代码,再将这个PHP代码进行格式化

得到PHP代码如下:

Network2020=@ini_set("display_errors", "0");
@set_time_limit(0);
$opdir=@ini_get("open_basedir");
if($opdir) {
	$ocwd=dirname($_SERVER["SCRIPT_FILENAME"]);
	$oparr=preg_split(base64_decode("Lzt8Oi8="),$opdir);
	@array_push($oparr,$ocwd,sys_get_temp_dir());
	foreach($oparr as $item) {
		if(!@is_writable($item)) {
			continue;
		}
		;
		$tmdir=$item."/.fd491f470fb7";
		@mkdir($tmdir);
		if(!@file_exists($tmdir)) {
			continue;
		}
		$tmdir=realpath($tmdir);
		@chdir($tmdir);
		@ini_set("open_basedir", "..");
		$cntarr=@preg_split("/\\\\|\//",$tmdir);
		for ($i=0;$i<sizeof($cntarr);$i++) {
			@chdir("..");
		}
		;
		@ini_set("open_basedir","/");
		@rmdir($tmdir);
		break;
	}
	;
}
;
;
function asenc($out) {
	return $out;
}
;
function asoutput() {
	$output=ob_get_contents();
	ob_end_clean();
	echo "4a0c"."dc70";
	echo @asenc($output);
	echo "db6"."da5";
}
ob_start();
try {
	$D=dirname($_SERVER["SCRIPT_FILENAME"]);
	if($D=="")$D=dirname($_SERVER["PATH_TRANSLATED"]);
	$R="{$D}	";
	if(substr($D,0,1)!="/") {
		foreach(range("C","Z")as $L)if(is_dir("{$L}:"))$R.="{$L}:";
	} else {
		$R.="/";
	}
	$R.="	";
	$u=(function_exists("posix_getegid"))?@posix_getpwuid(@posix_geteuid()):"";
	$s=($u)?$u["name"]:@get_current_user();
	$R.=php_uname();
	$R.="	{$s}";
	echo $R;
	;
}
catch(Exception $e) {
	echo "ERROR://".$e->getMessage();
}
;
asoutput();
die();

结合上面的代码和返回包的内容可以看出,攻击者获取了当前脚本所在目录、PHP版本信息、当前用户信息。
现在,我们得知该系统的目录在/www/wwwroot/127.0.0.1/下,查看该目录

[root@web-server 127.0.0.1]# ls -liah
total 12K
52362827 drwxr-xr-x 10 www  www   137 Mar 20 14:54 .
18205792 drwxr-xr-x  4 root root   38 Mar  7 12:01 ..
34114635 drwxr-xr-x  2 www  www   111 Sep 14  2023 api
33866315 drwxr-xr-x  2 root root  111 Mar 20 14:57 .api
52363663 drwxr-xr-x 15 www  www   186 Sep 14  2023 app
19094350 drwxr-xr-x  4 www  www    33 Mar  7 12:07 data
34416536 drwxr-xr-x  4 www  www    34 Sep 14  2023 files
52432799 -rwxr-xr-x  1 www  www   176 Jun 25  2023 index.php
52432800 drwxr-xr-x  3 www  www  4.0K Sep 14  2023 lib
52433597 drwxr-xr-x  2 www  www    41 Sep 14  2023 tasks
52362828 -rw-r--r--  1 root root   42 Mar  7 12:01 .user.ini
 1871062 drwxr-xr-x  2 www  www     6 Sep 13  2023 vendor

如果攻击者修改了管理员的密码,那么修改后的密码应该存在数据库中,但是我不知道数据库的账号密码,所以找找配置文件,看看能不能找到数据库的账号密码。在该目录下的lib目录下,找到了配置文件config.inc.php,发现数据库的账号密码为:kaoshi: 5Sx8mK5ieyLPb84m

[root@web-server lib]# cat config.inc.php 
<?php

/** 常规常量设置 */
...此处省略...

/**接口加密设置**/
define('APIKEY','356d9abc2532ceb0945b615a922c3370');
define('APIIV','#phpems90iv*');
/**composer开关**/
define('COMPOSER',0);
/** 数据库设置 */
define('SQLDEBUG',0);
define('DB','kaoshi');//MYSQL数据库名
define('DH','127.0.0.1');//MYSQL主机名,不用改
define('DU','kaoshi');//MYSQL数据库用户名
define('DP','5Sx8mK5ieyLPb84m');//MYSQL数据库用户密码
define('DTH','x2_');//系统表前缀,不用改

/** 微信相关设置 */
...此处省略...

/** 支付宝相关设置 */
...此处省略...
?>

登录数据库,直接查看用户数据表。

[root@web-server lib]# mysql -u kaoshi -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44-log Source distribution

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| kaoshi             |
+--------------------+
2 rows in set (0.00 sec)

mysql> use kaoshi;
Database changed
mysql> show tables;
+---------------------+
| Tables_in_kaoshi    |
+---------------------+
| x2_answer           |
| x2_app              |
| x2_area             |
| x2_ask              |
| x2_attach           |
...此处省略...

| x2_session          |
| x2_subject          |
| x2_user             |
| x2_user_group       |
| x2_wxlogin          |
+---------------------+
61 rows in set (0.00 sec)

mysql> select * from x2_user;
+--------+------------+-------------+-----------------+----------------+----------------------------------+----------+----------------+-------------+-------------+----------------+-------------+--------------+------------+------------------------------------------------------------------------------------------------------------------------------+--------------------+--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------+------------+-----------+-------------+---------------------------------------------------------+------------+------------+
| userid | useropenid | userunionid | username        | useremail      | userpassword                     | usercoin | userregip      | userregtime | userlogtime | userverifytime | usergroupid | usermoduleid | useranswer | manager_apps                                                                                                                 | usertruename       | normal_favor | teacher_subjects                                                                                                                                                                             | userprofile | usergender | userphone | useraddress | userphoto                                               | userstatus | normal_sfz |
+--------+------------+-------------+-----------------+----------------+----------------------------------+----------+----------------+-------------+-------------+----------------+-------------+--------------+------------+------------------------------------------------------------------------------------------------------------------------------+--------------------+--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------+------------+-----------+-------------+---------------------------------------------------------+------------+------------+
|      1 |            | NULL        | peadmin         | 958074@163.com | f6f6eb5ace977d7e114377cc7098b7e3 |      279 | 127.0.0.1      |  1471795200 |           0 |           NULL |           1 |            0 | NULL       | a:7:{i:0;s:4:"user";i:1;s:7:"content";i:2;s:4:"exam";i:3;s:8:"document";i:4;s:6:"course";i:5;s:4:"bank";i:6;s:8:"autoform";} | 111111111111111111 |              |                                                                                                                                                                                              |             | 男         |           | 信息部      | files/attach/images/content/20230802/16909740072788.jpg |          3 |            |
|      2 |            | NULL        | 教师管理员      | 958074@126.com | 96e79218965eb72c92a549dd5a330112 |       98 | 127.0.0.1      |  1471795200 |           0 |           NULL |           9 |            0 | NULL       |                                                                                                                              | 213123             |              | a:14:{i:0;s:2:"13";i:1;s:2:"12";i:2;s:1:"5";i:3;s:1:"4";i:4;s:1:"3";i:5;s:1:"1";i:6;s:1:"2";i:7;s:2:"17";i:8;s:2:"15";i:9;s:2:"16";i:10;s:2:"18";i:11;s:2:"19";i:12;s:2:"20";i:13;s:2:"21";} | 77777       |            |           |             |                                                         |          3 |            |
|      3 |            |             | zgsf            | zgsf@Admin.com | af0c68603004a1b5af4d87a71a813057 |        0 | 192.168.20.131 |  1709795218 |           0 |              0 |           8 |            0 |            |                                                                                                                              |                    |              |                                                                                                                                                                                              |             |            |           |             |                                                         |          0 |            |
|      4 |            |             | zgsfAdmin       | zgsf@zgsf.com  | ed2b3e3ce2425550d8bfdea8b80cc89a |        0 | 192.168.20.131 |  1709796233 |           0 |              0 |           8 |            0 |            |                                                                                                                              |                    |              |                                                                                                                                                                                              |             |            |           |             |                                                         |          0 |            |
+--------+------------+-------------+-----------------+----------------+----------------------------------+----------+----------------+-------------+-------------+----------------+-------------+--------------+------------+------------------------------------------------------------------------------------------------------------------------------+--------------------+--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------+------------+-----------+-------------+---------------------------------------------------------+------------+------------+
4 rows in set (0.12 sec)

mysql> 

通过查看x2_user_group表得知,groupid为1的是管理员。

mysql> select * from x2_user_group;
+---------+--------------+---------------+---------------+------------+--------------------+--------------+
| groupid | groupname    | groupmoduleid | groupdescribe | groupright | groupmoduledefault | groupdefault |
+---------+--------------+---------------+---------------+------------+--------------------+--------------+
|       1 | 管理员       |             1 | 管理员        |            |                  1 |            0 |
|       8 | 普通用户     |             9 | 普通用户      |            |                  0 |            1 |
|       9 | 教师         |            12 | 教师          |            |                  0 |            0 |
+---------+--------------+---------------+---------------+------------+--------------------+--------------+
3 rows in set (0.00 sec)

那么peadmin应该就是被修改密码的管理员,拿到密码的md5值f6f6eb5ace977d7e114377cc7098b7e3。找个网站解一下看看,得到明文密码为Network@2020,提交判题程序得知正确。

到此,前五个问题的答案的都有了,回去接着分析流量,找找攻击者后续上传的木马文件名称。
通过wireshark的过滤器,将http包和POST请求过滤出来,因为攻击者的操作在请求包中都可以看到。

分析一下请求index.php?user-app-register地址的第二个流量包,得到PHP代码如下:

Network2020=@ini_set("display_errors", "0");
@set_time_limit(0);
$opdir=@ini_get("open_basedir");
if($opdir) {
	$ocwd=dirname($_SERVER["SCRIPT_FILENAME"]);
	$oparr=preg_split(base64_decode("Lzt8Oi8="),$opdir);
	@array_push($oparr,$ocwd,sys_get_temp_dir());
	foreach($oparr as $item) {
		if(!@is_writable($item)) {
			continue;
		}
		;
		$tmdir=$item."/.24b0d";
		@mkdir($tmdir);
		if(!@file_exists($tmdir)) {
			continue;
		}
		$tmdir=realpath($tmdir);
		@chdir($tmdir);
		@ini_set("open_basedir", "..");
		$cntarr=@preg_split("/\\\\|\//",$tmdir);
		for ($i=0;$i<sizeof($cntarr);$i++) {
			@chdir("..");
		}
		;
		@ini_set("open_basedir","/");
		@rmdir($tmdir);
		break;
	}
	;
}
;
;
function asenc($out) {
	return $out;
}
;
function asoutput() {
	$output=ob_get_contents();
	ob_end_clean();
	echo "02e"."9bd";
	echo @asenc($output);
	echo "6f2"."72a8";
}
ob_start();
try {
	$D=base64_decode(substr($_POST["x0b6b31b98f31d"],2));
	$F=@opendir($D);
	if($F==NULL) {
		echo("ERROR:// Path Not Found Or No Permission!");
	} else {
		$M=NULL;
		$L=NULL;
		while($N=@readdir($F)) {
			$P=$D.$N;
			$T=@date("Y-m-d H:i:s",@filemtime($P));
			@$E=substr(base_convert(@fileperms($P),10,8),-4);
			$R="	".$T."	".@filesize($P)."	".$E."
";
			if(@is_dir($P))$M.=$N."/".$R; else $L.=$N.$R;
		}
		echo $M.$L;
		@closedir($F);
	}
	;
}
catch(Exception $e) {
	echo "ERROR://".$e->getMessage();
}
;
asoutput();
die();
x0b6b31b98f31d=TtL3d3dy93d3dyb290LzEyNy4wLjAuMS8=

其中,最后一行的x0b6b31b98f31d=TtL3d3dy93d3dyb290LzEyNy4wLjAuMS8=是需要执行的参数,因为蚁剑会将参数进行base64编码,然后在最前面随机添加两个字母,所以想知道这个参数是什么应该对L3d3dy93d3dyb290LzEyNy4wLjAuMS8=进行base64解码,得到参数为/www/wwwroot/127.0.0.1/,再结合x0b6b31b98f31d变量名的位置,得知攻击者做了查看当前目录下文件的操作,从返回包的内容也可以猜出来。
大致分析了一下这些流量包后,发现攻击者在/www/wwwroot/127.0.0.1/目录下创建了flag1 文件并写入flag,然后写入一个新的木马shell.php,再改名为version2.php,那么后续访问version2.php的流量包应该就是后续上传的木马文件了,将文件名称提交给判题程序得知正确。

通过追踪http流发现,version2.php内容如下:

搜索红框中的代码,发现好像是冰蝎的马。后续追踪version2.php的http流发现都是加密的了,攻击者可能是换了一个Webshell管理工具。

到这里,攻击者已经getshell。剩下两个flag应该都在Linux机器中了。
回到Linux机器中,看看攻击者在机器上进行了什么操作。

[root@web-server ~]# history
...此处省略...
   69  cd 127.0.0.1/
   70  ls
   71  ls -a
   72  vim .api
   73  ls
   74  ls -a
   75  mkdir .api
   76  ls
   77  ls -a
   78  cd .api/
   79  l
   80  ls
   81  cd ..
   82  ls
   83  cd ap
   84  cd api/
   85  ls
   86  cp * ../.api/
   87  ls
   88  cd ..
   89  ls
   90  cd .api/
   91  ls
   92  vim mpnotify.php 
   93  yum install vim
   94  ls
   95  vim  alinotify.php 
   96  cat /etc/shadow
   97  who
   98  w
   99  history
  100  useradd flag3{5LourqoFt5d2zyOVUoVPJbOmeVmoKgcy6OZ}
  101  env
  102  $flag3 = [root@web-server .api]# useradd flag3{5LourqoFt5d2zyOVUoVPJbOmeVmoKgcy6OZ}
  103  useradd: invalid user name 'flag3{5LourqoFt5d2zyOVUoVPJbOmeVmoKgcy6OZ}'
  104  $flag3 = flag{5LourqoFt5d2zyOVUoVPJbOmeVmoKgcy6OZ}
  105  vim /etc/profile
  106  source /etc/p
  107  source /etc/profile
  108  env

通过history命令,发现攻击者在环境变量中写入了第三个flag:flag{5LourqoFt5d2zyOVUoVPJbOmeVmoKgcy6OZ},并且攻击者在/www/wwwroot/127.0.0.1/中创建了文件夹.api隐藏文件,然后复制了api下的文件,并修改了其中的alinotify.php文件。查看一下这个文件,看看攻击者干了什么。

[root@web-server .api]# cat alinotify.php 
<?php
namespace PHPEMS;
/*
 * Created on 2013-12-26
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

define('PEPATH',dirname(dirname(__FILE__)));
class app
{
        public $G;

        public function __construct()
        {


                $this->ev = \PHPEMS\ginkgo::make('ev');
                $this->order = \PHPEMS\ginkgo::make('orders','bank');
        }

        public function run()
        {
                $alipay = \PHPEMS\ginkgo::make('alipay');
                $orderid = $this->ev->get('out_trade_no');
                $order = $this->order->getOrderById($orderid);
                $verify_result = $alipay->alinotify();
                if($verify_result)
                {
                        if($this->ev->get('trade_status') == 'TRADE_FINISHED' ||$this->ev->get('trade_status') == 'TRADE_SUCCESS')
                        {
                                if($order['orderstatus'] != 2)
                                {
                    $this->order->payforOrder($orderid,'alipay');
                                }
                                exit('sucess');
                        }
                        elseif($_POST['trade_status'] == 'WAIT_BUYER_PAY')
                        {
                                exit('fail');
                        }
                        else
                        {
                                exit('fail');
                        }
                }
                else
                {
                        exit('fail');
                }
        }
}

include PEPATH.'/lib/init.cls.php';
$app = new app(new ginkgo);
$app->run();
$flag2 = "flag{bL5Frin6JVwVw7tJBdqXlHCMVpAenXI9In9}";

?>

在文件的最后一行发现了flag2:flag{bL5Frin6JVwVw7tJBdqXlHCMVpAenXI9In9}
至此就拿到了所有答案,不过我感觉好像管理员密码和最后两个flag都是作者的非预期解法,等一手官方题解吧。

推测攻击者的入侵方式

水平有限,下面的内容都是我乱猜的:
根据目前的信息,知道攻击者先是通过蚁剑连接,写入冰蝎的马再通过冰蝎连接(?),然后在Linux机器上写入一个隐藏文件夹和环境变量,那么,攻击者是怎么打进来的?
通过搜索"index.php?user-app-register"发现该URI是PHPEMS模拟考试系统

再结合管理员密码被修改进行搜索,发现该系统可以通过反序列化+sql注入修改管理员密码。刚好在数据库中找密码时,看到了应该是反序列化时写入的内容(反序列化不太懂),因此推测攻击者应该是通过该方式修改了管理员密码,然后写入Webshell再使用蚁剑连接。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年03月23日 0

暂无评论

推荐阅读
  YOkriIV1Am1d   4天前   14   0   0 网络安全