All Projects → mariusbalcytis → gentle-force

mariusbalcytis / gentle-force

Licence: MIT license
Brute-force, error and request rate limiting

Programming Languages

PHP
23972 projects - #3 most used programming language
lua
6591 projects
Dockerfile
14818 projects

Projects that are alternatives of or similar to gentle-force

FireflySoft.RateLimit
It is a rate limiting library based on .Net standard.
Stars: ✭ 76 (+68.89%)
Mutual labels:  rate-limiting, leaky-bucket, rate-limit, token-bucket
adaptive throttler
manages multiple throttlers with ability to ramp up and down
Stars: ✭ 31 (-31.11%)
Mutual labels:  rate-limiting, rate-limit, rate-limiter
leaky-bucket
A tokio-based leaky bucket rate limiter
Stars: ✭ 32 (-28.89%)
Mutual labels:  leaky-bucket, rate-limiter, token-bucket
RateLimiter
简单限流算法实现
Stars: ✭ 22 (-51.11%)
Mutual labels:  leaky-bucket, rate-limiter, token-bucket
limitrr-php
Better PHP rate limiting using Redis.
Stars: ✭ 19 (-57.78%)
Mutual labels:  rate-limiting, brute-force, rate-limiter
phalcon-throttler
Phalcon Throttler is a Rate Limiter for the PHP Phalcon Framework.
Stars: ✭ 19 (-57.78%)
Mutual labels:  rate-limiting, rate-limiter
rush
rush.readthedocs.io/en/latest/
Stars: ✭ 42 (-6.67%)
Mutual labels:  rate-limiting, rate-limiter
aiolimiter
An efficient implementation of a rate limiter for asyncio.
Stars: ✭ 121 (+168.89%)
Mutual labels:  rate-limiting, leaky-bucket
Limitrr
Light NodeJS rate limiting and response delaying using Redis - including Express middleware.
Stars: ✭ 203 (+351.11%)
Mutual labels:  brute-force, rate-limiter
freebind
IPv4 and IPv6 address rate limiting evasion tool
Stars: ✭ 88 (+95.56%)
Mutual labels:  rate-limiting, rate-limit
zlimiter
A toolkit for rate limite,support memory and redis
Stars: ✭ 17 (-62.22%)
Mutual labels:  rate-limiting, rate-limiter
flaps
🛬 Modular rate limiting for PHP.
Stars: ✭ 64 (+42.22%)
Mutual labels:  rate-limiting, leaky-bucket
Gubernator
High Performance Rate Limiting MicroService and Library
Stars: ✭ 609 (+1253.33%)
Mutual labels:  rate-limiting, rate-limiter
Hammer
An Elixir rate-limiter with pluggable backends
Stars: ✭ 366 (+713.33%)
Mutual labels:  rate-limiting, rate-limiter
Redis Ratelimit
A fixed window rate limiter based on Redis
Stars: ✭ 15 (-66.67%)
Mutual labels:  rate-limiting, rate-limiter
Bottleneck
Job scheduler and rate limiter, supports Clustering
Stars: ✭ 1,113 (+2373.33%)
Mutual labels:  rate-limiting, rate-limiter
kong-scalable-rate-limiter
Kong plugin for Rate Limiting at high throughputs.
Stars: ✭ 19 (-57.78%)
Mutual labels:  rate-limiting, rate-limiter
Bucket4j
Java rate limiting library based on token/leaky-bucket algorithm.
Stars: ✭ 1,025 (+2177.78%)
Mutual labels:  rate-limiting, rate-limiter
Guzzle Advanced Throttle
A Guzzle middleware that can throttle requests according to (multiple) defined rules. It is also possible to define a caching strategy, e.g. get the response from cache when the rate limit is exceeded or always get a cached value to spare your rate limits. Using wildcards in host names is also supported.
Stars: ✭ 120 (+166.67%)
Mutual labels:  rate-limiting, rate-limiter
Mvcthrottle
ASP.NET MVC Throttling filter
Stars: ✭ 211 (+368.89%)
Mutual labels:  rate-limiting

Gentle-force: brute-force, error and request rate limiting

This is a library for rate-limiting both brute-force attempts (like invalid credentials) and ordinary requests.

Features

  • can be used to limit brute-force attempts;
  • can be used for request rate limiting;
  • uses leaky / token bucket algorithm. This means that user does not have to wait for next hour or day - additional attempts are possible as time goes by. This also means that requests does not come in big batches when every hour starts;
  • handles race-conditions. This is important for brute-force limiting. For example, if 1000 requests are issued at the same time to check same user's password, only configured number of attempts will be possible;
  • can have several limits configured for single use-case (for example maximum of 100 requests per minute and 200 per hour);
  • does not make assumptions about where and what it's used for - it can be used with user identifiers, API tokens, IP addresses or any other data to group usages.

Installation

composer require maba/gentle-force

Usage

<?php

use Maba\GentleForce\RateLimit\UsageRateLimit;
use Maba\GentleForce\RateLimitProvider;
use Maba\GentleForce\Throttler;
use Maba\GentleForce\Exception\RateLimitReachedException;

$rateLimitProvider = new RateLimitProvider();
$rateLimitProvider->registerRateLimits('credentials_error', [
    // allow 3 errors per hour; 2 additional errors if no errors were made during last hour
    (new UsageRateLimit(3, 3600))->setBucketedUsages(2),
    // allow 10 errors per day
    new UsageRateLimit(10, 3600 * 24),
]);
$rateLimitProvider->registerRateLimits('api_request', [
    // - allow 10 requests each minute;
    // - user can "save up" hour of usage if not using API.
    //   This means up to 610 requests at once, after that - 10 requests per minute,
    //   which could again save-up up to 610.
    (new UsageRateLimit(10, 60))->setBucketedPeriod(3600),
]);

$throttler = new Throttler(new \Predis\Client([
    'host' => '127.0.0.1',
]), $rateLimitProvider);

// rate limiting:
try {
    $result = $throttler->checkAndIncrease('api_request', $_SERVER['REMOTE_ADDR']);
    header('Requests-Available', $result->getUsagesAvailable());
    
} catch (RateLimitReachedException $exception) {
    header('Wait-For', $exception->getWaitForInSeconds(), 429);
    return;
}

// brute-force limiting:
try {
    // we must increase error count in-advance before even checking credentials
    // this avoids race-conditions with lots of requests
    $credentialsResult = $throttler->checkAndIncrease('credentials_error', $_POST['username']);
} catch (RateLimitReachedException $exception) {
    echo sprintf('Too much password tries for user. Please try after %s seconds', $exception->getWaitForInSeconds());
    return;
}

$credentialsValid = checkCredentials($_POST['username'], $_POST['password']);

if ($credentialsValid) {
    // as we've increased error count in advance, we need to decrease it if everything went fine
    $credentialsResult->decrease();
    
    // log user into system
}

Alternatives

Actually, there are quite many of them.

Unfortunately, as some provide additional features (like different storage methods: file, memcached etc.), none were found with these criteria:

  • usable for brute-forcing (only on errors), not for all requests;
  • abstract, so that limiting by user, IP and other identifiers would be possible;
  • rate limiting algorithm that would not block for too long for a legitimate user;
  • free of race-conditions where actual limit would not work correctly on high load.

Some of reviewed alternatives: RateLimitInterface, rate-limiter, LosRateLimit, Rate-limit, rate-limit, php-ratelimiter, tokenbucket, brute-force, LoginGateBundle, tresholds-governor, throttle, PeerjUserSecurityBundle, php-ratelimiter, RateLimitBundle, CybBotDetectBunble, CCDNUserSecurityBundle, limit-number-calls-bundle, rate-limiter-php, flaps, token-bucket

Semantic versioning

This library follows semantic versioning.

See Symfony BC rules for basic information about what can be changed and what not in the API.

Running tests

Travis status

Functional tests require Redis and several PHP extensions for forking, so that behaviour on high traffic could be tested. So, generally, it's easier to run them in docker.

composer update
cd docker
docker-compose up -d
docker exec -it gentle_force_test_php vendor/bin/phpunit
docker-compose down

Contributing

Feel free to create issues and give pull requests.

You can fix any code style issues using this command:

vendor/bin/php-cs-fixer fix --config=.php_cs
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].