p2p_invalid_messages.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2015-present 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."""
   6  
   7  import random
   8  import time
   9  
  10  from test_framework.messages import (
  11      CBlockHeader,
  12      CInv,
  13      MAX_HEADERS_RESULTS,
  14      MAX_INV_SIZE,
  15      MAX_PROTOCOL_MESSAGE_LENGTH,
  16      MSG_TX,
  17      from_hex,
  18      msg_getdata,
  19      msg_headers,
  20      msg_inv,
  21      msg_ping,
  22      msg_version,
  23      ser_string,
  24  )
  25  from test_framework.p2p import (
  26      P2PDataStore,
  27      P2PInterface,
  28  )
  29  from test_framework.test_framework import BitcoinTestFramework
  30  from test_framework.util import (
  31      assert_equal,
  32  )
  33  
  34  VALID_DATA_LIMIT = MAX_PROTOCOL_MESSAGE_LENGTH - 5  # Account for the 5-byte length prefix
  35  
  36  
  37  class msg_unrecognized:
  38      """Nonsensical message. Modeled after similar types in test_framework.messages."""
  39  
  40      msgtype = b'badmsg\x01'
  41  
  42      def __init__(self, *, str_data):
  43          self.str_data = str_data.encode() if not isinstance(str_data, bytes) else str_data
  44  
  45      def serialize(self):
  46          return ser_string(self.str_data)
  47  
  48      def __repr__(self):
  49          return "{}(data={})".format(self.msgtype, self.str_data)
  50  
  51  
  52  class SenderOfAddrV2(P2PInterface):
  53      def wait_for_sendaddrv2(self):
  54          self.wait_until(lambda: 'sendaddrv2' in self.last_message)
  55  
  56  
  57  class InvalidMessagesTest(BitcoinTestFramework):
  58      def set_test_params(self):
  59          self.num_nodes = 1
  60          self.setup_clean_chain = True
  61          self.extra_args = [["-whitelist=addr@127.0.0.1"]]
  62  
  63      def run_test(self):
  64          self.test_buffer()
  65          self.test_duplicate_version_msg()
  66          self.test_magic_bytes()
  67          self.test_checksum()
  68          self.test_size()
  69          self.test_msgtype()
  70          self.test_addrv2_empty()
  71          self.test_addrv2_no_addresses()
  72          self.test_addrv2_too_long_address()
  73          self.test_addrv2_unrecognized_network()
  74          self.test_oversized_inv_msg()
  75          self.test_oversized_getdata_msg()
  76          self.test_oversized_headers_msg()
  77          self.test_invalid_pow_headers_msg()
  78          self.test_noncontinuous_headers_msg()
  79          self.test_resource_exhaustion()
  80  
  81      def test_buffer(self):
  82          self.log.info("Test message with header split across two buffers is received")
  83          conn = self.nodes[0].add_p2p_connection(P2PDataStore())
  84          # After add_p2p_connection both sides have the verack processed.
  85          # However the pong from conn in reply to the ping from the node has not
  86          # been processed and recorded in totalbytesrecv.
  87          # Flush the pong from conn by sending a ping from conn.
  88          conn.sync_with_ping(timeout=1)
  89          # Create valid message
  90          msg = conn.build_message(msg_ping(nonce=12345))
  91          cut_pos = 12  # Chosen at an arbitrary position within the header
  92          # Send message in two pieces
  93          before = self.nodes[0].getnettotals()['totalbytesrecv']
  94          conn.send_raw_message(msg[:cut_pos])
  95          # Wait until node has processed the first half of the message
  96          self.wait_until(lambda: self.nodes[0].getnettotals()['totalbytesrecv'] != before)
  97          middle = self.nodes[0].getnettotals()['totalbytesrecv']
  98          assert_equal(middle, before + cut_pos)
  99          conn.send_raw_message(msg[cut_pos:])
 100          conn.sync_with_ping(timeout=1)
 101          self.nodes[0].disconnect_p2ps()
 102  
 103      def test_duplicate_version_msg(self):
 104          self.log.info("Test duplicate version message is ignored")
 105          conn = self.nodes[0].add_p2p_connection(P2PDataStore())
 106          with self.nodes[0].assert_debug_log(['redundant version message from peer']):
 107              conn.send_and_ping(msg_version())
 108          self.nodes[0].disconnect_p2ps()
 109  
 110      def test_magic_bytes(self):
 111          # Skip with v2, magic bytes are v1-specific
 112          if self.options.v2transport:
 113              return
 114          self.log.info("Test message with invalid magic bytes disconnects peer")
 115          conn = self.nodes[0].add_p2p_connection(P2PDataStore())
 116          with self.nodes[0].assert_debug_log(['Header error: Wrong MessageStart ffffffff received']):
 117              msg = conn.build_message(msg_unrecognized(str_data="d"))
 118              # modify magic bytes
 119              msg = b'\xff' * 4 + msg[4:]
 120              conn.send_raw_message(msg)
 121              conn.wait_for_disconnect(timeout=1)
 122          self.nodes[0].disconnect_p2ps()
 123  
 124      def test_checksum(self):
 125          # Skip with v2, the checksum is v1-specific
 126          if self.options.v2transport:
 127              return
 128          self.log.info("Test message with invalid checksum logs an error")
 129          conn = self.nodes[0].add_p2p_connection(P2PDataStore())
 130          with self.nodes[0].assert_debug_log(['Header error: Wrong checksum (badmsg, 2 bytes), expected 78df0a04 was ffffffff']):
 131              msg = conn.build_message(msg_unrecognized(str_data="d"))
 132              # Checksum is after start bytes (4B), message type (12B), len (4B)
 133              cut_len = 4 + 12 + 4
 134              # modify checksum
 135              msg = msg[:cut_len] + b'\xff' * 4 + msg[cut_len + 4:]
 136              conn.send_raw_message(msg)
 137              conn.sync_with_ping(timeout=1)
 138          # Check that traffic is accounted for (24 bytes header + 2 bytes payload)
 139          assert_equal(self.nodes[0].getpeerinfo()[0]['bytesrecv_per_msg']['*other*'], 26)
 140          self.nodes[0].disconnect_p2ps()
 141  
 142      def test_size(self):
 143          self.log.info("Test message with oversized payload disconnects peer")
 144          conn = self.nodes[0].add_p2p_connection(P2PDataStore())
 145          error_msg = (
 146              ['V2 transport error: packet too large (4000014 bytes)'] if self.options.v2transport
 147              else ['Header error: Size too large (badmsg, 4000001 bytes)']
 148          )
 149          with self.nodes[0].assert_debug_log(error_msg):
 150              msg = msg_unrecognized(str_data="d" * (VALID_DATA_LIMIT + 1))
 151              msg = conn.build_message(msg)
 152              conn.send_raw_message(msg)
 153              conn.wait_for_disconnect(timeout=1)
 154          self.nodes[0].disconnect_p2ps()
 155  
 156      def test_msgtype(self):
 157          self.log.info("Test message with invalid message type logs an error")
 158          conn = self.nodes[0].add_p2p_connection(P2PDataStore())
 159          if self.options.v2transport:
 160              msgtype = 99 # not defined
 161              msg = msg_unrecognized(str_data="d")
 162              contents = msgtype.to_bytes(1, 'big') + msg.serialize()
 163              tmsg = conn.v2_state.v2_enc_packet(contents, ignore=False)
 164              with self.nodes[0].assert_debug_log(['V2 transport error: invalid message type']):
 165                  conn.send_raw_message(tmsg)
 166                  conn.sync_with_ping(timeout=1)
 167              # Check that traffic is accounted for (20 bytes plus 3 bytes contents)
 168              assert_equal(self.nodes[0].getpeerinfo()[0]['bytesrecv_per_msg']['*other*'], 23)
 169          else:
 170              with self.nodes[0].assert_debug_log(['Header error: Invalid message type']):
 171                  msg = msg_unrecognized(str_data="d")
 172                  msg = conn.build_message(msg)
 173                  # Modify msgtype
 174                  msg = msg[:7] + b'\x00' + msg[7 + 1:]
 175                  conn.send_raw_message(msg)
 176                  conn.sync_with_ping(timeout=1)
 177                  # Check that traffic is accounted for (24 bytes header + 2 bytes payload)
 178                  assert_equal(self.nodes[0].getpeerinfo()[0]['bytesrecv_per_msg']['*other*'], 26)
 179          self.nodes[0].disconnect_p2ps()
 180  
 181      def test_addrv2(self, label, required_log_messages, raw_addrv2):
 182          node = self.nodes[0]
 183          conn = node.add_p2p_connection(SenderOfAddrV2())
 184  
 185          # Make sure bitcoind signals support for ADDRv2, otherwise this test
 186          # will bombard an old node with messages it does not recognize which
 187          # will produce unexpected results.
 188          conn.wait_for_sendaddrv2()
 189  
 190          self.log.info('Test addrv2: ' + label)
 191  
 192          msg = msg_unrecognized(str_data=b'')
 193          msg.msgtype = b'addrv2'
 194          with node.assert_debug_log(required_log_messages):
 195              # override serialize() which would include the length of the data
 196              msg.serialize = lambda: raw_addrv2
 197              conn.send_raw_message(conn.build_message(msg))
 198              conn.sync_with_ping()
 199  
 200          node.disconnect_p2ps()
 201  
 202      def test_addrv2_empty(self):
 203          self.test_addrv2('empty',
 204              [
 205                  'received: addrv2 (0 bytes)',
 206                  'ProcessMessages(addrv2, 0 bytes): Exception',
 207                  'end of data',
 208              ],
 209              b'')
 210  
 211      def test_addrv2_no_addresses(self):
 212          self.test_addrv2('no addresses',
 213              [
 214                  'received: addrv2 (1 bytes)',
 215              ],
 216              bytes.fromhex('00'))
 217  
 218      def test_addrv2_too_long_address(self):
 219          self.test_addrv2('too long address',
 220              [
 221                  'received: addrv2 (525 bytes)',
 222                  'ProcessMessages(addrv2, 525 bytes): Exception',
 223                  'Address too long: 513 > 512',
 224              ],
 225              bytes.fromhex(
 226                  '01' +       # number of entries
 227                  '61bc6649' + # time, Fri Jan  9 02:54:25 UTC 2009
 228                  '00' +       # service flags, COMPACTSIZE(NODE_NONE)
 229                  '01' +       # network type (IPv4)
 230                  'fd0102' +   # address length (COMPACTSIZE(513))
 231                  'ab' * 513 + # address
 232                  '208d'))     # port
 233  
 234      def test_addrv2_unrecognized_network(self):
 235          now_hex = int(time.time()).to_bytes(4, "little").hex()
 236          self.test_addrv2('unrecognized network',
 237              [
 238                  'received: addrv2 (25 bytes)',
 239                  '9.9.9.9:8333',
 240                  'Added 1 addresses',
 241              ],
 242              bytes.fromhex(
 243                  '02' +     # number of entries
 244                  # this should be ignored without impeding acceptance of subsequent ones
 245                  now_hex +  # time
 246                  '01' +     # service flags, COMPACTSIZE(NODE_NETWORK)
 247                  '99' +     # network type (unrecognized)
 248                  '02' +     # address length (COMPACTSIZE(2))
 249                  'ab' * 2 + # address
 250                  '208d' +   # port
 251                  # this should be added:
 252                  now_hex +  # time
 253                  '01' +     # service flags, COMPACTSIZE(NODE_NETWORK)
 254                  '01' +     # network type (IPv4)
 255                  '04' +     # address length (COMPACTSIZE(4))
 256                  '09' * 4 + # address
 257                  '208d'))   # port
 258  
 259      def test_oversized_msg(self, msg, size):
 260          msg_type = msg.msgtype.decode('ascii')
 261          self.log.info("Test {} message of size {} is logged as misbehaving".format(msg_type, size))
 262          with self.nodes[0].assert_debug_log(['Misbehaving', '{} message size = {}'.format(msg_type, size)]):
 263              conn = self.nodes[0].add_p2p_connection(P2PInterface())
 264              conn.send_without_ping(msg)
 265              conn.wait_for_disconnect()
 266          self.nodes[0].disconnect_p2ps()
 267  
 268      def test_oversized_inv_msg(self):
 269          size = MAX_INV_SIZE + 1
 270          self.test_oversized_msg(msg_inv([CInv(MSG_TX, 1)] * size), size)
 271  
 272      def test_oversized_getdata_msg(self):
 273          size = MAX_INV_SIZE + 1
 274          self.test_oversized_msg(msg_getdata([CInv(MSG_TX, 1)] * size), size)
 275  
 276      def test_oversized_headers_msg(self):
 277          size = MAX_HEADERS_RESULTS + 1
 278          self.test_oversized_msg(msg_headers([CBlockHeader()] * size), size)
 279  
 280      def test_invalid_pow_headers_msg(self):
 281          self.log.info("Test headers message with invalid proof-of-work is logged as misbehaving and disconnects peer")
 282          blockheader_tip_hash = self.nodes[0].getbestblockhash()
 283          blockheader_tip = from_hex(CBlockHeader(), self.nodes[0].getblockheader(blockheader_tip_hash, False))
 284  
 285          # send valid headers message first
 286          assert_equal(self.nodes[0].getblockchaininfo()['headers'], 0)
 287          blockheader = CBlockHeader()
 288          blockheader.hashPrevBlock = int(blockheader_tip_hash, 16)
 289          blockheader.nTime = int(time.time())
 290          blockheader.nBits = blockheader_tip.nBits
 291          while not blockheader.hash_hex.startswith('0'):
 292              blockheader.nNonce += 1
 293          peer = self.nodes[0].add_p2p_connection(P2PInterface())
 294          peer.send_and_ping(msg_headers([blockheader]))
 295          assert_equal(self.nodes[0].getblockchaininfo()['headers'], 1)
 296          chaintips = self.nodes[0].getchaintips()
 297          assert_equal(chaintips[0]['status'], 'headers-only')
 298          assert_equal(chaintips[0]['hash'], blockheader.hash_hex)
 299  
 300          # invalidate PoW
 301          while not blockheader.hash_hex.startswith('f'):
 302              blockheader.nNonce += 1
 303          with self.nodes[0].assert_debug_log(['Misbehaving', 'header with invalid proof of work']):
 304              peer.send_without_ping(msg_headers([blockheader]))
 305              peer.wait_for_disconnect()
 306  
 307      def test_noncontinuous_headers_msg(self):
 308          self.log.info("Test headers message with non-continuous headers sequence is logged as misbehaving")
 309          block_hashes = self.generate(self.nodes[0], 10)
 310          block_headers = []
 311          for block_hash in block_hashes:
 312              block_headers.append(from_hex(CBlockHeader(), self.nodes[0].getblockheader(block_hash, False)))
 313  
 314          # continuous headers sequence should be fine
 315          MISBEHAVING_NONCONTINUOUS_HEADERS_MSGS = ['Misbehaving', 'non-continuous headers sequence']
 316          peer = self.nodes[0].add_p2p_connection(P2PInterface())
 317          with self.nodes[0].assert_debug_log([], unexpected_msgs=MISBEHAVING_NONCONTINUOUS_HEADERS_MSGS):
 318              peer.send_and_ping(msg_headers(block_headers))
 319  
 320          # delete arbitrary block header somewhere in the middle to break link
 321          del block_headers[random.randrange(1, len(block_headers)-1)]
 322          with self.nodes[0].assert_debug_log(expected_msgs=MISBEHAVING_NONCONTINUOUS_HEADERS_MSGS):
 323              peer.send_without_ping(msg_headers(block_headers))
 324              peer.wait_for_disconnect()
 325          self.nodes[0].disconnect_p2ps()
 326  
 327      def test_resource_exhaustion(self):
 328          self.log.info("Test node stays up despite many large junk messages")
 329          # Don't use v2 here - the non-optimised encryption would take too long to encrypt
 330          # the large messages
 331          conn = self.nodes[0].add_p2p_connection(P2PDataStore(), supports_v2_p2p=False)
 332          conn2 = self.nodes[0].add_p2p_connection(P2PDataStore(), supports_v2_p2p=False)
 333          msg_at_size = msg_unrecognized(str_data="b" * VALID_DATA_LIMIT)
 334          assert_equal(len(msg_at_size.serialize()), MAX_PROTOCOL_MESSAGE_LENGTH)
 335  
 336          self.log.info("(a) Send 80 messages, each of maximum valid data size (4MB)")
 337          for _ in range(80):
 338              conn.send_without_ping(msg_at_size)
 339  
 340          # Check that, even though the node is being hammered by nonsense from one
 341          # connection, it can still service other peers in a timely way.
 342          self.log.info("(b) Check node still services peers in a timely way")
 343          for _ in range(20):
 344              conn2.sync_with_ping(timeout=2)
 345  
 346          self.log.info("(c) Wait for node to drop junk messages, while remaining connected")
 347          conn.sync_with_ping(timeout=400)
 348  
 349          # Despite being served up a bunch of nonsense, the peers should still be connected.
 350          assert conn.is_connected
 351          assert conn2.is_connected
 352          self.nodes[0].disconnect_p2ps()
 353  
 354  
 355  if __name__ == '__main__':
 356      InvalidMessagesTest(__file__).main()
 357