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'] = array(
43 'cmd=lostpw', // LinPHA
46 // Attacks we should detect and block
47 $GLOBALS['ctracker_get_blacklist'] = array(
49 'union ', ' union', 'insert ',
50 'select ', ' like', 'like ', 'drop ', 'update ',
53 // $GLOBAL/$_SERVER array elements
54 'HTTP_USER_AGENT', 'HTTP_HOST', 'HTTP_PHP', '_SESSION', 'CFG_ROOT',
55 'DOCUMENT_ROOT', '_SERVER',
58 '/environ', 'etc/shadow', 'etc/gshadow', 'etc/passwd', 'etc/group',
59 'etc/./shadow', 'etc/./gshadow', 'etc/./passwd', 'etc/./group',
60 '.htaccess', '.htpasswd', '.htgroup', '.history', 'bash_history',
63 // Other Linux/FreeBSD/??? programs (sometimes with space)
64 'traceroute ', 'ping ', 'bin/xterm', 'bin/./xterm', 'lsof ',
65 'telnet ', 'wget ', 'bin/perl', 'bin/id', 'uname\x20', 'uname ',
66 'killall', 'diff ', 'kill ', 'locate ', 'grep ', 'vi ', 'mv ',
67 'rmdir ', 'mcd ', 'mrd ', 'rm ', ' mcd', ' mrd', ' rm',
68 'passwd ', ' passwd', 'mdir ', ' mdir', 'cp ', ' cp',
69 'esystem ', 'chr ', ' chr', 'wget ', ' wget', ' cmd',
70 'cmd ', ' rush', 'rush ', ' echr', 'echr ', ' getenv',
71 'getenv', 'reboot ', 'halt ', 'powerdown ',
73 // Other Linux programs (+ brace)
74 'locate(', 'grep(', 'kill(', 'mcd(', 'mrd(', 'rm(', 'mv(', 'rmdir(',
75 'chmod(', 'chmod(', 'chown(', 'chgrp(', 'passwd(', 'vi(', 'cp(',
76 'mdir(', 'system(', 'chr(', 'wget(', 'rush(', 'echr(',
78 // Other Linux programs (+ equal)
79 'mcd=', 'mrd=', 'chmod=', 'chr=', 'rush=', 'echr=',
82 '/etc/', '/bin/', '/sbin/', '/self/', '/proc/', '../../','..//', '././',
83 '/home/ftp', '/home/./ftp', '/home/./www', '/home/www', '/www/virtual/',
87 '/chgrp', '/chown', '/chmod', 'chown ', 'chmod ', 'chgrp ',
89 // Compiler/interpreter
90 'bin/g++ ', 'bin/c++ ', 'cc ', 'bin/python', 'bin/python', 'bin/tclsh',
91 'bin/tclsh', 'bin/nasm', '/perl', 'cmd.exe',
95 'allow_url_fopen', 'allow_url_include', 'auto_prepend_file', 'disable_functions', 'safe_mode',
97 // PHP commands/scripts
98 'fopen', 'fwrite', 'phpinfo()', '\<?', '?\>', 'base64_decode', 'file_put_contents',
99 'set_magic_quotes_runtime', 'set_magic_quotes_runtime', 'display_errors', 'passthru',
101 // Typical PHP script remote-inclusions and typical include file names
102 '.inc.php', '.lib.php', '.class.php', 'config.php', '.inc', '_php',
103 'php_', 'class_', '_class.php', 'db_mysql.inc',
106 '_phplib', '__callbackparam',
108 // Generic remote inclusion
109 '=http://', '=https://',
111 '=%7BQUOT%7D', '=%5C', '=%22http','=%22ftp','=%22file','=%27http','=%27ftp',
115 'data://', 'tcp://', 'udp://', 'raw://', 'javascript://', 'file://', 'ftp://',
118 'nigga(', ' nigga', 'nigga ', 'starhack', 'busca',
121 ' style=', 'style =',
122 'overflow:auto', 'overflow: auto',
123 'overflow :auto', 'overflow : auto',
124 'display:hidden', 'display: hidden',
125 'display :hidden', 'display : hidden',
126 'height:0px', 'height: 0px','height:1px', 'height: 1px',
127 'width:0px', 'width: 0px','width:1px', 'width: 1px',
129 // Uncommon user websites
130 '~root', '~ftp', '~nobody',
132 // Windows XP (?) hacks
133 'xp_enumdsn', 'xp_availablemedia', 'xp_filelist', 'xp_cmdshell',
135 // Attempts to insert links into a badly secured URL
138 // Request header being inserted
141 // /proc/ and other forbidden paths
144 // MySQL internal functions
147 // @TODO Misc/unsorted
148 'cgi-', '.eml', '$_request', '$_get', '$request', '$get', '.system',
149 '&aim', 'new_password', '&icq', '.conf', 'motd ', 'HTTP/1.',
150 'window.open', 'img src', 'img src', '.jsp', 'servlet', 'org.apache',
151 'wwwacl', 'server-info', 'server-status', '/servlet/con', 'http_',
152 'secure_site, ok', 'chunked''<script', 'mod_gzip_status', '.system',
153 'uol.com', ',0x', '(0x'
156 // BLock these words found in User-Agent
157 $GLOBALS['ctracker_ua_blacklist'] = array(
158 // Compiler/interpreter
159 'bin/g++ ', 'bin/c++ ', 'cc ', 'bin/python', 'bin/python', 'bin/tclsh',
160 'bin/tclsh', 'bin/nasm', '/perl', 'cmd.exe',
161 'nc.exe', 'ftp.exe', 'wget ', 'system(', 'curl ',
164 'allow_url_fopen', 'allow_url_include', 'auto_prepend_file', 'disable_functions', 'safe_mode',
166 // PHP commands/scripts
167 'fopen', 'fwrite', 'phpinfo()', '\<?', '?\>', 'base64_decode', 'file_put_contents',
168 'set_magic_quotes_runtime', 'set_magic_quotes_runtime', 'display_errors', 'passthru',
170 // Typical PHP script remote-inclusions and typical include file names
171 '.inc.php', '.lib.php', '.class.php', 'config.php', '.inc', '_php',
172 'php_', 'class_', '_class.php', 'db_mysql.inc',
175 '_phplib', '__callbackparam',
177 // Request header being inserted
180 // /proc/ and other forbidden paths
184 // Block these words found in POST requests
185 $GLOBALS['ctracker_post_blacklist'] = array(
186 // This line is for detecting hidden link spam in wikis, forums, guestbooks, etc.
187 ' style=', 'overflow:auto', 'height:1px', 'width:1px', 'display:hidden', 'style.display',
189 // "Common" login names from VHCS exploiters ;-)
190 'starhack', 'DeLiMehmet', 'hisset', 'Hisset', 'delimert', 'MecTruy'
193 // Also block these requests (mostly you don't want CONNECT to some SMTP sites)
194 $GLOBALS['ctracker_blocked_requests'] = array('CONNECT' => TRUE);
196 // Init more elements
197 $GLOBALS['ctracker_post_track'] = '';
198 $GLOBALS['ctracker_checked_get'] = '';
199 $GLOBALS['ctracker_checked_post'] = '';
200 $GLOBALS['ctracker_checked_ua'] = '';
204 function isCrackerTrackerWormDetected () {
205 // Check against the whole list
206 $GLOBALS['ctracker_checked_get'] = urldecode(str_ireplace($GLOBALS['ctracker_get_blacklist'], '*', crackerTrackerQueryString(TRUE)));
207 $GLOBALS['ctracker_checked_ua'] = urldecode(str_ireplace($GLOBALS['ctracker_ua_blacklist'], '*', crackerTrackerUserAgent(TRUE)));
210 * If it differs to original and the *whole* request string is not in
211 * whitelist then blog the attempt.
215 $GLOBALS['ctracker_checked_get'] != crackerTrackerQueryString(TRUE) && (!in_array(crackerTrackerQueryString(TRUE), $GLOBALS['ctracker_whitelist']))
217 $GLOBALS['ctracker_checked_ua'] != crackerTrackerUserAgent(TRUE)
219 isset($GLOBALS['ctracker_blocked_requests'][crackerTrackerRequestMethod()])
222 //* 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);
229 function isCrackerTrackerPostAttackDetected () {
230 // Implode recursive the whole $_POST array
231 $GLOBALS['ctracker_post_track'] = urldecode(implode_r('&', $_POST));
233 // Check for suspicious POST data
234 $GLOBALS['ctracker_checked_post'] = urldecode(str_ireplace($GLOBALS['ctracker_post_blacklist'], '*', crackerTrackerSanitize($GLOBALS['ctracker_post_track'])));
237 return ((isCrackerTrackerWormDetected()) || ($GLOBALS['ctracker_checked_post'] != crackerTrackerSanitize($GLOBALS['ctracker_post_track'])));
240 // Prepares a mail and send it out
241 function sendCrackerTrackerMail () {
243 crackerTrackerLogAttack();
246 $mail = 'Attack detected:
247 -----------------------------------------------------
248 Remote-IP : ' . determineCrackerTrackerRealRemoteAddress() . '
249 User-Agent : ' . crackerTrackerUserAgent() . '
250 Request-string : ' . crackerTrackerQueryString() . '
251 Filtered string : ' . $GLOBALS['ctracker_checked_get'] . '
252 Server : ' . crackerTrackerServerName() . '
253 Script : ' . crackerTrackerScriptName() . '
254 Referrer : ' . crackerTrackerReferer() . '
255 -----------------------------------------------------
259 crackerTrackerSendMail($mail);
265 // Sends the ticket emails out
266 function sendCrackerTrackerTicketMails () {
267 // Load user template
268 $mail = crackerTrackerLoadEmailTemplate('user_add_ticket', $GLOBALS['ctracker_last_ticket']);
270 // Send email to the user
271 crackerTrackerSendMail($mail, $GLOBALS['ctracker_last_ticket']['ctracker_ticket_email'], getCrackerTrackerLocalized('user_add_ticket_subject'));
273 // Load webmaster template
274 $mail = crackerTrackerLoadEmailTemplate('webmaster_add_ticket', $GLOBALS['ctracker_last_ticket'], getCrackerTrackerConfig('ctracker_language'));
276 // Send email to the user
277 crackerTrackerSendMail($mail, NULL, getCrackerTrackerLocalized('webmaster_add_ticket_subject'));
281 function crackerTrackerSendMail ($mail, $recipient = NULL, $subject = NULL) {
282 // Construct dummy array
284 'remote_addr' => determineCrackerTrackerRealRemoteAddress(),
285 'proxy_addr' => getenv('REMOTE_ADDR'),
286 'check_get' => $GLOBALS['ctracker_checked_get'],
287 'server_name' => crackerTrackerServerName()
290 // Only send email if not yet found
291 if (!isCrackerTrackerEntryFound($rowData)) {
292 // Send the email out only in non-debug mode
293 if (isCrackerTrackerDebug()) {
295 print 'Recipient=' . $recipient . '<br />Subject=' . $subject . '<br />Text=<pre>' . $mail . '</pre>';
299 } elseif (!is_null($recipient)) {
300 // Recipient specified
301 return mail($recipient, $subject, $mail, $GLOBALS['ctracker_header']);
302 } elseif (isset($GLOBALS['ctracker_email'])) {
304 return mail($GLOBALS['ctracker_email'], 'CTracker: Attack detected!', $mail, $GLOBALS['ctracker_header']);
306 // Send it the deprecated way with constant
307 return mail(constant('__CTRACKER_EMAIL'), 'CTracker: Attack detected!', $mail, $GLOBALS['ctracker_header']);
309 } elseif (isCrackerTrackerDebug()) {
311 print 'Recipient=' . $recipient . '<br />Subject=' . $subject . '<br />Text=<pre>' . $mail . '</pre>';
318 // Sends a detected POST attack mail
319 function sendCrackerTrackerPostMail () {
321 crackerTrackerLogAttack();
324 $mail = 'POST-Attack detected:
325 -----------------------------------------------------
326 Remote-IP : ' . determineCrackerTrackerRealRemoteAddress() . '
327 User-Agent : ' . crackerTrackerUserAgent() . '
328 Request-string : ' . crackerTrackerQueryString() . '
329 Filtered string : ' . $GLOBALS['ctracker_checked_get'] . '
330 Server : ' . crackerTrackerServerName() . '
331 Script : ' . crackerTrackerScriptName() . '
332 Referrer : ' . crackerTrackerReferer() . '
333 -----------------------------------------------------
334 POST string : ' . $GLOBALS['ctracker_post_track'] . '
335 Filtered POST string : ' . $GLOBALS['ctracker_checked_post'] . '
336 -----------------------------------------------------
340 crackerTrackerSendMail($mail);
346 // Sleeps for a random time and aborts the script
347 function crackerTrackerDie () {
348 // Close database link
349 crackerTrackerCloseDatabaseLink();
351 // Do only sleep if debug/developer mode is not enabled
352 if (!isCrackerTrackerDebug()) {
353 // Sleep a little to waste the attacker's time
354 sleep(mt_rand(10,30));
358 if (isCrackerTrackerDebug()) {
359 // With debug backtrace in debug/developer mode
361 debug_print_backtrace();
369 // Logs the attack attempt
370 function crackerTrackerLogAttack () {
371 // Aquire database link
372 aquireCrackerTrackerDatabaseLink();
374 // By default no proxy is used
377 // Did the attacker use a proxy?
378 if (isCrackerTrackerProxyUsed()) {
383 // Prepare array for database insert
385 'remote_addr' => determineCrackerTrackerRealRemoteAddress(),
386 'proxy_addr' => getenv('REMOTE_ADDR'),
387 'user_agent' => crackerTrackerUserAgent(),
388 'get_data' => crackerTrackerQueryString(),
389 'post_data' => $GLOBALS['ctracker_post_track'],
390 'check_ua' => $GLOBALS['ctracker_checked_ua'],
391 'check_get' => $GLOBALS['ctracker_checked_get'],
392 'check_post' => $GLOBALS['ctracker_checked_post'],
393 'server_name' => crackerTrackerServerName(),
394 'script_name' => crackerTrackerScriptName(),
395 'referer' => crackerTrackerReferer(),
396 'request_method' => crackerTrackerRequestMethod(),
397 'proxy_used' => $proxyUsed,
398 'first_attempt' => 'NOW()'
401 // Insert the array in database
402 crackerTrackerInsertArray('ctracker_data', $rowData);
405 // Alerts the current user about malicious/suspicious traffic
406 function crackerTrackerAlertCurrentUser () {
407 // Is there some data?
408 if (isset($GLOBALS['ctracker_last_suspicious_entry'])) {
409 // Does the user have a ticket?
410 if (ifCrackerTrackerIpHasTicket()) {
411 // Load "Thank you" template
412 crackerTrackerLoadTemplate('add_ticket_thanks');
413 } elseif ((isset($_POST['ctracker_add_ticket'])) && (!empty($_POST['name'])) && (!empty($_POST['email']))) {
415 addCrackerTrackerTicket($_POST);
417 // Send the email out
418 sendCrackerTrackerTicketMails();
420 // Display the form for new ticket
421 crackerTrackerLoadTemplate('add_ticket');