Speeding up Scapy

Posted on March 22, 2019

A few weeks ago I revisited the DNS cache poisoning attack discovered by Dan Kaminsky in 2008. Implementing the attack requires some fairly low-level manipulation of DNS packets. Fortunately, we have Scapy, which makes packet manipulation easy and accessible from Python. But at first the performance of my script was so bad that I could not carry out the attack (maybe I just never waited long enough). In this post I want to record my journey that led to a faster and faster Scapy script.

A deep understanding of the Kaminsky attack won’t be necessary for this blog post. Nevertheless, if you want to learn more about the attack, I can recommend the paper from unixwiz.net, which helped me a lot in understanding the issue. For this post you only have to understand the following detail of the attack: we want to send a lot of spoofed DNS responses with varying DNS IDs. The more packets we send per second the higher our chance to succeed.

First attempt

Below you can find a simplified excerpt from my initial attack script. Packets are composed by gluing together the different layers with the / operator. For example, the DNS response over UDP follows the IP()/UDP()/DNS() structure. For now, you can safely ignore the parameters like source, destination or the particular DNS answer record. What is actually important is the for-loop, which sends 5000 packets using Scapy’s send() function.

This script is quite slow. It took over four minutes to send the 5000 packets.

> sudo python3 01_naive.py
...
sent 5000 responses in 265.825 seconds

To find the bottleneck that is wrecking performance I profiled the script with the cProfile module.

Most of the time is spent opening and closing sockets. It turns out send() opens a new socket for every single packet.

Second attempt: using a socket

To fix the performance problems discovered above, we must tell Scapy to use the same socket for all responses. Scapy supports two kinds of sockets. One at OSI Layer 2, the L2Socket, which accepts Ether()/... or bytes objects and the other at Layer 3, named L3Socket, which accepts IP()/... objects. The patch is quite simple: create an L3socket and replace send() with L3Socket.send().

When we execute the script, we can see that the performance increased by a factor of roughly 56.

> sudo python3 02_with_socket.py
sent 5000 responses in 4.743 seconds

We can further improve the script by roughly 1.5 seconds by preparing the response beforehand and only adjusting the ID within the loop:

Third attempt: pre-render response

The patch from above yielded in noticeably better performance, but can we do better? After another round of profiling the answer is: yes, but we have to fiddle with our responses on byte-level. It turns out that most of the time is spent encoding the Scapy objects to byte arrays which are suitable to transmit over the wire. Every time we send a packet, Scapy implicitly calls the raw() function to encode the object representing our DNS response to bytes. If we could do this only once before the loop, we would win even more performance. Recall that we have to adjust the DNS ID on every single packet. Therefore, preparing a static response using the raw() method will not be sufficient. My solution is to prepare the response before entering the loop and then to manually adjust the DNS ID and UDP checksum within the encoded byte array.

The performance critical part of the script is now 2000-3000 times faster than the initial version!

> sudo python3 03_without_raw.py
sent 5000 responses in 0.096 seconds