A lot of concern about the NSA's seemingly omnipresent surveillance over the last year has focused on the agency's efforts to install back doors in software and hardware. Those efforts are greatly aided, however, if the agency can piggyback on embedded software already on a system that can be exploited.

Two researchers have uncovered such built-in vulnerabilities in a large number of smartphones that would allow government spies and sophisticated hackers to install malicious code and take control of the device.

The attacks would require proximity to the phones, using a rogue base station or femtocell, and a high level of skill to pull off. But it took Mathew Solnik and Marc Blanchou, two research consultants with Accuvant Labs, just a few months to discover the vulnerabilities and exploit them.

The vulnerabilities lie within a device management tool carriers and manufacturers embed in handsets and tablets to remotely configure them. Though some design their own tool, most use a tool developed by a specific third-party vendor—which the researchers will not identify until they present their findings next week at the Black Hat security conference in Las Vegas. The tool is used in some form in more than 2 billion phones worldwide. The vulnerabilities, they say, were found so far in Android and BlackBerry devices and a small number of Apple iPhones used by Sprint customers. They haven't looked at Windows Mobile devices yet.

The researchers say there's no sign that anyone has exploited the vulnerabilities in the wild, and the company that makes the tool has issued a fix that solves the problem. But it's now up to carriers to distribute it to users in a firmware update.

Carriers use the management tool to send over-the-air firmware upgrades, to remotely configure handsets for roaming or voice-over WiFi and to lock the devices to specific service providers. But each carrier and manufacturer has its own custom implementation of the client, and there are many that provide the carrier with an array of additional features.

To give carriers the ability to do these things, the management tool operates at the highest level of privilege on devices, which means an attacker who accesses and exploits the tool has the same abilities as the carriers.

The management tools are implemented using a core standard, developed by the Open Mobile Alliance, called OMA device management. From these guidelines, each carrier can choose a base set of features or request additional ones. Solnik says they found that some phones have features for remotely wiping the device or conducting a factory reset, altering operating system settings and even remotely changing the PIN for the screen lock.

They've also found systems that allow the carrier to identify nearby WiFi networks, remotely enable and disable Bluetooth or disable the phone's camera. More significantly, they've found systems that allow the carrier to identify the applications on a handset, as well as activate or deactivate them or even add and remove applications. The systems give the carrier the option of making these changes with our without prompting the consumer. Carriers also can modify settings and servers for applications pre-installed by the carrier—something hackers could exploit to force the phone to communicate with a server of their choosing.

Furthermore, some of the systems can monitor the web browser's home page and in some cases retrieve synced contacts. Others include a call redirect function that can direct the phone to a specific phone number. Carriers typically use this feature to program shortcuts to their own phone numbers. For example, Verizon might program its phones so "299" dials customer service. But Solnik found this feature can be used to redirect any number; phone numbers also can be programmed to launch an application.

"Pretty much whatever number … if we programmed it, when you dial it, it would do whatever functionality we programmed it to do," Solnik says. "Whether you have the number 1 programmed for your mother, it would then do what we choose.”

The more features the management tool offers the carrier, the more an attacker can do as well. But at a minimum, every device they examined would allow an attacker to change all of the cellular network functionality. In many cases, they could also control firmware updates.

And even the phones that use only the most basic management system have memory corruption vulnerabilities that would still allow a hacker to execute code or install malicious applications, they found.

Two phones that provided the highest level of exploitation were the HTC One M7 and the Blackberry Z10. Among iOS devices, they found that only iPhones offered by Sprint and running an operating system prior to version 7.0.4 were vulnerable. The 7.0.4 version of the software, which Apple released in November, partially solved the issue.

The Blackberry Z10. Blackberry

Carriers recognize the risk these management tools present, and many have added encryption and authentication to bolster security. Accessing the management system in the device, for example, often requires a password. And the researchers found every carrier in the US encrypts communication between a device and the carrier's server. But these protections are so poorly implemented that the researchers could undermine them.

"Pretty much all the safeguards put into place to protect the clients in nearly all major devices we found can be bypassed," Solnik says.

In the case of the authentication, for example, they found that the systems use passwords that are generated in part using a public identifier—that is, the IMEI, or the cell phone’s serial number. That number is readily available by any base station that communicates with the phone. Solnik says that although each carrier’s system uses a slightly different method for generating passwords, they’re all based on the same core.

“They’re all taking a certain public identifier and a certain pre-shared token or secret and using that to derive the password," he says. "There is some secret sauce added, but because it’s derived from this token that is already public knowledge, that can be reverse-engineered and reproduced…. We can more or less pre-calculate all passwords for any device in order to manage the client."

They also found many ways to undermine the encryption. “It does require a deep understanding of what it’s doing, but once you understand how it works, you can pretty much turn off or just bypass or man-in-the-middle the encryption itself,” Solnik says.

Although the vulnerabilities are basic from a security perspective, exploiting them is not. Each requires extensive knowledge of the OMA-DM standard implementation and how cellular networks work. A successful hack also requires setting up a cellular base transceiver station or finding a vulnerability in a femtocell to take it over and use it for the attack. And cracking the encryption is also not trivial. Nonetheless, anyone with the same level of knowledge and skill as the researchers could conduct the attacks.

That said, the researchers don't believe anyone has exploited the vulnerabilities so far.

“During our disclosure with the vendors, different vendors have processes to look through to see if there are any traces of someone exploiting the vulnerabilities and we haven’t heard that there are any traces that anyone has seen so far," says Ryan Smith, chief scientist at Accuvant.

Solnik and Blanchou have notified the firm that makes the management tool used by so many, and the company has already issued a fix. They also notified baseband manufacturers, who have written code that would implement that fix. Carriers are in the process of distributing a fix to existing phones.

"It’s important that all users ... stay up to date with all the latest patches," Solnik says. "Users should contact their carrier to see if an update is already available."