I'm trying to create the ZMTP protocol for Scapy but can't find how to effectively pad a field and the packet up to a certain byte size.
I've tried creating the Greeting message described here, https://www.codeproject.com/Articles/863889/ZeroMQ-Diving-into-the-Wire, as a new protocol in Scapy:
class ZmtpGreeting(Packet):
name = "ZMTP Greeting"
fields_desc = [
XByteField("signature", "ff00000000000000117f"),
XByteField("version_major", "03"),
XByteField("version_minor", "00"),
PadField(XByteField("security_mechanism", "4e554c4c"), 20, padwith="\x00"),
XByteField("as_server", "00"),
]
Problem 1: The above implementation doesn't seem to include the padding up to 20 bytes for the security_mechanism field:
###[ IP ]###
version= 4
ihl= None
tos= 0x0
len= None
id= 1
flags=
frag= 0
ttl= 64
proto= tcp
chksum= None
src= 1.1.1.2
dst= 1.1.1.1
\options\
###[ TCP ]###
sport= 1111
dport= 2222
seq= 1
ack= 1
dataofs= None
reserved= 0
flags= PA
window= 8192
chksum= None
urgptr= 0
options= []
###[ ZMTP Greeting ]###
signature= ff00000000000000117f
version_major= 03
version_minor= 00
security_mechanism= 4e554c4c
as_server= 00
This entire packet should also be padded to 64 octets and I'm not sure how to include that in the protocol (or as padding outside of it).
Problem 2: I am unable to use raw(..) or len(..) because they fail with the following error:
fields.py in addfield(self, pkt, s, val)
138 `pkt`) to the raw string packet `s`, and return the new string packet.
139 """
--> 140 return s + struct.pack(self.fmt, self.i2m(pkt, val))
141
142 def getfield(self, pkt, s):
error: required argument is not an integer
Problem 3: This is in its own python file under layers, but only works when I directly paste it into the Scapy console.
I've been looking through the documentation for Scapy but there haven't found out how to achieve this. Thanks in advance!