First commit
This commit is contained in:
commit
c6e2478c40
13918 changed files with 2303184 additions and 0 deletions
136
sites/all/modules/civicrm/packages/IDS/Log/Composite.php
Normal file
136
sites/all/modules/civicrm/packages/IDS/Log/Composite.php
Normal file
|
@ -0,0 +1,136 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* PHPIDS
|
||||
*
|
||||
* Requirements: PHP5, SimpleXML
|
||||
*
|
||||
* Copyright (c) 2008 PHPIDS group (https://phpids.org)
|
||||
*
|
||||
* PHPIDS is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* PHPIDS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with PHPIDS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* PHP version 5.1.6+
|
||||
*
|
||||
* @category Security
|
||||
* @package PHPIDS
|
||||
* @author Mario Heiderich <mario.heiderich@gmail.com>
|
||||
* @author Christian Matthies <ch0012@gmail.com>
|
||||
* @author Lars Strojny <lars@strojny.net>
|
||||
* @license http://www.gnu.org/licenses/lgpl.html LGPL
|
||||
* @link http://php-ids.org/
|
||||
*/
|
||||
|
||||
require_once 'IDS/Log/Interface.php';
|
||||
|
||||
/**
|
||||
* Log Composite
|
||||
*
|
||||
* This class implements the composite pattern to allow to work with multiple
|
||||
* logging wrappers at once.
|
||||
*
|
||||
* @category Security
|
||||
* @package PHPIDS
|
||||
* @author Christian Matthies <ch0012@gmail.com>
|
||||
* @author Mario Heiderich <mario.heiderich@gmail.com>
|
||||
* @author Lars Strojny <lars@strojny.net>
|
||||
* @copyright 2007-2009 The PHPIDS Group
|
||||
* @license http://www.gnu.org/licenses/lgpl.html LGPL
|
||||
* @version Release: $Id:Composite.php 517 2007-09-15 15:04:13Z mario $
|
||||
* @link http://php-ids.org/
|
||||
*/
|
||||
class IDS_Log_Composite
|
||||
{
|
||||
|
||||
/**
|
||||
* Holds registered logging wrapper
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $loggers = array();
|
||||
|
||||
/**
|
||||
* Iterates through registered loggers and executes them
|
||||
*
|
||||
* @param object $data IDS_Report object
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function execute(IDS_Report $data)
|
||||
{
|
||||
// make sure request uri is set right on IIS
|
||||
if (!isset($_SERVER['REQUEST_URI'])) {
|
||||
$_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'], 1);
|
||||
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
|
||||
$_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
|
||||
}
|
||||
}
|
||||
|
||||
// make sure server address is set right on IIS
|
||||
if (isset($_SERVER['LOCAL_ADDR'])) {
|
||||
$_SERVER['SERVER_ADDR'] = $_SERVER['LOCAL_ADDR'];
|
||||
}
|
||||
|
||||
foreach ($this->loggers as $logger) {
|
||||
$logger->execute($data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a new logging wrapper
|
||||
*
|
||||
* Only valid IDS_Log_Interface instances passed to this function will be
|
||||
* registered
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addLogger()
|
||||
{
|
||||
|
||||
$args = func_get_args();
|
||||
|
||||
foreach ($args as $class) {
|
||||
if (!in_array($class, $this->loggers) &&
|
||||
($class instanceof IDS_Log_Interface)) {
|
||||
$this->loggers[] = $class;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a logger
|
||||
*
|
||||
* @param object $logger IDS_Log_Interface object
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function removeLogger(IDS_Log_Interface $logger)
|
||||
{
|
||||
$key = array_search($logger, $this->loggers);
|
||||
|
||||
if (isset($this->loggers[$key])) {
|
||||
unset($this->loggers[$key]);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim600: sw=4 ts=4 expandtab
|
||||
*/
|
300
sites/all/modules/civicrm/packages/IDS/Log/Database.php
Normal file
300
sites/all/modules/civicrm/packages/IDS/Log/Database.php
Normal file
|
@ -0,0 +1,300 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* PHPIDS
|
||||
*
|
||||
* Requirements: PHP5, SimpleXML
|
||||
*
|
||||
* Copyright (c) 2008 PHPIDS group (https://phpids.org)
|
||||
*
|
||||
* PHPIDS is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* PHPIDS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with PHPIDS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* PHP version 5.1.6+
|
||||
*
|
||||
* @category Security
|
||||
* @package PHPIDS
|
||||
* @author Mario Heiderich <mario.heiderich@gmail.com>
|
||||
* @author Christian Matthies <ch0012@gmail.com>
|
||||
* @author Lars Strojny <lars@strojny.net>
|
||||
* @license http://www.gnu.org/licenses/lgpl.html LGPL
|
||||
* @link http://php-ids.org/
|
||||
*/
|
||||
|
||||
require_once 'IDS/Log/Interface.php';
|
||||
|
||||
/*
|
||||
* Needed SQL:
|
||||
*
|
||||
CREATE DATABASE IF NOT EXISTS `phpids` DEFAULT CHARACTER
|
||||
SET utf8 COLLATE utf8_general_ci;
|
||||
DROP TABLE IF EXISTS `intrusions`;
|
||||
CREATE TABLE IF NOT EXISTS `intrusions` (
|
||||
`id` int(11) unsigned NOT null auto_increment,
|
||||
`name` varchar(128) NOT null,
|
||||
`value` text NOT null,
|
||||
`page` varchar(255) NOT null,
|
||||
`tags` varchar(128) NOT null,
|
||||
`ip` varchar(15) NOT null,
|
||||
`ip2` varchar(15) NOT null,
|
||||
`impact` int(11) unsigned NOT null,
|
||||
`origin` varchar(15) NOT null,
|
||||
`created` datetime NOT null,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=MyISAM ;
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Database logging wrapper
|
||||
*
|
||||
* The database wrapper is designed to store reports into an sql database. It
|
||||
* implements the singleton pattern and is based in PDO, supporting
|
||||
* different database types.
|
||||
*
|
||||
* @category Security
|
||||
* @package PHPIDS
|
||||
* @author Christian Matthies <ch0012@gmail.com>
|
||||
* @author Mario Heiderich <mario.heiderich@gmail.com>
|
||||
* @author Lars Strojny <lars@strojny.net>
|
||||
* @copyright 2007-2009 The PHPIDS Group
|
||||
* @license http://www.gnu.org/licenses/lgpl.html LGPL
|
||||
* @version Release: $Id:Database.php 517 2007-09-15 15:04:13Z mario $
|
||||
* @link http://php-ids.org/
|
||||
*/
|
||||
class IDS_Log_Database implements IDS_Log_Interface
|
||||
{
|
||||
|
||||
/**
|
||||
* Database wrapper
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $wrapper = null;
|
||||
|
||||
/**
|
||||
* Database user
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $user = null;
|
||||
|
||||
/**
|
||||
* Database password
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $password = null;
|
||||
|
||||
/**
|
||||
* Database table
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $table = null;
|
||||
|
||||
/**
|
||||
* Database handle
|
||||
*
|
||||
* @var object PDO instance
|
||||
*/
|
||||
private $handle = null;
|
||||
|
||||
/**
|
||||
* Prepared SQL statement
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $statement = null;
|
||||
|
||||
/**
|
||||
* Holds current remote address
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $ip = 'local/unknown';
|
||||
|
||||
/**
|
||||
* Instance container
|
||||
*
|
||||
* Due to the singleton pattern this class allows to initiate only one instance
|
||||
* for each database wrapper.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $instances = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Prepares the SQL statement
|
||||
*
|
||||
* @param mixed $config IDS_Init instance | array
|
||||
*
|
||||
* @return void
|
||||
* @throws PDOException if a db error occurred
|
||||
*/
|
||||
protected function __construct($config)
|
||||
{
|
||||
|
||||
if ($config instanceof IDS_Init) {
|
||||
$this->wrapper = $config->config['Logging']['wrapper'];
|
||||
$this->user = $config->config['Logging']['user'];
|
||||
$this->password = $config->config['Logging']['password'];
|
||||
$this->table = $config->config['Logging']['table'];
|
||||
|
||||
} elseif (is_array($config)) {
|
||||
$this->wrapper = $config['wrapper'];
|
||||
$this->user = $config['user'];
|
||||
$this->password = $config['password'];
|
||||
$this->table = $config['table'];
|
||||
}
|
||||
|
||||
// determine correct IP address and concat them if necessary
|
||||
$this->ip = $_SERVER['REMOTE_ADDR'];
|
||||
$this->ip2 = isset($_SERVER['HTTP_X_FORWARDED_FOR'])
|
||||
? $_SERVER['HTTP_X_FORWARDED_FOR']
|
||||
: '';
|
||||
|
||||
try {
|
||||
$this->handle = new PDO(
|
||||
$this->wrapper,
|
||||
$this->user,
|
||||
$this->password
|
||||
);
|
||||
|
||||
$this->statement = $this->handle->prepare('
|
||||
INSERT INTO ' . $this->table . ' (
|
||||
name,
|
||||
value,
|
||||
page,
|
||||
tags,
|
||||
ip,
|
||||
ip2,
|
||||
impact,
|
||||
origin,
|
||||
created
|
||||
)
|
||||
VALUES (
|
||||
:name,
|
||||
:value,
|
||||
:page,
|
||||
:tags,
|
||||
:ip,
|
||||
:ip2,
|
||||
:impact,
|
||||
:origin,
|
||||
now()
|
||||
)
|
||||
');
|
||||
|
||||
} catch (PDOException $e) {
|
||||
throw new PDOException('PDOException: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of this class
|
||||
*
|
||||
* This method allows the passed argument to be either an instance of IDS_Init or
|
||||
* an array.
|
||||
*
|
||||
* @param mixed $config IDS_Init | array
|
||||
* @param string $classname the class name to use
|
||||
*
|
||||
* @return object $this
|
||||
*/
|
||||
public static function getInstance($config, $classname = 'IDS_Log_Database')
|
||||
{
|
||||
if ($config instanceof IDS_Init) {
|
||||
$wrapper = $config->config['Logging']['wrapper'];
|
||||
} elseif (is_array($config)) {
|
||||
$wrapper = $config['wrapper'];
|
||||
}
|
||||
|
||||
if (!isset(self::$instances[$wrapper])) {
|
||||
self::$instances[$wrapper] = new $classname($config);
|
||||
}
|
||||
|
||||
return self::$instances[$wrapper];
|
||||
}
|
||||
|
||||
/**
|
||||
* Permitting to clone this object
|
||||
*
|
||||
* For the sake of correctness of a singleton pattern, this is necessary
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function __clone()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores given data into the database
|
||||
*
|
||||
* @param object $data IDS_Report instance
|
||||
*
|
||||
* @throws Exception if db error occurred
|
||||
* @return boolean
|
||||
*/
|
||||
public function execute(IDS_Report $data)
|
||||
{
|
||||
if (!isset($_SERVER['REQUEST_URI'])) {
|
||||
$_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'], 1);
|
||||
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
|
||||
$_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($data as $event) {
|
||||
$page = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
|
||||
$ip = $this->ip;
|
||||
$ip2 = $this->ip2;
|
||||
|
||||
$name = $event->getName();
|
||||
$value = $event->getValue();
|
||||
$impact = $event->getImpact();
|
||||
$tags = implode(', ', $event->getTags());
|
||||
|
||||
$this->statement->bindParam('name', $name);
|
||||
$this->statement->bindParam('value', $value);
|
||||
$this->statement->bindParam('page', $page);
|
||||
$this->statement->bindParam('tags', $tags);
|
||||
$this->statement->bindParam('ip', $ip);
|
||||
$this->statement->bindParam('ip2', $ip2);
|
||||
$this->statement->bindParam('impact', $impact);
|
||||
$this->statement->bindParam('origin', $_SERVER['SERVER_ADDR']);
|
||||
|
||||
if (!$this->statement->execute()) {
|
||||
|
||||
$info = $this->statement->errorInfo();
|
||||
throw new Exception(
|
||||
$this->statement->errorCode() . ', ' . $info[1] . ', ' . $info[2]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim600: sw=4 ts=4 expandtab
|
||||
*/
|
400
sites/all/modules/civicrm/packages/IDS/Log/Email.php
Normal file
400
sites/all/modules/civicrm/packages/IDS/Log/Email.php
Normal file
|
@ -0,0 +1,400 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* PHPIDS
|
||||
*
|
||||
* Requirements: PHP5, SimpleXML
|
||||
*
|
||||
* Copyright (c) 2008 PHPIDS group (https://phpids.org)
|
||||
*
|
||||
* PHPIDS is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* PHPIDS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with PHPIDS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* PHP version 5.1.6+
|
||||
*
|
||||
* @category Security
|
||||
* @package PHPIDS
|
||||
* @author Mario Heiderich <mario.heiderich@gmail.com>
|
||||
* @author Christian Matthies <ch0012@gmail.com>
|
||||
* @author Lars Strojny <lars@strojny.net>
|
||||
* @license http://www.gnu.org/licenses/lgpl.html LGPL
|
||||
* @link http://php-ids.org/
|
||||
*/
|
||||
|
||||
require_once 'IDS/Log/Interface.php';
|
||||
|
||||
/**
|
||||
* Email logging wrapper
|
||||
*
|
||||
* The Email wrapper is designed to send reports via email. It implements the
|
||||
* singleton pattern.
|
||||
*
|
||||
* @category Security
|
||||
* @package PHPIDS
|
||||
* @author Christian Matthies <ch0012@gmail.com>
|
||||
* @author Mario Heiderich <mario.heiderich@gmail.com>
|
||||
* @author Lars Strojny <lars@strojny.net>
|
||||
* @copyright 2007-2009 The PHPIDS Group
|
||||
* @license http://www.gnu.org/licenses/lgpl.html LGPL
|
||||
* @version Release: $Id:Email.php 517 2007-09-15 15:04:13Z mario $
|
||||
* @link http://php-ids.org/
|
||||
*/
|
||||
class IDS_Log_Email implements IDS_Log_Interface
|
||||
{
|
||||
|
||||
/**
|
||||
* Recipient container
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $recipients = array();
|
||||
|
||||
/**
|
||||
* Mail subject
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $subject = null;
|
||||
|
||||
/**
|
||||
* Additional mail headers
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $headers = null;
|
||||
|
||||
/**
|
||||
* Safemode switch
|
||||
*
|
||||
* Using this switch it is possible to enable safemode, which is a spam
|
||||
* protection based on the alert frequency.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $safemode = true;
|
||||
|
||||
/**
|
||||
* Urlencode for result strings
|
||||
*
|
||||
* This switch is true by default. Setting it to false removes
|
||||
* the 'better safe than sorry' urlencoding for the result string in
|
||||
* the report mails. Enhances readability but maybe XSSes email clients.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $urlencode = true;
|
||||
|
||||
/**
|
||||
* Send rate
|
||||
*
|
||||
* If safemode is enabled, this property defines how often reports will be
|
||||
* sent out. Default value is 15, which means that a mail will be sent on
|
||||
* condition that the last email has not been sent earlier than 15 seconds ago.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
protected $allowed_rate = 15;
|
||||
|
||||
/**
|
||||
* PHPIDS temp directory
|
||||
*
|
||||
* When safemod is enabled, a path to a temp directory is needed to
|
||||
* store some information. Default is IDS/tmp/
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $tmp_path = 'IDS/tmp/';
|
||||
|
||||
/**
|
||||
* File prefix for tmp files
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $file_prefix = 'PHPIDS_Log_Email_';
|
||||
|
||||
/**
|
||||
* Holds current remote address
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $ip = 'local/unknown';
|
||||
|
||||
/**
|
||||
* Instance container
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $instance = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param mixed $config IDS_Init instance | array
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function __construct($config)
|
||||
{
|
||||
|
||||
if ($config instanceof IDS_Init) {
|
||||
$this->recipients = $config->config['Logging']['recipients'];
|
||||
$this->subject = $config->config['Logging']['subject'];
|
||||
$this->headers = $config->config['Logging']['header'];
|
||||
$this->envelope = $config->config['Logging']['envelope'];
|
||||
$this->safemode = $config->config['Logging']['safemode'];
|
||||
$this->urlencode = $config->config['Logging']['urlencode'];
|
||||
$this->allowed_rate = $config->config['Logging']['allowed_rate'];
|
||||
$this->tmp_path = $config->getBasePath()
|
||||
. $config->config['General']['tmp_path'];
|
||||
|
||||
} elseif (is_array($config)) {
|
||||
$this->recipients[] = $config['recipients'];
|
||||
$this->subject = $config['subject'];
|
||||
$this->additionalHeaders = $config['header'];
|
||||
}
|
||||
|
||||
// determine correct IP address and concat them if necessary
|
||||
$this->ip = $_SERVER['REMOTE_ADDR'] .
|
||||
(isset($_SERVER['HTTP_X_FORWARDED_FOR']) ?
|
||||
' (' . $_SERVER['HTTP_X_FORWARDED_FOR'] . ')' : '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of this class
|
||||
*
|
||||
* This method allows the passed argument to be either an instance of
|
||||
* IDS_Init or an array.
|
||||
*
|
||||
* @param mixed $config IDS_Init | array
|
||||
* @param string $classname the class name to use
|
||||
*
|
||||
* @return object $this
|
||||
*/
|
||||
public static function getInstance($config, $classname = 'IDS_Log_Email')
|
||||
{
|
||||
if (!self::$instance) {
|
||||
self::$instance = new $classname($config);
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permitting to clone this object
|
||||
*
|
||||
* For the sake of correctness of a singleton pattern, this is necessary
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function __clone()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Detects spam attempts
|
||||
*
|
||||
* To avoid mail spam through this logging class this function is used
|
||||
* to detect such attempts based on the alert frequency.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
protected function isSpamAttempt()
|
||||
{
|
||||
|
||||
/*
|
||||
* loop through all files in the tmp directory and
|
||||
* delete garbage files
|
||||
*/
|
||||
$dir = $this->tmp_path;
|
||||
$numPrefixChars = strlen($this->file_prefix);
|
||||
$files = scandir($dir);
|
||||
foreach ($files as $file) {
|
||||
if (is_file($dir . DIRECTORY_SEPARATOR . $file)) {
|
||||
if (substr($file, 0, $numPrefixChars) == $this->file_prefix) {
|
||||
$lastModified = filemtime($dir . DIRECTORY_SEPARATOR . $file);
|
||||
if ((time() - $lastModified) > 3600) {
|
||||
unlink($dir . DIRECTORY_SEPARATOR . $file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* end deleting garbage files
|
||||
*/
|
||||
$remoteAddr = $this->ip;
|
||||
$userAgent = $_SERVER['HTTP_USER_AGENT'];
|
||||
$filename = $this->file_prefix . md5($remoteAddr.$userAgent) . '.tmp';
|
||||
$file = $dir . DIRECTORY_SEPARATOR . $filename;
|
||||
|
||||
if (!file_exists($file)) {
|
||||
$handle = fopen($file, 'w');
|
||||
fwrite($handle, time());
|
||||
fclose($handle);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$lastAttack = file_get_contents($file);
|
||||
$difference = time() - $lastAttack;
|
||||
if ($difference > $this->allowed_rate) {
|
||||
unlink($file);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares data
|
||||
*
|
||||
* Converts given data into a format that can be read in an email.
|
||||
* You might edit this method to your requirements.
|
||||
*
|
||||
* @param mixed $data the report data
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function prepareData($data)
|
||||
{
|
||||
|
||||
$format = "The following attack has been detected by PHPIDS\n\n";
|
||||
$format .= "IP: %s \n";
|
||||
$format .= "Date: %s \n";
|
||||
$format .= "Impact: %d \n";
|
||||
$format .= "Affected tags: %s \n";
|
||||
|
||||
$attackedParameters = '';
|
||||
foreach ($data as $event) {
|
||||
$attackedParameters .= $event->getName() . '=' .
|
||||
((!isset($this->urlencode) ||$this->urlencode)
|
||||
? urlencode($event->getValue())
|
||||
: $event->getValue()) . ", ";
|
||||
}
|
||||
|
||||
$format .= "Affected parameters: %s \n";
|
||||
$format .= "Request URI: %s \n";
|
||||
$format .= "Origin: %s \n";
|
||||
|
||||
return sprintf($format,
|
||||
$this->ip,
|
||||
date('c'),
|
||||
$data->getImpact(),
|
||||
join(' ', $data->getTags()),
|
||||
trim($attackedParameters),
|
||||
htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES, 'UTF-8'),
|
||||
$_SERVER['SERVER_ADDR']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the report to registered recipients
|
||||
*
|
||||
* @param object $data IDS_Report instance
|
||||
*
|
||||
* @throws Exception if data is no string
|
||||
* @return boolean
|
||||
*/
|
||||
public function execute(IDS_Report $data)
|
||||
{
|
||||
|
||||
if ($this->safemode) {
|
||||
if ($this->isSpamAttempt()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* In case the data has been modified before it might
|
||||
* be necessary to convert it to string since it's pretty
|
||||
* senseless to send array or object via e-mail
|
||||
*/
|
||||
$data = $this->prepareData($data);
|
||||
|
||||
if (is_string($data)) {
|
||||
$data = trim($data);
|
||||
|
||||
// if headers are passed as array, we need to make a string of it
|
||||
if (is_array($this->headers)) {
|
||||
$headers = "";
|
||||
foreach ($this->headers as $header) {
|
||||
$headers .= $header . "\r\n";
|
||||
}
|
||||
} else {
|
||||
$headers = $this->headers;
|
||||
}
|
||||
|
||||
if (!empty($this->recipients)) {
|
||||
if (is_array($this->recipients)) {
|
||||
foreach ($this->recipients as $address) {
|
||||
$this->send(
|
||||
$address,
|
||||
$data,
|
||||
$headers,
|
||||
$this->envelope
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$this->send(
|
||||
$this->recipients,
|
||||
$data,
|
||||
$headers,
|
||||
$this->envelope
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new Exception(
|
||||
'Please make sure that data returned by
|
||||
IDS_Log_Email::prepareData() is a string.'
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an email
|
||||
*
|
||||
* @param string $address email address
|
||||
* @param string $data the report data
|
||||
* @param string $headers the mail headers
|
||||
* @param string $envelope the optional envelope string
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
protected function send($address, $data, $headers, $envelope = null)
|
||||
{
|
||||
if (!$envelope || strpos(ini_get('sendmail_path'),' -f') !== false) {
|
||||
return mail($address,
|
||||
$this->subject,
|
||||
$data,
|
||||
$headers);
|
||||
} else {
|
||||
return mail($address,
|
||||
$this->subject,
|
||||
$data,
|
||||
$headers,
|
||||
'-f' . $envelope);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim600: sw=4 ts=4 expandtab
|
||||
*/
|
229
sites/all/modules/civicrm/packages/IDS/Log/File.php
Normal file
229
sites/all/modules/civicrm/packages/IDS/Log/File.php
Normal file
|
@ -0,0 +1,229 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* PHPIDS
|
||||
*
|
||||
* Requirements: PHP5, SimpleXML
|
||||
*
|
||||
* Copyright (c) 2008 PHPIDS group (https://phpids.org)
|
||||
*
|
||||
* PHPIDS is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* PHPIDS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with PHPIDS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* PHP version 5.1.6+
|
||||
*
|
||||
* @category Security
|
||||
* @package PHPIDS
|
||||
* @author Mario Heiderich <mario.heiderich@gmail.com>
|
||||
* @author Christian Matthies <ch0012@gmail.com>
|
||||
* @author Lars Strojny <lars@strojny.net>
|
||||
* @license http://www.gnu.org/licenses/lgpl.html LGPL
|
||||
* @link http://php-ids.org/
|
||||
*/
|
||||
|
||||
require_once 'IDS/Log/Interface.php';
|
||||
|
||||
/**
|
||||
* File logging wrapper
|
||||
*
|
||||
* The file wrapper is designed to store data into a flatfile. It implements the
|
||||
* singleton pattern.
|
||||
*
|
||||
* @category Security
|
||||
* @package PHPIDS
|
||||
* @author Christian Matthies <ch0012@gmail.com>
|
||||
* @author Mario Heiderich <mario.heiderich@gmail.com>
|
||||
* @author Lars Strojny <lars@strojny.net>
|
||||
* @copyright 2007-2009 The PHPIDS Group
|
||||
* @license http://www.gnu.org/licenses/lgpl.html LGPL
|
||||
* @version Release: $Id:File.php 517 2007-09-15 15:04:13Z mario $
|
||||
* @link http://php-ids.org/
|
||||
*/
|
||||
class IDS_Log_File implements IDS_Log_Interface
|
||||
{
|
||||
|
||||
/**
|
||||
* Path to the log file
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $logfile = null;
|
||||
|
||||
/**
|
||||
* Instance container
|
||||
*
|
||||
* Due to the singleton pattern this class allows to initiate only one
|
||||
* instance for each file.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $instances = array();
|
||||
|
||||
/**
|
||||
* Holds current remote address
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $ip = 'local/unknown';
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $logfile path to the log file
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function __construct($logfile)
|
||||
{
|
||||
|
||||
// determine correct IP address and concat them if necessary
|
||||
$this->ip = $_SERVER['REMOTE_ADDR'] .
|
||||
(isset($_SERVER['HTTP_X_FORWARDED_FOR']) ?
|
||||
' (' . $_SERVER['HTTP_X_FORWARDED_FOR'] . ')' : '');
|
||||
|
||||
$this->logfile = $logfile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of this class
|
||||
*
|
||||
* This method allows the passed argument to be either an instance of
|
||||
* IDS_Init or a path to a log file. Due to the singleton pattern only one
|
||||
* instance for each file can be initiated.
|
||||
*
|
||||
* @param mixed $config IDS_Init or path to a file
|
||||
* @param string $classname the class name to use
|
||||
*
|
||||
* @return object $this
|
||||
*/
|
||||
public static function getInstance($config, $classname = 'IDS_Log_File')
|
||||
{
|
||||
if ($config instanceof IDS_Init) {
|
||||
$logfile = $config->getBasePath() . $config->config['Logging']['path'];
|
||||
} elseif (is_string($config)) {
|
||||
$logfile = $config;
|
||||
}
|
||||
|
||||
if (!isset(self::$instances[$logfile])) {
|
||||
self::$instances[$logfile] = new $classname($logfile);
|
||||
}
|
||||
|
||||
return self::$instances[$logfile];
|
||||
}
|
||||
|
||||
/**
|
||||
* Permitting to clone this object
|
||||
*
|
||||
* For the sake of correctness of a singleton pattern, this is necessary
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function __clone()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares data
|
||||
*
|
||||
* Converts given data into a format that can be stored into a file.
|
||||
* You might edit this method to your requirements.
|
||||
*
|
||||
* @param mixed $data incoming report data
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function prepareData($data)
|
||||
{
|
||||
|
||||
$format = '"%s",%s,%d,"%s","%s","%s","%s"';
|
||||
|
||||
$attackedParameters = '';
|
||||
foreach ($data as $event) {
|
||||
$attackedParameters .= $event->getName() . '=' .
|
||||
rawurlencode($event->getValue()) . ' ';
|
||||
}
|
||||
|
||||
$dataString = sprintf($format,
|
||||
urlencode($this->ip),
|
||||
date('c'),
|
||||
$data->getImpact(),
|
||||
join(' ', $data->getTags()),
|
||||
urlencode(trim($attackedParameters)),
|
||||
urlencode($_SERVER['REQUEST_URI']),
|
||||
$_SERVER['SERVER_ADDR']
|
||||
);
|
||||
|
||||
return $dataString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores given data into a file
|
||||
*
|
||||
* @param object $data IDS_Report
|
||||
*
|
||||
* @throws Exception if the logfile isn't writeable
|
||||
* @return boolean
|
||||
*/
|
||||
public function execute(IDS_Report $data)
|
||||
{
|
||||
|
||||
/*
|
||||
* In case the data has been modified before it might be necessary
|
||||
* to convert it to string since we can't store array or object
|
||||
* into a file
|
||||
*/
|
||||
$data = $this->prepareData($data);
|
||||
|
||||
if (is_string($data)) {
|
||||
|
||||
if (file_exists($this->logfile)) {
|
||||
$data = trim($data);
|
||||
|
||||
if (!empty($data)) {
|
||||
if (is_writable($this->logfile)) {
|
||||
|
||||
$handle = fopen($this->logfile, 'a');
|
||||
fwrite($handle, trim($data) . "\n");
|
||||
fclose($handle);
|
||||
|
||||
} else {
|
||||
throw new Exception(
|
||||
'Please make sure that ' . $this->logfile .
|
||||
' is writeable.'
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new Exception(
|
||||
'Given file does not exist. Please make sure the
|
||||
logfile is present in the given directory.'
|
||||
);
|
||||
}
|
||||
} else {
|
||||
throw new Exception(
|
||||
'Please make sure that data returned by
|
||||
IDS_Log_File::prepareData() is a string.'
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim600: sw=4 ts=4 expandtab
|
||||
*/
|
65
sites/all/modules/civicrm/packages/IDS/Log/Interface.php
Normal file
65
sites/all/modules/civicrm/packages/IDS/Log/Interface.php
Normal file
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* PHPIDS
|
||||
*
|
||||
* Requirements: PHP5, SimpleXML
|
||||
*
|
||||
* Copyright (c) 2008 PHPIDS group (https://phpids.org)
|
||||
*
|
||||
* PHPIDS is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* PHPIDS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with PHPIDS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* PHP version 5.1.6+
|
||||
*
|
||||
* @category Security
|
||||
* @package PHPIDS
|
||||
* @author Mario Heiderich <mario.heiderich@gmail.com>
|
||||
* @author Christian Matthies <ch0012@gmail.com>
|
||||
* @author Lars Strojny <lars@strojny.net>
|
||||
* @license http://www.gnu.org/licenses/lgpl.html LGPL
|
||||
* @link http://php-ids.org/
|
||||
*/
|
||||
|
||||
/**
|
||||
* Interface for logging wrappers
|
||||
*
|
||||
* @category Security
|
||||
* @package PHPIDS
|
||||
* @author Christian Matthies <ch0012@gmail.com>
|
||||
* @author Mario Heiderich <mario.heiderich@gmail.com>
|
||||
* @author Lars Strojny <lars@strojny.net>
|
||||
* @copyright 2007-2009 The PHPIDS Group
|
||||
* @version Release: $Id:Interface.php 517 2007-09-15 15:04:13Z mario $
|
||||
* @license http://www.gnu.org/licenses/lgpl.html LGPL
|
||||
* @link http://php-ids.org/
|
||||
*/
|
||||
interface IDS_Log_Interface
|
||||
{
|
||||
/**
|
||||
* Interface method
|
||||
*
|
||||
* @param IDS_Report $data the report data
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function execute(IDS_Report $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim600: sw=4 ts=4 expandtab
|
||||
*/
|
Loading…
Add table
Add a link
Reference in a new issue