Kuboid
Open Luck·Kuboid.in

Never Enough About Cameras - The Firmware Encryption Keys Hidden Under the Rug

DEFCONConference400 views26:116 months ago

This talk demonstrates a methodology for extracting and decrypting firmware from Dahua IoT cameras to identify vulnerabilities. The researchers reverse-engineered the bootloader to recover AES-256-CBC encryption keys and subsequently discovered a stack-based buffer overflow in the HTTP service. The presentation details the process of bypassing security controls, including the use of LD_PRELOAD for privilege escalation and ROP chains to achieve code execution on the target device.

Cracking Dahua Firmware: From Bootloader Keys to Remote Root

TLDR: This research demonstrates a complete exploitation chain against Dahua IoT cameras, starting with firmware decryption via reverse-engineered bootloader keys and ending with remote root access. By identifying a stack-based buffer overflow in the HTTP service, the researchers bypassed authentication and achieved code execution. This post breaks down the methodology for extracting keys and crafting the ROP chain required to gain control of these ubiquitous devices.

Security researchers often treat IoT devices as black boxes, but the reality is that most firmware is just a collection of poorly protected binaries waiting to be unpacked. The recent analysis of Dahua devices highlights a recurring failure in embedded security: the assumption that obfuscation is equivalent to encryption. When you can pull the firmware off a flash chip using a Bus Pirate and reverse the bootloader in Ghidra, the manufacturer's "security" evaporates.

Extracting the Keys

The first hurdle in analyzing these cameras is the encrypted firmware. Many vendors use custom compression or simple XOR schemes to deter casual inspection. In this case, the researchers found that the firmware was not just compressed but protected by an AES-256-CBC implementation. The key was not stored in a secure enclave but was instead derived within the bootloader.

By loading the U-Boot binary into Ghidra, the team identified the key derivation function. It relied on hardcoded constants and a SHA-256 hash of the processor name. Once the derivation logic was mapped, the researchers could replicate the process to generate the correct 32-byte key.

The decryption process itself was straightforward once the key was recovered. The firmware images contained a standard U-Boot header, which provided the necessary metadata to carve the encrypted payload. Using a custom Python script to handle the decryption, they successfully extracted the filesystem. This is a critical step for any researcher; once you have the filesystem, you can move from static analysis to identifying the actual attack surface.

The HTTP Service Vulnerability

With the filesystem extracted, the researchers turned their attention to the network services. Dahua cameras expose a wide array of protocols, including ONVIF, RTSP, and a proprietary protocol called DHIP. These services are often implemented in a single binary that uses a regex-based parser to route incoming packets to the appropriate handler.

The vulnerability was located in the RPC2_UploadFileWithName handler. This function was intended to process file uploads, but it suffered from a classic stack-based buffer overflow. The code used strncpy to copy the Cseq header into a fixed-size buffer, but it incorrectly used the length of the string rather than the size of the destination buffer.

// Vulnerable pattern in the RPC2_UploadFileWithName handler
char *cseq = get_header("Cseq");
if (cseq) {
    strncpy(buffer, cseq, strlen(cseq)); // Incorrect length parameter
}

This oversight allowed an attacker to overwrite the stack, including the return address. Because the binary was not position-independent, the researchers had to contend with a fixed memory layout. However, the device's crash logs—which were conveniently sent over the network—provided the register state at the time of the crash, effectively acting as a remote debugger.

Exploitation and Privilege Escalation

Achieving code execution required a carefully crafted ROP chain. Since the binary was not position-independent, the researchers could rely on static addresses for their gadgets. The goal was to call system() with a controlled command.

The primary constraint was the presence of a null byte in the addresses of the available gadgets, which limited the ROP chain to a single gadget. The team identified a "system gadget" that allowed them to control the r0 register, where the command string would be passed.

To bypass the kernel's restriction on running unsigned binaries, they utilized an LD_PRELOAD technique. By uploading a malicious shared library (.so) via TFTP and forcing the application to load it, they could execute arbitrary code within the context of the running process. This effectively turned a simple buffer overflow into a full root shell.

Real-World Impact

For a pentester, this research is a masterclass in how to approach hardened IoT targets. You are rarely going to find a simple command injection in the web UI of a modern camera. Instead, you have to look at the proprietary RPC handlers and the underlying binary protocols.

If you are auditing these devices, start by mapping the attack surface with Nmap to identify all open ports, then focus your efforts on the proprietary handlers. The lack of authentication on some of these endpoints is a common finding, but the real prize is the memory corruption that allows you to bypass the authentication checks entirely.

Defenders should prioritize network segmentation for all IoT devices. These cameras should never have direct access to the internet, and their management interfaces should be restricted to a dedicated VLAN. If you are responsible for securing these deployments, ensure that firmware updates are applied immediately, as vendors are increasingly patching these types of memory corruption vulnerabilities once they are disclosed.

The path from a physical device to a root shell is rarely a straight line. It requires a combination of hardware hacking, reverse engineering, and a deep understanding of how embedded Linux systems handle memory. Keep digging into the binaries, because the most interesting bugs are almost always hidden in the code that the manufacturer assumes no one will ever look at.

Talk Type
research presentation
Difficulty
advanced
Category
iot security
Has Demo Has Code Tool Released


DC33 IoT Village Talks

9 talks · 2025
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