ssti注入又称服务器端模板注入攻击(Server-Side Template Injection),和sql注入一样,也是由于接受用户输入而造成的安全问题。
它的实质就是服务器端接受了用户的输入,没有经过过滤或者说过滤不严谨,将用户输入作为web应用模板的一部分,但是在进行编译渲染的过程中,执行了用户输入的恶意代码,造成信息泄露,代码执行,getshell等问题。
这个问题主要是出在web应用模板渲染的过程中,目前比较流行的渲染引擎模板主要有:smarty,twig,jinja2,freemarker,velocity
而python中的一个微型框架flask主要就是使用的jinja2来作为渲染模板,在目前的ctf中常见的SSTI也主要就是考察的python,因此我记录一下关于python flask的jinja2引发的SSTI,也帮助自己更深入的学习和理解ssti注入攻击这个知识点。
在学习jinja2造成的ssti时,先初步了解一下关于python的flask框架,以及flask是如何通过jinja2来进行模板渲染的。
flask的运行流程:
路由:
想要在浏览器中访问由flask创建的web,需要设置路由,看代码
//index.py
from flask import Flask,url_for,redirect,render_template,render_template_string,request
app = Flask(__name__)
@app.route("/index/")
def test():
return "Hello flask"
if __name__ == "__main__":
app.run()
@app.route("/index/") 中,route装饰器的作用就是将函数和url绑定起来,当运行这个脚本之后,访问
http://127.0.0.1:5000/index
就会返回Hello flask,这就是简单的flask框架的运行。
渲染:
flask有两种渲染方式,render_template() 和 render_template_string()。
render_template()是渲染文件的,render_template_string是渲染字符串的,ssti也主要与渲染字符串这种方式有关。
在网站的根目录下创建templates文件夹,主要用来存放html文件,也是渲染用的模板文件。
render_template:
//index.py
@app.route("/index/")
def test():
return render_template("index.html")
//index.html 在 /templates/index.html