How do I use wildcards to redirect/block domains on OS X?

2

1

I have tried dnsmasq, squid and this dns proxy, however, none of them does what I want. I want to do something like this:

127.0.0.1 *facebook.com
127.0.0.1 *imgur.com

Where every URL matched is redirected to 127.0.0.1, or just straight up blocked. (whether on the command line, on apps, on browsers, simply an emulation of some of the functions of /etc/hosts but with wildcards).

Although if nothing works, then via the browser is the most important.

DisplayName

Posted 2015-12-13T18:55:05.057

Reputation: 61

1AFAIK, no version of Unix supports wildcards in /etc/hosts. – G-Man Says 'Reinstate Monica' – 2015-12-13T19:16:39.440

@G-Man I know, that's why I want some Application to do this. – DisplayName – 2015-12-13T20:22:08.030

1Well, then, maybe you should have mentioned /etc/hosts fewer than three times in your question.  It looks like your question is "What name resolution service will let me do wildcard mapping of host/domain names to IP addresses?"  And that would be off-topic as a product request. – Scott – 2015-12-13T21:09:50.083

@Scott The title was misleading ok, but the last says what I want. – DisplayName – 2015-12-13T21:23:38.390

1@DisplayName, can you please clarify if you're only trying to use this from the command line or from with apps/browsers? What's the use case and maybe we can direct you towards a better solution. Please update your post with this info – SaxDaddy – 2015-12-15T23:49:09.473

Answers

2

Like Jeremy W. Sherman says, you should consider setting up a localised DNS server, as the etc/hosts file cannot support wildcards.

Consider installing a program called DNSMasq, which lets you create a kind of wildcard:

address=/facebook.com/127.0.0.1
address=/imgur.com/127.0.0.1

Which would reroute all domains with imgur.com and facebook.com in them to 127.0.0.1.

Read this blog post titled /etc/hosts Bugs, and DNS Resolution or this updated blog post: How to get .dev addresses on Mac OS X 10.10 Yosemite in 5 simple steps for more information on how to install this and why this happens.

Quill

Posted 2015-12-13T18:55:05.057

Reputation: 300

If you read my question you would have seen that I tried to install dnsmasq. I tried 8 different guides but none of them worked. – DisplayName – 2015-12-23T23:58:53.973

@DisplayName see my update – Quill – 2015-12-24T01:26:15.043

0

Browser:

  • Install something like RequestPolicy. (RequestPolicy is Firefox-only as far as I know. I believe uBlock or uMatrix might be able to do something similar for Chrome.)
  • Open Manage Policies.
  • Add a Block policy:
    • Leave the origin fields empty. (This implicitly matches all origins.)
    • In the destination fields, set the host to a wildcard like *.facebook.com.
    • Hit the "Add Rule" button.

Screenshot: Configuring RequestPolicy Continued to block requests to Facebook

DNS:

  • You'll want to run a full-on DNS server like named or unbound and configure System Preferences to use it as your system's DNS server.
  • You should be able to configure that to pretend to be authoritative for the entire facebook.com zone and then set up a wildcard within the zone configuration.
  • For stuff you don't want to block, you'll configure it to act as a local cache of some upstream DNS server.
  • This configuration is hinted at in the unbound documentation for its local-data configuration stanza:

    If you need more complicated authoritative data, with referrals, wildcards, CNAME/DNAME support, or DNSSEC authoritative service, setup a stub-zone for it as detailed in the stub zone section below. (emphasis added)

Running your own DNS server can get involved. Here is a walkthrough for setting up named for similar purposes.

Jeremy W. Sherman

Posted 2015-12-13T18:55:05.057

Reputation: 606

0

Run sudo nano -e /etc/hosts to open the hosts file and put inside

127.0.0.1 *.domain-name.com #Block subdomains
127.0.0.1 domain-name.com #Block domain

You should put both to block the web site.

Koraniar

Posted 2015-12-13T18:55:05.057

Reputation: 1