Skip to content

nginx filter module to append random generated string to the end of HTML response

License

Notifications You must be signed in to change notification settings

nulab/nginx-length-hiding-filter-module

Repository files navigation

Nginx Length Hiding Filter Module

Build Status

Introduction

In BREACH site, the mitigations against BREACH attack are given as follows:

  1. Disabling HTTP compression
  2. Separating secrets from user input
  3. Randomizing secrets per request
  4. Masking secrets (effectively randomizing by XORing with a random secret per request)
  5. Protecting vulnerable pages with CSRF
  6. Length hiding (by adding random number of bytes to the responses)
  7. Rate-limiting the requests

BREACH relies on HTTP compression and it's reasonable to disable it to secure your website. However without compresseion, some websites may meet severe performance degression or the cost may increase if you're charged based on the volume of traffic like AWS. In such case it may be difficult to turn off HTML compression for whole responses from your website and need to adopt other proper ways.

Other mitigations listed from the 2nd to 5th above are basically applicable to your application but the 6th one, Length hiding, can be done on nginx. This filter module provides functionality to append randomly generated HTML comment to the end of response body to hide correct response length and make it difficult for attackers to guess secure token.

The sample of randomly appended HTML comment is here.

<!-- random-length HTML comment: JnSLGWeWYWsoJ4dXS3ubLw3YOu3zfGTotlzx7UJUo26xuXICQ2cbpVy1Dprgv8Icj6QfOZx2Ptp9HxCVoevTxhKzMzV6xeYXao0oCngRWJRb4Tvive1iBAXLzrHlLg6jKwNKXrct0tJuA2TvWIRVIng6UoffIbCQLPbi63PwmWemOxVi6m3CPa6hCbAK2CaBR1jLux7UJa4WNN4H0yIDMElMglWWouY5m5FUqAn0afMmtErj0zkA2LMWxisZRES38XLoYycySmaBrIih5IixUsJFR0ei4uZ0IifgV5SnitoNzMusSQem9npObHuU2HKApneAjwnFdPSQZA9sRdSOE8agDI05P832mV1JIcOjsg0FgzxvSG7UEX0HdqBqp2jPOYYW0k5gGtmkiXWydRJfn9lGomxReUeqq2Aec69gplEM6a8aqH5TFgXrGK8jcaPISQlsKkMxJQ7Fp6fVDbmI59xCIvlk -->

For every response, length of the random strings will vary within a given range.

This idea originally came from breach-mitigation-rails. Thanks team!

Warning

As said in breach-migration-rails, BREACH is complicated and wide-ranging attack and this module provides only PARTIAL protection. To secure your website or service wholly, you need to review BREACH paper and find proper way according to your own website or service.

Installation

Module version Nginx version
1.1.x 1.10.1 or higher
1.0.0 1.10.0 or earlier

Download nginx sources from http://nginx.org and unpack it.

Run configure script with adding --add-module option with the directory where this module is extracted like this:

./configure --add-module=/path/to/nginx-length-hiding-filter-module

To compile this module as dynamic module available in 1.9.11 or later, use --add-dynamic-module instead

./configure --add-dynamic-module=/path/to/nginx-length-hiding-filter-module

You can add other options along with it. Then build and install.

make
sudo make install

Configuration Directives

length_hiding

  • syntax: length_hiding on | off
  • default: off
  • context: http, server, location, if in location

Enables or disables adding random generated HTML comment.

length_hiding_max

  • syntax: length_hiding_max size
  • default: 2048
  • context: http, server, location

Sets maximum length of random generated string used in HTML comment. The size should be within a range from 256 and 2048.

length_hiding_types

  • syntax: length_hiding_types <mime_type> [..]
  • default: text/html
  • context: http, server, location, if in location

Enables adding random generated HTML comment to responses of the specified MIME types in addition to text/html. The special value * matches any MIME type.

Example Configuration

Enable this module for specific location ('/hiding'). In this example, the length of random strings will be less than 1024.

server {
    listen       443 default_server deferred ssl http2;
    server_name  example.com;
    length_hiding_max 1024;

    location /hiding {
        length_hiding on;
    }
}

If this module is built as dynamic module, do NOT forget including load_module line in nginx configuration.

load_module modules/ngx_http_length_hiding_filter_module.so;

Services using this module