Best API to intercept and modify network packets?

In some countries, the government deploys DPI (deep packet inspection) systems for censorship. These systems usually don't implement TCP completely and thus can be tricked pretty easily into allowing a connection to a blocked resource to go through, e.g. by fragmenting a ClientHello and optionally shuffling the fragments around.

There exists this app for Windows: https://github.com/ValdikSS/GoodbyeDPI

It uses WinDivert to intercept the network traffic and modify it as needed. I'd like to build a similar tool for macOS but I struggle to understand which of the many APIs I should use.

I need two main features from the API in question:

  • The ability to drop a packet sent by an application and send something else, e.g. several TCP fragments with the same data, instead.
  • The ability to drop incoming packets because some DPI equipment works by sending RST before the origin server has time to respond.
  • Ideally, I'd filter the connections by destination IP address and only work on those that deal with blocked resources, leaving the other ones to be dealt with completely by to system so that there's no needless performance regression caused by all traffic passing through my code.

So which API do I use for this? NetworkExtension — which kind? BPF? Some other unix API? Or I'll have to resort to making it a kernel extension?

This is tricky because you want direct control over how IP packets are rendered and parsed, and that rules out the higher-level Network Extension providers like transparent proxies. I think you’ll have to work at the Ethernet level, and that means:

  • An Network Extension Ethernet tunnel provider

  • BPF

  • A virtual DriverKit Ethernet driver

  • A virtual I/O Kit Ethernet driver

I’ve listed these from most to least preferred, btw, so start with the Ethernet tunnel provider and see where that gets you.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Best API to intercept and modify network packets?
 
 
Q