1#!/usr/bin/env python3 2# Copyright (c) 2015-2019 The Bitcoin Core developers 3# Distributed under the MIT software license, see the accompanying 4# file COPYING or http://www.opensource.org/licenses/mit-license.php. 5"""Test node responses to invalid network messages.""" 6import asyncio 7import struct 8import sys 9 10from test_framework import messages 11from test_framework.mininode import P2PDataStore, NetworkThread 12from test_framework.test_framework import BitcoinTestFramework 13 14from test_framework.qtumconfig import FACTOR_REDUCED_BLOCK_TIME 15 16class msg_unrecognized: 17 """Nonsensical message. Modeled after similar types in test_framework.messages.""" 18 19 command = b'badmsg' 20 21 def __init__(self, *, str_data): 22 self.str_data = str_data.encode() if not isinstance(str_data, bytes) else str_data 23 24 def serialize(self): 25 return messages.ser_string(self.str_data) 26 27 def __repr__(self): 28 return "{}(data={})".format(self.command, self.str_data) 29 30 31class InvalidMessagesTest(BitcoinTestFramework): 32 def set_test_params(self): 33 self.num_nodes = 1 34 self.setup_clean_chain = True 35 36 def run_test(self): 37 """ 38 . Test msg header 39 0. Send a bunch of large (4MB) messages of an unrecognized type. Check to see 40 that it isn't an effective DoS against the node. 41 42 1. Send an oversized (4MB+) message and check that we're disconnected. 43 44 2. Send a few messages with an incorrect data size in the header, ensure the 45 messages are ignored. 46 """ 47 self.test_magic_bytes() 48 self.test_checksum() 49 self.test_size() 50 self.test_command() 51 52 node = self.nodes[0] 53 self.node = node 54 node.add_p2p_connection(P2PDataStore()) 55 conn2 = node.add_p2p_connection(P2PDataStore()) 56 57 msg_limit = 8 * 1000 * 1000 // FACTOR_REDUCED_BLOCK_TIME # 4MB, per MAX_PROTOCOL_MESSAGE_LENGTH 58 valid_data_limit = msg_limit - 5 # Account for the 4-byte length prefix 59 60 # 61 # 0. 62 # 63 # Send as large a message as is valid, ensure we aren't disconnected but 64 # also can't exhaust resources. 65 # 66 msg_at_size = msg_unrecognized(str_data="b" * valid_data_limit) 67 assert len(msg_at_size.serialize()) == msg_limit 68 69 self.log.info("Sending a bunch of large, junk messages to test memory exhaustion. May take a bit...") 70 71 # Run a bunch of times to test for memory exhaustion. 72 for _ in range(80): 73 node.p2p.send_message(msg_at_size) 74 75 # Check that, even though the node is being hammered by nonsense from one 76 # connection, it can still service other peers in a timely way. 77 for _ in range(20): 78 conn2.sync_with_ping(timeout=2) 79 80 # Peer 1, despite serving up a bunch of nonsense, should still be connected. 81 self.log.info("Waiting for node to drop junk messages.") 82 node.p2p.sync_with_ping(timeout=400) 83 assert node.p2p.is_connected 84 85 # 86 # 1. 87 # 88 # Send an oversized message, ensure we're disconnected. 89 # 90 # Under macOS this test is skipped due to an unexpected error code 91 # returned from the closing socket which python/asyncio does not 92 # yet know how to handle. 93 # 94 if sys.platform != 'darwin': 95 msg_over_size = msg_unrecognized(str_data="b" * (valid_data_limit + 1)) 96 assert len(msg_over_size.serialize()) == (msg_limit + 1) 97 98 # An unknown message type (or *any* message type) over 99 # MAX_PROTOCOL_MESSAGE_LENGTH should result in a disconnect. 100 node.p2p.send_message(msg_over_size) 101 node.p2p.wait_for_disconnect(timeout=4) 102 103 node.disconnect_p2ps() 104 conn = node.add_p2p_connection(P2PDataStore()) 105 conn.wait_for_verack() 106 else: 107 self.log.info("Skipping test p2p_invalid_messages/1 (oversized message) under macOS") 108 109 # 110 # 2. 111 # 112 # Send messages with an incorrect data size in the header. 113 # 114 actual_size = 100 115 msg = msg_unrecognized(str_data="b" * actual_size) 116 117 # TODO: handle larger-than cases. I haven't been able to pin down what behavior to expect. 118 for wrong_size in (2, 77, 78, 79): 119 self.log.info("Sending a message with incorrect size of {}".format(wrong_size)) 120 121 # Unmodified message should submit okay. 122 node.p2p.send_and_ping(msg) 123 124 # A message lying about its data size results in a disconnect when the incorrect 125 # data size is less than the actual size. 126 # 127 # TODO: why does behavior change at 78 bytes? 128 # 129 node.p2p.send_raw_message(self._tweak_msg_data_size(msg, wrong_size)) 130 131 # For some reason unknown to me, we sometimes have to push additional data to the 132 # peer in order for it to realize a disconnect. 133 try: 134 node.p2p.send_message(messages.msg_ping(nonce=123123)) 135 except IOError: 136 pass 137 138 node.p2p.wait_for_disconnect(timeout=10) 139 node.disconnect_p2ps() 140 node.add_p2p_connection(P2PDataStore()) 141 142 # Node is still up. 143 conn = node.add_p2p_connection(P2PDataStore()) 144 145 def test_magic_bytes(self): 146 conn = self.nodes[0].add_p2p_connection(P2PDataStore()) 147 148 async def swap_magic_bytes(): 149 conn._on_data = lambda: None # Need to ignore all incoming messages from now, since they come with "invalid" magic bytes 150 conn.magic_bytes = b'\x00\x11\x22\x32' 151 152 # Call .result() to block until the atomic swap is complete, otherwise 153 # we might run into races later on 154 asyncio.run_coroutine_threadsafe(swap_magic_bytes(), NetworkThread.network_event_loop).result() 155 156 with self.nodes[0].assert_debug_log(['PROCESSMESSAGE: INVALID MESSAGESTART ping']): 157 conn.send_message(messages.msg_ping(nonce=0xff)) 158 conn.wait_for_disconnect(timeout=1) 159 self.nodes[0].disconnect_p2ps() 160 161 def test_checksum(self): 162 conn = self.nodes[0].add_p2p_connection(P2PDataStore()) 163 with self.nodes[0].assert_debug_log(['CHECKSUM ERROR (badmsg, 2 bytes), expected 78df0a04 was ffffffff']): 164 msg = conn.build_message(msg_unrecognized(str_data="d")) 165 cut_len = ( 166 4 + # magic 167 12 + # command 168 4 #len 169 ) 170 # modify checksum 171 msg = msg[:cut_len] + b'\xff' * 4 + msg[cut_len + 4:] 172 self.nodes[0].p2p.send_raw_message(msg) 173 conn.sync_with_ping(timeout=1) 174 self.nodes[0].disconnect_p2ps() 175 176 def test_size(self): 177 conn = self.nodes[0].add_p2p_connection(P2PDataStore()) 178 with self.nodes[0].assert_debug_log(['']): 179 msg = conn.build_message(msg_unrecognized(str_data="d")) 180 cut_len = ( 181 4 + # magic 182 12 # command 183 ) 184 # modify len to MAX_SIZE + 1 185 msg = msg[:cut_len] + struct.pack("<I", 0x02000000 + 1) + msg[cut_len + 4:] 186 self.nodes[0].p2p.send_raw_message(msg) 187 conn.wait_for_disconnect(timeout=1) 188 self.nodes[0].disconnect_p2ps() 189 190 def test_command(self): 191 conn = self.nodes[0].add_p2p_connection(P2PDataStore()) 192 with self.nodes[0].assert_debug_log(['PROCESSMESSAGE: ERRORS IN HEADER']): 193 msg = msg_unrecognized(str_data="d") 194 msg.command = b'\xff' * 12 195 msg = conn.build_message(msg) 196 # Modify command 197 msg = msg[:7] + b'\x00' + msg[7 + 1:] 198 self.nodes[0].p2p.send_raw_message(msg) 199 conn.sync_with_ping(timeout=1) 200 self.nodes[0].disconnect_p2ps() 201 202 def _tweak_msg_data_size(self, message, wrong_size): 203 """ 204 Return a raw message based on another message but with an incorrect data size in 205 the message header. 206 """ 207 raw_msg = self.node.p2p.build_message(message) 208 209 bad_size_bytes = struct.pack("<I", wrong_size) 210 num_header_bytes_before_size = 4 + 12 211 212 # Replace the correct data size in the message with an incorrect one. 213 raw_msg_with_wrong_size = ( 214 raw_msg[:num_header_bytes_before_size] + 215 bad_size_bytes + 216 raw_msg[(num_header_bytes_before_size + len(bad_size_bytes)):] 217 ) 218 assert len(raw_msg) == len(raw_msg_with_wrong_size) 219 220 return raw_msg_with_wrong_size 221 222 223if __name__ == '__main__': 224 InvalidMessagesTest().main() 225