How ad-blockers work ?

Main principles

Ad-blockers are web-browser extensions that prevent the loading of undesired content, usually advertisements. Sometimes the content is still loaded but simply hidden from the user. These extensions are piece of code that interact with the web-browser and the browser engine. So first, what is the browser engine ?

Browser engines are the main software component of every web-browser. Its role is to transform HTML (the code language used to construct your web page) and other resources into the actual webpage that users see. To make it simple, it takes the complicated stuff and transform it into a beautiful web page. Gecko and Blink are famous browser engine, used by Firefox and Chrome respectively. When the browser engine loads contents, it can follow some rules about what to do with them. These rules are called “content-policy”. In our case, this rules indicate whether or not something should be loaded. Lots of rules are already integrated and web extensions add new rules on top of them. So basically, ad-blockers only need to add new content-policy. Then, all the work is done by the browser engine. The ad-blocker extension tell the engine that an ad should not be download and the browser engine prevent it.

adblock schema

To resume : the browser engine downloads contents, and checks them against its rules called “control policy”. If it is again the rules then the content is not loaded. Ad-Blocker write new rules against advertisement.

If we take the example of “Ad-Block Plus”, a famous ad-blocker, it uses Javascript to write content-policy.

Today, every ad-blockers relies on filter rules to block or authorize content. They inspect every HTTP request looking for specific strings that match rules in lists. If a string matches a rule in the list that you subscribed to or added manually, then the request is blocked and the content is not loaded. For example, “Ad-Block Plus” uses EasyList but other list can be added.

Nevertheless, a common way of injecting ads it to encode an image with data:image/png*;.* That way, no HTTP request is made and the content cannot be block. So, Ad-blockers rely on CSS rules to hide the content. They inject style-sheet into the webpage with selectors like *display:none !important*, and then reconstruct the web page to fill the gap left by the ad.

Filters lists

Most of the ad-blocking extension rely on EasyList. It is an huge collection of known ads for English websites. Users can also subscribe to other lists or even to specific lists that doesn’t particularly block ads but annoying content such as social media button or certain aspect of Facebook. One famous list is called “Acceptable ads” which allow some non-intrusive ads, that respect privacy as well. Company can pay to be on this list if they respect the code of conduct for non-intrusive ads. Of course, people can unsubscribed from this list and even add custom URL that they don’t want to be loaded.

Lists consist of rules that are then transformed into regular expression (regex). However, regex matching complexity is roughly O(n). It means that the overall complexity would be O(n * f) with f the number of filter. Which is bad because it depends on the number of filters.

Because rules are not regex but only converted into it, we can extract a characteristic substring (“shortcut”) that we know must be present in any string matched by the filter. For example in : ||[](*, the shortcut “[http://evil](http://evil/)” or “ttp://evil” can be used. So we lookup for this shortcut in the string and if we find one, then we test the string against the corresponding filter. And we only need to test against filters whose shortcuts we find, meaning that this operation will happen rarely, and only the effectiveness of the shortcut search is relevant for the overall performance.

We end up with an overall complexity, after the creation of hash table, of O(n) that doesn’t depends on f any more.

Evading anti-ad-blocker

The advertising industry is a giant cat-and-mouse game, where people try to block ads while companies try to display them. Every one develop tools and new technologies to help them.

User can used tools to escape anti-ad-blockers. These tools are extensions that function on the same concept as ad-blockers. It consists of lists that matches Javascript code used to detect ad-blockers.

One of the most famous extension is “Anti-AdBlock Killer” which used AakList that contains rules like this :

! if (!awm) {window.location =’’;}


  1. Thomas May 14, 2019 at 4:13 pm - Reply

    Amazing article, thank you !

  2. Alex May 21, 2019 at 10:04 am - Reply

    It was a very interesting article, keep writing!

  3. Laetitia June 11, 2019 at 5:07 pm - Reply

    Amazing !

Leave A Comment

How ad-blockers work ?

2019-03-29T14:22:30+01:00 February 18th, 2019|Categories: Discover|Tags: , , , |3 Comments