| 1 |
#!/usr/bin/php |
| 2 |
<?php |
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
/***************************** |
| 6 |
* |
| 7 |
* Juniper PHP Integration for Fastnetmon |
| 8 |
* |
| 9 |
* This script connect to Juniper Router and add or remove a blackhole's rule for the IP attack |
| 10 |
* |
| 11 |
* Author: Christian David <[email protected]> |
| 12 |
* |
| 13 |
* Credits for the Netconf API By Juniper/netconf-php <https://github.com/Juniper/netconf-php> |
| 14 |
* Script based on Mikrotik Plugin by Maximiliano Dobladez <[email protected]> |
| 15 |
* |
| 16 |
* Made based on a MX5 CLI and not tested yet, please feedback-us in Issues on github |
| 17 |
* |
| 18 |
* LICENSE: GPLv2 GNU GENERAL PUBLIC LICENSE |
| 19 |
* |
| 20 |
* |
| 21 |
* v1.0 - 5 Dec 18 - initial version |
| 22 |
******************************/ |
| 23 |
|
| 24 |
error_reporting(E_ALL); |
| 25 |
ini_set('display_errors', 'On'); |
| 26 |
|
| 27 |
define("_VER", '1.0'); |
| 28 |
|
| 29 |
/* NOTE: YOU NEED TO ENABLE NETCONF ON YOUR JUNIPER */ |
| 30 |
/* https://www.juniper.net/documentation/en_US/junos/topics/task/configuration/netconf-ssh-connection-establishing.html#task-netconf-service-over-ssh-enabling */ |
| 31 |
|
| 32 |
/* |
| 33 |
|
| 34 |
Example configuration file /etc/juniper_integration.json: |
| 35 |
|
| 36 |
{ |
| 37 |
"hostname": "10.0.0.1", |
| 38 |
"port": 880, |
| 39 |
"username": "user", |
| 40 |
"password": "password" |
| 41 |
} |
| 42 |
|
| 43 |
*/ |
| 44 |
|
| 45 |
$config_path = "/etc/juniper_integration.json"; |
| 46 |
|
| 47 |
if (!file_exists($config_path)) { |
| 48 |
$msg = "Configuration file not found: " . $config_path; |
| 49 |
_log($msg); |
| 50 |
echo $msg . "\n"; |
| 51 |
exit(1); |
| 52 |
} |
| 53 |
|
| 54 |
$cfg = json_decode(file_get_contents($config_path), true); |
| 55 |
|
| 56 |
if (!is_array($cfg)) { |
| 57 |
$msg = "Failed to parse configuration file: " . $config_path; |
| 58 |
_log($msg); |
| 59 |
echo $msg . "\n"; |
| 60 |
exit(1); |
| 61 |
} |
| 62 |
|
| 63 |
foreach (['hostname', 'port', 'username', 'password'] as $key) { |
| 64 |
if (empty($cfg[$key])) { |
| 65 |
$msg = "Missing required config key: " . $key; |
| 66 |
_log($msg); |
| 67 |
echo $msg . "\n"; |
| 68 |
exit(1); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
// help |
| 73 |
if ($argc > 1 && $argv[1] == "help") { |
| 74 |
$msg = "Juniper API Integration for FastNetMon - Ver: " . _VER; |
| 75 |
echo $msg; |
| 76 |
exit(1); |
| 77 |
} |
| 78 |
|
| 79 |
/* |
| 80 |
|
| 81 |
This script will get following params from FastNetMon: |
| 82 |
$1 client_ip_as_string |
| 83 |
$2 data_direction |
| 84 |
$3 pps_as_string |
| 85 |
$4 action (ban or unban) |
| 86 |
*/ |
| 87 |
|
| 88 |
// Ensure that we got all required arguments |
| 89 |
if ($argc <= 4) { |
| 90 |
$msg = "Juniper API Integration for FastNetMon - Ver: " . _VER . "\n"; |
| 91 |
$msg .= "missing arguments\n"; |
| 92 |
$msg .= "php fastnetmon_juniper.php [IP] [data_direction] [pps_as_string] [action]\n"; |
| 93 |
|
| 94 |
_log($msg); |
| 95 |
echo $msg; |
| 96 |
exit(1); |
| 97 |
} |
| 98 |
|
| 99 |
// IPv4 or IPv6 address of attack |
| 100 |
$IP_ATTACK = $argv[1]; |
| 101 |
if (filter_var($IP_ATTACK, FILTER_VALIDATE_IP) === false) { |
| 102 |
$msg = "Invalid IP address: " . $IP_ATTACK; |
| 103 |
_log($msg); |
| 104 |
echo $msg . "\n"; |
| 105 |
exit(1); |
| 106 |
} |
| 107 |
|
| 108 |
// incoming, outgoing or unknown |
| 109 |
$DIRECTION_ATTACK = $argv[2]; |
| 110 |
if (!in_array($DIRECTION_ATTACK, ['incoming', 'outgoing', 'unknown'], true)) { |
| 111 |
$msg = "Invalid direction: " . $DIRECTION_ATTACK . ". Must be incoming, outgoing, or unknown."; |
| 112 |
_log($msg); |
| 113 |
echo $msg . "\n"; |
| 114 |
exit(1); |
| 115 |
} |
| 116 |
|
| 117 |
// Power of attack in packets per second |
| 118 |
$POWER_ATTACK = $argv[3]; |
| 119 |
if (!ctype_digit($POWER_ATTACK)) { |
| 120 |
$msg = "Invalid pps value: " . $POWER_ATTACK . ". Must be a positive integer."; |
| 121 |
_log($msg); |
| 122 |
echo $msg . "\n"; |
| 123 |
exit(1); |
| 124 |
} |
| 125 |
|
| 126 |
// Action: ban or unban |
| 127 |
$ACTION_ATTACK = $argv[4]; |
| 128 |
if (!in_array($ACTION_ATTACK, ['ban', 'unban'], true)) { |
| 129 |
$msg = "Invalid action: " . $ACTION_ATTACK . ". Must be ban or unban."; |
| 130 |
_log($msg); |
| 131 |
echo $msg . "\n"; |
| 132 |
exit(1); |
| 133 |
} |
| 134 |
|
| 135 |
$netconf_path = __DIR__ . "/netconf/netconf/Device.php"; |
| 136 |
if (!file_exists($netconf_path)) { |
| 137 |
$msg = "Netconf library not found: " . $netconf_path; |
| 138 |
_log($msg); |
| 139 |
echo $msg . "\n"; |
| 140 |
exit(1); |
| 141 |
} |
| 142 |
|
| 143 |
if (!function_exists('expect_popen')) { |
| 144 |
$msg = "PHP expect extension is required but not installed. Install it with PECL: pecl install expect"; |
| 145 |
_log($msg); |
| 146 |
echo $msg . "\n"; |
| 147 |
exit(1); |
| 148 |
} |
| 149 |
|
| 150 |
set_include_path(get_include_path() . PATH_SEPARATOR . dirname($netconf_path)); |
| 151 |
require_once $netconf_path; |
| 152 |
$conn = new Device($cfg); |
| 153 |
|
| 154 |
$time_now = date("Y-m-d H:i:s", time()); |
| 155 |
|
| 156 |
if ($ACTION_ATTACK == 'ban') { |
| 157 |
try { |
| 158 |
$desc = 'FastNetMon Community: IP ' . $IP_ATTACK . ' blocked because ' . $DIRECTION_ATTACK . ' attack with power ' . $POWER_ATTACK . ' pps | at ' . $time_now; |
| 159 |
$conn->connect(); |
| 160 |
$locked = $conn->lock_config(); |
| 161 |
|
| 162 |
if ($locked) { |
| 163 |
// Community 65535:666 = BLACKHOLE |
| 164 |
$conn->load_set_configuration("set routing-options static route {$IP_ATTACK} community 65535:666 discard"); |
| 165 |
$conn->commit(); |
| 166 |
} |
| 167 |
$conn->unlock(); |
| 168 |
$conn->close(); |
| 169 |
_log($desc); |
| 170 |
echo $desc . "\n"; |
| 171 |
} catch (NetconfException $e) { |
| 172 |
$msg = "Couldn't connect to " . $cfg['hostname'] . " - " . $e->getMessage(); |
| 173 |
_log($msg); |
| 174 |
echo $msg . "\n"; |
| 175 |
exit(1); |
| 176 |
} catch (Exception $e) { |
| 177 |
$msg = "Ban failed for " . $IP_ATTACK . " - " . $e->getMessage(); |
| 178 |
_log($msg); |
| 179 |
echo $msg . "\n"; |
| 180 |
exit(1); |
| 181 |
} |
| 182 |
} elseif ($ACTION_ATTACK == 'unban') { |
| 183 |
try { |
| 184 |
$desc = 'FastNetMon Community: IP ' . $IP_ATTACK . ' remove from blacklist.'; |
| 185 |
$conn->connect(); |
| 186 |
$locked = $conn->lock_config(); |
| 187 |
if ($locked) { |
| 188 |
$conn->load_set_configuration("delete routing-options static route {$IP_ATTACK}/32"); |
| 189 |
$conn->commit(); |
| 190 |
} |
| 191 |
$conn->unlock(); |
| 192 |
$conn->close(); |
| 193 |
_log($desc); |
| 194 |
echo $desc . "\n"; |
| 195 |
} catch (NetconfException $e) { |
| 196 |
$msg = "Couldn't connect to " . $cfg['hostname'] . " - " . $e->getMessage(); |
| 197 |
_log($msg); |
| 198 |
echo $msg . "\n"; |
| 199 |
exit(1); |
| 200 |
} catch (Exception $e) { |
| 201 |
$msg = "Unban failed for " . $IP_ATTACK . " - " . $e->getMessage(); |
| 202 |
_log($msg); |
| 203 |
echo $msg . "\n"; |
| 204 |
exit(1); |
| 205 |
} |
| 206 |
} else { |
| 207 |
$msg = "Unknown action: " . $ACTION_ATTACK; |
| 208 |
_log($msg); |
| 209 |
echo $msg . "\n"; |
| 210 |
exit(1); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* [_log Write a log file] |
| 215 |
* @param string $msg text to log |
| 216 |
*/ |
| 217 |
function _log(string $msg): void |
| 218 |
{ |
| 219 |
$FILE_LOG_TMP = "/tmp/fastnetmon_api_juniper.log"; |
| 220 |
$line = date("D M j H:i:s T Y") . " - [FASTNETMON] - " . $msg . "\n"; |
| 221 |
file_put_contents($FILE_LOG_TMP, $line, FILE_APPEND); |
| 222 |
} |
| 223 |
|
| 224 |
?> |