# lambda_remanence.py - Simulated Lambda environment reuse
import os, secrets
def first_invocation():
# Simulates storing a secret in a global variable
global SECRET
SECRET = secrets.token_hex(16)
return "Initialized"
def second_invocation():
# Another function in same container? Actually, separate invocations share memory.
# Here we just read the global if it exists
global SECRET
return SECRET if 'SECRET' in globals() else "No secret"
print("Cold start: ", first_invocation())
print("Warm start: ", second_invocation())
无服务器平台在多次函数调用之间复用执行环境(容器),但不会清零全局内存。攻击者的函数如果与先前执行共享相同的底层主机(甚至同一账户),则可能访问先前执行的残留数据,从而导致机密泄漏。
运行:
python lambda_remanence.py
第二次调用读取了第一次调用期间设置的机密。