Skip to content

Instantly share code, notes, and snippets.

@uakfdotb
Last active September 13, 2021 01:38
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save uakfdotb/ff3abac9bf663d84ca3b to your computer and use it in GitHub Desktop.
Save uakfdotb/ff3abac9bf663d84ca3b to your computer and use it in GitHub Desktop.
<?php
// https://lunanode.com/
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
function println($s) {
echo "$s\n";
}
function ipInRanges($ip, $ranges) {
foreach($ranges as $range) {
if(ip2long($ip) >= ip2long($range[0]) && ip2long($ip) <= ip2long($range[1])) {
return true;
}
}
return false;
}
$lastPackets = false;
$sleepInterval = 5;
$blockableRanges = array(array('1.1.1.0', '1.1.1.255'));
$interface = 'eth0';
$ppsTrigger = 60000;
$ppsCritical = 85000;
while(true) {
sleep($sleepInterval);
$lines = explode("\n", shell_exec('ifconfig ' . escapeshellarg($interface)));
$txLine = false;
foreach($lines as $line) {
if(strpos($line, 'TX packets:') !== false && strpos($line, 'errors:') !== false) {
$txLine = $line;
}
}
if($txLine === false) {
println("[protect] no tx packets line found");
$lastPackets = false;
continue;
}
$newPackets = trim(explode('errors:', explode('TX packets:', $txLine)[1])[0]);
if($lastPackets === false || $newPackets < $lastPackets) {
$lastPackets = $newPackets;
println("[protect] initializing tx packet counter to $lastPackets");
continue;
}
$packetsPerSecond = ($newPackets - $lastPackets) / floatval($sleepInterval);
$lastPackets = $newPackets;
if($packetsPerSecond < $ppsTrigger) {
println("[protect] good, only $packetsPerSecond packets per second");
continue;
}
//run tcpdump
println("[protect] got $packetsPerSecond packets per second! running tcpdump");
$pid = exec('/usr/sbin/tcpdump -n -c 5000 -i ' . escapeshellarg($interface) . ' 2>/dev/null > dump.txt & echo $!;');
if(empty($pid)) {
println("[protect] empty PID after running tcpdump?");
continue;
}
println("[protect] tcpdump started as $pid, taking five");
sleep(5);
println("[protect] killing tcpdump");
exec('kill -s INT ' . $pid);
//find packet transmit counts
$lines = explode("\n", file_get_contents('dump.txt'));
$accounting = array();
$minTime = false;
$maxTime = false;
foreach($lines as $line) {
if(strpos($line, ' IP ') !== false && strpos($line, ' > ') !== false) {
$source_ip = explode(' > ', explode(' IP ', $line)[1])[0];
$source_ip = substr($source_ip, 0, strrpos($source_ip, '.'));
if(ipInRanges($source_ip, $blockableRanges)) {
$packet_weight = 1;
if(!isset($accounting[$source_ip])) {
$accounting[$source_ip] = $packet_weight;
} else {
$accounting[$source_ip] += $packet_weight;
}
$time = explode(' ', $line)[0];
$time = floatval(substr($time, strrpos($time, ':') + 1));
if($minTime === false || $time < $minTime) $minTime = $time;
if($maxTime === false || $time > $maxTime) $maxTime = $time;
}
}
}
//find highest count
$maxAddress = false;
foreach($accounting as $address => $count) {
if($maxAddress === false || $count > $accounting[$maxAddress]) {
$maxAddress = $address;
}
}
$duration = $maxTime - $minTime;
println("[protect] captured " . count($lines) . " packets over " . number_format($duration, 5) . " sec");
//finish
$rate = $accounting[$maxAddress] / $duration;
println("[protect] found $rate packets per second from $maxAddress");
if($rate > $ppsCritical) {
// email yourself
// shutdown the instance at $maxAddress
sleep(20);
} else if($rate > $ppsTrigger) {
// email yourself
sleep(20);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment