Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add responsive firewall / adbuseipdb integration plugin #4149

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
df2fe54
Add abuseipdb plugin
CRCinAU Jul 30, 2024
e910dc0
Update rc.d script and actions file
CRCinAU Jul 30, 2024
dbfa667
Fix rc.d location
CRCinAU Jul 30, 2024
b31143f
Update scripts
CRCinAU Jul 30, 2024
c86ad1c
Add more fields to the UI
CRCinAU Jul 30, 2024
2f88070
More updates
CRCinAU Jul 30, 2024
1c16971
Fix indenting
CRCinAU Jul 30, 2024
e75bbf8
Add inc file
CRCinAU Jul 30, 2024
bbaa4f9
More fixes...
CRCinAU Jul 30, 2024
d0e384b
Import config from OPNsense vars
CRCinAU Jul 30, 2024
6b17f64
Write PID to /var/run/abuseipdb.pid
CRCinAU Jul 30, 2024
9053259
Merge branch 'opnsense:master' into master
CRCinAU Aug 1, 2024
a6ed7af
Minor init script fixes
CRCinAU Aug 1, 2024
29a9d3c
Add filter_id to web UI and script
CRCinAU Aug 2, 2024
1dc4ba6
Update inc file to create firewall rule
CRCinAU Aug 2, 2024
c9ee2df
Fix pid file cleanup on exit
CRCinAU Aug 2, 2024
2dd1f52
Fix inc file
CRCinAU Aug 2, 2024
a1abe37
Remove unneeded template
CRCinAU Aug 2, 2024
cb6b733
Remove rc.d script and use configd instead
CRCinAU Aug 2, 2024
492a7b6
Move main daemon to a proper location
CRCinAU Aug 2, 2024
ce79bd4
Update rc.d file and references
CRCinAU Aug 2, 2024
73561a6
Move echo logs to syslog
CRCinAU Aug 3, 2024
ae87654
Add Log Files entry to menu
CRCinAU Aug 3, 2024
6fddc53
Rename main daemon script & hopefully fix syslog
CRCinAU Aug 3, 2024
e2f4e67
Fix initscript
CRCinAU Aug 3, 2024
d84d588
Actually open the syslog before we log to it
CRCinAU Aug 3, 2024
60a1657
Add more logging on adding IPs to blocklist
CRCinAU Aug 3, 2024
a599625
Rework daemon a little
CRCinAU Aug 3, 2024
8e5f818
Add option to toggle logging for auto-firewall rule
CRCinAU Aug 3, 2024
c5a3938
Fix API for stopping / starting service
CRCinAU Aug 3, 2024
8e3e663
Add some more logging on startup
CRCinAU Aug 3, 2024
0404367
Add startup hook
CRCinAU Aug 3, 2024
991e22e
Merge branch 'opnsense:master' into master
CRCinAU Aug 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions security/abuseipdb/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PLUGIN_NAME= abuseipdb
PLUGIN_VERSION= 0.1
PLUGIN_REVISION= 1
PLUGIN_COMMENT= Block hosts based on incoming rules
PLUGIN_MAINTAINER= netwiz@crc.id.au

.include "../../Mk/plugins.mk"
94 changes: 94 additions & 0 deletions security/abuseipdb/src/etc/inc/plugins.inc.d/abuseipdb.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
// kate: space-indent off; indent-width 4; mixedindent off; indent-mode cstyle;
/*
* Copyright (C) 2017 Michael Muenz <m.muenz@gmail.com>
* 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.
*/
use OPNsense\Core\Config;
use OPNsense\Firewall\Alias;
use OPNsense\Firewall\Plugin;

function add_alias_if_not_exist($name, $description)
{
$model = new Alias();

if ($model->getByName($name) != null) {
return;
}

$new_alias = $model->aliases->alias->Add();
$new_alias->name = $name;
$new_alias->description = $description;
$new_alias->type = 'external';
$model->serializeToConfig();
Config::getInstance()->save();
}

function abuseipdb_firewall(Plugin $fw)
{
global $config;
if (
isset($config['OPNsense']['abuseipdb']['general']['enabled']) &&
$config['OPNsense']['abuseipdb']['general']['enabled'] == 1
) {
add_alias_if_not_exist('abuseipdb', 'abuseipdb blocklist');
$fw->registerFilterRule(
1, /* priority */
array(
'ipprotocol' => 'inet46',
'descr' => 'abuseipdb blocklist',
'from' => '<abuseipdb>',
'direction' => 'in',
'type' => 'block',
'log' => $config['OPNsense']['abuseipdb']['general']['log_denies'],
'tag' => "",
'quick' => true
)
);
}
}

function abuseipdb_services()
{
global $config;
$services = array();

if (
isset($config['OPNsense']['abuseipdb']['general']['enabled']) &&
$config['OPNsense']['abuseipdb']['general']['enabled'] == 1
) {
$services[] = array(
'description' => 'abuseipdb Daemon',
'configd' => array(
'restart' => array('abuseipdb restart'),
'start' => array('abuseipdb start'),
'stop' => array('abuseipdb stop'),
),
'name' => 'abuseipdb',
'pidfile' => '/var/run/abuseipdb.pid'
);
}

return $services;
}
50 changes: 50 additions & 0 deletions security/abuseipdb/src/etc/rc.d/abuseipdb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/sh
# PROVIDE: abuseipdb
# REQUIRE: DAEMON NETWORKING syslogd

. /etc/rc.subr

name="abuseipdb"
rcvar="abuseipdb_enable"

load_rc_config $name

: "${abuseipdb_enable="NO"}"

start() {
/usr/local/opnsense/scripts/abuseipdb/abuseipdb &
exit $?
}

stop() {
read pid < /var/run/abuseipdb.pid
kill -TERM $pid
}

status() {
if [ -f /var/run/abuseipdb.pid ]; then
read pid < /var/run/abuseipdb.pid
ps $pid > /dev/null
if [ "$?" == "0" ]; then
echo "Process is running: $(ps $pid)"
exit 0
fi
fi
echo "Process is not running..."
}

case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
esac
3 changes: 3 additions & 0 deletions security/abuseipdb/src/etc/rc.syshook.d/start/99-abuseipdb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
# https://docs.opnsense.org/development/backend/autorun.html
/usr/local/etc/rc.d/abuseipdb start
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* Copyright (C) 2015 Deciso B.V.
*
* 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\abuseipdb\Api;

use OPNsense\Base\ApiMutableServiceControllerBase;
use OPNsense\Core\Config;

class ServiceController extends ApiMutableServiceControllerBase
{
protected static $internalServiceClass = '\OPNsense\abuseipdb\abuseipdb';
protected static $internalServiceTemplate = 'OPNsense/abuseipdb';
protected static $internalServiceEnabled = 'general.enabled';
protected static $internalServiceName = 'abuseipdb';

protected function reconfigureForceRestart()
{
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* Copyright (C) 2015-2019 Deciso B.V.
*
* 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\abuseipdb\Api;

use OPNsense\Base\ApiControllerBase;
use OPNsense\abuseipdb\abuseipdb;
use OPNsense\Core\Config;

/**
* Class SettingsController Handles settings related API actions for the abuseipdb module
* @package OPNsense\abuseipdb
*/
class SettingsController extends ApiControllerBase
{
/**
* retrieve abuseipdb general settings
* @return array general settings
* @throws \OPNsense\Base\ModelException
* @throws \ReflectionException
*/
public function getAction()
{
// define list of configurable settings
$result = array();
if ($this->request->isGet()) {
$mdlabuseipdb = new abuseipdb();
$result['abuseipdb'] = $mdlabuseipdb->getNodes();
}
return $result;
}

/**
* update abuseipdb settings
* @return array status
* @throws \OPNsense\Base\ModelException
* @throws \ReflectionException
*/
public function setAction()
{
$result = array("result" => "failed");
if ($this->request->isPost()) {
// load model and update with provided data
$mdlabuseipdb = new abuseipdb();
$mdlabuseipdb->setNodes($this->request->getPost("abuseipdb"));

// perform validation
$valMsgs = $mdlabuseipdb->performValidation();
foreach ($valMsgs as $field => $msg) {
if (!array_key_exists("validations", $result)) {
$result["validations"] = array();
}
$result["validations"]["abuseipdb." . $msg->getField()] = $msg->getMessage();
}

// serialize model to config and save
if ($valMsgs->count() == 0) {
$mdlabuseipdb->serializeToConfig();
Config::getInstance()->save();
$result["result"] = "saved";
}
}
return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* Copyright (C) 2015-2019 Deciso B.V.
*
* 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\abuseipdb\Api;

use OPNsense\Base\ApiMutableModelControllerBase;

/**
* a simplified settings controller for our abuseipdb app, uses our ApiMutableModelControllerBase type
* @package OPNsense\abuseipdb
*/
class SimplifiedSettingsController extends ApiMutableModelControllerBase
{
protected static $internalModelName = 'abuseipdb';
protected static $internalModelClass = 'OPNsense\abuseipdb\abuseipdb';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* Copyright (C) 2015 Deciso B.V.
*
* 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\abuseipdb;

/**
* Class IndexController
* @package OPNsense\abuseipdb
*/
class IndexController extends \OPNsense\Base\IndexController
{
public function indexAction()
{
// pick the template to serve to our users.
$this->view->pick('OPNsense/abuseipdb/index');
// fetch form data "general" in
$this->view->generalForm = $this->getForm("general");
}
}
Loading