-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDatabase.php
More file actions
285 lines (255 loc) · 7.41 KB
/
Database.php
File metadata and controls
285 lines (255 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
* PHPIDS
*
* Requirements: PHP5, SimpleXML
*
* Copyright (c) 2008 PHPIDS group (http://php-ids.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,
`ip` 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 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
*/
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
if ($_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
$this->ip = $_SERVER['REMOTE_ADDR'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$this->ip = $_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,
ip,
impact,
origin,
created
)
VALUES (
:name,
:value,
:page,
:ip,
:impact,
:origin,
now()
)
');
} catch (PDOException $e) {
die('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
*
* @return object $this
*/
public static function getInstance($config)
{
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 IDS_Log_Database($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;
$this->statement->bindParam('name', $event->getName());
$this->statement->bindParam('value', $event->getValue());
$this->statement->bindParam('page', $page);
$this->statement->bindParam('ip', $ip);
$this->statement->bindParam('impact', $data->getImpact());
$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
*/