Hosts

A hosts file is a list of IP addresses that are associated with particular hostnames. It was originally used for machines to locate other machines on the Internet, where site administrators would synchronize them using FTP or other means. The growth of the Internet after commercialization in the early 1990s led to the development of the Domain Name System (DNS), but even as of 2013, hosts files are still used to override DNS entries. 127.0.0.1 localhost 64.23.67.55 example.com www.example.com 55.230.133.158 mail.example.com 0.0.0.0 badserver.com www.badserver.com maliciousserver.info infectedserver.info 0.0.0.0 ad.tripleclick.net tracking.tripleclick.net socialwidgets.buttbook.com One can use a hosts file to disable lookup entirely for a hostname by adding an entry mapping it to. Some people advocate using this feature to disable lookup entirely for servers containing malicious software, third-party tracking servers such as those used by social recommendation widgets, or machines involved in serving rich-media advertisements whose display slows a computer down and runs up the data bill of a heavily capped Internet access subscription.[//slashdot.org/comments.pl?sid=4178141&cid=44789551]
 * 1) Example hosts file (all hosts fictitious)
 * 2) The first word on the line is an IPv4 or IPv6 address; the rest
 * 3) are hostnames.  However, some operating systems may allow only
 * 4) one hostname on each line.

Advantages
As a firewall mechanism, hosts files are easy for a computer's owner to deploy. Their common text-based file format across multiple operating systems allows for cross-platform tools and simple hand-editing by novices using a text editor. And because they are stored on a single machine, they follow that machine no matter what networks it joins. This proves helpful on a laptop that connects to several wireless networks open to the public. Hosts files can be used as a countermeasure against DNS cache poisoning, especially when it's [//slashdot.org/story/09/06/15/2056219/The-Next-Ad-You-Click-May-Be-a-Virus so easy to upload malware to ad networks] that popular sites end up hosting even more malware than porn sites.

Disadvantages
Hosts files have several drawbacks compared to other means of blocking undesirable connections, which can be split into those of the format itself and those of resolvers using the format.

The hosts file format has at least two theoretical drawbacks. One is lack of support for wildcards, such as  or   or. Some tracking providers have begun to exploit this, changing the hostname every day. Another is lack of a way to specify, as applications may not treat a   result the same.

The resolver built into popular operating systems for home PCs is slow. It scans the hosts file linearly for each resolution instead of loading the entire file into an in-memory tree, trie, or Bloom filter that can be searched quickly. This can cause a hostname not found in a multi-megabyte hosts file to take longer than a second, forcing a certain popular tool to have to reinvent the DNS wheel by managing its own cache of commonly used hostnames at the top of the file.

This resolver also tends to support only one hosts file per machine, which the administrator installs system-wide in a place like, not one per user. In addition, [//slashdot.org/story/12/08/19/1923210/windows-8-changes-host-file-blocking Windows 8 and later remove certain entries from the hosts file] unless the user tells Windows Defender to exclude the file from protection. This is ostensibly to stop phishing attacks where malware adds malicious entries to the hosts file, but it ends up getting in the way of blocking the slow, privacy-invading scripts used by advertisement and social networks. And devices running a mobile operating system such as iOS or Android typically don't give the device's owner enough administrative privileges to edit the hosts file.

In some situations, it might be preferable to run your own DNS server on a computer or router appliance on your network. For example, unlike a hosts file on each machine, a DNS server works across multiple machines within a LAN. A DNS server also supports more flexible file formats that can block an entire domain, not individual hostnames as one must with a hosts file.

Providers of hosts files
Several enthusiasts compile lists of these undesirable servers and make these lists available for download through the web. The spyware removal program Spybot – Search & Destroy has an "inoculate" feature that makes changes to the system to thwart spyware installers; one of these is a hosts file. Someone else makes this Spybot hosts file available separately.

Alexander P. Kowalski (APK), a staunch advocate of hosts files as an element of layered security[//slashdot.org/comments.pl?sid=5671455&cid=47871651] and the self-proclaimed "Lord of HOSTS", wrote an application to manage hosts files in Windows that draws from about a dozen of these blocking lists.[//slashdot.org/comments.pl?sid=3626185&cid=43390539] The user can choose to build a complete list of threats (over 4 million as of December 2015) or [//slashdot.org/comments.pl?sid=8506505&cid=51156185 limit it to current threats] (far smaller). This application is [//slashdot.org/comments.pl?sid=8163763&cid=50726445 proprietary] because APK fears that his software may be repackaged by malware authors the way the Chromium web browser was repackaged as eFast.[//slashdot.org/comments.pl?sid=8340575&cid=50950201] (In 2014, he branched out into [//slashdot.org/comments.pl?sid=4961607&cid=46609941 registry edits] that install [//slashdot.org/comments.pl?sid=4895159&cid=46477951 firewall rules that block IP addresses] shared by loads of individual hostnames at Akamai and other CDNs that are impractical to block with a hosts file, as well as [//slashdot.org/comments.pl?sid=4895069&cid=46481399 boosting the priority of hosts in the Windows registry's DNS section][//slashdot.org/comments.pl?sid=5671455&cid=47871779].) APK Hosts File Engine is said to do [//slashdot.org/comments.pl?sid=5671455&cid=47860661 things that ad-blocking browser extensions can't do], such as protect applications other than web browsers, cache IP addresses to work around DNS downtime and poisoning, and operate in kernel mode. It has been [//slashdot.org/comments.pl?sid=7672637&cid=50087671 recommended by an employee of MalwareBytes]. As of July 2015, the latest version is version 9.0. APK has built up a reputation for spamming his app on Slashdot, but [//slashdot.org/comments.pl?sid=8506505&cid=51156123 some Slashdot users seem to like it].

A few Slashdot users other than APK use a hosts file or other means of DNS-level blocking, such as [//slashdot.org/comments.pl?sid=8185377&cid=50749669 SuricouRaven], [//slashdot.org/comments.pl?sid=8340575&cid=50948531 shellbeach], and [//slashdot.org/comments.pl?sid=8969853&cid=51871111 bmo].

Efficient resolver
By May 2013, one popular hosts file manager was generating hosts files with over two million entries. This slows the performance of name resolution in popular desktop operating systems whose resolver rescans the hosts file linearly for every DNS resolution. This shouldn't be too hard to fix at the operating system level: scan it once and build a Bloom filter, a data structure that acts as a lossy compression of a set. The so-called "safe browsing" filter in Google Chrome uses the same data structure. This allows the resolver to rapidly determine whether or not a particular hostname is in the hosts file, at the cost of a few false positives. For a false positive probability of 1/256 and a set cardinality of 2 million, the size of the Bloom filter is 23083120 bits, or just under 3 MB. But an implementation might round the size to the nearest power of 2 so that it can more easily use something like SHA-256 or SHA-512 on the hostname to generate a set of bit addresses into the filter. In addition, the resolver would store the most commonly accessed entries and false positives in a cache in RAM.

A resolver could follow this algorithm:
 * 1) Compare the revision date of the hosts file to that of the current Bloom filter.
 * 2) If the dates differ, rescan the entire hosts file to build a new Bloom filter and clear the cache. This blocks DNS resolution until the hosts file is scanned once.
 * 3) If an entry or lack thereof for the hostname is in the cache, return that entry.
 * 4) Hash the hostname and look up the hash in the Bloom filter. If the hostname is definitely absent, stop.
 * 5) Otherwise, the hostname is very probably present. Scan the hosts file linearly for the hostname, and store it in the cache. Eviction policy for this cache is left as an exercise for the reader.