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
44 'secure_session=1', // Mantis Bug Tracker
47 // Attacks we should detect and block
48 $GLOBALS['ctracker_get_blacklist'] = array(
50 'union ', ' union', 'insert ',
51 'select ', ' like', 'like ', 'drop ', 'update ',
54 // $GLOBAL/$_SERVER array elements
55 'HTTP_USER_AGENT', 'HTTP_HOST', 'HTTP_PHP', '_SESSION','CFG_ROOT',
56 'DOCUMENT_ROOT', '_SERVER',
59 '/environ', 'etc/shadow', 'etc/gshadow', 'etc/passwd', 'etc/group',
60 'etc/./shadow', 'etc/./gshadow', 'etc/./passwd', 'etc/./group',
61 '.htaccess', '.htpasswd', '.htgroup', '.history', 'bash_history',
64 // Other Linux/FreeBSD/??? programs (sometimes with space)
65 'traceroute ', 'ping ', 'bin/xterm', 'bin/./xterm', 'lsof ',
66 'telnet ', 'wget ', 'bin/id', 'uname\x20', 'uname ', 'killall',
67 'diff ', 'kill ', 'locate ', 'grep ', 'vi ', 'mv ',
68 'rmdir ', 'mcd ', 'mrd ', 'rm ', ' mcd', ' mrd', ' rm',
69 'passwd ', ' passwd', 'mdir ', ' mdir', 'cp ', ' cp',
70 'esystem ', 'chr ', ' chr', 'wget ', ' wget', ' cmd',
71 'cmd ', ' rush', 'rush ', ' echr', 'echr ', ' getenv',
72 'getenv', 'reboot ', 'halt ', 'powerdown ',
74 // Other Linux programs (+ brace)
75 'locate(', 'grep(', 'kill(', 'mcd(', 'mrd(', 'rm(', 'mv(', 'rmdir(',
76 'chmod(', 'chmod(', 'chown(', 'chgrp(', 'passwd(', 'vi(', 'cp(',
77 'mdir(', 'esystem(', 'chr(', 'wget(', 'rush(', 'echr(',
79 // Other Linux programs (+ equal)
80 'mcd=', 'mrd=', 'chmod=', 'chr=', 'rush=', 'echr=',
83 '/etc/', '/bin/', '/sbin/', '/self/', '/proc/', '../../','..//', '././',
84 '/home/ftp', '/home/./ftp', '/home/./www', '/home/www', '/www/virtual/',
88 '/chgrp', '/chown', '/chmod', 'chown ', 'chmod ', 'chgrp ',
90 // Compiler/interpreter
91 'g++ ', 'c++ ', 'cc ', 'bin/./python', 'bin/python', 'bin/tclsh',
92 'bin/./tclsh', 'bin/nasm', 'bin/./nasm', '/perl', 'perl ', 'cmd.exe',
96 'allow_url_fopen', 'allow_url_include', 'auto_prepend_file', 'disable_functions', 'safe_mode',
98 // PHP commands/scripts
99 'fopen', 'fwrite', 'phpinfo()', '\<?', '?\>', 'base64_decode', 'file_put_contents',
100 'set_magic_quotes_runtime', 'set_magic_quotes_runtime', 'display_errors', 'passthru',
102 // Typical PHP script remote-inclusions and typical include file names
103 '.inc.php', '.lib.php', '.class.php', 'config.php', '.inc', '_php',
104 'php_', 'class_', '_class.php', 'db_mysql.inc',
109 // Generic remote inclusion
110 '=http://', '=https://',
112 '=%7BQUOT%7D', '=%5C', '=%22http','=%22ftp','=%22file','=%27http','=%27ftp',
116 'data://', 'tcp://', 'udp://', 'raw://', 'javascript://', 'file://', 'ftp://',
119 'nigga(', ' nigga', 'nigga ', 'starhack', 'busca',
122 ' style=', 'style =',
123 'overflow:auto', 'overflow: auto',
124 'overflow :auto', 'overflow : auto',
125 'display:hidden', 'display: hidden',
126 'display :hidden', 'display : hidden',
127 'height:0px', 'height: 0px','height:1px', 'height: 1px',
128 'width:0px', 'width: 0px','width:1px', 'width: 1px',
130 // Uncommon user websites
131 '~root', '~ftp', '~nobody',
133 // Windows XP (?) hacks
134 'xp_enumdsn', 'xp_availablemedia', 'xp_filelist', 'xp_cmdshell',
136 // Attempts to insert links into a badly secured URL
139 // /proc/ and other forbidden paths
142 // MySQL internal functions
145 // @TODO Misc/unsorted
146 'cgi-', '.eml', '$_request', '$_get', '$request', '$get', '.system',
147 '&aim', 'new_password', '&icq', '.conf', 'motd ', 'HTTP/1.',
148 'window.open', 'img src', 'img src', '.jsp', 'servlet',
149 'wwwacl', '.js', '.jsp', 'server-info', 'server-status',
150 'secure_site, ok', 'chunked', 'org.apache', '/servlet/con',
151 '<script', 'mod_gzip_status', '.system', 'http_',
152 'uol.com', ',0x', '(0x'
155 // Block these words found in POST requests
156 $GLOBALS['ctracker_post_blacklist'] = array(
157 // This line is for detecting hidden link spam in wikis, forums, guestbooks, etc.
158 ' style=', 'overflow:auto', 'height:1px', 'width:1px', 'display:hidden', 'style.display',
160 // "Common" login names from VHCS exploiters ;-)
161 'starhack', 'DeLiMehmet', 'hisset', 'Hisset', 'delimert', 'MecTruy'
164 // Init more elements
165 $GLOBALS['ctracker_post_track'] = '';
166 $GLOBALS['ctracker_checkworm'] = '';
167 $GLOBALS['ctracker_check_post'] = '';
171 function isCrackerTrackerWormDetected () {
172 // Check against the whole list
173 $GLOBALS['ctracker_checkworm'] = urldecode(str_ireplace($GLOBALS['ctracker_get_blacklist'], '*', crackerTrackerQueryString()));
175 // If it differs to original and the *whole* request string is not in whitelist
176 // then blog the attempt
177 return ($GLOBALS['ctracker_checkworm'] != crackerTrackerQueryString() && (!in_array(crackerTrackerQueryString(), $GLOBALS['ctracker_whitelist'])));
181 function isCrackerTrackerPostAttackDetected () {
182 // Implode recursive the whole $_POST array
183 $GLOBALS['ctracker_post_track'] = urldecode(implode_r('', $_POST));
185 // Check for suspicious POST data
186 $GLOBALS['ctracker_check_post'] = str_ireplace($GLOBALS['ctracker_post_blacklist'], '*', $GLOBALS['ctracker_post_track']);
189 return ((isCrackerTrackerWormDetected()) || ($GLOBALS['ctracker_check_post'] != $GLOBALS['ctracker_post_track']));
192 // Prepares a mail and send it out
193 function sendCrackerTrackerMail () {
195 crackerTrackerLogAttack();
198 $mail = 'Attack detected:
199 -----------------------------------------------------
200 Remote-IP : ' . determineCrackerTrackerRealRemoteAddress() . '
201 User-Agent : ' . crackerTrackerUserAgent() . '
202 Request-string : ' . crackerTrackerQueryString() . '
203 Filtered string : ' . $GLOBALS['ctracker_checkworm'] . '
204 Server : ' . crackerTrackerServerName() . '
205 Script : ' . crackerTrackerScriptName() . '
206 Referrer : ' . crackerTrackerReferer() . '
207 -----------------------------------------------------
211 crackerTrackerSendMail($mail);
217 // Sends the ticket emails out
218 function sendCrackerTrackerTicketMails () {
219 // Load user template
220 $mail = crackerTrackerLoadEmailTemplate('user_add_ticket', $GLOBALS['ctracker_last_ticket']);
222 // Send email to the user
223 crackerTrackerSendMail($mail, $GLOBALS['ctracker_last_ticket']['ctracker_ticket_email'], getCrackerTrackerLocalized('user_add_ticket_subject'));
225 // Load webmaster template
226 $mail = crackerTrackerLoadEmailTemplate('webmaster_add_ticket', $GLOBALS['ctracker_last_ticket'], getCrackerTrackerConfig('ctracker_language'));
228 // Send email to the user
229 crackerTrackerSendMail($mail, NULL, getCrackerTrackerLocalized('webmaster_add_ticket_subject'));
233 function crackerTrackerSendMail ($mail, $recipient = NULL, $subject = NULL) {
234 // Construct dummy array
236 'remote_addr' => determineCrackerTrackerRealRemoteAddress(),
237 'proxy_addr' => getenv('REMOTE_ADDR'),
238 'check_worm' => $GLOBALS['ctracker_checkworm'],
239 'server_name' => crackerTrackerServerName()
242 // Only send email if not yet found
243 if (!isCrackerTrackerEntryFound($rowData)) {
244 // Send the email out only in non-debug mode
245 if (isCrackerTrackerDebug()) {
247 print 'Recipient=' . $recipient . '<br />Subject=' . $subject . '<br />Text=<pre>' . $mail . '</pre>';
251 } elseif (!is_null($recipient)) {
252 // Recipient specified
253 return mail($recipient, $subject, $mail, $GLOBALS['ctracker_header']);
254 } elseif (isset($GLOBALS['ctracker_email'])) {
256 return mail($GLOBALS['ctracker_email'], 'CTracker: Attack detected!', $mail, $GLOBALS['ctracker_header']);
258 // Send it the deprecated way with constant
259 return mail(constant('__CTRACKER_EMAIL'), 'CTracker: Attack detected!', $mail, $GLOBALS['ctracker_header']);
261 } elseif (isCrackerTrackerDebug()) {
263 print 'Recipient=' . $recipient . '<br />Subject=' . $subject . '<br />Text=<pre>' . $mail . '</pre>';
270 // Sends a detected POST attack mail
271 function sendCrackerTrackerPostMail () {
273 crackerTrackerLogAttack();
276 $mail = 'POST-Attack detected:
277 -----------------------------------------------------
278 Remote-IP : '.determineCrackerTrackerRealRemoteAddress().'
279 User-Agent : '.crackerTrackerUserAgent().'
280 Request-string : '.crackerTrackerQueryString().'
281 Filtered string : '.$GLOBALS['ctracker_checkworm'].'
282 Server : '.crackerTrackerServerName().'
283 Script : '.crackerTrackerScriptName().'
284 Referrer : '.crackerTrackerReferer().'
285 -----------------------------------------------------
286 POST string : '.$GLOBALS['ctracker_post_track'].'
287 Filtered POST string : '.$GLOBALS['ctracker_check_post'].'
288 -----------------------------------------------------
292 crackerTrackerSendMail($mail);
298 // Sleeps for a random time and aborts the script
299 function crackerTrackerDie () {
300 // Close database link
301 crackerTrackerCloseDatabaseLink();
303 // Do only sleep if debug/developer mode is not enabled
304 if (!isCrackerTrackerDebug()) {
305 // Sleep a little to waste the attacker's time
306 sleep(mt_rand(10,30));
310 if (isCrackerTrackerDebug()) {
311 // With debug backtrace in debug/developer mode
313 debug_print_backtrace();
321 // Logs the attack attempt
322 function crackerTrackerLogAttack () {
323 // Aquire database link
324 aquireCrackerTrackerDatabaseLink();
326 // By default no proxy is used
329 // Did the attacker use a proxy?
330 if (isCrackerTrackerProxyUsed()) {
335 // Prepare array for database insert
337 'remote_addr' => determineCrackerTrackerRealRemoteAddress(),
338 'proxy_addr' => getenv('REMOTE_ADDR'),
339 'user_agent' => crackerTrackerUserAgent(),
340 'get_data' => crackerTrackerQueryString(),
341 'post_data' => $GLOBALS['ctracker_post_track'],
342 'check_worm' => $GLOBALS['ctracker_checkworm'],
343 'check_post' => $GLOBALS['ctracker_check_post'],
344 'server_name' => crackerTrackerServerName(),
345 'script_name' => crackerTrackerScriptName(),
346 'referer' => crackerTrackerReferer(),
347 'request_method' => $_SERVER['REQUEST_METHOD'],
348 'proxy_used' => $proxyUsed,
349 'first_attempt' => 'NOW()'
352 // Insert the array in database
353 crackerTrackerInsertArray('ctracker_data', $rowData);
356 // Alerts the current user about malicious/suspicious traffic
357 function crackerTrackerAlertCurrentUser () {
358 // Is there some data?
359 if (isset($GLOBALS['ctracker_last_suspicious_entry'])) {
360 // Does the user have a ticket?
361 if (ifCrackerTrackerIpHasTicket()) {
362 // Load "Thank you" template
363 crackerTrackerLoadTemplate('add_ticket_thanks');
364 } elseif ((isset($_POST['ctracker_add_ticket'])) && (!empty($_POST['name'])) && (!empty($_POST['email']))) {
366 addCrackerTrackerTicket($_POST);
368 // Send the email out
369 sendCrackerTrackerTicketMails();
371 // Display the form for new ticket
372 crackerTrackerLoadTemplate('add_ticket');