Skip to content

Instantly share code, notes, and snippets.

@SethCalkins
Last active January 5, 2023 05:17
Show Gist options
  • Save SethCalkins/1ac3bee593b37067b489cd6e275b60ff to your computer and use it in GitHub Desktop.
Save SethCalkins/1ac3bee593b37067b489cd6e275b60ff to your computer and use it in GitHub Desktop.
Little Snitch Script to block Ad Servers

Here is a script to take the hosts from From http://pgl.yoyo.org/adservers/ and convert them to rules for Little Snitch.

It has options to grab the entire list or grab new entries past a certain date.

Just grab the output and copy/paste into Little Snitch.

The script is written to block access to any process, any port. You can also delete those lines and it will only block Mail. Or use the options to enter any process, port or protocol you want to block.

Installation: Copy the text generated by the output from a plaintext editor into LittleSnitch Rules


Save the script in your home directory, I called mine getadblock.sh.

Make sure to make it executable:

chmod +x getadblock.sh

Output looks like:


# Ad server hostnames for the Little Snitch Mac OSX applications
#
# For more information about this list, see: http://pgl.yoyo.org/adservers/
# ----
# last updated:   Mon, 21 Dec 2015 16:15:24 GMT
# entries:        2443
# format:         little-snitch
# credits:        Peter Lowe - [email protected] - http://pgl.yoyo.org/ - https://twitter.com/pgl
# this URL:       http://pgl.yoyo.org/adservers/serverlist.php?hostformat=one-line&showintro=1&mimetype=plaintext
# other formats:  http://pgl.yoyo.org/adservers/formats.php
# policy:         http://pgl.yoyo.org/adservers/policy.php
#


action: deny
direction: outgoing
process: any
owner: me
destination: adblockanalytics.com
port: any
protocol: any
help: From http://pgl.yoyo.org/adservers/

File - GetAdblock.sh


#!/bin/sh

usage()
{

echo "Usage:      "${0##*/}" [-d day -m month -y year] [-p processname] [-P port] [-r protocol]

OPTIONS:
   -d   day
   -m   month
   -y   year
   -p   Name of OSX Process to block, default is all
   -P   Port # to block
   -r   Protocol o block (6 = IP)
   -?   List this help page"
}

DAY=""
MONTH=""
YEAR=""

#block mail only
PROCESS="/Applications/Mail.app/Contents/MacOS/Mail
via: /System/Library/Frameworks/WebKit.framework/Versions/A/XPCServices/com.apple.WebKit.WebContent.xpc/Contents/MacOS/com.apple.WebKit.WebContent"
PORT=80
PROTOCOL=6

#block any access
PROCESS="any"
PORT="any"
PROTOCOL="any"



while getopts “p:P:r:d:m:y:?” OPTION
do
     case $OPTION in
         p)
           PROCESS=$OPTARG 
           ;;
        P)
           PORT=$OPTARG 
           ;;
        r)
           PROTOCOL=$OPTARG 
           ;;
        d)
           DAY=$OPTARG
           ;;
        m)
           MONTH=$OPTARG
           ;;
        y)
           if [ $OPTARG -lt 100 ]
           then
              let YEAR=$OPTARG+2000
           else
              YEAR=$OPTARG
           fi
           ;;
        ?)
           usage
           exit
           ;;
     esac
done

if [ -n "$DAY" ]
then
   STARTDATE=`date -j -f "%Y%m%d" "$YEAR$MONTH$DAY"`
   COMMENT="From http://pgl.yoyo.org/adservers/ - start date $STARTDATE"
else
   COMMENT="From http://pgl.yoyo.org/adservers/"
fi

RULES=`curl -fs "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=one-line&showintro=0&startdate%5Bday%5D=$DAY&startdate%5Bmonth%5D=$MONTH&startdate%5Byear%5D=$YEAR&mimetype=plaintext" | grep -v \#`
DATE=`curl -fs "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&startdate%5Bday%5D=&startdate%5Bmonth%5D=&startdate%5Byear%5D=" | grep "# last updated:"`
ENTRIES=`curl -fs "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&startdate%5Bday%5D=&startdate%5Bmonth%5D=&startdate%5Byear%5D=" | grep "entries"`

echo "# Ad server hostnames for the Little Snitch Mac OSX applications
#
# For more information about this list, see: http://pgl.yoyo.org/adservers/
# ----
$DATE
$ENTRIES
# format:         little-snitch
# credits:        Peter Lowe - [email protected] - http://pgl.yoyo.org/ - https://twitter.com/pgl
# this URL:       http://pgl.yoyo.org/adservers/serverlist.php?hostformat=one-line&showintro=1&mimetype=plaintext
# other formats:  http://pgl.yoyo.org/adservers/formats.php
# policy:         http://pgl.yoyo.org/adservers/policy.php
#"
if [ -n "$DAY" ]
then
   echo "# start date:    $STARTDATE"
fi
echo ""

for RULE in $(echo $RULES | tr "," "\n")
do
  echo "
action: deny
direction: outgoing
process: $PROCESS
owner: me
destination: $RULE
port: $PORT
protocol: $PROTOCOL
help: $COMMENT
"
done

exit

Run Command

./GetAdBlock.sh | pbcopy
@lordlycastle
Copy link

Does this still work with Little Snitch 4.4.3?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment