PhpIDS in ZendFramework
Autor: Norman Rauthe, Ralf v.d.Mark
eingetragen: Dienstag, 12. Juli 2011 um 15:28 Uhr (28/2011 Kalenderwoche)
geändert: Mittwoch, 13. Juni 2012 um 09:34 Uhr (24/2012 Kalenderwoche)
Keywords: ZEND phpids intrusion impact framework
Kategorien: PHP-ZF, PHP, IDS (PHP-ids),
Text:
Einbinden von PhpIDS in Zend-Framework
Im Quellcode-Bereich finden Sie:
1.) Ausschnitt der INI-Datei "IdsConfig.ini"
2.) Plugin_Ids.php (Komplett)
3.) Ausschnitt "ErrorController.php"
Download der Libery PhpIDS:
https://phpids.org/downloads/
Hier noch mal ein bisschen Lesestoff:
http://www.net-developers.de/blog/2009/05/23/phpids-intrusion-dete...
Quellcode:
START: Ausschnitt der IdsConfig.ini
###########################################
; Einstellungen in der PHPIDS Config.ini
[General]
...
;==========================================================================
;Setzen des AngriffsLevels und der Ausnahmefelder/-Controllern
;Grundsaetzlich alle Angriffe werden erst ab dieser Stufe behandelt
Set_Intrusion_Level = 3
;In folgenden Feldern wird immer reagiert
Set_Intrusion_Level_Ausnahme = username,password
;In folgenden Feldern wird nie reagiert
Set_Intrusion_Ausnahme_Felder = dummy1,dummy2
;In folgenden Controllern wird nie reagiert. Alles erlaubt - Vorsicht!!!
Set_Intrusion_Ausnahme_Controller = controller1,controller2
;==========================================================================
...
ENDE: Ausschnitt der IdsConfig.ini
###########################################
START: Plugin_Ids.php
<?php
/**
* Plugin zur Steuerung von PhpIDS
* http://phpids.org/downloads/
*
* @name Datei: Ids.php
* @abstract siehe oben
* @author Noman Rauthe (NR) <nr@zadi.de>, Ref. 621, BLE
* @author Ralf von der Mark (vdM) <vdM@zadi.de>, Ref. 621, BLE
* @copyright Copyright (c) 2011, BLE
* @version Version vom 12.07.2011
* @package Plugin
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
class Plugin_Ids extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
$config = Zend_Registry::get('CONFIG');
$tmpPath = substr($_SERVER['DOCUMENT_ROOT'],
0,
stripos($_SERVER['DOCUMENT_ROOT'],
$config->APPLICATION->PublicPath)
);
$init = IDS_Init::init($tmpPath.'application/config/IdsConfig.ini');
$tmpPath .= 'application/cache/IDS/';
$init->config['General']['tmp_path'] = $tmpPath;
//folgendes wird in der IDS-Config eingestellt:
$impactLevel = $init->config['General']['Set_Intrusion_Level'];
$intrLevelAusnahmeFelder = explode(',', $init->config['General']['Set_Intrusion_Level_Ausnahme']);
$intrusionAusnahmeFelder = explode(',', $init->config['General']['Set_Intrusion_Ausnahme_Felder']);
$intrusionAusnahmeController = explode(',', $init->config['General']['Set_Intrusion_Ausnahme_Controller']);
if (in_array($request->getControllerName(), $intrusionAusnahmeController)) {
//Keine Reaktion! - Hier ist alles erlaubt. - Vorsicht!!!!!!!!!!!!!
} else {
$requestArray = array(
'COOKIE' => $_COOKIE,
'ZendParam' => $request->getParams()
);
$ids = new IDS_Monitor($requestArray, $init);
$intrusionResult = $ids->run();
if (!$intrusionResult->isEmpty()) {
$ausgabeArray = array();//Starten des Ausgabe-Array
foreach ($intrusionResult as $feld) {
//$feld->getName() => Feld-Name mit der Problem-Variable (ZendParam.feldname)
//$feld->getValue() => der Wert der Problem-Variable
//$feld->getImpact() => festgestellter impactLevel;
//echo 'impactLevel('.$feld->getValue().'):'.$feld->getImpact();
//Manipulieren des Feldnamens (Schreibweise: "ZendParam.feldname")
$feldName = substr(strstr($feld->getName(), '.'), 1);
if (in_array($feldName, $intrusionAusnahmeFelder)) {
//keine Reaktion! - Hier ist alles erlaubt. - Vorsicht!
} elseif (!in_array($feldName, $intrLevelAusnahmeFelder)) {
if ($feld->getImpact() >= $impactLevel) {
$ausgabeArray[$feldName]['Level'] = $feld->getImpact();
$ausgabeArray[$feldName]['Wert'] = $feld->getValue();
$request->setActionName('intrusion');
$request->setControllerName('error');
}//ENDE: if ($feld->getImpact() >= $impactLevel) {
} else {
/* Die restl. Felder werden mit hoechster Sicherheitsstufe kontolliert */
$ausgabeArray[$feldName]['Level'] = $feld->getImpact();
$ausgabeArray[$feldName]['Wert'] = $feld->getValue();
$request->setActionName('intrusion');
$request->setControllerName('error');
}//ENDE: else ==> if (!in_array($feldName, $intrLevelAusna...
}//ENDE: foreach ($intrusionResult as $feld)
if (!empty($ausgabeArray)) {
$registry = Zend_Registry::getInstance();
$registry->set('intrusionMessage', $ausgabeArray);
}
}//ENDE: if (!$intrusionResult->isEmpty()) {
}//ENDE: else ==> if(if (Zend_Registry::get('CONTROLLERNAME') == 'error' && Ze...
}//ENDE: public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
}//ENDE: class Plugin_Ids extends Zend_Controller_P...
ENDE: Plugins fuer IDS "Plugin_Ids.php"
###########################################
START: Ausschnitt des ErrorController
<?php
/**
* ErrorController: Komplettes Errorhandling mit Versendung der
* Adminmail inkl. der ZEND-Fehlermeldung
*
* @name Datei: ErrorController.php
* @abstract siehe oben
* @copyright Copyright (c) 2011, BLE
* @version Version vom 12.07.2011
*
* @package Controller
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
class ErrorController extends Zend_Controller_Action {
public function intrusionAction()
{
if ($arrIntrusionMessage = Zend_Registry::get('intrusionMessage')) {
$intrusionMessage = '<div class="error">
<ul>';
$stringFunctions = new BleHP_UTF8_StringFunctions();
foreach ($arrIntrusionMessage as $key => $wert) {
$intrusionMessage .=
'<li><strong>Das Feld "'.$key.'" enthält Zeichen,
die nicht erlaubt sind!</strong>
<ul>
<li><strong>Hier Ihr eingegebener Wert:</strong>
<p>'.htmlentities(utf8_decode($wert['Wert'])).'</p>
<p><strong>Stufe '.$wert['Level'].' des IDS-Level.</strong></li>
</ul>
</li>'.PHP_EOL;
}//ENDE: foreach
}//ENDE: if ($arrIntrusionMessage = Zend_Registry::get('intrusionMessage'))
$intrusionMessage .= PHP_EOL.PHP_EOL
.'</ul>
</div><!-- div class="error" -->
<p><a href=""><<< Zurück</a></p>';
$this->errorMailSenden('PhpIDS: '.PHP_IDS_INTRUSION,
utf8_encode(utf8_encode(print_r($arrIntrusionMessage, true))),
(isset($_SESSION) ? print_r($_SESSION, true) : array()),
PHP_EOL.PHP_EOL.'POST:'.PHP_EOL
.print_r($_POST, true)
.PHP_EOL.PHP_EOL.'GET:'.PHP_EOL
.print_r($_GET, true)
.PHP_EOL.PHP_EOL.'REQUEST:'.PHP_EOL
.print_r($_REQUEST, true)
.PHP_EOL.PHP_EOL.'COOKIE:'.PHP_EOL
.print_r($_COOKIE, true)
.PHP_EOL.PHP_EOL.'SERVER:'.PHP_EOL
.print_r($_SERVER, true)
.PHP_EOL);
$this->view->title = 'Sie haben unerlaubte Zeichen eingegeben!';
$this->view->readyHtmlOutput = $intrusionMessage;
$this->renderScript('error/error.phtml');
}
START: Ausschnitt des ErrorController
###########################################