linux_basics-Bandit(1)

写在前面

这是一次对 Overthewire 中 Bandit 靶场的一次复盘。

我推荐小白玩这个,它能够培养你的 Linux 基础?并且或许能培养你对黑客世界的基本认识。
学习 linux 是接触开源世界的一个重要的支柱,如果你的常用系统是 windows ,可以尝试使用 windows 的 linux 子系统 WSL2 ,非常方便。
大家可以登录这网站先看一下他的介绍,还是一样,这篇博客只记录有价值的地方,同学们可以自己去阅读相关网站或文档。

另外,如果各位有能力的话,也可以赞助一下这个靶场哦。

如何学习命令行?

一般来说,虽然你可以通过 gemini,chatGPT等 AI 直接获取答案,但是我更推荐的方式是通过搜索引擎以及 man 文档和命令本身自带的 help 选项,虽然一开始阅读英文会很痛苦,不过这是有价值的。

man文档使用方法: man 命令

help一般使用方法: 命令 –help 或 命令 -h

那么

  • 废话不多说,直接开始。

参考

你可能需要的知识

WSL2 bash 文件描述符 正则表达式 ASCII Base64

正文

注:ssh hostname: bandit*@bandit.labs.overthewire.org

level 0

The goal of this level is for you to log into the game using SSH. The host to which you need to connect is bandit.labs.overthewire.org, on port 2220. The username is bandit0 and the password is bandit0. Once logged in, go to the Level 1 page to find out how to beat Level 1.

密码就是 bandit0

level 0 -> level 1

The password for the next level is stored in a file called readme located in the home directory. Use this password to log into bandit1 using SSH. Whenever you find a password for a level, use SSH (on port 2220) to log into that level and continue the game.

使用ssh连接bandit服务器,即可拿到密码

1
2
ls
cat readme

alt text
key: 6y2kwnwK6grgvwvpvLaa2T1cpFEKOhNR

level 1 -> level 2

The password for the next level is stored in a file called - located in the home directory

直接 cat - ,会卡住,输入 ctrl+C 退出

原因: 在 Linux 很多命令,在参数中约定 - 为标准输入,因此,我们需要输入绝对/相对路径来向 cat 表示“我们要输出名为 “-“ 的文件”,这里我们用相对路径。

突然想顺便提一嘴: 在 linux 的文件描述符中,0表示标准输入,1表示标准输出,2表示标准错误
嗯,这样记吧 “输入输出标准错”
再提一嘴,在 cd 命令中,“ - ”表示OLDPWD,这个环境变量存储上一次的工作目录,使用 cd 就会更新一次

1
cat ./-

alt text
key: PK8fYLZg2hnHSz83plBL1iEPKdD3QToB

level 2 -> level 3

The password for the next level is stored in a file called –spaces in this filename– located in the home directory

这一题的坑主要在于文件名有空格而且还有 – 这种东西,会把你输入的东西当成 long option,所以我们加个双引号,并且用相对路径解析就行了

补充: bash 的三种基本引用方式

  • \ 反斜杠转义:仅剥夺紧随其后的单个字符的特殊含义
  • ‘’ 强引用: 绝对字面量,内部所有字符全失去特殊作用
  • “” 弱引用:阻止分词(空格把两个词分开)和通配符展开,保留变量与命令的执行能力
1
cat ./"--spaces in this filename--"

alt text

  • 另一种做法: 你也可以使用 – 提前忽略后面所有的选项(终止参数解析)

key: 7ZZ2LFrykP2zEyvBl4m3clcL7tGYJPME

level 3 -> level 4

The password for the next level is stored in a hidden file in the inhere directory.

隐藏文件以“.” 开头,所以用 ls -a 列出所有文件后,cat 即可,或者直接用 cat .* ,因为这个文件夹只有一个隐藏文件。

注: 如果使用通配符写法,建议写成 .??* 来排除 . 和 .. 这两个文件,找三个字符以上的文件名,这里我偷懒了,演示的话就用这个吧
单纯 * 会忽略隐藏文件,所以才要加一个 .

  • 补充:通配符
  • *:匹配任意长度的任意字符(包括 0 个字符)。
  • ?:匹配恰好1个任意字符。
  • […]:匹配括号内的任意单个字符(如 [0-9] 或 [abc])
1
2
cd inhere
cat .??*

alt text

key: xzTXq1rDJQVVAzdv5cHq1TQytTWufAMq

level 4 -> level 5

The password for the next level is stored in the only human-readable file in the inhere directory. Tip: if your terminal is messed up, try the “reset” command.

使用 file 命令查看文件类型
我们可以看到man文档对他的描述

1
2
3
file tests each argument in an attempt to classify it.  There are three sets of tests, performed in this  order:
filesystem tests, magic tests, and language tests
......

可以看到 file 命令是一个将传入文件进行分类的工具,具体如何分类,可以自行查看 man 文档。使用通配符一次性查看所有文件类型

1
file *

alt text
可以看到竟然报了这么多错,因为通配符展开在 bash 中是在执行命令之前,
file * 会被展开为 file -file00 -file01 -file02 -file03 -file04 -file05 -file06 -file07 -file08 -file09再执行,而 - 会被识别为短选项,因此报错。

1
file ./*

修改后,可以看到第七个文件是人类可读的,cat即可
alt text

key: 6C7h9GD8M6ai5nr7wo1RonrzFjj9yIrG

level 5 -> level 6

The password for the next level is stored in a file somewhere under the inhere directory and has all of the following properties:

human-readable
1033 bytes in size
not executable

使用 file * 来查看inhere中的文件,发现全都是 directory ,因此,我们可以使用 find 命令
阅读 find document ,可以看到 find 是对指定目录进行递归搜索并在每个文件上执行表达式的工具
因此,我们只需要使用 TEST 表达式,后面接 ACTION 表达式 -print即可,(当然这是默认的, The -print action is performed on all files for which the whole expression is true)

建议你去看 man 文档的 expression 章节

1
find . -readable -size 1033c ! -executable

拿到文件后直接 cat 即可
alt text

key:pXa26xhMWaC2SvDotA4r9EgZkulOeSBW

level 6 -> level 7

The password for the next level is stored somewhere on the server and has all of the following properties:

owned by user bandit7
owned by group bandit6
33 bytes in size

从根目录开始搜索就行了,你要是不记得 TEST 完全可以 find –help然后对着抄

这里我想多说一嘴 uid 和 user 的区别,一个是“数字 ID”给操作系统看的,一个是“字符串名字”给人类看的

1
find / -user bandit7 -group bandit6 -size 33c

alt text
可以看到出现了一堆 stderr 的报错,重定向把他们扔了即可,然后用 xargs 把 stdout 转化成参数, cat 密码即可

1
find / -user bandit7 -group bandit6 -size 33c 2> /dev/null | xargs cat

alt text

key: Bmnnvf82KzQlfxgAI2d1zYbr1u9pr3E3

level 7 -> level 8

The password for the next level is stored in the file data.txt next to the word millionth

使用 grep 进行正则表达式匹配即可

1
grep millionth data.txt

alt text

key: VR1ljMayciFxbnUokuQmJFw6QC9VKtub

level 8 -> level 9

The password for the next level is stored in the file data.txt and is the only line of text that occurs only once

使用 sort 和 uniq 进行去重即可,学过算法的人都知道原理,就不细讲了,想深入了解这两个命令的自己去看文档

1
sort data.txt |uniq -u #-u选项只显示单独出现的行

alt text

key: EjmOSvuAu7sGAHqHVcBDPirRe9T03kxl

level 9 -> level 10

The password for the next level is stored in the file data.txt in one of the few human-readable strings, preceded by several ‘=’ characters.

我们直接 grep -a == data.txt也能做出来,但是不够优雅。

为什么不优雅?
因为二进制数据包含大量不可见控制字符和终端转义序列,直接 grep 会把终端搞乱码;而strings 默认会提取连续 4 个及以上的可打印字符,过滤掉纯粹的机器码

优雅做法:
从二进制文件里读取人类可读文字,阅读 man 文档查看 strings 命令的用法,操,这英文太烦了,不过我们要坚持。
看完之后大致知道strings是从非文本文件中读取 ASCII 的字符,那我们直接管道给grep就行了

1
strings data.txt|grep ==

alt text

key: B0s2khmbT9u0geKuOoVGW3JZKhndE3BG

level 10 -> level 11

The password for the next level is stored in the file data.txt, which contains base64 encoded data

base64解码即可,简单的钥匙。

1
base64 -d data.txt

alt text

key: pYfOY6HwUsDj5rL9UvyhU7MCmv8vN5Ro

写在最后

暂时先复盘到这吧,一共有30关,我打算分成3部分。
话说,有点想开个随笔分类了,丢点心灵垃圾进去如何呢?

IMPORTANT

!!!然后祝自己生日快乐!!!
生日快乐,祝你在20岁幸福快乐!永远开心!

Icon
致谢名单
本作品由 NoobOmega 于 2026-10-27 11:23:28 发布
作品地址:linux_basics-Bandit(1)
除特别声明外,本站作品均采用 CC BY-NC-SA 4.0 许可协议,转载请注明来自 Noob的家
Logo
下一篇搭建自己的第一个个人博客