5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2009 Cracker Tracker Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 // Initializes all detector arrays
26 function initCrackerTrackerArrays () {
27 // Set error_reporting
28 if (isCrackerTrackerDebug()) {
29 // For debugging purposes, this is fine
30 @error_reporting(E_ALL | E_STRICT);
37 $GLOBALS['ctracker_base_path'] = dirname(dirname(__FILE__));
39 // Whitelist some absolute query strings (see below)
40 $GLOBALS['ctracker_whitelist'] = [
43 'cmd=lostpw', // LinPHA
44 '/css/status_config.php', // MantisBT
45 '/css/common_config.php', // MantisBT
46 '/javascript_config.php', // MantisBT
49 // Attacks we should detect and block
50 $GLOBALS['ctracker_get_blacklist'] = [
52 'union ', ' union', 'insert ',
53 'select ', ' like', 'like ', 'drop ', 'update ',
56 // $GLOBAL/$_SERVER array elements
57 'HTTP_USER_AGENT', 'HTTP_HOST', 'HTTP_PHP', '_SESSION', 'CFG_ROOT',
58 'DOCUMENT_ROOT', '_SERVER',
61 '/environ', 'etc/shadow', 'etc/gshadow', 'etc/passwd', 'etc/group',
62 'etc/./shadow', 'etc/./gshadow', 'etc/./passwd', 'etc/./group',
63 '.htaccess', '.htpasswd', '.htgroup', '.history', 'bash_history',
66 // Other Linux/FreeBSD/??? programs (sometimes with space)
67 'traceroute ', 'ping ', 'bin/xterm', 'bin/./xterm', 'lsof ',
68 'telnet ', 'wget ', 'bin/perl', 'bin/id', 'uname\x20', 'uname ',
69 'killall', 'diff ', 'kill ', 'locate ', 'grep ', 'vi ', 'mv ',
70 'rmdir ', 'mcd ', 'mrd ', 'rm ', ' mcd', ' mrd', ' rm',
71 'passwd ', ' passwd', 'mdir ', ' mdir', 'cp ', ' cp',
72 'esystem ', 'chr ', ' chr', 'wget ', ' wget', ' cmd',
73 'cmd ', ' rush', 'rush ', ' echr', 'echr ', ' getenv',
74 'getenv', 'reboot ', 'halt ', 'powerdown ',
76 // Other Linux programs (+ brace)
77 'locate(', 'grep(', 'kill(', 'mcd(', 'mrd(', 'rm(', 'mv(', 'rmdir(',
78 'chmod(', 'chmod(', 'chown(', 'chgrp(', 'passwd(', 'vi(', 'cp(',
79 'mdir(', 'system(', 'chr(', 'wget(', 'rush(', 'echr(',
81 // Other Linux programs (+ equal)
82 'mcd=', 'mrd=', 'chmod=', 'chr=', 'rush=', 'echr=',
85 '/etc/', '/bin/', '/sbin/', '/self/', '/proc/', '../../','..//', '././',
86 '/home/ftp', '/home/./ftp', '/home/./www', '/home/www', '/www/virtual/',
90 '/chgrp', '/chown', '/chmod', 'chown ', 'chmod ', 'chgrp ',
92 // Compiler/interpreter
93 'bin/g++ ', 'bin/c++ ', 'cc ', 'bin/python', 'bin/python', 'bin/tclsh',
94 'bin/tclsh', 'bin/nasm', '/perl', 'cmd.exe',
98 'allow_url_fopen', 'allow_url_include', 'auto_prepend_file', 'disable_functions', 'safe_mode',
101 // PHP commands/scripts
102 'fopen', 'fwrite', 'phpinfo()', '\<?', '?\>', 'base64_decode', 'file_put_contents',
103 'set_magic_quotes_runtime', 'set_magic_quotes_runtime', 'display_errors', 'passthru',
105 // Typical PHP script remote-inclusions and typical include file names
106 '.inc.php', '.lib.php', '.class.php', 'config.php', '.inc', '_php',
107 'php_', 'class_', '_class.php', 'db_mysql.inc',
110 '_phplib', '__callbackparam',
112 // Generic remote inclusion
113 '=http://', '=https://', '=php://',
115 '=%7BQUOT%7D', '=%5C', '=%22http','=%22ftp','=%22file','=%27http','=%27ftp',
119 'data://', 'tcp://', 'udp://', 'raw://', 'javascript://', 'file://', 'ftp://',
122 'nigga(', ' nigga', 'nigga ', 'starhack', 'busca',
125 ' style=', 'style =',
126 'overflow:auto', 'overflow: auto',
127 'overflow :auto', 'overflow : auto',
128 'display:hidden', 'display: hidden',
129 'display :hidden', 'display : hidden',
130 'height:0px', 'height: 0px','height:1px', 'height: 1px',
131 'width:0px', 'width: 0px','width:1px', 'width: 1px',
133 // Uncommon user websites
134 '~root', '~ftp', '~nobody',
136 // Windows XP (?) hacks
137 'xp_enumdsn', 'xp_availablemedia', 'xp_filelist', 'xp_cmdshell',
139 // Attempts to insert links into a badly secured URL
142 // Request header being inserted
145 // /proc/ and other forbidden paths
148 // MySQL internal functions
151 // @TODO Misc/unsorted
152 'cgi-', '.eml', '$_request', '$_get', '$request', '$get', '.system',
153 '&aim', 'new_password', '&icq', '.conf', 'motd ', 'HTTP/1.',
154 'window.open', 'img src', 'img src', '.jsp', 'servlet', 'org.apache',
155 'wwwacl', 'server-info', 'server-status', '/servlet/con', 'http_',
156 'secure_site, ok', 'chunked', '<script', 'mod_gzip_status', '.system',
157 'uol.com', ',0x', '(0x', 'INSERT_RANDOM_NUMBER_HERE',
160 // BLock these words found in User-Agent
161 $GLOBALS['ctracker_ua_blacklist'] = [
162 // Compiler/interpreter
163 'bin/g++ ', 'bin/c++ ', 'cc ', 'bin/python', 'bin/python', 'bin/tclsh',
164 'bin/tclsh', 'bin/nasm', '/perl', 'cmd.exe',
165 'nc.exe', 'ftp.exe', 'wget ', 'system(', 'curl ',
168 'allow_url_fopen', 'allow_url_include', 'auto_prepend_file', 'disable_functions', 'safe_mode',
170 // PHP commands/scripts
171 'fopen', 'fwrite', 'phpinfo()', '\<?', '?\>', 'base64_decode', 'file_put_contents',
172 'set_magic_quotes_runtime', 'set_magic_quotes_runtime', 'display_errors', 'passthru',
174 // Typical PHP script remote-inclusions and typical include file names
175 '.inc.php', '.lib.php', '.class.php', 'config.php', '.inc', '_php',
176 'php_', 'class_', '_class.php', 'db_mysql.inc',
179 '_phplib', '__callbackparam',
181 // Request header being inserted
184 // /proc/ and other forbidden paths
188 // Block these words found in POST requests
189 $GLOBALS['ctracker_post_blacklist'] = [
190 // This line is for detecting hidden link spam in wikis, forums, guestbooks, etc.
191 ' style=', 'overflow:auto', 'height:1px', 'width:1px', 'display:hidden', 'style.display',
193 // "Common" login names from VHCS exploiters ;-)
194 'starhack', 'DeLiMehmet', 'hisset', 'Hisset', 'delimert', 'MecTruy'
197 // Also block these requests (mostly you don't want CONNECT to some SMTP sites)
198 $GLOBALS['ctracker_blocked_methods'] = [
202 // Init more elements
203 $GLOBALS['ctracker_post_track'] = '';
204 $GLOBALS['ctracker_checked_get'] = '';
205 $GLOBALS['ctracker_checked_post'] = '';
206 $GLOBALS['ctracker_checked_ua'] = '';
210 function isCrackerTrackerWormDetected () {
211 // Check against the whole list
212 $GLOBALS['ctracker_checked_get'] = urldecode(str_ireplace($GLOBALS['ctracker_get_blacklist'], '*', crackerTrackerQueryString(TRUE)));
213 $GLOBALS['ctracker_checked_ua'] = urldecode(str_ireplace($GLOBALS['ctracker_ua_blacklist'], '*', crackerTrackerUserAgent(TRUE)));
216 * If it differs to original and the *whole* request string is not in
217 * whitelist then blog the attempt.
221 $GLOBALS['ctracker_checked_get'] != crackerTrackerQueryString(TRUE) && (!in_array(crackerTrackerQueryString(TRUE), $GLOBALS['ctracker_whitelist']))
223 $GLOBALS['ctracker_checked_ua'] != crackerTrackerUserAgent(TRUE)
225 isset($GLOBALS['ctracker_blocked_methods'][crackerTrackerRequestMethod()])
228 //* DEBUG-DIE: */ die('isWorm='.intval($isWorm).PHP_EOL.'get='.PHP_EOL.'"'.$GLOBALS['ctracker_checked_get'].'"'.PHP_EOL.'"'.crackerTrackerQueryString().'"'.PHP_EOL.'ua='.PHP_EOL.'"'.$GLOBALS['ctracker_checked_ua'].'"'.PHP_EOL.'"'.crackerTrackerUserAgent().'"'.PHP_EOL);
235 function isCrackerTrackerPostAttackDetected () {
236 // Implode recursive the whole $_POST array
237 $GLOBALS['ctracker_post_track'] = urldecode(implode_r('&', $_POST));
239 // Check for suspicious POST data
240 $GLOBALS['ctracker_checked_post'] = urldecode(str_ireplace($GLOBALS['ctracker_post_blacklist'], '*', crackerTrackerSanitize($GLOBALS['ctracker_post_track'])));
243 return ((isCrackerTrackerWormDetected()) || ($GLOBALS['ctracker_checked_post'] != crackerTrackerSanitize($GLOBALS['ctracker_post_track'])));
246 // Prepares a mail and send it out
247 function sendCrackerTrackerMail () {
249 crackerTrackerLogAttack();
252 $mail = 'Attack detected:
253 -----------------------------------------------------
254 Remote-IP : ' . determineCrackerTrackerRealRemoteAddress() . '
255 User-Agent : ' . crackerTrackerUserAgent() . '
256 Request-string : ' . crackerTrackerQueryString() . '
257 Filtered string : ' . $GLOBALS['ctracker_checked_get'] . '
258 Server : ' . crackerTrackerServerName() . '
259 Script : ' . crackerTrackerScriptName() . '
260 Referrer : ' . crackerTrackerReferer() . '
261 -----------------------------------------------------
265 crackerTrackerSendMail($mail);
271 // Sends the ticket emails out
272 function sendCrackerTrackerTicketMails () {
273 // Load user template
274 $mail = crackerTrackerLoadEmailTemplate('user_add_ticket', $GLOBALS['ctracker_last_ticket']);
276 // Send email to the user
277 crackerTrackerSendMail($mail, $GLOBALS['ctracker_last_ticket']['ctracker_ticket_email'], getCrackerTrackerLocalized('user_add_ticket_subject'));
279 // Load webmaster template
280 $mail = crackerTrackerLoadEmailTemplate('webmaster_add_ticket', $GLOBALS['ctracker_last_ticket'], getCrackerTrackerConfig('ctracker_language'));
282 // Send email to the user
283 crackerTrackerSendMail($mail, NULL, getCrackerTrackerLocalized('webmaster_add_ticket_subject'));
287 function crackerTrackerSendMail ($mail, $recipient = NULL, $subject = NULL) {
288 // Construct dummy array
290 'remote_addr' => determineCrackerTrackerRealRemoteAddress(),
291 'proxy_addr' => getenv('REMOTE_ADDR'),
292 'check_get' => $GLOBALS['ctracker_checked_get'],
293 'server_name' => crackerTrackerServerName()
296 // Only send email if not yet found
297 if (!isCrackerTrackerEntryFound($rowData)) {
298 // Send the email out only in non-debug mode
299 if (isCrackerTrackerDebug()) {
301 print 'Recipient=' . $recipient . '<br />Subject=' . $subject . '<br />Text=<pre>' . $mail . '</pre>';
305 } elseif (!is_null($recipient)) {
306 // Recipient specified
307 return mail($recipient, $subject, $mail, $GLOBALS['ctracker_header']);
308 } elseif (isset($GLOBALS['ctracker_email'])) {
310 return mail($GLOBALS['ctracker_email'], 'CTracker: Attack detected!', $mail, $GLOBALS['ctracker_header']);
312 // Send it the deprecated way with constant
313 return mail(constant('__CTRACKER_EMAIL'), 'CTracker: Attack detected!', $mail, $GLOBALS['ctracker_header']);
315 } elseif (isCrackerTrackerDebug()) {
317 print 'Recipient=' . $recipient . '<br />Subject=' . $subject . '<br />Text=<pre>' . $mail . '</pre>';
324 // Sends a detected POST attack mail
325 function sendCrackerTrackerPostMail () {
327 crackerTrackerLogAttack();
330 $mail = 'POST-Attack detected:
331 -----------------------------------------------------
332 Remote-IP : ' . determineCrackerTrackerRealRemoteAddress() . '
333 User-Agent : ' . crackerTrackerUserAgent() . '
334 Request-string : ' . crackerTrackerQueryString() . '
335 Filtered string : ' . $GLOBALS['ctracker_checked_get'] . '
336 Server : ' . crackerTrackerServerName() . '
337 Script : ' . crackerTrackerScriptName() . '
338 Referrer : ' . crackerTrackerReferer() . '
339 -----------------------------------------------------
340 POST string : ' . $GLOBALS['ctracker_post_track'] . '
341 Filtered POST string : ' . $GLOBALS['ctracker_checked_post'] . '
342 -----------------------------------------------------
346 crackerTrackerSendMail($mail);
352 // Sleeps for a random time and aborts the script
353 function crackerTrackerDie () {
354 // Close database link
355 crackerTrackerCloseDatabaseLink();
357 // Do only sleep if debug/developer mode is not enabled
358 if (!isCrackerTrackerDebug()) {
359 // Sleep a little to waste the attacker's time
360 sleep(mt_rand(10,30));
364 if (isCrackerTrackerDebug()) {
365 // With debug backtrace in debug/developer mode
367 debug_print_backtrace();
375 // Logs the attack attempt
376 function crackerTrackerLogAttack () {
377 // Aquire database link
378 aquireCrackerTrackerDatabaseLink();
380 // By default no proxy is used
383 // Did the attacker use a proxy?
384 if (isCrackerTrackerProxyUsed()) {
389 // Prepare array for database insert
391 'remote_addr' => determineCrackerTrackerRealRemoteAddress(),
392 'proxy_addr' => getenv('REMOTE_ADDR'),
393 'user_agent' => crackerTrackerUserAgent(),
394 'get_data' => crackerTrackerQueryString(),
395 'post_data' => $GLOBALS['ctracker_post_track'],
396 'check_ua' => $GLOBALS['ctracker_checked_ua'],
397 'check_get' => $GLOBALS['ctracker_checked_get'],
398 'check_post' => $GLOBALS['ctracker_checked_post'],
399 'server_name' => crackerTrackerServerName(),
400 'script_name' => crackerTrackerScriptName(),
401 'referer' => crackerTrackerReferer(),
402 'request_method' => crackerTrackerRequestMethod(),
403 'proxy_used' => $proxyUsed,
404 'first_attempt' => 'NOW()'
407 // Insert the array in database
408 crackerTrackerInsertArray('ctracker_data', $rowData);
411 // Alerts the current user about malicious/suspicious traffic
412 function crackerTrackerAlertCurrentUser () {
413 // Is there some data?
414 if (isset($GLOBALS['ctracker_last_suspicious_entry'])) {
415 // Does the user have a ticket?
416 if (ifCrackerTrackerIpHasTicket()) {
417 // Load "Thank you" template
418 crackerTrackerLoadTemplate('add_ticket_thanks');
419 } elseif ((isset($_POST['ctracker_add_ticket'])) && (!empty($_POST['name'])) && (!empty($_POST['email']))) {
421 addCrackerTrackerTicket($_POST);
423 // Send the email out
424 sendCrackerTrackerTicketMails();
426 // Display the form for new ticket
427 crackerTrackerLoadTemplate('add_ticket');