Git Product home page Git Product logo

firewall's Introduction

Firewall Build Status

This PHP 5.4+ library provides IP filtering features.
A lot of filters can be used.
It is also possible to customize the error handling.

Installation

Add this line in your composer.json :

{
    "require": {
        "m6web/firewall": "dev-master"
    }
}

Update your vendors :

$ composer update m6web/firewall

Usage

Basic usage

use M6Web\Component\Firewall\Firewall;

$whiteList = array(
    '127.0.0.1',
    '192.168.0.*',
);

$blackList = array(
    '192.168.0.50',
);

$firewall = new Firewall();

$connAllowed = $firewall
    ->setDefaultState(false)
    ->addList($whiteList, 'local', true)
    ->addList($blackList, 'localBad', false)
    ->setIpAddress('195.88.195.146')
    ->handle()
;

if (!$connAllowed) {
    http_response_code(403); // Forbidden
    exit();
}

In this example, only IPs starting with 192.168.0 (but not 192.168.0.50) and 127.0.0.1 will be allowed by the firewall.
In all other case handle() return false.

  • setDefaultState(false) defines default firewall response (Optional - Default false),
  • addList($whiteList, 'local', true) defines $whiteList list, called local as allowed (true),
  • addList($blackList, 'localBad', false); defines $blackList list, called localBad as rejected (false).

Entries Formats

Type Syntax Details
IPV6 ::1 Short notation
IPV4 192.168.0.1
Range 192.168.0.0-192.168.1.60 Includes all IPs from 192.168.0.0 to 192.168.0.255
and from 192.168.1.0 to 198.168.1.60
Wild card 192.168.0.* IPs starting with 192.168.0
Same as IP Range 192.168.0.0-192.168.0.255
Subnet mask 192.168.0.0/255.255.255.0 IPs starting with 192.168.0
Same as 192.168.0.0-192.168.0.255 and 192.168.0.*
CIDR Mask 192.168.0.0/24 IPs starting with 192.168.0
Same as 192.168.0.0-192.168.0.255 and 192.168.0.*
and 192.168.0.0/255.255.255.0

Custom error handling

use M6Web\Component\Firewall\Firewall;

function handleFirewallReturn(Firewall $firewall, $response) {
    if (false === $response) {
        header($_SERVER["SERVER_PROTOCOL"]." 403 Forbiden");
        exit();
    }

    return $response;
}

$whiteList = array(
    '127.0.0.1',
    '198.168.0.*',
);

$blackList = array(
    '192.168.0.50',
);

$firewall = new Firewall();
$firewall
    ->setDefaultState(true)
    ->addList($whiteList, 'local', true)
    ->addList($blackList, 'localBad', false)
    ->setIpAddress('195.88.195.146')
    ->handle('handleFirewallReturn')
;

handle('handleFirewallReturn') calls handleFirewallReturn with Firewall object and response as arguments (true or false).

Running the tests

$ php composer.phar install --dev
$ ./vendor/bin/atoum -d Tests

Credits

Developped by the Cytron Team of M6 Web.
Tested with atoum.

License

Firewall is licensed under the MIT license.

firewall's People

Contributors

adel-e avatar adriensamson avatar b-viguier avatar dipston avatar divinity76 avatar fdubost avatar jubianchi avatar kuikui avatar omansour avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

firewall's Issues

x.y.z.t ip is not allowed even if we add x.y.z.t/32 as whitelist also broadcast ip is always allowed

For the network 20.21.22.22/32, the ip address 20.21.22.22 is not allowed but it should be allowed
the problem is in M6Web\Component\Firewall\Entry\Traits\IPMask in the function getRange
public function getRange()
{
...
if ($parts['mask'] == 32)
...
}

I have debuged this and $parts['mask'] contains "255.255.255.255" and not 32

the solution is to change the check to:
if ($parts['mask'] == 32 || $parts['mask'] == "255.255.255.255")

Another problem is that for network 20.21.22.0/24 , i think it shouldn't accept the ip 20.21.22.255 because it's broadcast ip address but it accept it.
Finally i think the solution is to change the getRange() function to:

public function getRange()
{
...
if ($parts['mask'] != 32 && $parts['mask'] != "255.255.255.255") {
$ret['begin'] = $this->IPLongAdd($ret['begin'], 1);
$ret['end'] = $this->IPLongAdd($ret['end'], -1);
}
...
}

because if the mask is not 255.255.255.255 or 32 we should prevent access for the network ip adress and broadcast ip address.

ip2long in 32 bit allow all Ip

hello

we have encountered an issue with your library.
On a 32bit installation when we try to run the following code. The Ip is always allowed.

use M6Web\Component\Firewall\Firewall;

$whiteList = array( 
    '10.68.112.0/24'
);

$firewall = new Firewall();

$connAllowed = $firewall
    ->setDefaultState(false)
    ->addList($whiteList, 'local', true)
    ->setIpAddress('10.68.20.205')
    ->handle()
;

if (!$connAllowed) {
    echo "Not allowed";
} else {
    echo "Allowed";
}

This is caused by Ip2Long that doesnt work properly on 32bit

Because PHP's integer type is signed, and many IP addresses will result in negative 
integers on 32-bit architectures, you need to use the "%u" formatter of sprintf() or printf() 
to get the string representation of the unsigned IP address.

To solve this issue we need to change in https://github.com/M6Web/Firewall/blob/master/Entry/AbstractIP.php#L68

    return strval(ip2long($long));

//By

    return  sprintf('%u', ip2long($long));

Feature Request: isValidIP function

How much trouble would it be to add an isValidIP function to the firewall to do return a boolean true or false before adding an IP to the white or black listings? I feel this could be pretty useful.

I would not mind adding it myself if yall can point me in the right direction?

long2ip() expects parameter 1 to be integer, string given

long2ip() expects parameter 1 to be integer, string given

protected function long2ip($long, $abbr = true) { switch(static::NB_BITS) { case 128: return $this->long2ip6($long, $abbr); default: return strval(long2ip($long)); } }

Example does not work.

So, I decided to test the example yall provided by

$whiteList = array(
        '127.0.0.1',
        '198.168.0.*',
    );

    $blackList = array(
        '192.168.0.50',
    );

    $firewall = new Firewall();

    $connAllowed = $firewall
        ->setDefaultState(false)
        ->addList($whiteList, 'local', true)
        ->addList($blackList, 'localBad', false)
        ->setIpAddress('192.168.0.25')
        ->handle();

This does not work. Although, it clearly should allow 192.168.0.25 through.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.