Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
32 | */ |
33 | class 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 | /** | |
100fef9d | 45 | * The init object |
6a488035 TO |
46 | */ |
47 | private $init = NULL; | |
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 | * | |
3bdca100 | 55 | * @param object $args cake controller object |
6a488035 | 56 | * |
3bdca100 | 57 | * @return bool |
6a488035 TO |
58 | */ |
59 | public function check(&$args) { | |
60 | // lets bypass a few civicrm urls from this check | |
aa00da9b | 61 | $skip = array('civicrm/admin/setting/updateConfigBackend', 'civicrm/admin/messageTemplates'); |
62 | CRM_Utils_Hook::idsException($skip); | |
6a488035 TO |
63 | $path = implode('/', $args); |
64 | if (in_array($path, $skip)) { | |
3bdca100 | 65 | return NULL; |
6a488035 TO |
66 | } |
67 | ||
ea3ddccf | 68 | // Add request url and user agent. |
6a488035 TO |
69 | $_REQUEST['IDS_request_uri'] = $_SERVER['REQUEST_URI']; |
70 | if (isset($_SERVER['HTTP_USER_AGENT'])) { | |
71 | $_REQUEST['IDS_user_agent'] = $_SERVER['HTTP_USER_AGENT']; | |
72 | } | |
73 | ||
74 | $configFile = self::createConfigFile(FALSE); | |
75 | ||
76 | // init the PHPIDS and pass the REQUEST array | |
77 | require_once 'IDS/Init.php'; | |
78 | try { | |
79 | $init = IDS_Init::init($configFile); | |
353ffa53 | 80 | $ids = new IDS_Monitor($_REQUEST, $init); |
0db6c3e1 TO |
81 | } |
82 | catch (Exception $e) { | |
6a488035 TO |
83 | // might be an old stale copy of Config.IDS.ini |
84 | // lets try to rebuild it again and see if it works | |
85 | $configFile = self::createConfigFile(TRUE); | |
86 | $init = IDS_Init::init($configFile); | |
353ffa53 | 87 | $ids = new IDS_Monitor($_REQUEST, $init); |
6a488035 TO |
88 | } |
89 | ||
90 | $result = $ids->run(); | |
91 | if (!$result->isEmpty()) { | |
92 | $this->react($result); | |
93 | } | |
94 | ||
95 | return TRUE; | |
96 | } | |
97 | ||
98 | /** | |
fe482240 | 99 | * Create the default config file for the IDS system. |
6a488035 | 100 | * |
6a0b768e TO |
101 | * @param bool $force |
102 | * Should we recreate it irrespective if it exists or not. | |
6a488035 | 103 | * |
a6c01b45 CW |
104 | * @return string |
105 | * the full path to the config file | |
6a488035 | 106 | */ |
00be9182 | 107 | public static function createConfigFile($force = FALSE) { |
6a488035 TO |
108 | $config = CRM_Core_Config::singleton(); |
109 | $configFile = $config->configAndLogDir . 'Config.IDS.ini'; | |
110 | if (!$force && file_exists($configFile)) { | |
111 | return $configFile; | |
112 | } | |
113 | ||
114 | $tmpDir = empty($config->uploadDir) ? CIVICRM_TEMPLATE_COMPILEDIR : $config->uploadDir; | |
115 | ||
116 | // also clear the stat cache in case we are upgrading | |
117 | clearstatcache(); | |
118 | ||
119 | global $civicrm_root; | |
120 | $contents = " | |
121 | [General] | |
122 | filter_type = xml | |
123 | filter_path = {$civicrm_root}/packages/IDS/default_filter.xml | |
124 | tmp_path = $tmpDir | |
125 | HTML_Purifier_Path = IDS/vendors/htmlpurifier/HTMLPurifier.auto.php | |
126 | HTML_Purifier_Cache = $tmpDir | |
127 | scan_keys = false | |
128 | exceptions[] = __utmz | |
129 | exceptions[] = __utmc | |
130 | exceptions[] = widget_code | |
131 | exceptions[] = html_message | |
132 | exceptions[] = text_message | |
133 | exceptions[] = body_html | |
134 | exceptions[] = msg_html | |
135 | exceptions[] = msg_text | |
136 | exceptions[] = msg_subject | |
137 | exceptions[] = description | |
138 | exceptions[] = intro | |
139 | exceptions[] = thankyou_text | |
140 | exceptions[] = intro_text | |
141 | exceptions[] = body_text | |
142 | exceptions[] = footer_text | |
143 | exceptions[] = thankyou_text | |
144 | exceptions[] = tf_thankyou_text | |
145 | exceptions[] = thankyou_footer | |
146 | exceptions[] = thankyou_footer_text | |
147 | exceptions[] = new_text | |
148 | exceptions[] = renewal_text | |
149 | exceptions[] = help_pre | |
150 | exceptions[] = help_post | |
151 | exceptions[] = confirm_title | |
152 | exceptions[] = confirm_text | |
153 | exceptions[] = confirm_footer_text | |
154 | exceptions[] = confirm_email_text | |
155 | exceptions[] = report_header | |
156 | exceptions[] = report_footer | |
157 | exceptions[] = data | |
bfc7747b | 158 | exceptions[] = json |
6a488035 TO |
159 | exceptions[] = instructions |
160 | exceptions[] = suggested_message | |
161 | exceptions[] = page_text | |
162 | "; | |
163 | if (file_put_contents($configFile, $contents) === FALSE) { | |
164 | CRM_Core_Error::movedSiteError($configFile); | |
165 | } | |
166 | ||
6a488035 TO |
167 | // also create the .htaccess file so we prevent the reading of the log and ini files |
168 | // via a browser, CRM-3875 | |
169 | CRM_Utils_File::restrictAccess($config->configAndLogDir); | |
170 | ||
171 | return $configFile; | |
172 | } | |
173 | ||
174 | /** | |
ea3ddccf | 175 | * This function reacts on the values in the incoming results array. |
6a488035 TO |
176 | * |
177 | * Depending on the impact value certain actions are | |
178 | * performed. | |
179 | * | |
180 | * @param IDS_Report $result | |
181 | * | |
3bdca100 | 182 | * @return bool |
6a488035 | 183 | */ |
d3e86119 | 184 | private function react(IDS_Report $result) { |
6a488035 TO |
185 | |
186 | $impact = $result->getImpact(); | |
187 | if ($impact >= $this->threshold['kick']) { | |
188 | $this->log($result, 3, $impact); | |
189 | $this->kick($result); | |
190 | return TRUE; | |
191 | } | |
192 | elseif ($impact >= $this->threshold['warn']) { | |
193 | $this->log($result, 2, $impact); | |
194 | $this->warn($result); | |
195 | return TRUE; | |
196 | } | |
197 | elseif ($impact >= $this->threshold['log']) { | |
198 | $this->log($result, 0, $impact); | |
199 | return TRUE; | |
200 | } | |
201 | else { | |
202 | return TRUE; | |
203 | } | |
204 | } | |
205 | ||
206 | /** | |
ea3ddccf | 207 | * This function writes an entry about the intrusion to the database. |
6a488035 | 208 | * |
c490a46a | 209 | * @param array $result |
77b97be7 EM |
210 | * @param int $reaction |
211 | * | |
3bdca100 | 212 | * @return bool |
6a488035 TO |
213 | */ |
214 | private function log($result, $reaction = 0) { | |
215 | $ip = (isset($_SERVER['SERVER_ADDR']) && | |
3bdca100 | 216 | $_SERVER['SERVER_ADDR'] != '127.0.0.1') ? $_SERVER['SERVER_ADDR'] : ( |
217 | isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : '127.0.0.1' | |
006389de | 218 | ); |
6a488035 TO |
219 | |
220 | $data = array(); | |
221 | $session = CRM_Core_Session::singleton(); | |
222 | foreach ($result as $event) { | |
223 | $data[] = array( | |
224 | 'name' => $event->getName(), | |
225 | 'value' => stripslashes($event->getValue()), | |
226 | 'page' => $_SERVER['REQUEST_URI'], | |
227 | 'userid' => $session->get('userID'), | |
228 | 'session' => session_id() ? session_id() : '0', | |
229 | 'ip' => $ip, | |
230 | 'reaction' => $reaction, | |
231 | 'impact' => $result->getImpact(), | |
232 | ); | |
233 | } | |
234 | ||
235 | CRM_Core_Error::debug_var('IDS Detector Details', $data); | |
236 | return TRUE; | |
237 | } | |
238 | ||
239 | /** | |
ea3ddccf | 240 | * Warn about IDS. |
241 | * | |
242 | * @param array $result | |
243 | * | |
244 | * @return array | |
6a488035 TO |
245 | */ |
246 | private function warn($result) { | |
247 | return $result; | |
248 | } | |
249 | ||
250 | /** | |
ea3ddccf | 251 | * Kick (whatever that means!). |
252 | * | |
253 | * @param array $result | |
254 | * | |
255 | * @throws \Exception | |
6a488035 TO |
256 | */ |
257 | private function kick($result) { | |
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 | ||
6a488035 | 263 | $path = implode('/', $args); |
f3a27f08 | 264 | if (in_array( |
353ffa53 TO |
265 | $path, |
266 | array("civicrm/ajax/rest", "civicrm/api/json") | |
267 | )) { | |
f3a27f08 DL |
268 | require_once "api/v3/utils.php"; |
269 | $error = civicrm_api3_create_error( | |
270 | $msg, | |
6a488035 TO |
271 | array( |
272 | 'IP' => $_SERVER['REMOTE_ADDR'], | |
273 | 'error_code' => 'IDS_KICK', | |
274 | 'level' => 'security', | |
275 | 'referer' => $_SERVER['HTTP_REFERER'], | |
276 | 'reason' => 'XSS suspected', | |
277 | ) | |
278 | ); | |
ecdef330 | 279 | CRM_Utils_JSON::output($error); |
6a488035 | 280 | } |
f3a27f08 | 281 | CRM_Core_Error::fatal($msg); |
6a488035 | 282 | } |
96025800 | 283 | |
6a488035 | 284 | } |