How do I block ads on YouTube?

Ok :slight_smile:

Hope http://dnsdumpster.com fix the error.

But again, thanks for the files.

OK, how can i do the same on my pi? IM really a noob.

Btw. Here is a list of youtube blocks: https://github.com/HenningVanRaumle/pihole-ytadblock

that github link is really dated, and not updated, it seems.
since they change those domains often, i do not use it. just sayin, ymmv.

as to the other entries - i added two lines to my shell script :
after this line ...
grep r*.googlevideo.com /var/log/pihole.log | awk '{print $6}'| grep -v
^googlevideo.com|redirector.googlevideo.com' | sort -nr | uniq >> /var/www/html/youtube-ads-list.txt

added these two :
grep r*.googlevideo.com /var/log/pihole.log.1 | awk '{print $6}'| grep -v '^googlevideo.com|redirector.googlevideo.com' | sort -nr | uniq >> /var/www/html/youtube-ads-list.txt
zgrep r*.googlevideo.com /var/log/pihole.log.2.gz | awk '{print $6}'| grep -v '^googlevideo.com|redirector.googlevideo.com' | sort -nr | uniq >> /var/www/html/youtube-ads-list.txt

Thx for your reply.

Yes, I can see the list i posted is over 10 months old.

Removed the list again!

Any solution here?

I've found a solution using DNS Lookup - Online Tool | HackerTarget.com

Luckily the site has an API to which makes downloading a list of DNS records simple using something like curl. The API is designed to be used in an ad-hoc fashion not for bulk queries and is limited to 100 (total) requests from a single IP Address per day.

Quick word of warning, I'm using this script on a pfSense server that's using pfblocker for ad domain blocking. Whilst the script uses standard FreeBSD/Linux type commands, the paths to those commands maybe different when run on a pi-hole.

Enough preamble, here's the script:

echo "" > /root/youtube/youtube-domains.txt
echo "" > /root/youtube/youtube-filtered.txt
echo "" > /root/youtube/youtube-ads.txt
/usr/local/bin/curl "https://api.hackertarget.com/hostsearch/?q=googlevideo.com" > 
/root/youtube/youtube-domains.txt
cat /root/youtube/youtube-domains.txt | cut -d, -f1 > /root/youtube/youtube-filtered.txt
/usr/bin/sed '1d' /root/youtube/youtube-filtered.txt > /root/youtube/youtube-ads.txt 

Lines 1 - 3 clear down text files, as per previous scripts above
Line 4 downloads the DNS records from api.hackertarget.com in comma separated variables format: domain,IP address
Line 5 removes the second entry per line (the IP address)
Line 6 removes the top entry from the resulting file. This just has the text "googlevideo.com" in it

Done!

As I say, works for me under pfSense with pfblocker, so should work under pi-hole with minor modification

1 Like

wow !
Works perfectly on my R-pi3 :grinning:

Thank you.

Your script could be be condensed a little bit, assuming all these googlevideo.com domains are in fact ad domains (ergo: I haven't tested this! :laughing:) :

curl -s "https://api.hackertarget.com/hostsearch/?q=googlevideo.com" | awk -F, 'NR>1 {print $1}' | sudo tee /var/www/html/youtube-ads.txt

This creates a blacklist you can use at http://pi.hole/youtube-ads.txt :slight_smile:

2 Likes

Can you make a step by step guide, to get it to work? I´m a big noob in Linux..

I want the script to run ones time a day and auto-update the youtube-ads.txt

Can i just make a youtube.sh file

and enter

#!/bin/bash
curl -s "https://api.hackertarget.com/hostsearch/?q=googlevideo.com" | awk -F, 'NR>1 {print $1}' | sudo tee /var/www/html/youtube-ads.txt
#Update Pihole Gravity
pihole -g
echo Update Done

Add this the to the block list: http://pi.hole.ip/youtube-ads.txt

and then make cronjob that runs any night

As long as you added http://pi.hole/youtube-ads.txt to your block list, then yeah your steps would work. However, I have extreme doubts about the domains being provided being ad-serving domains - therefore, you could be very likely blocking access to legitimate content.

The only way I know to verify an ad-serving content server is by coming across it yourself and verifying the host via YT's "Stats for nerds" panel.

1 Like

Thanks for you reply!

Ok, I think I will use a plugin for chrome to block youtube add, and don´t use the pihole for that

http://pi.hole/youtube-ads.txt is no long accessible, or it's down. Any workaround?

Friend, that is a url that goes directly to your pi hole. If that doesn't load that means you don't have the file in your html /var/www/html/.. directory

3 Likes

I have made that running every minute. Still some adds propagated through.

UBlock fixes it.
Youtube is something that requires application level filtering.

indeed, i'm just an idiot and wasn't paying attention. thanks

Update
Well with the hackertarget api there is no need for dnsdumpster so temp files will be saved in pihole dir from now:

uninstall dnsdumpster, python python-pip, or let it as it is.

important part:
Update the /etc/pihole/youtube-ads.sh file to the following:


echo off
rm /etc/pihole/youtube-filtered.txt
rm /etc/pihole/youtube-ads.txt
curl -s "https://api.hackertarget.com/hostsearch/?q=googlevideo.com" | awk -F, 'NR>1 {print $1}' | sudo tee /etc/pihole/youtube-filtered.txt > /dev/null
sed 's/\s.$//' /etc/pihole/youtube-filtered.txt >> /etc/pihole/youtube-ads.txt
cat /etc/pihole/youtube-ads.txt > /var/www/html/youtube-ads-list.txt
#greps the log for youtube ads and appends to /var/www/html/youtube-ads-list.txt
grep r
.googlevideo.com /var/log/pihole.log | awk '{print $6}'| grep -v '^googlevideo.com|redirector' | sort -nr | uniq >> /var/www/html/youtube-ads-list.txt
#removes duplicate lines from /var/www/html/youtube-ads-list.txt
perl -i -ne 'print if ! $x{$_}++' /var/www/html/youtube-ads-list.txt
#updates pihole blacklist/whitelist
pihole -g

flush your logs from within the pihole admin interface.
wait or manualy run /etc/pihole/youtube-ads.sh (2x)

thanks ppl good luck

The page https://api.hackertarget.com/hostsearch/?q=googlevideo.com somehow did generate some of the
fingerprints of googlevideo.com but didn't have the rxxxsnxxxx.googlevideo.com that my phone received so all the ads just slipped through. Can you help me with this ?

had to change the collumn AWK selects to filter the "r5---sn-5hnednlr.googlevideo.com" look-alike lines from the pihole log. Collumn 6 seems to feed my device ipaddresses to the youtube-ads-list.txt instead.

from:
"grep r.googlevideo.com /var/log/pihole.log | awk ‘{print $6}’| grep -v ‘^googlevideo.com|redirector’ | sort -nr | uniq >> /var/www/html/youtube-ads-list.txt" to
to:
"grep r.googlevideo.com /var/log/pihole.log | awk ‘{print $8}’| grep -v ‘^googlevideo.com|redirector’ | sort -nr | uniq >> /var/www/html/youtube-ads-list.txt

Based on the info in this thread I've put together a simple script. I don't use the YouTube app that often, so I'm not 100% this works perfectly. It also appends the urls with the r00---sn-xxxxxx.googlevideos.com pattern, which are not in the hackertarget list.

Place it somewhere convenient, add execute permissions (chmod +x filter-youtube-domains.sh) and add it to your cron jobs.

Edit:
I have noticed some ads still (although less than I used to). I'm not sure yet if the domains provided by hackertarget are incomplete or that I need to run my cron job more often (now it runs every 24h). I did find a longer list on Wolfram Alpha (click on subdomains) but they don't seem to have an easy way to get those in plain text.

1 Like

see my script below, it fixes those urls too :slight_smile: