5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2009 - 2017 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', '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 ', 'invokefunction',
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',
97 'cmd.exe', 'nc.exe', 'ftp.exe', 'powershell', 'system.net.webclient', '%systemroot%',
100 'allow_url_fopen', 'allow_url_include', 'auto_prepend_file', 'disable_functions', 'safe_mode',
101 'open_basedir', 'suhosin', 'cgi.force_redirect', 'cgi.redirect_status_env',
103 // PHP commands/scripts
104 'fopen', 'fwrite', 'phpinfo()', '\<?', '?\>', 'base64_decode', 'file_put_contents',
105 'set_magic_quotes_runtime', 'set_magic_quotes_runtime', 'display_errors', 'passthru',
106 'call_user_func', 'set_time_limit', 'urldecode',
108 // Typical PHP script remote-inclusions and typical include file names
109 '.inc.php', '.lib.php', '.class.php', 'config.php', '.inc', '_php',
110 'php_', 'class_', '_class.php', 'db_mysql.inc',
113 '_phplib', '__callbackparam',
115 // Generic remote inclusion
116 '=http://', '=https://', '=php://',
118 '=%7BQUOT%7D', '=%5C', '=%22http','=%22ftp','=%22file','=%27http','=%27ftp',
122 'data://', 'tcp://', 'udp://', 'raw://', 'javascript://', 'file://', 'ftp://',
125 'nigga(', ' nigga', 'nigga ', 'starhack', 'busca',
128 ' style=', 'style =',
129 'overflow:auto', 'overflow: auto',
130 'overflow :auto', 'overflow : auto',
131 'display:hidden', 'display: hidden',
132 'display :hidden', 'display : hidden',
133 'height:0px', 'height: 0px','height:1px', 'height: 1px',
134 'width:0px', 'width: 0px','width:1px', 'width: 1px',
136 // Uncommon user websites
137 '~root', '~ftp', '~nobody',
139 // Windows XP (?) hacks
140 'xp_enumdsn', 'xp_availablemedia', 'xp_filelist', 'xp_cmdshell',
142 // Attempts to insert links into a badly secured URL
145 // Request header being inserted
148 // /proc/ and other "forbidden" paths
151 // MySQL internal functions/tables
152 'name_const', 'information_schema',
154 // Server configuration (e.g. Apache)
155 'application/x-httpd-php', 'addtype', 'server-info', 'server-status',
157 // Annoying script name
160 // @TODO Misc/unsorted
161 'cgi-', '.eml', '$_request', '$_get', '$request', '$get', '.system',
162 '&aim', 'new_password', '&icq', '.conf', 'motd ', 'HTTP/1.',
163 'window.open', 'img src', 'img src', '.jsp', 'servlet', 'org.apache',
164 'wwwacl', '/servlet/con', 'http_', 'secure_site, ok', 'chunked',
165 '<script', 'mod_gzip_status', '.system', 'uol.com', ',0x', '(0x',
166 'INSERT_RANDOM_NUMBER_HERE', '=passthru',
169 // BLock these words found in User-Agent
170 $GLOBALS['ctracker_ua_blacklist'] = [
172 'drop database', 'drop table',
174 // Compiler/interpreter
175 'bin/g++ ', 'bin/c++ ', 'cc ', 'bin/python', 'bin/python', 'bin/tclsh',
176 'bin/tclsh', 'bin/nasm', '/perl', 'wget ', 'system(', 'curl ',
179 'cmd.exe', 'nc.exe', 'ftp.exe', 'powershell', 'system.net.webclient',
182 'allow_url_fopen', 'allow_url_include', 'auto_prepend_file', 'disable_functions', 'safe_mode',
184 // PHP commands/scripts
185 'fopen', 'fwrite', 'phpinfo()', '\<?', '?\>', 'base64_decode', 'file_put_contents',
186 'set_magic_quotes_runtime', 'set_magic_quotes_runtime', 'display_errors', 'passthru',
189 // Server configuration (e.g. Apache)
190 'application/x-httpd-php',
192 // Typical PHP script remote-inclusions and typical include file names
193 '.inc.php', '.lib.php', '.class.php', 'config.php', '.inc', '_php',
194 'php_', 'class_', '_class.php', 'db_mysql.inc',
197 '_phplib', '__callbackparam',
199 // Request header being inserted
202 // /proc/ and other forbidden paths
206 // Block these words found in POST requests
207 $GLOBALS['ctracker_post_blacklist'] = [
208 // This line is for detecting hidden link spam in wikis, forums, guestbooks, etc.
209 ' style=', 'overflow:auto', 'height:1px', 'width:1px', 'display:hidden', 'style.display',
212 'cmd.exe', 'nc.exe', 'ftp.exe', 'powershell', 'system.net.webclient',
214 // Server configuration (e.g. Apache)
215 'application/x-httpd-php',
217 // Annoying script name
220 // "Common" login names from VHCS exploiters
221 'starhack', 'DeLiMehmet', 'hisset', 'Hisset', 'delimert', 'MecTruy'
224 // Also block these requests (mostly you don't want CONNECT to some SMTP sites)
225 $GLOBALS['ctracker_blocked_methods'] = [
229 // Init more elements
230 $GLOBALS['ctracker_post_track'] = '';
231 $GLOBALS['ctracker_checked_get'] = '';
232 $GLOBALS['ctracker_checked_post'] = '';
233 $GLOBALS['ctracker_checked_ua'] = '';
237 function isCrackerTrackerWormDetected () {
238 // Check against the whole list
239 $GLOBALS['ctracker_checked_get'] = urldecode(str_ireplace($GLOBALS['ctracker_get_blacklist'], '*', crackerTrackerQueryString(TRUE)));
240 $GLOBALS['ctracker_checked_ua'] = urldecode(str_ireplace($GLOBALS['ctracker_ua_blacklist'], '*', crackerTrackerUserAgent(TRUE)));
243 * If it differs to original and the *whole* request string is not in
244 * whitelist then blog the attempt.
248 $GLOBALS['ctracker_checked_get'] != crackerTrackerQueryString(TRUE) && (!in_array(crackerTrackerQueryString(TRUE), $GLOBALS['ctracker_whitelist']))
250 $GLOBALS['ctracker_checked_ua'] != crackerTrackerUserAgent(TRUE)
252 isset($GLOBALS['ctracker_blocked_methods'][crackerTrackerRequestMethod()])
255 //* 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);
262 function isCrackerTrackerPostAttackDetected () {
263 // Implode recursive the whole $_POST array
264 $GLOBALS['ctracker_post_track'] = urldecode(implode_r('&', $_POST));
266 // Check for suspicious POST data
267 $GLOBALS['ctracker_checked_post'] = urldecode(str_ireplace($GLOBALS['ctracker_post_blacklist'], '*', crackerTrackerSanitize($GLOBALS['ctracker_post_track'])));
270 return ((isCrackerTrackerWormDetected()) || ($GLOBALS['ctracker_checked_post'] != crackerTrackerSanitize($GLOBALS['ctracker_post_track'])));
273 // Prepares a mail and send it out
274 function sendCrackerTrackerMail () {
276 crackerTrackerLogAttack();
279 $mail = 'Attack detected:
280 -----------------------------------------------------
281 Remote-IP : ' . determineCrackerTrackerRealRemoteAddress() . '
282 User-Agent : ' . crackerTrackerUserAgent() . '
283 Request-string : ' . crackerTrackerQueryString() . '
284 Filtered string : ' . $GLOBALS['ctracker_checked_get'] . '
285 Server : ' . crackerTrackerServerName() . '
286 Script : ' . crackerTrackerScriptName() . '
287 Method : ' . crackerTrackerRequestMethod() . '
288 Referrer : ' . crackerTrackerReferer() . '
289 -----------------------------------------------------
293 crackerTrackerSendMail($mail);
299 // Sends the ticket emails out
300 function sendCrackerTrackerTicketMails () {
301 // Load user template
302 $mail = crackerTrackerLoadEmailTemplate('user_add_ticket', $GLOBALS['ctracker_last_ticket']);
304 // Send email to the user
305 crackerTrackerSendMail($mail, $GLOBALS['ctracker_last_ticket']['ctracker_ticket_email'], getCrackerTrackerLocalized('user_add_ticket_subject'));
307 // Load webmaster template
308 $mail = crackerTrackerLoadEmailTemplate('webmaster_add_ticket', $GLOBALS['ctracker_last_ticket'], getCrackerTrackerConfig('ctracker_language'));
310 // Send email to the user
311 crackerTrackerSendMail($mail, NULL, getCrackerTrackerLocalized('webmaster_add_ticket_subject'));
315 function crackerTrackerSendMail ($mail, $recipient = NULL, $subject = NULL) {
316 // Construct dummy array
318 'remote_addr' => determineCrackerTrackerRealRemoteAddress(),
319 'proxy_addr' => getenv('REMOTE_ADDR'),
320 'check_get' => $GLOBALS['ctracker_checked_get'],
321 'server_name' => crackerTrackerServerName(),
322 'request_method' => crackerTrackerRequestMethod(),
325 // Only send email if not yet found
326 if (!isCrackerTrackerEntryFound($rowData)) {
327 // Send the email out only in non-debug mode
328 if (isCrackerTrackerDebug()) {
330 print 'Recipient=' . $recipient . '<br />Subject=' . $subject . '<br />Text=<pre>' . $mail . '</pre>';
334 } elseif (!is_null($recipient)) {
335 // Recipient specified
336 return mail($recipient, $subject, $mail, $GLOBALS['ctracker_header']);
337 } elseif (isset($GLOBALS['ctracker_email'])) {
339 return mail($GLOBALS['ctracker_email'], 'CTracker: Attack detected!', $mail, $GLOBALS['ctracker_header']);
341 // Send it the deprecated way with constant
342 return mail(constant('__CTRACKER_EMAIL'), 'CTracker: Attack detected!', $mail, $GLOBALS['ctracker_header']);
344 } elseif (isCrackerTrackerDebug()) {
346 print 'Recipient=' . $recipient . '<br />Subject=' . $subject . '<br />Text=<pre>' . $mail . '</pre>';
353 // Sends a detected POST attack mail
354 function sendCrackerTrackerPostMail () {
356 crackerTrackerLogAttack();
359 $mail = 'POST-Attack detected:
360 -----------------------------------------------------
361 Remote-IP : ' . determineCrackerTrackerRealRemoteAddress() . '
362 User-Agent : ' . crackerTrackerUserAgent() . '
363 Request-string : ' . crackerTrackerQueryString() . '
364 Filtered string : ' . $GLOBALS['ctracker_checked_get'] . '
365 Server : ' . crackerTrackerServerName() . '
366 Script : ' . crackerTrackerScriptName() . '
367 Method : ' . crackerTrackerRequestMethod() . '
368 Referrer : ' . crackerTrackerReferer() . '
369 -----------------------------------------------------
370 POST string : ' . $GLOBALS['ctracker_post_track'] . '
371 Filtered POST string : ' . $GLOBALS['ctracker_checked_post'] . '
372 -----------------------------------------------------
376 crackerTrackerSendMail($mail);
382 // Sleeps for a random time and aborts the script
383 function crackerTrackerDie () {
384 // Close database link
385 crackerTrackerCloseDatabaseLink();
387 // Do only sleep if debug/developer mode is not enabled
388 if (!isCrackerTrackerDebug()) {
389 // Sleep a little to waste the attacker's time
390 sleep(mt_rand(10,30));
394 if (isCrackerTrackerDebug()) {
395 // With debug backtrace in debug/developer mode
397 debug_print_backtrace();
405 // Logs the attack attempt
406 function crackerTrackerLogAttack () {
407 // Aquire database link
408 aquireCrackerTrackerDatabaseLink();
410 // By default no proxy is used
413 // Did the attacker use a proxy?
414 if (isCrackerTrackerProxyUsed()) {
419 // Prepare array for database insert
421 'remote_addr' => determineCrackerTrackerRealRemoteAddress(),
422 'proxy_addr' => getenv('REMOTE_ADDR'),
423 'user_agent' => crackerTrackerUserAgent(),
424 'get_data' => crackerTrackerQueryString(),
425 'post_data' => $GLOBALS['ctracker_post_track'],
426 'check_ua' => $GLOBALS['ctracker_checked_ua'],
427 'check_get' => $GLOBALS['ctracker_checked_get'],
428 'check_post' => $GLOBALS['ctracker_checked_post'],
429 'server_name' => crackerTrackerServerName(),
430 'script_name' => crackerTrackerScriptName(),
431 'referer' => crackerTrackerReferer(),
432 'request_method' => crackerTrackerRequestMethod(),
433 'proxy_used' => $proxyUsed,
434 'first_attempt' => 'NOW()'
437 // Insert the array in database
438 crackerTrackerInsertArray('ctracker_data', $rowData);
441 function ctrackerIncreaseAntiSpambotCount () {
444 'remote_addr' => determineCrackerTrackerRealRemoteAddress(),
445 'proxy_addr' => getenv('REMOTE_ADDR'),
449 updateCrackerTrackerEntry($rowData, 'spam_bot_dectections');
452 // Alerts the current user about malicious/suspicious traffic
453 function crackerTrackerAlertCurrentUser () {
454 // Is there some data?
455 if (isset($GLOBALS['ctracker_last_suspicious_entry'])) {
456 // Does the user have a ticket?
457 if (ifCtrackerTrackerAntiSpamFieldGiven()) {
458 // Spam-bot filled out anti-spam field, register count
459 ctrackerIncreaseAntiSpambotCount();
461 // Output message to spammer
462 ctrackerTrackerLoadTemplate('bot_captured');
463 } elseif (ifCrackerTrackerIpHasTicket()) {
464 // Load "Thank you" template
465 crackerTrackerLoadTemplate('add_ticket_thanks');
466 } elseif ((isset($_POST['ctracker_add_ticket'])) && (!empty($_POST['name'])) && (!empty($_POST['email']))) {
468 addCrackerTrackerTicket($_POST);
470 // Send the email out
471 sendCrackerTrackerTicketMails();
473 // Display the form for new ticket
474 crackerTrackerLoadTemplate('add_ticket');