forked from civicrm/civicrm-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIDS.php
282 lines (257 loc) · 8.48 KB
/
IDS.php
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
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2016 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM 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 Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2016
*/
class CRM_Core_IDS {
/**
* Define the threshold for the ids reactions.
*/
private $threshold = array(
'log' => 25,
'warn' => 50,
'kick' => 75,
);
/**
* @var string
*/
private $path;
/**
* Check function.
*
* This function includes the IDS vendor parts and runs the
* detection routines on the request array.
*
* @param array $args
* List of path parts.
*
* @return bool
*/
public function check($args) {
// lets bypass a few civicrm urls from this check
$skip = array('civicrm/admin/setting/updateConfigBackend', 'civicrm/admin/messageTemplates');
CRM_Utils_Hook::idsException($skip);
$this->path = implode('/', $args);
if (in_array($this->path, $skip)) {
return NULL;
}
// Add request url and user agent.
$_REQUEST['IDS_request_uri'] = $_SERVER['REQUEST_URI'];
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$_REQUEST['IDS_user_agent'] = $_SERVER['HTTP_USER_AGENT'];
}
$configFile = self::createConfigFile(FALSE);
// init the PHPIDS and pass the REQUEST array
require_once 'IDS/Init.php';
try {
$init = IDS_Init::init($configFile);
$ids = new IDS_Monitor($_REQUEST, $init);
}
catch (Exception $e) {
// might be an old stale copy of Config.IDS.ini
// lets try to rebuild it again and see if it works
$configFile = self::createConfigFile(TRUE);
$init = IDS_Init::init($configFile);
$ids = new IDS_Monitor($_REQUEST, $init);
}
$result = $ids->run();
if (!$result->isEmpty()) {
$this->react($result);
}
return TRUE;
}
/**
* Create the default config file for the IDS system.
*
* @param bool $force
* Should we recreate it irrespective if it exists or not.
*
* @return string
* the full path to the config file
*/
public static function createConfigFile($force = FALSE) {
$config = CRM_Core_Config::singleton();
$configFile = $config->configAndLogDir . 'Config.IDS.ini';
if (!$force && file_exists($configFile)) {
return $configFile;
}
$tmpDir = empty($config->uploadDir) ? CIVICRM_TEMPLATE_COMPILEDIR : $config->uploadDir;
// also clear the stat cache in case we are upgrading
clearstatcache();
global $civicrm_root;
$contents = "
[General]
filter_type = xml
filter_path = {$civicrm_root}/packages/IDS/default_filter.xml
tmp_path = $tmpDir
HTML_Purifier_Path = IDS/vendors/htmlpurifier/HTMLPurifier.auto.php
HTML_Purifier_Cache = $tmpDir
scan_keys = false
exceptions[] = __utmz
exceptions[] = __utmc
exceptions[] = widget_code
exceptions[] = html_message
exceptions[] = text_message
exceptions[] = body_html
exceptions[] = msg_html
exceptions[] = msg_text
exceptions[] = msg_subject
exceptions[] = description
exceptions[] = intro
exceptions[] = thankyou_text
exceptions[] = intro_text
exceptions[] = body_text
exceptions[] = footer_text
exceptions[] = thankyou_text
exceptions[] = tf_thankyou_text
exceptions[] = thankyou_footer
exceptions[] = thankyou_footer_text
exceptions[] = new_text
exceptions[] = renewal_text
exceptions[] = help_pre
exceptions[] = help_post
exceptions[] = confirm_title
exceptions[] = confirm_text
exceptions[] = confirm_footer_text
exceptions[] = confirm_email_text
exceptions[] = report_header
exceptions[] = report_footer
exceptions[] = data
exceptions[] = json
exceptions[] = instructions
exceptions[] = suggested_message
exceptions[] = page_text
";
if (file_put_contents($configFile, $contents) === FALSE) {
CRM_Core_Error::movedSiteError($configFile);
}
// also create the .htaccess file so we prevent the reading of the log and ini files
// via a browser, CRM-3875
CRM_Utils_File::restrictAccess($config->configAndLogDir);
return $configFile;
}
/**
* This function reacts on the values in the incoming results array.
*
* Depending on the impact value certain actions are
* performed.
*
* @param IDS_Report $result
*
* @return bool
*/
private function react(IDS_Report $result) {
$impact = $result->getImpact();
if ($impact >= $this->threshold['kick']) {
$this->log($result, 3, $impact);
$this->kick();
return TRUE;
}
elseif ($impact >= $this->threshold['warn']) {
$this->log($result, 2, $impact);
$this->warn($result);
return TRUE;
}
elseif ($impact >= $this->threshold['log']) {
$this->log($result, 0, $impact);
return TRUE;
}
else {
return TRUE;
}
}
/**
* This function writes an entry about the intrusion to the database.
*
* @param array $result
* @param int $reaction
*
* @return bool
*/
private function log($result, $reaction = 0) {
$ip = (isset($_SERVER['SERVER_ADDR']) &&
$_SERVER['SERVER_ADDR'] != '127.0.0.1') ? $_SERVER['SERVER_ADDR'] : (
isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : '127.0.0.1'
);
$data = array();
$session = CRM_Core_Session::singleton();
foreach ($result as $event) {
$data[] = array(
'name' => $event->getName(),
'value' => stripslashes($event->getValue()),
'page' => $_SERVER['REQUEST_URI'],
'userid' => $session->get('userID'),
'session' => session_id() ? session_id() : '0',
'ip' => $ip,
'reaction' => $reaction,
'impact' => $result->getImpact(),
);
}
CRM_Core_Error::debug_var('IDS Detector Details', $data);
return TRUE;
}
/**
* Warn about IDS.
*
* @param array $result
*
* @return array
*/
private function warn($result) {
return $result;
}
/**
* Create an error that prevents the user from continuing.
*
* @throws \Exception
*/
private function kick() {
$session = CRM_Core_Session::singleton();
$session->reset(2);
$msg = ts('There is a validation error with your HTML input. Your activity is a bit suspicious, hence aborting');
if (in_array(
$this->path,
array("civicrm/ajax/rest", "civicrm/api/json")
)) {
require_once "api/v3/utils.php";
$error = civicrm_api3_create_error(
$msg,
array(
'IP' => $_SERVER['REMOTE_ADDR'],
'error_code' => 'IDS_KICK',
'level' => 'security',
'referer' => $_SERVER['HTTP_REFERER'],
'reason' => 'XSS suspected',
)
);
CRM_Utils_JSON::output($error);
}
CRM_Core_Error::fatal($msg);
}
}