TaloTheGuardian - PHP Sentinel

dkiller92

Nuovo Utente
5 Mar 2010
6
0
0
Ciao ragazzi, sto sviluppando un sentinel PHP cercando di mantenerlo nè troppo semplice nè troppo pesante (senza pannello ammininistrazione ecc...).
Dato che lo faccio più per me stesso, per imparare l'OOP e per passare un po' il tempo, non soffermatevi troppo sull'aspetto grafico, quello lo aggiusterò alla fine del progetto.

Quello che vi posto è solo la classe, il resto del codice è poca roba e comunque la rilascierò quando avrò terminato tutto.

Il sentinel comprende:
- Rilevamento di SQL Injection e XSS
- Logging degli attacchi su file HTML o TXT o via mail

Sono aperto a consigli e critiche di ogni genere, sempre che siano civili. :)

PS: NON necessita di database.


PHP:
<?php
//CLASS SENTINEL

/*Talo The Guardian v0.11 BETA
Coded by DKiller92
All rights reserved
This code is free but not editable
*/

class TaloTheGuardian {
	
	
	function secure() {
	echo "
	<html>
	<head><title>Talo The Guardian</title>
	</head>
	<body>
	<h1>Talo The Guardian</h1>
	<font size='4'>Your attempt has been detected and blocked by this protection system.</font>
	</body></html>
	";
	die;
	}
	
function report($attack_type, $variable_type, $variable_name, $variable_value) {
	include("config.php");
	
	if($html_log) {
    $this->html_log($attack_type, $variable_type, $variable_name, $variable_value);
	}
	elseif($txt_log) {
	$this->txt_log($attack_type, $variable_type, $variable_name, $variable_value);
	}
	
	if($mail_log) {
	$this->mail_log($attack_type, $variable_type, $variable_name, $variable_value);
	}
}


function html_log($attack_type, $variable_type, $variable_name, $variable_value) {
	
$fp = fopen("id.txt", "r");
$id = fread($fp, 4096);
$id = $id + 1;
fclose($fp);

$fp = fopen("id.txt", "w");
fwrite($fp, $id);
fclose($fp);
	
	$hour = date("G:i:s ");
	$date = date("d/m/Y");
$report = "
<html>
<head><title>Talo The Guardian | Attack Attempt Detected</title>
<link rel='stylesheet' href='style.css' type='text/css'>
</head>
<body>
<center><h1>Talo The Guardian</h1>
<h2>Attack Report</h2>
<br />
<font size='4'>Attack Type: <b><span class='report'>".$attack_type."</span></b><br />
Dangerous Variable: <span class='report'><b>".$variable_name."</b></span> (".$variable_type.")<br />
Dangerous String: <span class='report'><b>".htmlentities($variable_value)."</span></b><br />
Target Page: <span class='report'><b>".$_SERVER['PHP_SELF']."</span></b><br />
Date: <span class='report'><b>".$hour." (".$date.")</span></b><br />
Striker IP: <span class='report'><b>".$_SERVER['REMOTE_ADDR']."</span></b><br />
Useragent: <span class='report'><b>".$_SERVER['HTTP_USER_AGENT']."</span></b><br /><br />
Report ID: <i>".$id."</i>
</font>
</center></body></html>
";

$file = "logs/".$id.".html";
$fp = fopen($file, "w+");
fwrite($fp, $report);
fclose($fp);
}



function txt_log($attack_type, $variable_type, $variable_name, $variable_value) {
	
$fp = fopen("id.txt", "r");
$id = fread($fp, 4096);
$id = $id + 1;
fclose($fp);

$fp = fopen("id.txt", "w");
fwrite($fp, $id);
fclose($fp);
	
	$hour = date("G:i:s ");
	$date = date("d/m/Y");
$report = "
Talo The Guardian | Attack Attempt Detected \n
Attack Type: ".$attack_type."\n
Dangerous Variable: ".$variable_name." (".$variable_type.")\n
Dangerous String: ".$variable_value."\n
Target Page: ".$_SERVER['PHP_SELF']."\n
Date: ".$hour." (".$date.")\n
Striker IP: ".$_SERVER['REMOTE_ADDR']."\n
Useragent: ".$_SERVER['HTTP_USER_AGENT']."\n\n

Report ID: ".$id;

$file = "logs/".$id.".txt";
$fp = fopen($file, "w+");
fwrite($fp, $report);
fclose($fp);
}


function mail_log($attack_type, $variable_type, $variable_name, $variable_value) {
include("config.php");
$fp = fopen("id.txt", "r");
$id = fread($fp, 4096);
$id = $id + 1;
fclose($fp);

$fp = fopen("id.txt", "w");
fwrite($fp, $id);
fclose($fp);
	
	$hour = date("G:i:s ");
	$date = date("d/m/Y");
	
$report = "
Talo The Guardian | Attack Attempt Detected \n
Attack Type: ".$attack_type."\n
Dangerous Variable: ".$variable_name." (".$variable_type.")\n
Dangerous String: ".$variable_value."\n
Target Page: ".$_SERVER['PHP_SELF']."\n
Date: ".$hour." (".$date.")\n
Striker IP: ".$_SERVER['REMOTE_ADDR']."\n
Useragent: ".$_SERVER['HTTP_USER_AGENT']."\n\n

Report ID: ".$id;

mail($admin_mail, "Attack Detected - TaloTheGuardian", $report);
}


function CrossSiteScripting() {
  foreach($_POST as $key => $value) {
	 $value = urldecode($value);
    if(preg_match('/<+(script|iframe|frameset|img|body|input|link|style|meta|table|embed|object|div)+/i', $value)) {
	$this->report("XSS", "POST", $key, $value);
	$this->secure();
	}
  }
  foreach($_GET as $key => $value) {
	 $value = urldecode($value);
    if(preg_match('/<+(script|iframe|frameset|img|body|input|link|style|meta|table|embed|object|div)+/i', $value)) {
	$this->report("XSS", "GET", $key, $value);
	$this->secure();
	}
  }
}


function SqlInjection() {
  foreach($_POST as $key => $value) {
    if(preg_match('/(UNION)*.?(ALL)?.*(SELECT)+/i', $value) || preg_match('/\'?(OR|AND)+.?\'?[0-9]*\'?=+\'?[0-9, a-z, A-Z]*\'?/i', $value) || preg_match('/(HAVING)+.*[0-9, a-z, A-Z]*=+[0-9, a-z, A-Z]*/i', $value) || preg_match('/(ORDER)+.*(BY)+.*[0-9]+-+-+/i', $value)) {
	$this->report("SQL INJECTION", "POST", $key, $value);
	$this->secure();
	}
  }
  foreach($_GET as $key => $value) {
    if(preg_match('/(UNION)*.?(ALL)?.*(SELECT)+/i', $value) || preg_match('/\'?(OR|AND)+.?\'?[0-9]*\'?=+\'?[0-9, a-z, A-Z]*\'?/i', $value) || preg_match('/(HAVING)+.*[0-9, a-z, A-Z]*=+[0-9, a-z, A-Z]*/i', $value) || preg_match('/(ORDER)+.*(BY)+.*[0-9]+-+-+/i', $value)) {
	$this->report("SQL INJECTION", "GET", $key, $value);
	$this->secure();
	}
  }
 }
 

}//CLASS END

$class = new TaloTheGuardian();
$class->CrossSiteScripting();
$class->SqlInjection();
?>
 
Ultima modifica:
Un consiglio: perché invece di richiamare i metodi per la protezioni contro gli attacchi XSS e le SQL injection non li richiami nel costruttore della classe? Una cosa così:
PHP:
class TaloTheGuardian
{
    public function __construct()
    {
        $this->CrossSiteScripting();
        $this->SqlInjection();
    }
    
    // ...
}

// ...
 
un ulteriore modo di alleggerire la classe sarebbe quello di non utilizzare tag HTML all'interno delle funzioni, in particolare cerca di evitare attributi cone "font size" che sono ormai deprecati da anni
 

Discussioni simili