www/nginx: Naxsi whitelist improvements (#2156)

This commit is contained in:
Manuel Faux
2021-04-10 10:42:10 +02:00
committed by GitHub
parent 49fce40ef7
commit 69a9a1d454
4 changed files with 124 additions and 12 deletions
@@ -19,19 +19,23 @@
<field>
<id>naxsi_rule.identifier</id>
<label>ID</label>
<type>text</type>
<help>The unique numerical ID of the rule, that will be used in logs and in whitelists. IDs inferior to 1000 are reserved for Naxsi internal rules (protocol mismatch etc.)</help>
<type>select_multiple</type>
<allownew>true</allownew>
<style>tokenize</style>
<type>select_multiple</type>
<help>For blacklist rules, the unique numerical ID of the rule, that will be used in logs and in whitelists. IDs inferior to 1000 are reserved for Naxsi internal rules (protocol mismatch etc.)&lt;br&gt;For whitelists, specify one or more comma separated IDs to whitelist. 0 whitelists all rules.</help>
</field>
<field>
<id>naxsi_rule.ruletype</id>
<label>Rule Type</label>
<type>dropdown</type>
<help>Main rules increase the policiy's score if matched. Basic rules are added directly to the location and match immeditaly. Basic rules are specifically useful for whitelist rules. Also basic rules need to be added to a policy to allow association with a location.</help>
</field>
<field>
<id>naxsi_rule.regex</id>
<label>Use Regular Expressions</label>
<type>checkbox</type>
<help>If enabled, the match value, the URL, named parameters and headers are matched using regular expressions; otherwise only exact matches trigger the rule.</help>
<help>If enabled, the match value, the URL, named parameters and headers are matched using regular expressions; otherwise only exact matches trigger the rule.</help>
</field>
<field>
<id>naxsi_rule.match_value</id>
@@ -43,12 +47,13 @@
<label>Match Type</label>
<type>dropdown</type>
<style>selectpicker</style>
<help>A blacklist rule increases the policy's score if the pattern matches. A whitelist rule instructs Naxsi to ignore specific rules under specific conditions.</help>
</field>
<field>
<id>naxsi_rule.args</id>
<label>Search in any GET Argument</label>
<type>checkbox</type>
<help>Search for matchs in a request's GET arguments.</help>
<help>Search for matchs in a request's GET arguments.</help>
</field>
<field>
<id>naxsi_rule.url</id>
@@ -60,7 +65,7 @@
<id>naxsi_rule.headers</id>
<label>Search in any HTTP Header</label>
<type>checkbox</type>
<help>Search for matchs in a request's HTTP headers.</help>
<help>Search for matchs in a request's HTTP headers.</help>
</field>
<field>
<id>naxsi_rule.body</id>
@@ -0,0 +1,98 @@
<?php
/*
* Copyright (C) 2020 Manuel Faux
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED ``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 THE
* AUTHOR 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.
*/
namespace OPNsense\Base\Constraints;
use Phalcon\Validation\Message;
/**
* a very specific nginx check for Naxsi rule IDs - not reusable
*
* Class NaxsiIdentifierConstraint
* @package OPNsense\Nginx\Constraints
*/
class NaxsiIdentifierConstraint extends BaseConstraint
{
public function validate(\Phalcon\Validation $validator, $attribute)
{
$node = $this->getOption('node');
if ($node) {
$parentNode = $node->getParentNode();
// Validate whitelist IDs
if ($parentNode->match_type == 'wl') {
$vals = explode(",", (string)$node); // Whitelists can use several IDs
$pos = 0;
$neg = 0;
// Check each ID
foreach ($vals as $val) {
$intval = intval($val);
if (!is_numeric($val)) {
$validator->appendMessage(new Message(gettext("All rule IDs need to be numeric."), $attribute));
}
// 0 can only be used solely
elseif ($intval == 0 && count($vals) > 1) {
$validator->appendMessage(new Message(gettext("If ID 0 is specified, no other IDs can be listed."), $attribute));
}
elseif ($intval < 0) {
$neg++;
}
elseif ($intval > 0) {
$pos++;
}
}
// All IDs need to be positive or all negative
if ($neg > 0 && $pos > 0) {
$validator->appendMessage(new Message(gettext("Negative and positive IDs cannot be mixed."), $attribute));
}
}
// Validate rule IDs
else {
$val = (string)$node;
if (!is_numeric($val)) {
// Did the user try to specify multiple IDs?
if (strpos($val, ',')) {
$validator->appendMessage(new Message(gettext("Rules can only have a single ID."), $attribute));
}
else {
$validator->appendMessage(new Message(gettext("Rule IDs need to be numeric."), $attribute));
}
}
// Check that no internal ID was used
elseif (intval($val) < 1000) {
$validator->appendMessage(new Message(gettext("Rule IDs lower than 1000 are reserved for internal rules."), $attribute));
}
}
}
return true;
}
}
@@ -1,6 +1,6 @@
<model>
<mount>//OPNsense/Nginx</mount>
<version>1.20.0</version>
<version>1.20.1</version>
<description>nginx web server, reverse proxy and waf</description>
<items>
<general>
@@ -530,9 +530,13 @@
</check001>
</Constraints>
</message>
<identifier type="IntegerField">
<identifier type="CSVListField">
<Required>Y</Required>
<minValue>1000</minValue>
<Constraints>
<check001>
<type>NaxsiIdentifierConstraint</type>
</check001>
</Constraints>
</identifier>
<dollar_url type="TextField">
<Required>N</Required>
@@ -557,6 +561,11 @@
<id>Blacklist</id>
<wl>Whitelist</wl>
</OptionValues>
<Constraints>
<check001>
<reference>identifier.check001</reference>
</check001>
</Constraints>
</match_type>
<negate type="BooleanField">
<Required>Y</Required>
@@ -45,13 +45,13 @@
{{ mz_matches|join('|') }}
{%- endmacro %}
{% macro naxsi_rule(uuid, rule, ruletype) -%}
{% if rule.message is defined and rule.match_value is defined %}
{% if rule.match_type == 'id' %}
{{ ruletype }}{% if rule.negate is defined and rule.negate == '1' %} negative{% endif
%} {{ rule.match_type }}:{{ rule.identifier }} "{% if rule.regex == '1' %}rx{% else %}str{% endif
%} id:{{ rule.identifier }} "{% if rule.regex == '1' %}rx{% else %}str{% endif
%}:{{ rule.match_value }}" "msg:{{ rule.message }}" "mz:{{ naxsi_mzhelper(rule)
}}" "s:$policy{{ uuid.replace('-', '') }}:{{ rule.score }}";
{% else %}
{{ ruletype }} {{ rule.match_type }}:{{ rule.identifier }};
{% elif rule.match_type == 'wl' %}
{{ ruletype }} wl:{{ rule.identifier }}{% if naxsi_mzhelper(rule) != '' %} "mz:{{ naxsi_mzhelper(rule) }}"{% endif %};
{% endif %}
{%- endmacro %}
{% if naxsi_ruletype == 'basic' %}