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 ',
54 'union(', 'union=', 'dbms_pipe',
57 // $GLOBAL/$_SERVER array elements
58 'HTTP_USER_AGENT', 'HTTP_HOST', 'HTTP_PHP', '_SESSION', 'CFG_ROOT',
59 'DOCUMENT_ROOT', '_SERVER',
61 // Don't run XDEBUG on production servers
65 '/environ', 'etc/shadow', 'etc/gshadow', 'etc/passwd', 'etc/group',
66 'etc/./shadow', 'etc/./gshadow', 'etc/./passwd', 'etc/./group',
67 '.htaccess', '.htpasswd', '.htgroup', '.history', 'bash_history',
70 // Other Linux/FreeBSD/??? programs (sometimes with space)
71 'traceroute ', 'ping ', 'bin/xterm', 'bin/./xterm', 'lsof ',
72 'telnet ', 'wget ', ' wget', 'bin/perl', 'bin/id', 'uname\x20',
73 'uname ', 'killall', 'diff ', 'kill ', 'locate ', 'grep ', 'vi ',
74 'mv ', 'rmdir ', 'mcd ', 'mrd ', 'rm ', ' mcd', ' mrd', ' rm',
75 'passwd ', ' passwd', 'mdir ', ' mdir', 'cp ', ' cp',
76 'esystem ', 'chr ', ' chr', ' cmd',
77 'cmd ', ' rush', 'rush ', ' echr', 'echr ', ' getenv',
78 'getenv', 'reboot ', 'halt ', 'powerdown ', 'invokefunction',
80 // Other Linux programs (+ brace)
81 'locate(', 'grep(', 'kill(', 'mcd(', 'mrd(', 'rm(', 'mv(', 'rmdir(',
82 'chmod(', 'chmod(', 'chown(', 'chgrp(', 'passwd(', 'vi(', 'cp(',
83 'mdir(', 'system(', 'chr(', 'wget(', 'rush(', 'echr(',
85 // Other Linux programs (+ equal)
86 'mcd=', 'mrd=', 'chmod=', 'chr=', 'rush=', 'echr=',
88 // Generic Linux paths
89 '/etc/', '/bin/', '/sbin/', '/self/', '/proc/', '/sys/',
91 // Dotted paths (to break out of DOCUMENT_ROOT)
92 '../../','..//', '././',
94 // Home paths and maybe common virtual hosting paths (ISCP)
95 '/home/ftp', '/home/./ftp', '/home/./www', '/home/www',
96 '/www/virtual/', '/www/./virtual/',
98 // User right, group and user changing
99 '/chgrp', '/chown', '/chmod', 'chown ', 'chmod ', 'chgrp ',
101 // Compiler/interpreter
102 'bin/g++ ', 'bin/c++ ', 'cc ', 'bin/python', 'bin/python', 'bin/tclsh',
103 'bin/tclsh', 'bin/nasm', '/perl',
106 'cmd.exe', 'nc.exe', 'ftp.exe', 'powershell', 'system.net.webclient', '%systemroot%',
109 'allow_url_fopen', 'allow_url_include', 'auto_prepend_file', 'disable_functions', 'safe_mode',
110 'open_basedir', 'suhosin', 'cgi.force_redirect', 'cgi.redirect_status_env',
112 // PHP commands/scripts
113 'fopen', 'fwrite', 'phpinfo()', '=phpinfo', '\<?', '?\>', 'base64_decode', 'file_put_contents',
114 'set_magic_quotes_runtime', 'set_magic_quotes_runtime', 'display_errors', 'passthru',
115 'call_user_func', 'set_time_limit', 'urldecode',
117 // PHPUnit is strictly console!
121 'disable_functions', 'safe_mode', 'allow_url_include', 'auto_prepend_file',
123 // Typical PHP script remote-inclusions and typical include file names
124 '.inc.php', '.lib.php', '.class.php', 'config.php', '.inc', '_php',
125 'php_', 'class_', '_class.php', 'db_mysql.inc',
128 '_phplib', '__callbackparam',
130 // Generic remote inclusion
131 '=http:/', '=https:/', '=php:/',
133 '=%7BQUOT%7D', '=%5C', '=%22http','=%22ftp','=%22file','=%27http','=%27ftp',
137 'data://', 'tcp://', 'udp://', 'raw://', 'javascript://', 'file://', 'ftp://',
140 'nigga(', ' nigga', 'nigga ', 'starhack', 'busca',
143 ' style=', 'style =',
144 'overflow:auto', 'overflow: auto',
145 'overflow :auto', 'overflow : auto',
146 'display:hidden', 'display: hidden',
147 'display :hidden', 'display : hidden',
148 'height:0px', 'height: 0px','height:1px', 'height: 1px',
149 'width:0px', 'width: 0px','width:1px', 'width: 1px',
151 // Uncommon user websites
152 '~root', '~ftp', '~nobody', '~www-data',
154 // Windows XP (?) hacks
155 'xp_enumdsn', 'xp_availablemedia', 'xp_filelist', 'xp_cmdshell',
157 // Attempts to insert links into a badly secured URL
160 // Request header being inserted
163 // /proc/ and other "forbidden" paths
166 // MySQL internal functions/tables
167 'name_const', 'information_schema',
169 // Server configuration (e.g. Apache)
170 'application/x-httpd-php', 'addtype', 'server-info', 'server-status',
172 // Annoying script name
175 // @TODO Misc/unsorted
176 'cgi-', '.eml', '$_request', '$_get', '$request', '$get', '.system',
177 '&aim', 'new_password', '&icq', '.conf', 'motd ', 'HTTP/1.',
178 'window.open', 'img src', '.jsp', 'servlet', 'org.apache',
179 'wwwacl', '/servlet/con', 'http_', 'secure_site, ok', 'chunked',
180 '<script', 'mod_gzip_status', '.system', 'uol.com', ',0x', '(0x',
181 'INSERT_RANDOM_NUMBER_HERE', '=passthru',
184 // BLock these words found in User-Agent
185 $GLOBALS['ctracker_ua_blacklist'] = [
187 'drop database', 'drop table',
189 // Compiler / interpreter (Linux)
190 'bin/g++ ', 'bin/c++ ', 'cc ', 'bin/python', 'bin/tclsh', 'bin/nasm', 'bin/perl',
196 'cmd.exe', 'nc.exe', 'ftp.exe', 'powershell', 'system.net.webclient',
197 'perl.exe', 'python.exe', 'python3.exe', 'bash.exe',
200 'allow_url_fopen', 'allow_url_include', 'auto_prepend_file', 'disable_functions', 'safe_mode',
202 // PHP commands to execute local shell commands
203 // @see https://de.php.net/<system|passtyhru>
204 'system(', 'passthru',
206 // More PHP commands/scripts
207 'fopen', 'fwrite', 'phpinfo()', '\<?', '?\>', 'base64_decode', 'file_put_contents',
208 'set_magic_quotes_runtime', 'set_magic_quotes_runtime', 'display_errors',
211 // Server configuration (e.g. Apache)
212 'application/x-httpd-php',
214 // Typical PHP script remote-inclusions and typical include file names
215 '.inc.php', '.lib.php', '.class.php', 'config.php', '.inc', '_php',
216 'php_', 'class_', '_class.php', 'db_mysql.inc',
219 '_phplib', '__callbackparam',
221 // Request header being inserted
224 // /proc/ and other forbidden paths
228 // Block these words found in POST requests
229 $GLOBALS['ctracker_post_blacklist'] = [
230 // This line is for detecting hidden link spam in wikis, forums, guestbooks, etc.
231 ' style=', 'overflow:auto', 'height:1px', 'width:1px', 'display:hidden', 'style.display',
234 'cmd.exe', 'nc.exe', 'ftp.exe', 'powershell', 'system.net.webclient',
236 // Server configuration (e.g. Apache)
237 'application/x-httpd-php',
239 // Annoying script name
242 // "Common" login names from VHCS exploiters
243 'starhack', 'DeLiMehmet', 'hisset', 'Hisset', 'delimert', 'MecTruy'
246 // Also block these requests (mostly you don't want CONNECT to some SMTP sites)
247 $GLOBALS['ctracker_blocked_methods'] = [
251 // Init more elements
252 $GLOBALS['ctracker_post_track'] = '';
253 $GLOBALS['ctracker_checked_get'] = '';
254 $GLOBALS['ctracker_checked_post'] = '';
255 $GLOBALS['ctracker_checked_ua'] = '';
259 function isCrackerTrackerWormDetected () {
260 // Check against the whole list
261 $GLOBALS['ctracker_checked_get'] = urldecode(str_ireplace($GLOBALS['ctracker_get_blacklist'], '*', crackerTrackerQueryString(true)));
262 $GLOBALS['ctracker_checked_ua'] = urldecode(str_ireplace($GLOBALS['ctracker_ua_blacklist'], '*', crackerTrackerUserAgent(true)));
265 * If it differs to original and the *whole* request string is not in
266 * whitelist then blog the attempt.
270 $GLOBALS['ctracker_checked_get'] != crackerTrackerQueryString(true) && (!in_array(crackerTrackerQueryString(true), $GLOBALS['ctracker_whitelist']))
272 $GLOBALS['ctracker_checked_ua'] != crackerTrackerUserAgent(true)
274 isset($GLOBALS['ctracker_blocked_methods'][crackerTrackerRequestMethod()])
277 //* 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);
284 function isCrackerTrackerPostAttackDetected () {
285 // Implode recursive the whole $_POST array
286 $GLOBALS['ctracker_post_track'] = urldecode(implode_r('&', $_POST));
288 // Check for suspicious POST data
289 $GLOBALS['ctracker_checked_post'] = urldecode(str_ireplace($GLOBALS['ctracker_post_blacklist'], '*', crackerTrackerSanitize($GLOBALS['ctracker_post_track'])));
292 return ((isCrackerTrackerWormDetected()) || ($GLOBALS['ctracker_checked_post'] != crackerTrackerSanitize($GLOBALS['ctracker_post_track'])));
295 // Prepares a mail and send it out
296 function sendCrackerTrackerMail () {
298 crackerTrackerLogAttack();
301 $mail = 'Attack detected:
302 -----------------------------------------------------
303 Remote-IP : ' . determineCrackerTrackerRealRemoteAddress() . '
304 User-Agent : ' . crackerTrackerUserAgent() . '
305 Request-string : ' . crackerTrackerQueryString() . '
306 Filtered string : ' . $GLOBALS['ctracker_checked_get'] . '
307 Server : ' . crackerTrackerServerName() . '
308 Script : ' . crackerTrackerScriptName() . '
309 Method : ' . crackerTrackerRequestMethod() . '
310 Referrer : ' . crackerTrackerReferer() . '
311 -----------------------------------------------------
315 crackerTrackerSendMail($mail);
321 // Sends the ticket emails out
322 function sendCrackerTrackerTicketMails () {
323 // Load user template
324 $mail = crackerTrackerLoadEmailTemplate('user_add_ticket', $GLOBALS['ctracker_last_ticket']);
326 // Send email to the user
327 crackerTrackerSendMail($mail, $GLOBALS['ctracker_last_ticket']['ctracker_ticket_email'], getCrackerTrackerLocalized('user_add_ticket_subject'));
329 // Load webmaster template
330 $mail = crackerTrackerLoadEmailTemplate('webmaster_add_ticket', $GLOBALS['ctracker_last_ticket'], getCrackerTrackerConfig('ctracker_language'));
332 // Send email to the user
333 crackerTrackerSendMail($mail, NULL, getCrackerTrackerLocalized('webmaster_add_ticket_subject'));
337 function crackerTrackerSendMail (string $mail, string $recipient = NULL, string $subject = NULL) {
338 // Construct dummy array
340 'remote_addr' => determineCrackerTrackerRealRemoteAddress(),
341 'proxy_addr' => getenv('REMOTE_ADDR'),
342 'check_get' => $GLOBALS['ctracker_checked_get'],
343 'server_name' => crackerTrackerServerName(),
344 'request_method' => crackerTrackerRequestMethod(),
347 // Only send email if not yet found
348 if (!isCrackerTrackerEntryFound($rowData)) {
349 // Send the email out only in non-debug mode
350 if (isCrackerTrackerDebug()) {
352 print 'Recipient=' . $recipient . '<br />Subject=' . $subject . '<br />Text=<pre>' . $mail . '</pre>';
356 } elseif (!is_null($recipient)) {
357 // Recipient specified
358 return mail($recipient, $subject, $mail, $GLOBALS['ctracker_header']);
359 } elseif (isset($GLOBALS['ctracker_email'])) {
361 return mail($GLOBALS['ctracker_email'], 'CTracker: Attack detected!', $mail, $GLOBALS['ctracker_header']);
363 // Send it the deprecated way with constant
364 return mail(constant('__CTRACKER_EMAIL'), 'CTracker: Attack detected!', $mail, $GLOBALS['ctracker_header']);
366 } elseif (isCrackerTrackerDebug()) {
368 print 'Recipient=' . $recipient . '<br />Subject=' . $subject . '<br />Text=<pre>' . $mail . '</pre>';
375 // Sends a detected POST attack mail
376 function sendCrackerTrackerPostMail () {
378 crackerTrackerLogAttack();
381 $mail = 'POST-Attack detected:
382 -----------------------------------------------------
383 Remote-IP : ' . determineCrackerTrackerRealRemoteAddress() . '
384 User-Agent : ' . crackerTrackerUserAgent() . '
385 Request-string : ' . crackerTrackerQueryString() . '
386 Filtered string : ' . $GLOBALS['ctracker_checked_get'] . '
387 Server : ' . crackerTrackerServerName() . '
388 Script : ' . crackerTrackerScriptName() . '
389 Method : ' . crackerTrackerRequestMethod() . '
390 Referrer : ' . crackerTrackerReferer() . '
391 -----------------------------------------------------
392 POST string : ' . $GLOBALS['ctracker_post_track'] . '
393 Filtered POST string : ' . $GLOBALS['ctracker_checked_post'] . '
394 -----------------------------------------------------
398 crackerTrackerSendMail($mail);
404 // Sleeps for a random time and aborts the script
405 function crackerTrackerDie () {
406 // Check if link is up
407 if (isCrackerTrackerDatabaseLinkUp()) {
408 // Close database link
409 crackerTrackerCloseDatabaseLink();
412 // Do only sleep if debug/developer mode is not enabled
413 if (!isCrackerTrackerDebug()) {
414 // Sleep a little to waste the attacker's time
415 sleep(mt_rand(10,30));
419 if (isCrackerTrackerDebug()) {
420 // With debug backtrace in debug/developer mode
422 debug_print_backtrace();
430 // Logs the attack attempt
431 function crackerTrackerLogAttack () {
432 // Aquire database link
433 aquireCrackerTrackerDatabaseLink();
435 // Prepare array for database insert
437 'remote_addr' => determineCrackerTrackerRealRemoteAddress(),
438 'proxy_addr' => getenv('REMOTE_ADDR'),
439 'user_agent' => crackerTrackerUserAgent(),
440 'get_data' => crackerTrackerQueryString(),
441 'post_data' => $GLOBALS['ctracker_post_track'],
442 'check_ua' => $GLOBALS['ctracker_checked_ua'],
443 'check_get' => $GLOBALS['ctracker_checked_get'],
444 'check_post' => $GLOBALS['ctracker_checked_post'],
445 'server_name' => crackerTrackerServerName(),
446 'script_name' => crackerTrackerScriptName(),
447 'referer' => crackerTrackerReferer(),
448 'request_method' => crackerTrackerRequestMethod(),
449 'proxy_used' => (isCrackerTrackerProxyUsed() ? 'Y' : 'N'),
450 'first_attempt' => 'NOW()',
453 // Insert the array in database
454 crackerTrackerInsertArray('ctracker_data', $rowData);
457 // Increases count of spam-bot accesses for the current IP
458 function ctrackerIncreaseAntiSpambotCount () {
461 'remote_addr' => determineCrackerTrackerRealRemoteAddress(),
462 'proxy_addr' => getenv('REMOTE_ADDR'),
466 updateCrackerTrackerEntry($rowData, 'spam_bot_detections');
469 // Alerts the current user about malicious/suspicious traffic
470 function crackerTrackerAlertCurrentUser () {
471 // Is there some data?
472 if (isset($GLOBALS['ctracker_last_suspicious_entry'])) {
473 // Does the user have a ticket?
474 if (ifCtrackerTrackerAntiSpamFieldGiven()) {
475 // Spam-bot filled out anti-spam field, register count
476 ctrackerIncreaseAntiSpambotCount();
478 // Output message to spammer
479 ctrackerTrackerLoadTemplate('bot_captured');
480 } elseif (ifCrackerTrackerIpHasTicket()) {
481 // Load "Thank you" template
482 crackerTrackerLoadTemplate('add_ticket_thanks');
483 } elseif ((isset($_POST['ctracker_add_ticket'])) && (!empty($_POST['name'])) && (!empty($_POST['email']))) {
485 addCrackerTrackerTicket($_POST);
487 // Send the email out
488 sendCrackerTrackerTicketMails();
490 // Display the form for new ticket
491 crackerTrackerLoadTemplate('add_ticket');