Kuboid
Open Luck·Kuboid.in
Black Hat2023
Open in YouTube ↗

Nothing but Net: Leveraging macOS Networking Frameworks to Heuristically Detect Malware

Black Hat2,350 views48:18about 2 years ago

This talk demonstrates how to leverage native macOS networking frameworks, such as Network Extension and Network Statistics, to perform host-based network monitoring and detect malicious activity. The speaker details how to enumerate network state via file descriptors and implement DNS proxies to intercept and analyze traffic directly on the host. The presentation highlights the effectiveness of these techniques for identifying stealthy malware and supply-chain attacks that might bypass traditional network appliances. Several open-source tools, including LuLu, DNSMonitor, and Dedicate, are introduced to facilitate this host-based analysis.

Bypassing macOS Network Security: Why Host-Based Monitoring is Your New Best Friend

TLDR: Traditional network appliances often fail to provide visibility into encrypted traffic or identify the specific process responsible for a connection. By leveraging native macOS frameworks like Network Extension and Network Statistics, researchers can perform granular, host-based monitoring that exposes malicious activity at the source. This approach effectively bypasses the limitations of perimeter-based defenses and provides a clear view of process-level network behavior.

Security researchers often rely on network-level visibility to catch command-and-control (C2) traffic, but the shift toward ubiquitous encryption and the rise of sophisticated supply-chain attacks have rendered many perimeter-based appliances obsolete. When a binary is signed and notarized by Apple, it often gains a level of implicit trust that allows it to bypass traditional network inspection. Relying on a network appliance to flag malicious traffic is a losing game when the malware is running on the host, potentially using encrypted channels that your firewall cannot inspect without intrusive man-in-the-middle techniques.

Host-based monitoring changes the game. Instead of looking at traffic from the outside, you look at it from the inside, where the process identity is still clear and the data is still unencrypted.

The Power of Native macOS Frameworks

Apple provides powerful, albeit often undocumented, frameworks that allow developers to hook into the networking stack. The Network Extension framework is the primary tool for building modern security software on macOS. It allows for the creation of content filters and proxy providers that operate in user space, avoiding the stability risks associated with legacy kernel extensions.

For a pentester or researcher, the goal is to identify unauthorized network activity. You can achieve this by enumerating the network state directly from the process perspective. On macOS, sockets are treated as file descriptors. By using the proc_pidinfo API, you can query a process to list its open file descriptors, including sockets.

// Example of querying process file descriptors
int proc_pidinfo(int pid, int flavor, uint64_t arg, void *buffer, int buffersize);

Once you have the list of file descriptors, you can filter for those of type PROX_FDTYPE_SOCKET. From there, you can extract the protocol, local and remote endpoints, and the connection state. This gives you a real-time snapshot of what a process is doing, regardless of whether it is using standard system libraries or custom networking code.

Moving Beyond Snapshots with Network Statistics

While polling file descriptors provides a point-in-time view, it is not ideal for continuous monitoring. For a more robust approach, the private NetworkStatistics.framework is the gold standard. This framework powers native tools like nettop and provides a global view of network activity, including byte counts and round-trip times.

Because this framework is private, it requires a bit of reverse engineering to use effectively. Security researcher Jonathan Levin has done extensive work in this area, documenting the framework and creating NetBottom, an open-source command-line implementation of nettop. By leveraging this, you can build tools that monitor traffic in real-time and associate that traffic with a specific process ID.

Detecting Malicious Behavior

The real value for a bug bounty hunter or red teamer lies in the ability to classify traffic as "suspicious" based on process metadata. A connection to a known C2 IP address is a clear indicator, but you can go further. Consider these heuristics:

  • Process Provenance: Is the process signed and notarized? If a non-platform binary is making network connections, it warrants immediate investigation.
  • Persistence Mechanisms: Does the process appear in the background task management database? Tools like DumpBTM allow you to inspect the BTM database to see if a process is configured to start automatically, a common tactic for malware persistence.
  • Process Hierarchy: Is the process being spawned by an unusual parent? A shell spawned by a background daemon that then initiates a network connection is a classic indicator of a reverse shell.

During a penetration test, you can use these techniques to validate whether your C2 infrastructure is being flagged by host-based security tools. If you are developing your own tools, the DNSMonitor project serves as an excellent reference for how to implement a DNS proxy using the Network Extension framework. It intercepts DNS requests, allowing you to log the query and the responsible process, and even block requests to known malicious domains.

The Defensive Reality

For blue teams, the shift toward host-based monitoring is essential. Perimeter defenses are no longer sufficient to stop modern, targeted attacks. By deploying tools that utilize these native frameworks, organizations can gain visibility into the "blind spots" created by encryption and process-level obfuscation.

If you are building security tooling, stop relying on external network taps. Start building at the host level. The APIs are there, the frameworks are documented, and the visibility you gain is far superior to anything you will get from a network appliance. Investigate the process hierarchy, monitor DNS resolutions, and keep a close eye on non-notarized binaries. The next time you are on an engagement, don't just look at the network traffic — look at the process that generated it.

Talk Type
talk
Difficulty
advanced
Has Demo Has Code Tool Released


Black Hat USA 2023

118 talks · 2023
Browse conference →
Premium Security Audit

We break your app before they do.

Professional penetration testing and vulnerability assessments by the Kuboid Secure Layer team. Securing your infrastructure at every layer.

Get in Touch
Official Security Partner
kuboid.in