2019-06-18 15:28:55 +00:00
|
|
|
import
|
2022-03-15 17:08:15 +00:00
|
|
|
std/net,
|
2021-09-07 14:00:01 +00:00
|
|
|
testutils/fuzzing, chronicles, nimcrypto/keccak,
|
|
|
|
../../../eth/p2p/[discovery, enode], ../../../eth/[keys, rlp],
|
2020-06-12 14:19:58 +00:00
|
|
|
../../p2p/p2p_test_helper
|
2019-06-18 15:28:55 +00:00
|
|
|
|
|
|
|
const DefaultListeningPort = 30303
|
2019-09-03 13:45:26 +00:00
|
|
|
var targetNode: DiscoveryProtocol
|
|
|
|
|
2021-09-07 14:00:01 +00:00
|
|
|
proc packData(payload: openArray[byte], pk: PrivateKey): seq[byte] =
|
|
|
|
let
|
|
|
|
payloadSeq = @payload
|
|
|
|
signature = @(pk.sign(payload).toRaw())
|
|
|
|
msgHash = keccak256.digest(signature & payloadSeq)
|
|
|
|
result = @(msgHash.data) & signature & payloadSeq
|
|
|
|
|
2019-09-25 15:02:43 +00:00
|
|
|
init:
|
2019-09-03 13:45:26 +00:00
|
|
|
# Set up a discovery node, this is the node we target when fuzzing
|
|
|
|
var
|
2020-06-12 14:19:58 +00:00
|
|
|
targetNodeKey = PrivateKey.fromHex("a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a617")[]
|
2019-09-03 13:45:26 +00:00
|
|
|
targetNodeAddr = localAddress(DefaultListeningPort)
|
2022-03-15 17:08:15 +00:00
|
|
|
targetNode = newDiscoveryProtocol(
|
|
|
|
targetNodeKey, targetNodeAddr, @[], Port(DefaultListeningPort))
|
2019-09-03 13:45:26 +00:00
|
|
|
# Create the transport as else replies on the messages send will fail.
|
|
|
|
targetNode.open()
|
2019-06-18 15:28:55 +00:00
|
|
|
|
2019-09-25 15:02:43 +00:00
|
|
|
test:
|
2019-06-18 15:28:55 +00:00
|
|
|
var
|
|
|
|
msg: seq[byte]
|
|
|
|
address: Address
|
|
|
|
|
2019-09-25 15:02:43 +00:00
|
|
|
# Sending raw payload is possible but won't find us much. We need a hash and
|
|
|
|
# a signature, and without it there is a big chance it will always result in
|
|
|
|
# "Wrong msg mac from" error.
|
2020-06-12 14:19:58 +00:00
|
|
|
let nodeKey = PrivateKey.fromHex("a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a618")[]
|
2019-09-25 15:02:43 +00:00
|
|
|
msg = packData(payload, nodeKey)
|
|
|
|
address = localAddress(DefaultListeningPort + 1)
|
|
|
|
|
|
|
|
try:
|
|
|
|
targetNode.receive(address, msg)
|
|
|
|
# These errors are also catched in `processClient` in discovery.nim
|
|
|
|
# TODO: move them a layer down in discovery so we can do a cleaner test there?
|
2020-06-12 14:19:58 +00:00
|
|
|
except RlpError as e:
|
|
|
|
debug "Receive failed", err = e.msg
|
|
|
|
except DiscProtocolError as e:
|
2020-04-06 16:24:15 +00:00
|
|
|
debug "Receive failed", err = e.msg
|