All Projects → kyprizel → Testcookie Nginx Module

kyprizel / Testcookie Nginx Module

simple robot mitigation module using cookie based challenge/response technique. Not supported any more.

Programming Languages

c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to Testcookie Nginx Module

Ngx dynamic limit req module
The ngx_dynamic_limit_req_module module is used to dynamically lock IP and release it periodically.
Stars: ✭ 57 (-87.25%)
Mutual labels:  ddos, nginx, nginx-module
Zstd Nginx Module
Nginx modules for the Zstandard compression
Stars: ✭ 64 (-85.68%)
Mutual labels:  nginx, nginx-module
Nginx Nonewlines
This is an nginx module to strip the served HTML of all newlines (\n and \r characters)
Stars: ✭ 17 (-96.2%)
Mutual labels:  nginx, nginx-module
Nginx Tutorial
最全面,最深入的nginx从入门到精通的教程
Stars: ✭ 416 (-6.94%)
Mutual labels:  nginx, nginx-module
Nginx Module Sysguard
Nginx sysguard module
Stars: ✭ 568 (+27.07%)
Mutual labels:  nginx, nginx-module
Nginx Builder
A tool to build deb or rpm package of required Nginx version from the source code, with the ability to connect third-party modules. Nginx parameters are set in the yaml configuration file.
Stars: ✭ 123 (-72.48%)
Mutual labels:  nginx, nginx-module
Ngx healthcheck module
nginx module for upstream servers health check. support stream and http upstream. 该模块可以为Nginx提供主动式后端服务器健康检查的功能(同时支持四层和七层后端服务器的健康检测)
Stars: ✭ 145 (-67.56%)
Mutual labels:  nginx, nginx-module
Mod zip
Streaming ZIP archiver for nginx 📦
Stars: ✭ 178 (-60.18%)
Mutual labels:  nginx, nginx-module
Nginx Http Echo Module
A simple Nginx echo module
Stars: ✭ 192 (-57.05%)
Mutual labels:  nginx, nginx-module
Nginx Module Vts
Nginx virtual host traffic status module
Stars: ✭ 2,518 (+463.31%)
Mutual labels:  nginx, nginx-module
Ngx php7
ngx_php7 - Embedded php7 scripting language for nginx module. Mainline development version of the ngx_php.
Stars: ✭ 337 (-24.61%)
Mutual labels:  nginx, nginx-module
Ngx php
ngx_php - Embedded php scripting language for nginx module. All released versions of the code repository.
Stars: ✭ 290 (-35.12%)
Mutual labels:  nginx, nginx-module
Nginx Opentracing
NGINX plugin for OpenTracing
Stars: ✭ 341 (-23.71%)
Mutual labels:  nginx, nginx-module
Django React Boilerplate
DIY Django + React Boilerplate for starting your SaaS
Stars: ✭ 385 (-13.87%)
Mutual labels:  nginx
Awesome Nginx Security
🔥 A curated list of awesome links related to application security related to the environments with NGINX or Kubernetes Ingres Controller (based on NGINX)
Stars: ✭ 417 (-6.71%)
Mutual labels:  nginx
Nginx Ui
Nginx UI allows you to access and modify the nginx configurations files without cli.
Stars: ✭ 4,067 (+809.84%)
Mutual labels:  nginx
Naxsi
NAXSI is an open-source, high performance, low rules maintenance WAF for NGINX
Stars: ✭ 3,927 (+778.52%)
Mutual labels:  nginx
Ezhttp
The bash shell script stack for installation of Nginx OpenResty Tengine lua_nginx_module nginx_concat_module nginx_upload_module ngx_substitutions_filter_module Apache-2.2 Apache-2.4 MySQL-5.1 MySQL-5.5 MySQL-5.6 MySQL-5.7 PHP-5.2 PHP-5.3 PHP-5.4 PHP-5.5 PHP-5.6 ZendOptimizer ZendGuardLoader Xcache Eaccelerator Imagemagick IonCube Memcache Memcached Redis Mongo Xdebug Mssql Memcached PureFtpd PhpMyAdmin Redis Mongodb PhpRedisAdmin MemAdmin RockMongo Jdk7 Jdk8 Tomcat7 Tomcat8
Stars: ✭ 443 (-0.89%)
Mutual labels:  nginx
Vue2 blog
使用vue2.x + vue-cli +vue-router+ vuex + axios + mysql + express + pm2 + webpack+nginx构建的具有登录,注册,留言,用户发帖,用户评论等功能的SPA Blog。注意,注意,注意,后端API全部自己手写,很适合刚学习vue以及express的小伙伴学习,喜欢请Star鼓励一下我,谢谢!项目预览:
Stars: ✭ 417 (-6.71%)
Mutual labels:  nginx
Highdsa
2018年本科毕设项目,已更新所有开发和部署文档。基于Dubbo、SSM、Shiro、ELK、ActiveMQ、Redis等实现的一套高可用、高性能、高可扩展的分布式系统架构,实现可支持业务的基础公共服务,API使用Restful风格对外暴露。已经实现的包括:发送邮件服务、FastDFS文件存储服务、ELK实时日志查询服务、Redis缓存服务、Mybatis数据库、阿里短信推送、Goeasy消息推送、Druid监控、ActiveMQ消息队列、shiro权限认证、cas单点登录、权限配置web系统、移动端后台系统。持续更新中......
Stars: ✭ 385 (-13.87%)
Mutual labels:  nginx

Description

testcookie-nginx-module is a simple robot mitigation module using cookie based challenge/response.

Challenge cookies can be set using different methods:

  • "Set-Cookie" + 302/307 HTTP Location redirect
  • "Set-Cookie" + HTML meta refresh redirect
  • Custom template, JavaScript can be used here.

To prevent automatic parsing, challenge cookie value can be encrypted with AES-128 in CBC mode using custom/random key and iv, and then decrypted at client side with JavaScript.

Directives

testcookie

syntax: testcookie (on|off|var);

default: off

context: http, server, location, if

on - Enable module

off - Disable module

var - Don't intercept requests, only set module variables.

testcookie_name

syntax: testcookie_name <string>

default: TCK

context: http, server, location

Sets cookie name.

testcookie_domain

syntax: testcookie_domain <string>

default: none, set by browser

context: http, server, location

Sets cookie domain.

testcookie_expires

syntax: testcookie_expires <string>

default: 31 Dec 2037 23:55:55 GMT

context: http, server, location

Sets cookie expiration value.

testcookie_path

syntax: testcookie_path <string>

default: /

context: http, server, location

Sets cookie path, useful if you plan to use different keys for locations.

testcookie_samesite

syntax: testcookie_samesite <string>

default: None

context: http, server, location

Sets cookie attribute, allows you to declare if your cookie should be restricted to a first-party or same-site context. Default is None (Cookies will be sent in all contexts, i.e sending cross-origin is allowed.) Accepts values: Lax, Strict, None.

testcookie_secret

syntax: testcookie_secret <string>

default: required configuration directive

context: http, server, location

Secret string, used in challenge cookie computation, should be 32 bytes or more, better to be long but static to prevent cookie reset for legitimate users every server restart. If set to "random" - new secret will be generated every server restart, not recomended(all cookies with previous key will be invalid),

testcookie_session

syntax: testcookie_session <variable>

default: required configuration directive

context: http, server, location

Sets the challenge generation function input,

  • $remote_addr - clients IP address will be used as an user unique identifier
  • $remote_addr$http_user_agent - clients IP + User-Agent

testcookie_arg

syntax: testcookie_arg <string>

default: none

context: http, server, location

Sets GET parameter name, used for cookie setting attempts computation,

If not set - server will try to set cookie infinitely.

testcookie_max_attempts

syntax: testcookie_max_attempts <integer>

default: 5

context: http, server, location

Sets maximum number of redirects before user will be sent to fallback URL, according to RFC1945 can't be more than 5.

If set to 0 - server will try to set cookie infinitely(actually, browser will show the error page).

testcookie_p3p

syntax: testcookie_p3p <string>

default: none

context: http, server, location

Sets P3P policy.

testcookie_fallback

syntax: testcookie_fallback <script>

default: none

context: http, server, location

Sets the fallback URL, user will be redirected to after maximum number of attempts, specified by directive testcookie_max_attempts exceded. Nginx scripting variables can be used here. If not set - client will get 403 after max attempts reached.

testcookie_whitelist

syntax: testcookie_whitelist <network list>

default: none

context: http, server

Sets the networks for which the testing will not be used, add search engine networks here. Currently IPv4 CIDR only.

testcookie_pass

syntax: testcookie_pass $variable;

default: none

context: http, server

Sets the variable name to test if cookie check should be bypassed. If variable value set to 1 during the request - cookie check will not be performed. Can be used for more complex whitelisting.

testcookie_redirect_via_refresh

syntax: testcookie_redirect_via_refresh (on|off);

default: off

context: http, server, location

Set cookie and redirect using HTTP meta refresh, required if testcookie_refresh_template used.

testcookie_refresh_template

syntax: testcookie_refresh_template <string>

default: none

context: http, server, location

Use custom html instead of simple HTTP meta refresh, you need to set cookie manually from the template Available all the nginx variables and

$testcookie_nexturl - URL the client should be redirected to, if max_attempts exceeded *testcookie_fallback* value will be here
$testcookie_got - cookie value received from client, empty if no cookie or it does not match format
$testcookie_set - correct cookie value we're expecting from client
$testcookie_ok - user passed test (1 - passed, 0 - not passed) Note: changed from "yes"/"no" in v1.10

also, if testcookie_refresh_encrypt_cookie enabled there are three more variables:

$testcookie_enc_key - encryption key (32 hex digits)
$testcookie_enc_iv - encryption iv (32 hex digits)
$testcookie_enc_sec - encrypted cookie value (32 hex digits)

testcookie_refresh_status

syntax: testcookie_refresh_status <code>

default: 200

context: http, server, location

Use custom HTTP status code when serving html.

testcookie_deny_keepalive

syntax: testcookie_deny_keepalive (on|off);

default: off

context: http, server, location

Close connection just after setting the cookie, no reason to keep connections with bots.

testcookie_get_only

syntax: testcookie_get_only (on|off);

default: off

context: http, server, location

Process only GET requests, POST requests will be bypassed.

testcookie_https_location

syntax: testcookie_https_location (on|off);

default: off

context: http, server, location

Redirect client to https protocol after setting the cookie, also affects $testcookie_nexturl, useful with 3dparty SSL offload.

testcookie_refresh_encrypt_cookie

syntax: testcookie_refresh_encrypt_cookie (on|off);

default: off

context: http, server, location

Encrypt cookie variable, used with testcookie_refresh_template to force client-side decryption with AES-128 CBC.

testcookie_refresh_encrypt_cookie_key

syntax: testcookie_refresh_encrypt_cookie_key <32 hex digits|random>

default: required directive if encryption enabled

context: http, server, location

Sets encryption key.

Possible values:

random - new key generated every nginx restart
32 hex digits - static key, useful if you plan to obfuscate it deep in client-side javascript.

testcookie_refresh_encrypt_iv

syntax: testcookie_refresh_encrypt_iv <32 hex digits|random|random2>

default: random

context: http, server, location

Sets encryption iv.

Possible values: random - new iv generated for every client request random2 - new iv generated for every nginx restart 32 hex digits - static iv, useful if you plan to obfuscate it deep in client-side javascript

testcookie_internal

syntax: testcookie_internal (on|off);

default: off

context: http, server, location

Enable testcookie check for internal redirects (disabled by default for optimization purposes!), useful for this type of configs:

rewrite ^/(.*)$ /index.php?$1 last;

testcookie_httponly_flag

syntax: testcookie_httponly_flag (on|off);

default: off

context: http, server, location

Enable HttpOnly flag for cookie.

testcookie_secure_flag

syntax: testcookie_secure_flag (on|off|$variable);

default: on

context: http, server, location

Enable Secure flag for cookie. Any variable value except "on" interpreted as False.

testcookie_port_in_redirect

syntax: testcookie_port_in_redirect (on|off);

default: off

context: http, server, location

Expose port in redirect.

Installation

Grab the nginx source code from nginx.org, for example, the version 1.1.15 (see nginx compatibility), and then build the source with this module:

wget 'http://nginx.org/download/nginx-1.1.15.tar.gz'
tar -xzvf nginx-1.1.15.tar.gz
cd nginx-1.1.15/
./configure --add-module=/path/to/testcookie-nginx-module

make
make install

If you use nginx >= 1.9.11 you can compile Dynamic module.

wget 'http://nginx.org/download/nginx-1.9.11.tar.gz'
tar -xzvf nginx-1.9.11.tar.gz
cd nginx-1.9.11/
./configure --add-dynamic-module=/path/to/testcookie-nginx-module

make
make install

Then load "ngx_http_testcookie_access_module.so" using "load_module" directive.

For using client-side cookie decryption, you need to manually grab SlowAES JavaScript AES implementation, patch it(utils/aes.patch) and put it to document root.

Compatibility

Module was tested with nginx 1.1+, but should work with 1.0+.

Example configuration

http {
    #default config, module disabled
    testcookie off;

    #setting cookie name
    testcookie_name BPC;

    #setting secret
    testcookie_secret keepmesecret;

    #setting session key
    testcookie_session $remote_addr;

    #setting argument name
    testcookie_arg ckattempt;

    #setting maximum number of cookie setting attempts
    testcookie_max_attempts 3;

    #setting p3p policy
    testcookie_p3p 'CP="CUR ADM OUR NOR STA NID", policyref="/w3c/p3p.xml"';

    #setting fallback url
    testcookie_fallback http://google.com/cookies.html?backurl=http://$host$request_uri;

    #configuring whitelist
    testcookie_whitelist {
        8.8.8.8/32;
    }


    #setting redirect via html code
    testcookie_redirect_via_refresh on;

    #enable encryption
    testcookie_refresh_encrypt_cookie on;

    #setting encryption key
    testcookie_refresh_encrypt_cookie_key deadbeefdeadbeefdeadbeefdeadbeef;

    #setting encryption iv
    testcookie_refresh_encrypt_cookie_iv deadbeefdeadbeefdeadbeefdeadbeef;

    #setting response template
    testcookie_refresh_template '<html><body>setting cookie...<script type=\"text/javascript\" src=\"/aes.min.js\" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("$testcookie_enc_key"),b=toNumbers("$testcookie_enc_iv"),c=toNumbers("$testcookie_enc_set");document.cookie="BPC="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";location.href="$testcookie_nexturl";</script></body></html>';

    server {
        listen 80;
        server_name test.com;


        location = /aes.min.js {
            gzip  on;
            gzip_min_length 1000;
            gzip_types      text/plain;
            root /var/www/public_html;
        }

        location = /w3c/p3p.xml {
            root /var/www/public_html;
        }

        location / {
            #enable module for specific location
            testcookie on;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_pass http://127.0.0.1:80;
        }
    }
}

See more cases in "docs" directory of the project.

Test suite

This module comes with a Perl-driven test suite. Thanks to the Test::Nginx module in the Perl world.

Sources

Available on github at kyprizel/testcookie-nginx-module.

TODO

  • Code review
  • Statistics (?)

Bugs

Feel free to report bugs and send patches to [email protected] or using github's issue tracker.

Support the project

Send your donations to 1FHmPTP6aDBAzVtM7Pe7Y69zqhjPRx847s

Copyright & License

Copyright (C) 2011-2017 Eldar Zaitov ([email protected]).

All rights reserved.

This module is licenced under the terms of BSD license.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

*   Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*   Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*   Neither the name of the authors nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].