Merge pull request #10549 from jitendrapurohit/CRM-20761
[civicrm-core.git] / CRM / Core / IDS.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33class CRM_Core_IDS {
34
35 /**
fe482240 36 * Define the threshold for the ids reactions.
6a488035
TO
37 */
38 private $threshold = array(
39 'log' => 25,
40 'warn' => 50,
41 'kick' => 75,
42 );
43
44 /**
7c877abd 45 * @var string
6a488035 46 */
7c877abd 47 private $path;
6a488035
TO
48
49 /**
ea3ddccf 50 * Check function.
51 *
6a488035
TO
52 * This function includes the IDS vendor parts and runs the
53 * detection routines on the request array.
54 *
7c877abd
CW
55 * @param array $args
56 * List of path parts.
6a488035 57 *
3bdca100 58 * @return bool
6a488035 59 */
7c877abd 60 public function check($args) {
6a488035 61 // lets bypass a few civicrm urls from this check
aa00da9b 62 $skip = array('civicrm/admin/setting/updateConfigBackend', 'civicrm/admin/messageTemplates');
63 CRM_Utils_Hook::idsException($skip);
7c877abd
CW
64 $this->path = implode('/', $args);
65 if (in_array($this->path, $skip)) {
3bdca100 66 return NULL;
6a488035
TO
67 }
68
ea3ddccf 69 // Add request url and user agent.
6a488035
TO
70 $_REQUEST['IDS_request_uri'] = $_SERVER['REQUEST_URI'];
71 if (isset($_SERVER['HTTP_USER_AGENT'])) {
72 $_REQUEST['IDS_user_agent'] = $_SERVER['HTTP_USER_AGENT'];
73 }
74
75 $configFile = self::createConfigFile(FALSE);
76
77 // init the PHPIDS and pass the REQUEST array
78 require_once 'IDS/Init.php';
79 try {
80 $init = IDS_Init::init($configFile);
353ffa53 81 $ids = new IDS_Monitor($_REQUEST, $init);
0db6c3e1
TO
82 }
83 catch (Exception $e) {
6a488035
TO
84 // might be an old stale copy of Config.IDS.ini
85 // lets try to rebuild it again and see if it works
86 $configFile = self::createConfigFile(TRUE);
87 $init = IDS_Init::init($configFile);
353ffa53 88 $ids = new IDS_Monitor($_REQUEST, $init);
6a488035
TO
89 }
90
91 $result = $ids->run();
92 if (!$result->isEmpty()) {
93 $this->react($result);
94 }
95
96 return TRUE;
97 }
98
99 /**
fe482240 100 * Create the default config file for the IDS system.
6a488035 101 *
6a0b768e
TO
102 * @param bool $force
103 * Should we recreate it irrespective if it exists or not.
6a488035 104 *
a6c01b45
CW
105 * @return string
106 * the full path to the config file
6a488035 107 */
00be9182 108 public static function createConfigFile($force = FALSE) {
6a488035
TO
109 $config = CRM_Core_Config::singleton();
110 $configFile = $config->configAndLogDir . 'Config.IDS.ini';
111 if (!$force && file_exists($configFile)) {
112 return $configFile;
113 }
114
115 $tmpDir = empty($config->uploadDir) ? CIVICRM_TEMPLATE_COMPILEDIR : $config->uploadDir;
116
117 // also clear the stat cache in case we are upgrading
118 clearstatcache();
119
120 global $civicrm_root;
121 $contents = "
122[General]
123 filter_type = xml
124 filter_path = {$civicrm_root}/packages/IDS/default_filter.xml
125 tmp_path = $tmpDir
126 HTML_Purifier_Path = IDS/vendors/htmlpurifier/HTMLPurifier.auto.php
127 HTML_Purifier_Cache = $tmpDir
128 scan_keys = false
129 exceptions[] = __utmz
130 exceptions[] = __utmc
131 exceptions[] = widget_code
132 exceptions[] = html_message
133 exceptions[] = text_message
134 exceptions[] = body_html
135 exceptions[] = msg_html
136 exceptions[] = msg_text
137 exceptions[] = msg_subject
138 exceptions[] = description
139 exceptions[] = intro
140 exceptions[] = thankyou_text
141 exceptions[] = intro_text
142 exceptions[] = body_text
143 exceptions[] = footer_text
144 exceptions[] = thankyou_text
145 exceptions[] = tf_thankyou_text
146 exceptions[] = thankyou_footer
147 exceptions[] = thankyou_footer_text
148 exceptions[] = new_text
149 exceptions[] = renewal_text
150 exceptions[] = help_pre
151 exceptions[] = help_post
152 exceptions[] = confirm_title
153 exceptions[] = confirm_text
154 exceptions[] = confirm_footer_text
155 exceptions[] = confirm_email_text
156 exceptions[] = report_header
157 exceptions[] = report_footer
158 exceptions[] = data
bfc7747b 159 exceptions[] = json
6a488035
TO
160 exceptions[] = instructions
161 exceptions[] = suggested_message
162 exceptions[] = page_text
09c5ed84 163 exceptions[] = details
6a488035
TO
164";
165 if (file_put_contents($configFile, $contents) === FALSE) {
166 CRM_Core_Error::movedSiteError($configFile);
167 }
168
6a488035
TO
169 // also create the .htaccess file so we prevent the reading of the log and ini files
170 // via a browser, CRM-3875
171 CRM_Utils_File::restrictAccess($config->configAndLogDir);
172
173 return $configFile;
174 }
175
176 /**
ea3ddccf 177 * This function reacts on the values in the incoming results array.
6a488035
TO
178 *
179 * Depending on the impact value certain actions are
180 * performed.
181 *
182 * @param IDS_Report $result
183 *
3bdca100 184 * @return bool
6a488035 185 */
d3e86119 186 private function react(IDS_Report $result) {
6a488035
TO
187
188 $impact = $result->getImpact();
189 if ($impact >= $this->threshold['kick']) {
190 $this->log($result, 3, $impact);
7c877abd 191 $this->kick();
6a488035
TO
192 return TRUE;
193 }
194 elseif ($impact >= $this->threshold['warn']) {
195 $this->log($result, 2, $impact);
196 $this->warn($result);
197 return TRUE;
198 }
199 elseif ($impact >= $this->threshold['log']) {
200 $this->log($result, 0, $impact);
201 return TRUE;
202 }
203 else {
204 return TRUE;
205 }
206 }
207
208 /**
ea3ddccf 209 * This function writes an entry about the intrusion to the database.
6a488035 210 *
c490a46a 211 * @param array $result
77b97be7
EM
212 * @param int $reaction
213 *
3bdca100 214 * @return bool
6a488035
TO
215 */
216 private function log($result, $reaction = 0) {
217 $ip = (isset($_SERVER['SERVER_ADDR']) &&
3bdca100 218 $_SERVER['SERVER_ADDR'] != '127.0.0.1') ? $_SERVER['SERVER_ADDR'] : (
219 isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : '127.0.0.1'
006389de 220 );
6a488035
TO
221
222 $data = array();
223 $session = CRM_Core_Session::singleton();
224 foreach ($result as $event) {
225 $data[] = array(
226 'name' => $event->getName(),
227 'value' => stripslashes($event->getValue()),
228 'page' => $_SERVER['REQUEST_URI'],
229 'userid' => $session->get('userID'),
230 'session' => session_id() ? session_id() : '0',
231 'ip' => $ip,
232 'reaction' => $reaction,
233 'impact' => $result->getImpact(),
234 );
235 }
236
237 CRM_Core_Error::debug_var('IDS Detector Details', $data);
238 return TRUE;
239 }
240
241 /**
ea3ddccf 242 * Warn about IDS.
243 *
244 * @param array $result
245 *
246 * @return array
6a488035
TO
247 */
248 private function warn($result) {
249 return $result;
250 }
251
252 /**
7c877abd 253 * Create an error that prevents the user from continuing.
ea3ddccf 254 *
255 * @throws \Exception
6a488035 256 */
7c877abd 257 private function kick() {
6a488035
TO
258 $session = CRM_Core_Session::singleton();
259 $session->reset(2);
260
f3a27f08
DL
261 $msg = ts('There is a validation error with your HTML input. Your activity is a bit suspicious, hence aborting');
262
f3a27f08 263 if (in_array(
7c877abd 264 $this->path,
353ffa53
TO
265 array("civicrm/ajax/rest", "civicrm/api/json")
266 )) {
f3a27f08
DL
267 require_once "api/v3/utils.php";
268 $error = civicrm_api3_create_error(
269 $msg,
6a488035
TO
270 array(
271 'IP' => $_SERVER['REMOTE_ADDR'],
272 'error_code' => 'IDS_KICK',
273 'level' => 'security',
274 'referer' => $_SERVER['HTTP_REFERER'],
275 'reason' => 'XSS suspected',
276 )
277 );
ecdef330 278 CRM_Utils_JSON::output($error);
6a488035 279 }
f3a27f08 280 CRM_Core_Error::fatal($msg);
6a488035 281 }
96025800 282
6a488035 283}