[PYTHON] Let's take a look at the Scapy code. Overload of special methods __div__, __getitem__ and so on.

Purpose

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.

What I found

All quote codes are https://github.com/secdev/scapy From.

\ _ \ _ Div \ _ \ _ overload

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)

Overload of \ _ \ _ getitem \ _ \ _

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

Summary

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

Let's take a look at the Scapy code. Overload of special methods __div__, __getitem__ and so on.
Let's take a look at the Scapy code. How are you processing the structure?
Let's take a look at the feature map of YOLO v3
Let's take a look at the forest fire on the west coast of the United States with satellite images.
Let's take a look at the infection tendency of the new coronavirus COVID-19 in each country and the medical response status (additional information).
Summarize the titles of Hottentori at the end and look at the present on the Web
[Python3] Take a screenshot of a web page on the server and crop it further
Take a peek at the processing of LightGBM Tuner
Let's take a look at the Scapy code. How are you processing the structure?
Take a look at profiling and dumping with Dataflow
Take a closer look at the Kaggle / Titanic tutorial
Obtain the name and securities code of a stock listed on the First Section of the Tokyo Stock Exchange
Challenge image classification by TensorFlow2 + Keras 2 ~ Let's take a closer look at the input data ~
Take a look at the Python built-in exception tree structure
The story of making a tool that runs on Mac and Windows at the game development site
Let's measure the test coverage of pushed python code on GitHub.
Take a look at the built-in exception tree structure in Python 3.8.2
[Go] Take a look at io.Writer
Take a look at Django's template.