# Bandit

overthewire 的 bandit 关卡记录。
<!--more-->
[OverTheWire: Bandit](https://overthewire.org/wargames/bandit/)
## Bandit Level 0 → 1
读取根目录下名为 `-` 的文件。

**操作**：
```
cat ./-
# 或者
cat -- -
```

**参数**：
- `--`：在很多 Bash 命令中，双横杠表示命令选项的结束。其后的任何内容都会被视为文件名，这可以防止 `cat` 将 `-` 误认为是一个无效的命令行选项。
- `./-`：通过指定相对路径来明确它是一个文件，从而避免歧义。





## Bandit Level 2 → 3
读取文件名中包含空格的文件。

**操作**：
```
cat "./--spaces in this filename--"
```

**参数**：
- `" "`：使用双引号将带有空格的文件名括起来，使其被识别为一个整体参数。




## Bandit Level 3 → 4
读取隐藏文件（以点开头的文件）。

**操作**：
```
cat "./...Hiding-From-You"
```



## Bandit Level 4 → 5

在 `inhere` 目录下寻找唯一的人类可读文件。

**操作**：
```
file ./*
cat "./-file07"
```

**参数**：
- `file`：检查文件类型。
- `*`：通配符，代表当前目录下的所有文件。通过对比结果，发现只有 `-file07` 是 ASCII text。




## Bandit Level 5 → 6

寻找符合特定属性的文件：1033 字节、不可执行、人类可读。

**操作**：
```
find . -type f -size 1033c ! -executable
```

**参数**：
- `-type f`：仅查找常规文件。
- `-size 1033c`：精确匹配 1033 字节（`c` 代表 bytes）。
- `!`：逻辑非，用于排除后续条件。
- `-executable`：匹配可执行文件。




## Bandit Level 6 → 7

在服务器任意位置寻找属于用户 `bandit7`、组 `bandit6` 且大小为 33 字节的文件。
**操作**：
```
find / -user bandit7 -group bandit6 -size 33c 2>/dev/null
```

**参数**：
- `/`：从根目录开始全局搜索。
- `2>/dev/null`：将标准错误（stderr）重定向到空设备，从而屏蔽因权限不足产生的报错信息。




## Bandit Level 7 → 8
在 `data.txt` 中提取关键词 `millionth` 旁边的密码。

**操作**：
```
cat data.txt | grep millionth
```

**参数**：
- `grep`：文本搜索工具，用于匹配特定的字符串行。




## Bandit Level 8 → 9
在 `data.txt` 中寻找唯一的（只出现过一次）文本行。

**操作**：
```
sort data.txt | uniq -u
```

**参数**：
- `sort`：将文本行排序。`uniq` 只能识别相邻的重复行，因此必须先排序。
- `uniq -u`：仅输出唯一的行（只出现过一次的行）。




## Bandit Level 9 → 10
在二进制文件中提取人类可读的字符串。

**操作**：
```
strings data.txt | grep "=="
```

**参数**：

- `strings`：打印文件中可打印的字符串，常用于在二进制文件中寻找文本信息。




## Bandit Level 10 → 11
对 Base64 编码的文件进行解码。

**操作**：

```
cat data.txt | base64 -d
```

**参数**：

- `-d`：decode，执行解码操作。




## Bandit Level 11 → 12
破解 ROT13 凯撒密码（偏移 13 位）。

**操作**：
```
cat data.txt | tr 'A-Za-z' 'N-ZA-Mn-za-m'
```

**参数**：
- `tr`：字符转换工具。此处将 A-Z 映射到 N-Z 再到 A-M，实现 13 位的字母移位。




## Bandit Level 12 → 13
反复解压被多次嵌套压缩的文件。

**操作**：
```
xxd -r data.txt > data.bin
file data.bin
# 之后根据 file 的反馈，反复使用 gzip -d, bzip2 -d, tar -xf 等命令
```

**参数**：
- `xxd -r`：将十六进制转储（hex dump）还原为原始二进制格式。




## Bandit Level 13 → 14
使用 SSH 私钥登录下一个级别。

**操作**：

```
ssh -i sshkey.private -p 2220 bandit14@localhost
```

**参数**：
- `-i`：指定身份文件（私钥文件）。




## Bandit Level 14 → 15
将当前密码提交到本地 30000 端口获取新密码。

**操作**：
```
nc localhost 30000
# 输入 bandit14 的密码
```

**参数**：
- `nc`（netcat）：用于网络连接和数据传输。




## Bandit Level 15 → 16
通过 SSL 加密连接提交密码。

**操作**：
```
openssl s_client -connect localhost:30001

```

**参数**：
- `s_client`：实现了一个通用的 SSL/TLS 客户端，用于连接远程服务。




## Bandit Level 16 → 17
扫描端口并识别提供 SSL 服务的端口，随后提交密码获取私钥。

**操作**：
```
nmap -p 31000-32000 localhost
nmap -sV -p 31000-32000 localhost # 缩小范围
echo "kSkvUpMQ7lBYyCM4GBPvCvT1BfWRy0Dx" | openssl s_client -connect localhost:31790 -quiet
# 或者使用 ncat --ssl localhost 31790
# ncat 是nc 的升级版，添加了ssl加密的功能
```

**参数**：
- `nmap -p`：扫描指定范围的端口。-sV 查找 ssl 的服务端口。
- `-quiet`：在 `openssl s_client` 中抑制建立连接时的冗余状态信息。




## Bandit Level 17 → 18
对比两个文件的差异。
**操作**：
```
diff passwords.new passwords.old
```



## Bandit Level 18 → 19
绕过 SSH 登录后立即退出的限制（`.bashrc` 被修改）。

**操作**：
```
ssh -p 2220 bandit18@bandit.labs.overthewire.org cat readme
```

**逻辑**：在 SSH 命令末尾直接接上要执行的命令，可以不进入交互式 Shell 而直接获取结果。



## Bandit Level 19 → 20
通过 SetUID 程序读取权限受限的文件。

**操作**：
```
./bandit20-do cat /etc/bandit_pass/bandit20
```

**逻辑**：`bandit20-do` 具有 `bandit20` 用户的权限位（SetUID），因此能以其身份读取密码文件。



## Bandit Level 20 → 21
建立连接并互相传递密码。

**操作**：
```
# 使用tmux 或者 参数&

# tmux new -s task1 创建回话
# control +b,d 持久化
# tmux kill-session -t task1 关闭

# 终端 A
nc -l -p 1234
# 终端 B
./suconnect 1234
```

**参数**：
- `-l`：监听模式（listen）。
- `-p`：指定端口。



## Bandit Level 21 → 22

分析 Cron 定时任务脚本。
**操作**：
```
cat /etc/cron.d/cronjob_bandit22
cat /usr/bin/cronjob_bandit22.sh
cat /tmp/t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv
```



## Bandit Level 22 → 23

预测由脚本生成的随机文件名。

**操作**：
```
echo "I am user bandit23" | md5sum
# 获取哈希值后去 /tmp 下查找对应文件
```



## Bandit Level 23 → 24

编写定时脚本以获取密码。

**操作**：
```
# 在 /var/spool/bandit24/foo 放入一个脚本，让 cron 以 bandit24 身份执行它
echo "cat /etc/bandit_pass/bandit24 > /tmp/pass" > /tmp/my.sh
chmod 777 /tmp/my.sh
cp /tmp/my.sh /var/spool/bandit24/foo/
```



## Bandit Level 24 → 25
暴力破解 4 位 PIN 码。

**操作**：
```
for i in {0000..9999}; do echo "gb8KRRCsshuZXI0tUuR6ypOFjiZbf3G8 $i"; done | nc localhost 30002 | grep -vi "Wrong"
```

**参数**：
- `{0000..9999}`：Bash 的序列生成器。
- `-v`：反向匹配，过滤掉包含 "Wrong" 的行。
- `-i `：忽略大小写。




## Bandit Level 25 → 26

退出受限的 Shell（`/usr/bin/showtext`）。

**操作**：
1. 缩小终端窗口，使 `more` 命令由于屏幕高度不足而分页。不要使用 PowerShell，使用 cmd。
2. 输入 `v` 进入编辑器模式。
3. 在编辑器中输入 `:e /etc/bandit_pass/bandit26`。

## 参考
1. [Bandit - MayADevBe Blog](https://mayadevbe.me/tags/bandit/)


---

> 作者: Chifan  
> URL: https://eo.chifan.de/posts/bandit/  

