I have a little errand, for example, what does the Scapy code look like? What is Scapy? Scapy is a powerful Python-based interactive packet manipulation program and library. (Quote: https://pypi.org/project/scapy/)
Python layman? (I'm not familiar with it), but it was a pretty difficult code for me.
First, ** around the overload of special methods, the code I don't understand **, so I'll make a note of this article.
All quote codes are https://github.com/secdev/scapy From.
How to use:
a = IP(dst=dest) / TCP(flags="S", seq=i, sport=65000, dport=55556)
b = IP(dst=dest)/ICMP()
Action: ** Attach objects with "/". ** **
It is overloaded with code that looks like this:
def __div__(self, other):
if isinstance(other, Packet):
cloneA = self.copy()
cloneB = other.copy()
cloneA.add_payload(cloneB)
return cloneA
elif isinstance(other, (bytes, str)):
return self / conf.raw_layer(load=other)
else:
return other.__rdiv__(self)
How to use:
seq = pkt[TCP].seq
Action: ** [] to take out a part. ** **
It is overloaded with code that looks like this:
def __getitem__(self, cls):
if isinstance(cls, slice):
lname = cls.start
if cls.stop:
ret = self.getlayer(cls.start, nb=cls.stop, **(cls.step or {}))
else:
ret = self.getlayer(cls.start, **(cls.step or {}))
else:
lname = cls
ret = self.getlayer(cls)
if ret is None:
if isinstance(lname, Packet_metaclass):
lname = lname.__name__
elif not isinstance(lname, bytes):
lname = repr(lname)
raise IndexError("Layer [%s] not found" % lname)
return ret
The above overload looks useful, so it may be used in other modules as well. Information that may be useful is below.
If you have any comments, please let us know. : candy: There may be a lot of this kind of processing in other languages as well. .. I'm a little confused when the basic symbols (operators) are overloaded for me, who only knows about C.
Recommended Posts