Cybercriminals have developed a Web-based attack tool to hijack routers on a large scale when users visit compromised websites or view malicious advertisements in their browsers.

The goal of these attacks is to replace the DNS (Domain Name System) servers configured on routers with rogue ones controlled by attackers. This allows hackers to intercept traffic, spoof websites, hijack search queries, inject rogue ads on Web pages and more.

The DNS is like the Internet’s phonebook and plays a critical role. It translates domain names, which are easy for people to remember, into numerical IP (Internet Protocol) addresses that computers need to know to communicate with each other.

The DNS works in a hierarchical manner. When a user types a website’s name in a browser, the browser asks the operating system for that website’s IP address. The OS then asks the local router, which then queries the DNS servers configured on it—typically servers run by the ISP. The chain continues until the request reaches the authoritative server for the domain name in question or until a server provides that information from its cache.

If attackers insert themselves in this process at any point, they can respond with a rogue IP address. This will trick the browser to look for the website on a different server; one that could, for example, host a fake version designed to steal the user’s credentials.

An independent security researcher known online as Kafeine recently observed drive-by attacks launched from compromised websites that redirected users to an unusual Web-based exploit kit that was specifically designed to compromise routers.

The vast majority of exploit kits sold on underground markets and used by cybercriminals target vulnerabilities in outdated browser plug-ins like Flash Player, Java, Adobe Reader or Silverlight. Their goal is to install malware on computers that don’t have the latest patches for popular software.

The attacks typically work like this: Malicious code injected into compromised websites or included in rogue ads automatically redirect users’ browsers to an attack server that determines their OS, IP address, geographical location, browser type, installed plug-ins and other technical details. Based on those attributes the server then selects and launches the exploits from its arsenal that are most likely to succeed.

The attacks observed by Kafeine were different. Google Chrome users were redirected to a malicious server that loaded code designed to determine the router models used by those users and to replace the DNS servers configured on the devices.

Many users assume that if their routers are not set up for remote management, hackers can’t exploit vulnerabilities in their Web-based administration interfaces from the Internet, because such interfaces are only accessible from inside the local area networks.

That’s false. Such attacks are possible through a technique called cross-site request forgery (CSRF) that allows a malicious website to force a user’s browser to execute rogue actions on a different website. The target website can be a router’s administration interface that’s only accessible via the local network.

Many websites on the Internet have implemented defenses against CSRF, but routers generally lack such protection.

The new drive-by exploit kit found by Kafeine uses CSRF to detect over 40 router models from a variety of vendors, including Asustek Computer, Belkin, D-Link, Edimax Technology, Linksys, Medialink, Microsoft, Netgear, Shenzhen Tenda Technology, TP-Link Technologies, Netis Systems, Trendnet, ZyXEL Communications and HooToo.

Depending on the detected model, the attack tool tries to change the router’s DNS settings by exploiting known command injection vulnerabilities or by using common administrative credentials. It uses CSRF for this as well.

If the attack is successful, the router’s primary DNS server is set to one controlled by attackers and the secondary one, which is used as a failover, is set to Google’s public DNS server. In this way, if the malicious server temporarily goes down, the router will still have a perfectly functional DNS server to resolve queries and its owner will have no reason to become suspicious and reconfigure the device.

According to Kafeine, one of the vulnerabilities exploited by this attack affects routers from multiple vendors and was disclosed in February. Some vendors have released firmware updates, but the number of routers updated over the past few months is probably very low, Kafeine said.

The vast majority of routers need to be updated manually through a process that requires some technical skill. That’s why many of them never get updated by their owners.

Attackers know this too. In fact, some of the other vulnerabilities targeted by this exploit kit include one from 2008 and one from 2013.

The attack seems to have been executed on a large scale. According to Kafeine, during the first week of May the attack server got around 250,000 unique visitors a day, with a spike to almost 1 million visitors on May 9. The most impacted countries were the U.S., Russia, Australia, Brazil and India, but the traffic distribution was more or less global.

To protect themselves, users should check manufacturers’ websites periodically for firmware updates for their router models and should install them, especially if they contain security fixes. If the router allows it, they should also restrict access to the administration interface to an IP address that no device normally uses, but which they can manually assign to their computer when they need to make changes to the router’s settings.