Friday, October 4, 2024

How to block web pages

I block myself from certain web pages because I find they hinder my productivity or because I'm mad at them for some long-forgotten reason.

The original way I blocked web pages was to make changes to the hosts file at the location below.  You'll need to run notepad as administrator and then open the "hosts" file (no extension).

C:\Windows\System32\drivers\etc\hosts

The operating system resolves DNS names to host names by first looking in the hosts file and then doing a DNS lookup on your configured DNS server.  Recall that 127.0.0.1 is your local host.  So if you add lines like the ones below, you tell the operating system to look to itself when it attempts to open web pages like the ones I specified here:

127.0.0.1 i.imgur.com
127.0.0.1 imgur.com

127.0.0.1 www.digg.com
127.0.0.1 digg.com

This blocks your computer from accessing the websites specified above.  If you ping the hostnames, you'll see it resolves to 127.0.0.1.

This used to work - and still does.  However, SOME browsers now bypass local DNS lookups from the operating system and do lookups by themselves using https and protocols other than the traditional DNS lookup.  The idea here is that it prevents third parties from snooping on your hostname lookups in clear text, forcing the browser to use https instead.  The problem with this approach is that it breaks decades of traditional hostname resolution.  The browser no longer honors my local hosts file.  You can disable the feature - details here:

https://support.mozilla.org/en-US/kb/firefox-dns-over-https

Thinking about this... what if you want to block only certain websites but still want the advantage of secure lookups?

Here is a way. You can force Firefox to block websites by adding domain names to a local policy file here:

C:\Program Files\Mozilla Firefox\distribution\policies.json

You'll need to run notepad as admin and then open the file.  Create the following block of config code:

{
  "policies": {
    "WebsiteFilter": {
      "Block": ["*://*.evilcorp.com/*",
"*://*.googlesucks.org/*",
"*://*.facebook.com/*"]
    }
  }
}

The code above would block evilcorp.com, googlesucks.org, and facebook.com

Notice when commas are needed and when they are not.  The last line does not need a comma.

Once you create the file, save it, make sure you closed out of Firefox, and then launch a new Firefox window.  Go to the offending website and it should tell you that it is blocked.

Chrome and Edge probably have similar mechanisms.  I only use Firefox for my general web browsing, so I only figured it out on Firefox.