首页 > 其他 > 详细

[Flask(Jinja2)服务端模板注入漏洞(SSTI)]学习简记

时间:2020-04-26 12:37:44      阅读:158      评论:0      收藏:0      [点我收藏+]

0x00  关于Flask框架

  https://www.cnblogs.com/hello-there/p/12776502.html

  我的这篇文章中简单介绍了Flask框架的基本知识


0x01

  参考:

  https://www.freebuf.com/column/187845.html


0x02 漏洞相关

  1)render_template()用来渲染模板文件,而render_template_string()用来渲染一个字符串.

index_page="<h1>This is index page!</h1>"
return render_template_string(index_page)

  2)漏洞形成原因:不正确的使用flask中的render_template_string方法会引发SSTI。

    a)下面来看一段存在漏洞的代码:

from flask import Flask,render_template_string,request

app = Flask(__name__)
@app.route(/test/)
def test():
    code = request.args.get(id)   //get方式获取id
    html = ‘‘‘
        <h3>%s</h3>
    ‘‘‘%(code)
    return render_template_string(html)

app.run()

输入[?id=1],页面如下:

技术分享图片

输入[?id=<script>alert(/xss/)</script>],看看什么效果?

技术分享图片

 

   很明显,造成了xss漏洞.

    b)将代码进行修改:

from flask import Flask,render_template_string,request

app = Flask(__name__)
@app.route(/test/)
def test():
    code = request.args.get(id)
    html = ‘‘‘
        <h3>{{code}}</h3>
    ‘‘‘
    return render_template_string(html,code=code)

app.run()

再输入[?id=<script>alert(/xss/)</script>],查看页面,如下:

技术分享图片

 

可以看到,js代码被原样输出了。这是因为模板引擎一般都默认对渲染的变量值进行编码转义,这样就不会存在xss了。在这段代码中用户所控的是code变量,而不是模板内容。存在漏洞的代码中,模板内容直接受用户控制的。


0x03 SSTI文件读取/命令执行

 

[Flask(Jinja2)服务端模板注入漏洞(SSTI)]学习简记

原文:https://www.cnblogs.com/hello-there/p/12777614.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!