何为模板注入?
模板引擎可以让(网站)程序实现界面与数据分离,业务代码与逻辑代码的分离,哲大大提升了开发效率,良好的设计也是的代码重用变得更加容易。
到那时模板引擎也拓宽了我们的攻击面,注入到模板中的代码可能会引发RCE或者XSS
flask基础
在学习SSTI之前,先把flask的运作流程搞明白,这样有利于更加快速的理解原理
路由
先看一段代码
from flask import flask
@app.route('/index/')
def hello_word():
return 'hello world'
route装饰器的作用是将函数与url绑定起来。例子中的代码的作用就是当你访问http:127.0.0.1:5000/index的时候,flask会返回hello world
渲染方法
flask的渲染方法有render_template和render_template_string两种。
render_template()是用来渲染一个指定文件的。使用如下
return render_template('indexhtml')
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代码,而是夹杂着模板的语法,因为页面不可能都是一个样子的,有一些地方是会变化的。比如说显示用户名的地方,这个时候就需要使用模板支持的语法,来传参。
例子