区块链预言机节点使用 Python 的 pickle 对来自智能合约事件的数据进行反序列化,从而使攻击者能够执行任意命令。
严重性: 严重(完整节点失陷)
#!/usr/bin/env python3
"""
vulnerable_oracle.py - Oracle node that listens to events and deserializes data unsafely.
"""
import pickle, socketserver, threading, time, base64
# Simulated blockchain event: attacker can emit a log with a pickled payload.
# The oracle fetches the log data and processes it.
class Oracle:
def process_event(self, log_data_b64):
data = base64.b64decode(log_data_b64)
# VULNERABILITY: deserializing untrusted pickle
obj = pickle.loads(data)
# The object could be anything; we expect a dict with 'price'
print(f"Price update: {obj.get('price', 'N/A')}")
return obj
# Simulate an event listener (HTTP server) where attacker pushes events
class EventHandler(socketserver.BaseRequestHandler):
def handle(self):
data = self.request.recv(4096).strip()
# data is base64 pickled payload
oracle.process_event(data.decode())
self.request.sendall(b"OK\n")
def run_server():
server = socketserver.TCPServer(("0.0.0.0", 9999), EventHandler)
server.serve_forever()
if __name__ == '__main__':
threading.Thread(target=run_server, daemon=True).start()
# Keep oracle running
time.sleep(1)
print("Oracle listening on :9999")
while True: time.sleep(10)
去中心化预言机节点通过反序列化来自不受信任的智能合约事件的 Python pickle 对象来处理链下数据。攻击者可以注入一个恶意 pickle,执行任意系统命令,从而危及整个节点。
pickle.loads(),而不进行任何验证。python vulnerable_oracle.py
python oracle_exploit.py