chain17

agent17

依赖如下,给了一个入口的hessian2反序列化,题目自己给了一个Bean,其中的getter可以触发原生反序列化,不过存在很多黑名单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>hessian-lite</artifactId>
<version>3.2.13</version>
</dependency>

<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.2.224</version>
</dependency>

hessian反序列化

首先想到的是入口的hessian反序列化,本题的hessian-lite版本为3.2.13,在3.2.12可以通过HashMap到Xstring来触发toString,最后调用到getter从而造成rce,而3.2.13这个版本通过添加黑名单已经修复了这个漏洞。

并且这题的jdk版本是17,存在模块隔离,Xstring这个类实测在hessian反序列化中无法正常触发,但是在原生反序列化中可以触发,可以参考DubheCTF的Javolution,而这里的hessian版本较高,并没有能触发toString的方法(可以参考hessianOnlyJdk)

不难想到hessian反序列化Map类型的对象时会调用put方法去恢复里面的键值对,这里可以利用hutool依赖中的JSONObject来触发getter。

JSONObject在toString时不会触发键值的getter,而是会在set/put的时候触发,并且触发的只能是已有成员变量的getter/setter,因此可以写出以下的代码

1
2
3
4
5
6
7
8
9
10
11
12
        Bean bean = new Bean();
Student student = new Student("test", 18);
bean.setData("test".getBytes());
JSONObject jsonObject = new JSONObject();
HashMap<Object, Object> map = new HashMap<>();
map.put("1", bean);
map.put("2", student);
setFieldValue(jsonObject, "raw", map);

String ss = hSerialize(jsonObject);
// System.out.println(ss);
hDeserialize(ss);

bean#getObject

正常的Student类是可以触发getter的,但是我们题目中的bean却无法如预期般触发getObject,因为bean中只有data属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Bean implements Serializable {
byte[] data;

public Bean() {
}

public void setData(byte[] data) {
this.data = data;
}

public Object getObject() throws Exception {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(this.data);
BeanInputStream beanInputStream = new BeanInputStream(byteArrayInputStream);
Object object = beanInputStream.readObject();
return object;
}
...
}

我们的思路到这里变成了可以触发任意已有属性的getter,那么我们还有可能的利用方式吗

TemplateImpl

由于此处是jdk17,在服务端上没法反序列化TemplateImpl

getConnection

容易想到的是getConnection来打jdbc,依赖有h2和hutool,但这里只能触发有属性的getter,DataSource里面也不会有connection这个属性

我们可以想到Jackson链中的POJONode

server17

jdk17+jooq,入口是裸的反序列化

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.jooq/jooq -->
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>3.19.3</version>
</dependency>

构建codeql数据库

从github下载jooq源码后解压,本地构建codeql数据库

1
codeql database create jOOQ-qldb --overwrite -l java -s .\jOOQ-version-3.19.3\ -c "mvn install -Dmaven.test.skip=true"

或者可以使用github action来构建

给出codeql.yml,这里设置了jdk版本

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ "version-3.19.3" ]
pull_request:
branches: [ "version-3.19.3" ]
jobs:
analyze:
name: Analyze (${{ matrix.language }})
# Runner size impacts CodeQL analysis time. To learn more, please see:
# - https://gh.io/recommended-hardware-resources-for-running-codeql
# - https://gh.io/supported-runners-and-hardware-resources
# - https://gh.io/using-larger-runners
# Consider using larger runners for possible analysis time improvements.
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }}
permissions:
# required for all workflows
security-events: write

# only required for workflows in private repositories
actions: read
contents: read

strategy:
fail-fast: false
matrix:
include:
- language: java-kotlin
build-mode: autobuild
# CodeQL supports the following values keywords for 'language': 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift'
# Use `c-cpp` to analyze code written in C, C++ or both
# Use 'java-kotlin' to analyze code written in Java, Kotlin or both
# Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both
# To learn more about changing the languages that are analyzed or customizing the build mode for your analysis,
# see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning.
# If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how
# your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Java JDK
uses: actions/setup-java@v4.2.1
with:
java-version: '17'
distribution: 'oracle'

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality

# If the analyze step fails for one of the languages you are analyzing with
# "We were unable to automatically build your code", modify the matrix above
# to set the build mode to "manual" for that language. Then modify this step
# to build your code.
# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
- if: matrix.build-mode == 'manual'
run: |
echo 'If you are using a "manual" build mode for one or more of the' \
'languages you are analyzing, replace this with the commands to build' \
'your code, for example:'
echo ' make bootstrap'
echo ' make release'
exit 1
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
with:
category: "/language:${{matrix.language}}"
- name: Upload CodeQL database as artifact
uses: actions/upload-artifact@v4
with:
name: codeql-database-${{ matrix.language }}
path: /home/runner/work/_temp/codeql_databases/

Reference

https://blog.queenbridge.tech/li-yong-Github-Actions-sheng-cheng-CodeQL-shu-ju-ku----yi-AliyunCTF2024-Chain17-de-fan-xu-lie-hua-lian-wa-jue-wei-li

https://pankas.top/2024/03/28/%E9%98%BF%E9%87%8C%E4%BA%91ctf2024%E5%9D%90%E7%89%A2%E8%AE%B0%E5%BD%95/

https://xz.aliyun.com/t/14190