Fixed Broken Shit

This commit is contained in:
2022-10-30 14:32:20 -07:00
parent 7335796263
commit 4dabf5a6bf
635 changed files with 74885 additions and 17688 deletions

View File

@ -0,0 +1,35 @@
YourlsBlacklistIPs
Plugin for Yourls allowing to blacklist IPs
This plugin is intended to be used with YOURLS (cf. http://yourls.org)
It has been tested on YOURLS v1.5 and v1.5.1
Current version is 1.3, updated on 18/09/2012
Contact : Ludo at Ludo.Boggio+GitHub@GMail.com
INSTALL :
- In /user/plugins, create a new folder named BlackListIP
- In this new directory, copy the plugin.php and ludo_blacklist_ip_Check_IP_Module.php files from this repository
- Go to the Plugins administration page and activate the plugin
You will see in the admin section a new admin page where you can add the IP addresses you want to blacklist.
Please enter one IP address per line. Other syntax should provide unexpected behaviours.
v1.0 : initialization
v1.1 : Add admin page
v1.2 : Add some checks on IP format and some warnings for use
v1.3 : Add several possibilities to provide IP ranges :
- A.B.C.D-X.Y.Z.T range : all IPs from A.B.C.D to X.Y.Z.T are blacklisted
- A.B.C.0 range : all IPs from A.B.C.0 to A.B.C.255 are blacklisted
- A.B.0.0 range : all IPs from A.B.0.0 to A.B.255.255 are blacklisted
- A.0.0.0 range : all IPs from A.0.0.0 to A.255.255.255 are blacklisted
- A.B.C.D/X.Y.Z.T : A.B.C.D is an IP address, X.Y.Z.T is a subnet mask, all IPs addresses corresponding to that IP and mask are blacklisted
- A.B.C.D/T, T between 0 TO 32 : CIDR notation.
For explanations, feel free to check http://en.wikipedia.org/wiki/IP_address .
Actual roadmap is empty, but I'm open to suggestions. Feel free to contact me.

View File

@ -0,0 +1,141 @@
<?php
function ludo_blacklist_ip_Analyze_IP ( $Input ) {
if ( strpos ( $Input , "/" ) !== FALSE ) { // Case input contain "/"
$Inputs = array_map ("trim", explode ( "/" , $Input ) );
if (ludo_blacklist_ip_Check_IP ( $Inputs[0] ) && ctype_digit ($Inputs[1]) && $Inputs[1] > 0 && $Inputs[1] <= 32) { // Cas CIDR
$Cible = ludo_blacklist_ip_CalculIPMask ( $Inputs[0] , ludo_blacklist_ip_MaskType2Mask ( $Inputs[1] ) ) ;
} elseif (ludo_blacklist_ip_Check_IP ( $Inputs[0] ) && ludo_blacklist_ip_Check_IP ($Inputs[1]) && ludo_blacklist_ip_Check_Mask ($Inputs[1]) ){ // Case IP/Mask
$Cible = ludo_blacklist_ip_CalculIPMask ( $Inputs[0] , $Inputs[1] ) ;
} else { // Contains "/" but invalid
$Cible="NULL";
}
}
elseif ( strpos ( $Input , "-" ) !== FALSE ) { // Case input contains "-"
$Inputs = array_map ("trim", explode ( "-" , $Input ) );
if ( ludo_blacklist_ip_Check_IP ( $Inputs[0] ) && ludo_blacklist_ip_Check_IP ( $Inputs[1] ) ) {
if ( $Inputs[0] < $Inputs[1] ) { // Check IP orders
$Cible = $Inputs[0] . "-" . $Inputs[1] ;
}
else { // If wrong order, reverse it
$Cible = $Inputs[1] . "-" . $Inputs[0] ;
}
} else { // Contains "-" but invalid
$Cible="NULL";
}
}
elseif (ludo_blacklist_ip_Check_IP ( $Input )) { // Case input is a single IP
$Inputs = array_map ("trim", explode ( "." , $Input ) );
if ( $Inputs[0] == 0 && $Inputs[1] == 0 && $Inputs[2] == 0 && $Inputs[3] == 0 ) { // Case 0.0.0.0
$Cible = "0.0.0.0-255.255.255.255" ;
}
elseif ( $Inputs[1] == 0 && $Inputs[2] == 0 && $Inputs[3] == 0 ) { // Case A.0.0.0
$Cible = $Inputs[0] . ".0.0.0-" . $Inputs[0] . ".255.255.255";
}
elseif ( $Inputs[2] == 0 && $Inputs[3] == 0 ) { // Case A.B.0.0
$Cible = $Inputs[0] . "." . $Inputs[1] . ".0.0-" . $Inputs[0] . "." . $Inputs[1] . ".255.255";
}
elseif ( $Inputs[3] == 0 ) { // Case A.B.C.0
$Cible = $Inputs[0] . "." . $Inputs[1] . "." . $Inputs[2] . ".0-" . $Inputs[0] . "." . $Inputs[1] . "." . $Inputs[2] . ".255";
}
else { // Case of a single IP address
$Cible = $Input . "-" . $Input ;
}
}
else { // Invalid IP address
$Cible="NULL";
}
return $Cible;
}
function ludo_blacklist_ip_Check_IP ( &$IP ) {
// Input : String of IP address
// Output : TRUE if string is a valid IP address
$IPs = array_map("ludo_blacklist_ip_IP_trim", explode ( "." , $IP ) ) ;
if (count ($IPs) != 4 ) return false ;
foreach ( $IPs as $value ) {
if ( $value < 0 || $value > 255 ) {
return false ;
}
}
$IP = implode ( ".", $IPs );
return true;
}
function ludo_blacklist_ip_IP_Trim ( $IP ) {
// Input : array of IP address with strings
// Output : array of the IP address with integer
return (int) ltrim ( trim ( $IP ) , "0" ) ;
}
function ludo_blacklist_ip_Check_Mask ( $Mask ) {
// Input : Mask to be checked, string
// Return OK if the Mask string is correct
$Masks = array_map("ludo_blacklist_ip_IP_trim", explode ( "." , $Mask ) ) ;
if (count ($Masks) != 4 ) return false ;
$OctetSignificatif = -1;
foreach ( $Masks as $key => $value ) {
if ( $value == 255 and $OctetSignificatif == -1 ) {
continue;
}
if ( $value == 255 and $OctetSignificatif != -1 ) {
return false ;
}
if ( $value >0 and $OctetSignificatif == -1 ) {
$OctetSignificatif = $key ;
continue;
}
if ( $value >0 and $OctetSignificatif != -1 ) {
return false ;
}
if ( $value == 0 and $OctetSignificatif == -1 and $Masks[$key-1] != 255) {
return false ;
}
if ( $value == 0 and $OctetSignificatif == -1 and $Masks[$key-1] == 255) {
$OctetSignificatif = $key;
continue ;
}
if ( $value == 0 and $OctetSignificatif != -1 ) {
continue ;
}
}
return (($OctetSignificatif != -1) and in_array ($Masks[$OctetSignificatif],array ("255","254","252","248","240","224","192","128","0")));
}
function ludo_blacklist_ip_MaskType2Mask ( $MaskType ) {
// Input : Integer value
// Output : Mask with $MaskType bit at 1, others at 0, string
for ($boucle = 0; $boucle < 4 ; $boucle++ ) {
if ( $MaskType > 8 ) $Masks[$boucle] = 255;
elseif ($MaskType <= 0 ) $Masks[$boucle] = 0;
else $Masks[$boucle] = bindec (str_repeat ( "1" , $MaskType ) . str_repeat ("0" , 8-$MaskType ) );
$MaskType -= 8;
}
return implode ( "." , $Masks);
}
function ludo_blacklist_ip_CalculIPMask ( $IP , $Mask ) {
// Input : IP address and Mask, strings
// Output a string $IPStart."-".$IPEnd for those IP and mask
$IPs = explode ( "." , $IP ) ;
$Masks = explode ( "." , $Mask ) ;
$OctetSignificatif = -1;
for ($boucle = 0 ; $boucle < sizeof ( $IPs ) ; $boucle++ ) {
$IP_Starts[$boucle] = (0+$IPs[$boucle]) & (0+$Masks[$boucle]) ;
if (($Masks[$boucle] < 255) && ( $OctetSignificatif == -1 ) ) {
$OctetSignificatif = $boucle;
}
}
for ($boucle = 0 ; $boucle < sizeof ( $IPs ) ; $boucle++ ) {
if ($boucle < $OctetSignificatif )
$IP_Ends[$boucle] = 0+$IPs[$boucle] ;
elseif ($boucle == $OctetSignificatif )
$IP_Ends[$boucle] = (0+$IPs[$boucle]) | ( 255-$Masks[$boucle] ) ;
else
$IP_Ends[$boucle] = 255;
}
return implode ( "." , $IP_Starts)."-".implode ( "." , $IP_Ends);
}
?>

View File

@ -0,0 +1,118 @@
<?php
/*
Plugin Name: BlackListIP
Plugin URI: https://github.com/LudoBoggio/YourlsBlackListIPs
Description: Plugin which block blacklisted IPs
Version: 1.3
Author: Ludo
Author URI: http://ludovic.boggio.fr
*/
// No direct call
if( !defined( 'YOURLS_ABSPATH' ) ) die();
include "ludo_blacklist_ip_Check_IP_Module.php";
// Hook the custom function into the 'pre_check_ip_flood' event
yourls_add_action( 'pre_check_ip_flood', 'ludo_blacklist_ip_root' );
// Hook the admin page into the 'plugins_loaded' event
yourls_add_action( 'plugins_loaded', 'ludo_blacklist_ip_add_page' );
// Get blacklisted IPs from YOURLS options feature and compare with current IP address
function ludo_blacklist_ip_root ( $args ) {
$IP = $args[0];
$Intervalle_IP = yourls_get_option ('ludo_blacklist_ip_liste');
$Intervalle_IP = ( $Intervalle_IP ) ? ( unserialize ( $Intervalle_IP ) ):((array)NULL);
foreach ( $Intervalle_IP as $value ) {
$IPs = explode ( "-" , $value );
if ( $IP >= $IPs[0] AND $IP <= $IPs[1]) {
// yourls_die ( "Your IP has been blacklisted.", "Black list",403);
echo "<center>Your IP has been blacklisted.</center>";
die();
}
}
}
// Add admin page
function ludo_blacklist_ip_add_page () {
yourls_register_plugin_page( 'ludo_blacklist_ip', 'Blacklist IPs', 'ludo_blacklist_ip_do_page' );
}
// Display admin page
function ludo_blacklist_ip_do_page () {
if( isset( $_POST['action'] ) && $_POST['action'] == 'blacklist_ip' ) {
ludo_blacklist_ip_process ();
} else {
ludo_blacklist_ip_form ();
}
}
// Display form to administrate blacklisted IPs list
function ludo_blacklist_ip_form () {
$nonce = yourls_create_nonce( 'blacklist_ip' ) ;
$liste_ip = yourls_get_option ('ludo_blacklist_ip_liste','Enter IP addresses here, one entry per line');
if ($liste_ip != 'Enter IP addresses here, one entry per line' )
$liste_ip_display = implode ( "\r\n" , unserialize ( $liste_ip ) );
else
$liste_ip_display=$liste_ip;
echo <<<HTML
<h2>BlackList IPs</h2>
<form method="post">
<input type="hidden" name="action" value="blacklist_ip" />
<input type="hidden" name="nonce" value="$nonce" />
<p>Blacklist following IPs (one range or IP per line, no wildcards allowed) :</p>
<p><textarea cols="50" rows="10" name="blacklist_form">$liste_ip_display</textarea></p>
<p><input type="submit" value="Save" /></p>
<p>I suggest to add here IPs that you saw adding bulk URL. It is your own responsibility to check the use of the IPs you block. WARNING : erroneous entries may create unexpected behaviours, please double-check before validation.</p>
<p>Examples :
<ul>
<li>10.0.0.1/24 : blacklist from 10.0.0.0 to 10.0.0.255 (CIDR notation).</li>
<li>192.168.1.2/255.255.255.128 : blacklist from 192.168.1.0 to 192.168.0.128.</li>
<li>192.168.1.12-192.168.1.59 : blacklist from 192.168.1.12 to 192.168.1.59.</li>
<li>192.168.0.0 : blacklist from 192.168.0.0 to 192.168.255.255</li>
<li>10.0.0.58 : blacklist only 10.0.0.58 IP address.</li>
</ul>
</p>
</form>
HTML;
}
// Update blacklisted IPs list
function ludo_blacklist_ip_process () {
// Check nonce
yourls_verify_nonce( 'blacklist_ip' ) ;
// Check if the answer is correct.
$IP_Form = explode ( "\r\n" , $_POST['blacklist_form'] ) ;
if (! is_array ($IP_Form) ) {
echo "Bad answer, Blacklist not updated";
die ();
}
$boucle = 0;
foreach ($IP_Form as $value) {
$Retour = ludo_blacklist_ip_Analyze_IP ( $value ) ;
if ( $Retour != "NULL" ) {
$IPList[$boucle++] = $Retour ;
}
}
// Update list
yourls_update_option ( 'ludo_blacklist_ip_liste', serialize ( $IPList ) );
echo "Black list updated. New blacklist is " ;
if ( count ( $IPList ) == 0 )
echo "empty.";
else {
echo ":<BR />";
foreach ($IPList as $value) echo $value."<BR />";
}
}