[GXYCTF2019]Ping Ping Ping
这一题学到了一些新知识固做此篇以记之
废话不多说,题解什么的通过其他的博客就能知道。
$IFS$1
在看到空格绕过中有个 $IFS$1 来绕过,我就觉得奇怪了❓众所周知$IFS是Unix系统的一个预设变量表示分隔符Input Field Separators在这题中本身就可以表示空格了吧,那么这个$1又是干什么的呢?众所周知$1也是预设变量之一,代表接收的第一个参数。
经过在Ubuntu VM上实验,原因其实非常简单。
首先我们创建2个文件来方便观察
第一个是1.txt
This is test1!
第二个是2.txt
This is test2!
进入终端看看吧
highway@ubuntu:~/Desktop$ cat 1.txt 2.txt
This is test1!
This is test2!
用刚刚绕过的方法来看看先看看单个的吧
highway@ubuntu:~/Desktop$ cat$IFS$11.txt
This is test1!
显示出来了,再看看
highway@ubuntu:~/Desktop$ cat$IFS$21.txt
This is test1!
highway@ubuntu:~/Desktop$ cat$IFS$31.txt
This is test1!
highway@ubuntu:~/Desktop$ cat$IFS$a1.txt
cat: .txt: No such file or directory
我觉得你应该明白怎么回事了吧?通过第三个例子 $a1 被解释成一个变量了,而真正被cat的反而变成了 .txt
我们创建一个 .txt 验证一下这个猜想吧
highway@ubuntu:~/Desktop$ vim ".txt"
highway@ubuntu:~/Desktop$ cat$IFS$a1.txt
This is .txt!
猜想完全正确。而预设变量 $1~9 是因为可以被shell识别
highway@ubuntu:~/Desktop$ echo "\$1 is $1,\$2 is $2,\$a1 is $a1"
$1 is ,$2 is ,$a1 is
可以看到因为我们并没有传入任何参数所以这些变量echo出来就是什么都没有,也就是空字符串。到这里其实就非常清楚了 $IFS$1 中的$1只是为了不让后面的文件名被shell识别成变量名例如前面的$a1
highway@ubuntu:~/Desktop$ cat$IFS1.txt 2.txt
cat.txt: command not found
highway@ubuntu:~/Desktop$ cat$IFS$11.txt$IFS$22.txt
This is test1!
This is test2!
可以看到不加$1甚至连第二个文件名和中间的空格都会被shell认为是变量名,而且我们不止可以用$1来分隔
highway@ubuntu:~/Desktop$ cat$IFS$21.txt
This is test1!
highway@ubuntu:~/Desktop$ cat$IFS$31.txt
This is test1!
highway@ubuntu:~/Desktop$ cat$IFS$61.txt
This is test1!
只要是$1~9就可以
${IFS}
这个绕过其实非常简单易懂了,就是一个Shell Parameter Expansion
<>
这个其实是元符号Opening File Descriptors for Reading and Writing
The redirection operator
[n]<>word
causes the file whose name is the expansion of word to be opened for both reading and writing on file descriptor n, or on file descriptor 0 if n is not specified. If the file does not exist, it is created.
这也解释了为什么我的实验ls<>-l的时候会创建一个名字为-l且无任何内容的文件
highway@ubuntu:~/Desktop/testfloder$ ls -l
total 4
-rw-rw-r-- 1 highway highway 18 Jul 13 18:05 testfile.txt
highway@ubuntu:~/Desktop/testfloder$ ls<>-l
-l testfile.txt
highway@ubuntu:~/Desktop/testfloder$ cat<>-l
highway@ubuntu:~/Desktop/testfloder$ cat<>testfile.txt
This is test File
我在StackOverflow上提了这个问题非常感谢他们的回答How dose SHELL paresd ls<>-l
{cat,flag.php}
这里很多网上的解释是逗号代替了空格,其实我觉得这个说法有问题。经过我一天查阅资料提问和研究,其中在我的提问中Why the {cat,filename} Group command with braces in Linux shell can work?我采用的答案里解释的非常明白
When
{cat,ls}expands, it does so to two words,catandls, which is then recognized as a commandcatand its argumentls.
这是一个花括号展开(brace expansions)根据shell解析顺序Simple Command Expansion
- 是否存在变量名能够进行解析
- 如果不存在变量名,将会进行各种展开,上述的花括号展开便是其中一种。展开完后第一个word会被当成command随后的word会被解析成command的参数
那么这很明显,当我输入{cat,flag.php} 后因为不存在预先设置的变量名叫cat所以shell 会进行花括号展开 。很明显第一个word是cat 。然后在花括号展开中由,分割,那么第二个word flag.php 就被shell解释成了cat的参数。很自然的cat了flag.php 。
解释完之后可以看出在花括号展开中,其实根本不需要用到空格 ,何来代替一说呢?
本文详细解释了在Unix/Linux系统中,IFS变量如何影响空格绕过,并揭示了$1作为分隔符的特殊用法。同时介绍了花括号展开在命令执行中的作用,如{cat,flag.php}
3572

被折叠的 条评论
为什么被折叠?



