
# proto_vuln.py - Insecure handling of Any type
import addressbook_pb2 # Example protobuf
# Simulated deserialization that trusts an injected type_url
from google.protobuf import any_pb2
any_msg = any_pb2.Any()
any_msg.type_url = "type.googleapis.com/attacker.Evil"
any_msg.value = b'\x0a\x05admin' # serialized payload
# Application unpacks to expected type but could instantiate arbitrary class if using dynamic loading
if any_msg.Is(addressbook_pb2.Person.DESCRIPTOR):
person = addressbook_pb2.Person()
any_msg.Unpack(person)
print("Person unpacked, but type_url was spoofed!")
服务使用 Protocol Buffers 的 Any 类型封装消息,并通过 type_url 字段动态解包,但未验证预期类型。攻击者可以发送带有精心构造的 type_url 的消息,使其指向危险的消息类(或非预期的类),从而造成逻辑错误或代码执行。
type_url 反序列化负载,而未进行白名单校验。运行模拟:
pip install protobuf
python proto_vuln.py
这表明 Any 类型可以被欺骗;如果代码根据 type_url 动态加载类,则可能导致 RCE。