相同命令的变体返回相同/相似的结果

相同命令的变体返回相同/相似的结果

我正在开发一个公共服务器,该服务器提供旨在教人们 Linux 和道德黑客技能的游戏。每个级别的目标(到目前为止)都是找到下一个级别的 ssh 密码,以及建议可能对完成此任务有用的各种命令。我目前正在研究一个级别,其中列出了以下目标:

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

建议的命令是:ls、cd、cat、file、du、find

我在 Unix & Linux StackExchange 上发现了两个与此练习相关的独立问题/答案。这些都可以找到这里这里。两组命令输出基本上相同的信息。例如,从第一个链接开始,接受的答案和输出是:

bandit5@bandit:~/inhere$ find . -type f -size 1033c ! -executable -exec file {} + | grep ASCII                          
./maybehere07/.file2: ASCII text, with very long lines

我决定稍微玩一下这个命令,所以首先,我从命令中删除了 grep,然后再次运行它。然后我删除了不可执行的开关和文件命令并再次运行它。

bandit5@bandit:~/inhere$ find . -type f -size 1033c ! -executable -exec file {} +                                       
./maybehere07/.file2: ASCII text, with very long lines

bandit5@bandit:~/inhere$ find . -type f -size 1033c                                                                     
./maybehere07/.file2

显而易见,该命令的最短版本返回了我正在搜索的确切信息。上面来自 Unix & Linux StackExchange 的第二个链接是一个更长、更精确的命令,返回与上面命令的最短版本相同的信息。

在类似的情况下,这些较长的命令是否会被证明对于检索输出有用?这些较长的命令会缩小所寻求的答案的范围,同时解析出较短命令可能不会的输出吗?

答案1

要回答你的问题(尽管投了反对票),是的,这只是因为第一个编写该命令的人试图使用他们可用的所有信息。问题指出该文件是human-readable1033 bytes in size、 和not executable。原来的命令是:

find . -type f -size 1033c ! -executable -exec file {} + | grep ASCII

因此,让我们将其分解为几个部分:

-type f意味着您正在寻找一个文件(而不是一个目录)。

-size 1033c意味着您正在寻找 1033 字节的文件。

! -executable意味着您正在寻找不可执行的文件。

-exec file {} +表示您要对 find 找到的每个文件执行 file 命令。

| grep ASCII在这里,您将上一个命令的输出通过管道传输到 grep 中,以仅搜索包含 ASCII 的行。

我创建了 3 个文件,每个文件都是 1033 字节,file1是二进制数据,file2是您正在查找的文件,file3是其副本,file1但被标记为可执行文件。在此目录中,这是建议命令的输出:

jon@jon-HP-Pavilion-15-Notebook-PC:~/temp$ find . -type f -size 1033c ! -executable -exec file {} + | grep ASCII
./file2: ASCII text, with very long lines
jon@jon-HP-Pavilion-15-Notebook-PC:~/temp$ find . -type f -size 1033c ! -executable -exec file {} +
./file2: ASCII text, with very long lines
./file1: data
jon@jon-HP-Pavilion-15-Notebook-PC:~/temp$ find . -type f -size 1033c
./file2
./file1
./file3

正如您所看到的,对于多个文件,这些命令的行为非常不同,它们在您的测试环境中看起来相同的原因是因为只有一个 1033 字节的文件(公平地说,现实中也可能是这种情况,但是在事实上,您通常不知道文件的确切大小)。

相关内容