Changeset View
Changeset View
Standalone View
Standalone View
test/functional/p2p_invalid_messages.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2015-2019 The Bitcoin Core developers | # Copyright (c) 2015-2019 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Test node responses to invalid network messages.""" | """Test node responses to invalid network messages.""" | ||||
import asyncio | import asyncio | ||||
import struct | import struct | ||||
import time | |||||
from test_framework.messages import ( | from test_framework.messages import ( | ||||
CBlockHeader, | CBlockHeader, | ||||
CInv, | CInv, | ||||
msg_getdata, | msg_getdata, | ||||
msg_headers, | msg_headers, | ||||
msg_inv, | msg_inv, | ||||
msg_ping, | msg_ping, | ||||
MSG_TX, | MSG_TX, | ||||
ser_string, | ser_string, | ||||
) | ) | ||||
from test_framework.mininode import ( | from test_framework.mininode import ( | ||||
NetworkThread, | NetworkThread, | ||||
P2PDataStore, | P2PDataStore, | ||||
P2PInterface, | P2PInterface, | ||||
) | ) | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import hex_str_to_bytes | |||||
class msg_unrecognized: | class msg_unrecognized: | ||||
"""Nonsensical message. Modeled after similar types in test_framework.messages.""" | """Nonsensical message. Modeled after similar types in test_framework.messages.""" | ||||
msgtype = b'badmsg' | msgtype = b'badmsg' | ||||
def __init__(self, *, str_data): | def __init__(self, *, str_data): | ||||
self.str_data = str_data.encode() if not isinstance(str_data, bytes) else str_data | self.str_data = str_data.encode() if not isinstance(str_data, bytes) else str_data | ||||
def serialize(self): | def serialize(self): | ||||
return ser_string(self.str_data) | return ser_string(self.str_data) | ||||
def __repr__(self): | def __repr__(self): | ||||
return "{}(data={})".format(self.msgtype, self.str_data) | return "{}(data={})".format(self.msgtype, self.str_data) | ||||
class SenderOfAddrV2(P2PInterface): | |||||
def wait_for_sendaddrv2(self): | |||||
self.wait_until(lambda: 'sendaddrv2' in self.last_message) | |||||
class InvalidMessagesTest(BitcoinTestFramework): | class InvalidMessagesTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
def run_test(self): | def run_test(self): | ||||
""" | """ | ||||
. Test msg header | . Test msg header | ||||
0. Send a bunch of large (2MB) messages of an unrecognized type. Check to see | 0. Send a bunch of large (2MB) messages of an unrecognized type. Check to see | ||||
that it isn't an effective DoS against the node. | that it isn't an effective DoS against the node. | ||||
1. Send an oversized (2MB+) message and check that we're disconnected. | 1. Send an oversized (2MB+) message and check that we're disconnected. | ||||
2. Send a few messages with an incorrect data size in the header, ensure the | 2. Send a few messages with an incorrect data size in the header, ensure the | ||||
messages are ignored. | messages are ignored. | ||||
""" | """ | ||||
self.test_magic_bytes() | self.test_magic_bytes() | ||||
self.test_checksum() | self.test_checksum() | ||||
self.test_size() | self.test_size() | ||||
self.test_msgtype() | self.test_msgtype() | ||||
self.test_addrv2_empty() | |||||
self.test_addrv2_no_addresses() | |||||
self.test_addrv2_too_long_address() | |||||
self.test_addrv2_unrecognized_network() | |||||
self.test_large_inv() | self.test_large_inv() | ||||
node = self.nodes[0] | node = self.nodes[0] | ||||
self.node = node | self.node = node | ||||
node.add_p2p_connection(P2PDataStore()) | node.add_p2p_connection(P2PDataStore()) | ||||
conn2 = node.add_p2p_connection(P2PDataStore()) | conn2 = node.add_p2p_connection(P2PDataStore()) | ||||
# 2MB, per MAX_PROTOCOL_MESSAGE_LENGTH | # 2MB, per MAX_PROTOCOL_MESSAGE_LENGTH | ||||
▲ Show 20 Lines • Show All 148 Lines • ▼ Show 20 Lines | def test_msgtype(self): | ||||
msg.msgtype = b'\xff' * 12 | msg.msgtype = b'\xff' * 12 | ||||
msg = conn.build_message(msg) | msg = conn.build_message(msg) | ||||
# Modify msgtype | # Modify msgtype | ||||
msg = msg[:7] + b'\x00' + msg[7 + 1:] | msg = msg[:7] + b'\x00' + msg[7 + 1:] | ||||
self.nodes[0].p2p.send_raw_message(msg) | self.nodes[0].p2p.send_raw_message(msg) | ||||
conn.sync_with_ping(timeout=1) | conn.sync_with_ping(timeout=1) | ||||
self.nodes[0].disconnect_p2ps() | self.nodes[0].disconnect_p2ps() | ||||
def test_addrv2(self, label, required_log_messages, raw_addrv2): | |||||
node = self.nodes[0] | |||||
conn = node.add_p2p_connection(SenderOfAddrV2()) | |||||
# Make sure bitcoind signals support for ADDRv2, otherwise this test | |||||
# will bombard an old node with messages it does not recognize which | |||||
# will produce unexpected results. | |||||
conn.wait_for_sendaddrv2() | |||||
self.log.info('Test addrv2: ' + label) | |||||
msg = msg_unrecognized(str_data=b'') | |||||
msg.msgtype = b'addrv2' | |||||
with node.assert_debug_log(required_log_messages): | |||||
# override serialize() which would include the length of the data | |||||
msg.serialize = lambda: raw_addrv2 | |||||
conn.send_raw_message(conn.build_message(msg)) | |||||
conn.sync_with_ping() | |||||
node.disconnect_p2ps() | |||||
def test_addrv2_empty(self): | |||||
self.test_addrv2('empty', | |||||
[ | |||||
'received: addrv2 (0 bytes)', | |||||
'ProcessMessages(addrv2, 0 bytes): Exception', | |||||
'end of data', | |||||
], | |||||
b'') | |||||
def test_addrv2_no_addresses(self): | |||||
self.test_addrv2('no addresses', | |||||
[ | |||||
'received: addrv2 (1 bytes)', | |||||
], | |||||
hex_str_to_bytes('00')) | |||||
def test_addrv2_too_long_address(self): | |||||
self.test_addrv2('too long address', | |||||
[ | |||||
'received: addrv2 (525 bytes)', | |||||
'ProcessMessages(addrv2, 525 bytes): Exception', | |||||
'Address too long: 513 > 512', | |||||
], | |||||
hex_str_to_bytes( | |||||
# number of entries | |||||
'01' + | |||||
# time, Fri Jan 9 02:54:25 UTC 2009 | |||||
'61bc6649' + | |||||
# service flags, COMPACTSIZE(NODE_NONE) | |||||
'00' + | |||||
# network type (IPv4) | |||||
'01' + | |||||
# address length (COMPACTSIZE(513)) | |||||
'fd0102' + | |||||
# address | |||||
'ab' * 513 + | |||||
# port | |||||
'208d')) | |||||
def test_addrv2_unrecognized_network(self): | |||||
now_hex = struct.pack('<I', int(time.time())).hex() | |||||
self.test_addrv2('unrecognized network', | |||||
[ | |||||
'received: addrv2 (25 bytes)', | |||||
'IP 9.9.9.9 mapped', | |||||
'Added 1 addresses', | |||||
], | |||||
hex_str_to_bytes( | |||||
# number of entries | |||||
'02' + | |||||
# this should be ignored without impeding acceptance of | |||||
# subsequent ones | |||||
# time | |||||
now_hex + | |||||
# service flags, COMPACTSIZE(NODE_NETWORK) | |||||
'01' + | |||||
# network type (unrecognized) | |||||
'99' + | |||||
# address length (COMPACTSIZE(2)) | |||||
'02' + | |||||
# address | |||||
'ab' * 2 + | |||||
# port | |||||
'208d' + | |||||
# this should be added: | |||||
# time | |||||
now_hex + | |||||
# service flags, COMPACTSIZE(NODE_NETWORK) | |||||
'01' + | |||||
# network type (IPv4) | |||||
'01' + | |||||
# address length (COMPACTSIZE(4)) | |||||
'04' + | |||||
# address | |||||
'09' * 4 + | |||||
# port | |||||
'208d')) | |||||
def test_large_inv(self): | def test_large_inv(self): | ||||
conn = self.nodes[0].add_p2p_connection(P2PInterface()) | conn = self.nodes[0].add_p2p_connection(P2PInterface()) | ||||
with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (0 -> 20): oversized-inv: message inv size() = 50001']): | with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=8 (0 -> 20): oversized-inv: message inv size() = 50001']): | ||||
msg = msg_inv([CInv(MSG_TX, 1)] * 50001) | msg = msg_inv([CInv(MSG_TX, 1)] * 50001) | ||||
conn.send_and_ping(msg) | conn.send_and_ping(msg) | ||||
with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (20 -> 40): too-many-inv: message getdata size() = 50001']): | with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=8 (20 -> 40): too-many-inv: message getdata size() = 50001']): | ||||
msg = msg_getdata([CInv(MSG_TX, 1)] * 50001) | msg = msg_getdata([CInv(MSG_TX, 1)] * 50001) | ||||
conn.send_and_ping(msg) | conn.send_and_ping(msg) | ||||
with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (40 -> 60): too-many-headers: headers message size = 2001']): | with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=8 (40 -> 60): too-many-headers: headers message size = 2001']): | ||||
msg = msg_headers([CBlockHeader()] * 2001) | msg = msg_headers([CBlockHeader()] * 2001) | ||||
conn.send_and_ping(msg) | conn.send_and_ping(msg) | ||||
self.nodes[0].disconnect_p2ps() | self.nodes[0].disconnect_p2ps() | ||||
def _tweak_msg_data_size(self, message, wrong_size): | def _tweak_msg_data_size(self, message, wrong_size): | ||||
""" | """ | ||||
Return a raw message based on another message but with an incorrect data size in | Return a raw message based on another message but with an incorrect data size in | ||||
the message header. | the message header. | ||||
Show All 19 Lines |