5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2009 Cracker Tracker Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.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);
36 // Whitelist some absolute query strings (see below)
37 $GLOBALS['ctracker_whitelist'] = array(
40 'cmd=lostpw', // LinPHA
41 'secure_session=1', // Mantis Bug Tracker
44 // Attacks we should detect and block
45 $GLOBALS['ctracker_get_blacklist'] = array(
47 'union ', ' union', 'insert ',
48 'select ', ' like', 'like ', 'drop ', 'update ',
51 // $GLOBAL/$_SERVER array elements
52 'HTTP_USER_AGENT', 'HTTP_HOST', 'HTTP_PHP', '_SESSION','CFG_ROOT',
53 'DOCUMENT_ROOT', '_SERVER',
56 '/environ', 'etc/shadow', 'etc/gshadow', 'etc/passwd', 'etc/group',
57 'etc/./shadow', 'etc/./gshadow', 'etc/./passwd', 'etc/./group',
58 '.htaccess', '.htpasswd', '.htgroup', '.history', 'bash_history',
61 // Other Linux/FreeBSD/??? programs (sometimes with space)
62 'traceroute ', 'ping ', 'bin/xterm', 'bin/./xterm', 'lsof ',
63 'telnet ', 'wget ', 'bin/id', 'uname\x20', 'uname ', 'killall',
64 'diff ', 'kill ', 'locate ', 'grep ', 'vi ', 'mv ',
65 'rmdir ', 'mcd ', 'mrd ', 'rm ', ' mcd', ' mrd', ' rm',
66 'passwd ', ' passwd', 'mdir ', ' mdir', 'cp ', ' cp',
67 'esystem ', 'chr ', ' chr', 'wget ', ' wget', ' cmd',
68 'cmd ', ' rush', 'rush ', ' echr', 'echr ', ' getenv',
69 'getenv', 'reboot ', 'halt ', 'powerdown ',
71 // Other Linux programs (+ brace)
72 'locate(', 'grep(', 'kill(', 'mcd(', 'mrd(', 'rm(', 'mv(', 'rmdir(',
73 'chmod(', 'chmod(', 'chown(', 'chgrp(', 'passwd(', 'vi(', 'cp(',
74 'mdir(', 'esystem(', 'chr(', 'wget(', 'rush(', 'echr(',
76 // Other Linux programs (+ equal)
77 'mcd=', 'mrd=', 'chmod=', 'chr=', 'rush=', 'echr=',
80 '/etc/', '/bin/', '/sbin/', '/self/', '/proc/', '../../','..//', '././',
81 '/home/ftp', '/home/./ftp', '/home/./www', '/home/www', '/www/virtual/',
85 '/chgrp', '/chown', '/chmod', 'chown ', 'chmod ', 'chgrp ',
87 // Compiler/interpreter
88 'g++ ', 'c++ ', 'cc ', 'bin/./python', 'bin/python', 'bin/tclsh',
89 'bin/./tclsh', 'bin/nasm', 'bin/./nasm', '/perl', 'perl ', 'cmd.exe',
93 'allow_url_fopen', 'allow_url_include', 'auto_prepend_file', 'disable_functions', 'safe_mode',
95 // PHP commands/scripts
96 'fopen', 'fwrite', 'phpinfo()', '\<?', '?\>', 'base64_decode', 'file_put_contents',
97 'set_magic_quotes_runtime', 'set_magic_quotes_runtime', 'display_errors', 'passthru',,
99 // Typical PHP script remote-inclusions and typical include file names
100 '.inc.php', '.lib.php', '.class.php', 'config.php', '.inc', '_php',
101 'php_', 'class_', '_class.php', 'db_mysql.inc',
106 // Generic remote inclusion
107 '=http://', '=https://',
109 '=%7BQUOT%7D', '=%5C', '=%22http','=%22ftp','=%22file','=%27http','=%27ftp',
113 'data://', 'tcp://', 'udp://', 'raw://', 'javascript://', 'file://', 'ftp://',
116 'nigga(', ' nigga', 'nigga ', 'starhack', 'busca',
119 ' style=', 'style =',
120 'overflow:auto', 'overflow: auto',
121 'overflow :auto', 'overflow : auto',
122 'display:hidden', 'display: hidden',
123 'display :hidden', 'display : hidden',
124 'height:0px', 'height: 0px','height:1px', 'height: 1px',
125 'width:0px', 'width: 0px','width:1px', 'width: 1px',
127 // Uncommon user websites
128 '~root', '~ftp', '~nobody',
130 // Windows XP (?) hacks
131 'xp_enumdsn', 'xp_availablemedia', 'xp_filelist', 'xp_cmdshell',
133 // Attempts to insert links into a badly secured URL
136 // /proc/ and other forbidden paths
139 // @TODO Misc/unsorted
140 'cgi-', '.eml', '$_request', '$_get', '$request', '$get', '.system',
141 '&aim', 'new_password', '&icq', '.conf', 'motd ', 'HTTP/1.',
142 'window.open', 'img src', 'img src', '.jsp', 'servlet',
143 'wwwacl', '.js', '.jsp', 'server-info', 'server-status',
144 'secure_site, ok', 'chunked', 'org.apache', '/servlet/con',
145 '<script', 'mod_gzip_status', '.system', 'http_',
146 'uol.com', ',0x', '(0x'
149 // Block these words found in POST requests
150 $GLOBALS['ctracker_post_blacklist'] = array(
151 // This line is for detecting hidden link spam in wikis, forums, guestbooks, etc.
152 ' style=', 'overflow:auto', 'height:1px', 'width:1px', 'display:hidden', 'style.display',
154 // "Common" login names from VHCS exploiters ;-)
155 'starhack', 'DeLiMehmet', 'hisset', 'Hisset', 'delimert', 'MecTruy'
159 $GLOBALS['ctracker_header'] = crackerTrackerLoadEmailTemplate('header');
161 // Init more elements
162 $GLOBALS['ctracker_post_track'] = '';
163 $GLOBALS['ctracker_checkworm'] = '';
164 $GLOBALS['ctracker_check_post'] = '';
168 function isCrackerTrackerWormDetected () {
169 // Check against the whole list
170 $GLOBALS['ctracker_checkworm'] = urldecode(str_ireplace($GLOBALS['ctracker_get_blacklist'], '*', crackerTrackerQueryString()));
172 // If it differs to original and the *whole* request string is not in whitelist
173 // then blog the attempt
174 return ($GLOBALS['ctracker_checkworm'] != crackerTrackerQueryString() && (!in_array(crackerTrackerQueryString(), $GLOBALS['ctracker_whitelist'])));
178 function isCrackerTrackerPostAttackDetected () {
179 // Implode recursive the whole $_POST array
180 $GLOBALS['ctracker_post_track'] = urldecode(implode_r('', $_POST));
182 // Check for suspicious POST data
183 $GLOBALS['ctracker_check_post'] = str_ireplace($GLOBALS['ctracker_post_blacklist'], '*', $GLOBALS['ctracker_post_track']);
186 return ((isCrackerTrackerWormDetected()) || ($GLOBALS['ctracker_check_post'] != $GLOBALS['ctracker_post_track']));
189 // Prepares a mail and send it out
190 function sendCrackerTrackerMail () {
192 crackerTrackerLogAttack();
195 $mail = 'Attack detected:
196 -----------------------------------------------------
197 Remote-IP : ' . determineCrackerTrackerRealRemoteAddress() . '
198 User-Agent : ' . crackerTrackerUserAgent() . '
199 Request-string : ' . crackerTrackerQueryString() . '
200 Filtered string : ' . $GLOBALS['ctracker_checkworm'] . '
201 Server : ' . crackerTrackerServerName() . '
202 Script : ' . crackerTrackerScriptName() . '
203 Referrer : ' . crackerTrackerReferer() . '
204 -----------------------------------------------------
208 crackerTrackerSendMail($mail);
214 // Sends the ticket emails out
215 function sendCrackerTrackerTicketMails () {
216 // Load user template
217 $mail = crackerTrackerLoadEmailTemplate('user_add_ticket', $GLOBALS['ctracker_last_ticket']);
219 // Send email to the user
220 crackerTrackerSendMail($mail, $GLOBALS['ctracker_last_ticket']['ctracker_ticket_email'], getCrackerTrackerLocalized('user_add_ticket_subject'));
222 // Load webmaster template
223 $mail = crackerTrackerLoadEmailTemplate('webmaster_add_ticket', $GLOBALS['ctracker_last_ticket'], getCrackerTrackerConfig('ctracker_language'));
225 // Send email to the user
226 crackerTrackerSendMail($mail, NULL, getCrackerTrackerLocalized('webmaster_add_ticket_subject'));
230 function crackerTrackerSendMail ($mail, $recipient = NULL, $subject = NULL) {
231 // Construct dummy array
233 'remote_addr' => determineCrackerTrackerRealRemoteAddress(),
234 'proxy_addr' => getenv('REMOTE_ADDR'),
235 'check_worm' => $GLOBALS['ctracker_checkworm'],
236 'server_name' => crackerTrackerServerName()
239 // Only send email if not yet found
240 if (!isCrackerTrackerEntryFound($rowData)) {
241 // Send the email out only in non-debug mode
242 if (isCrackerTrackerDebug()) {
244 print 'Recipient=' . $recipient . '<br />Subject=' . $subject . '<br />Text=<pre>' . $mail . '</pre>';
248 } elseif (!is_null($recipient)) {
249 // Recipient specified
250 return mail($recipient, $subject, $mail, $GLOBALS['ctracker_header']);
251 } elseif (isset($GLOBALS['ctracker_email'])) {
253 return mail($GLOBALS['ctracker_email'], 'CTracker: Attack detected!', $mail, $GLOBALS['ctracker_header']);
255 // Send it the deprecated way with constant
256 return mail(constant('__CTRACKER_EMAIL'), 'CTracker: Attack detected!', $mail, $GLOBALS['ctracker_header']);
258 } elseif (isCrackerTrackerDebug()) {
260 print 'Recipient=' . $recipient . '<br />Subject=' . $subject . '<br />Text=<pre>' . $mail . '</pre>';
267 // Sends a detected POST attack mail
268 function sendCrackerTrackerPostMail () {
270 crackerTrackerLogAttack();
273 $mail = 'POST-Attack detected:
274 -----------------------------------------------------
275 Remote-IP : '.determineCrackerTrackerRealRemoteAddress().'
276 User-Agent : '.crackerTrackerUserAgent().'
277 Request-string : '.crackerTrackerQueryString().'
278 Filtered string : '.$GLOBALS['ctracker_checkworm'].'
279 Server : '.crackerTrackerServerName().'
280 Script : '.crackerTrackerScriptName().'
281 Referrer : '.crackerTrackerReferer().'
282 -----------------------------------------------------
283 POST string : '.$GLOBALS['ctracker_post_track'].'
284 Filtered POST string : '.$GLOBALS['ctracker_check_post'].'
285 -----------------------------------------------------
289 crackerTrackerSendMail($mail);
295 // Sleeps for a random time and aborts the script
296 function crackerTrackerDie () {
297 // Close database link
298 crackerTrackerCloseDatabaseLink();
300 // Do only sleep if debug/developer mode is not enabled
301 if (!isCrackerTrackerDebug()) {
302 // Sleep a little to waste the attacker's time
303 sleep(mt_rand(10,30));
307 if (isCrackerTrackerDebug()) {
308 // With debug backtrace in debug/developer mode
310 debug_print_backtrace();
318 // Logs the attack attempt
319 function crackerTrackerLogAttack () {
320 // Aquire database link
321 aquireCrackerTrackerDatabaseLink();
323 // By default no proxy is used
326 // Did the attacker use a proxy?
327 if (isCrackerTrackerProxyUsed()) {
332 // Prepare array for database insert
334 'remote_addr' => determineCrackerTrackerRealRemoteAddress(),
335 'proxy_addr' => getenv('REMOTE_ADDR'),
336 'user_agent' => crackerTrackerUserAgent(),
337 'get_data' => crackerTrackerQueryString(),
338 'post_data' => $GLOBALS['ctracker_post_track'],
339 'check_worm' => $GLOBALS['ctracker_checkworm'],
340 'check_post' => $GLOBALS['ctracker_check_post'],
341 'server_name' => crackerTrackerServerName(),
342 'script_name' => crackerTrackerScriptName(),
343 'referer' => crackerTrackerReferer(),
344 'proxy_used' => $proxyUsed,
345 'first_attempt' => 'NOW()'
348 // Insert the array in database
349 crackerTrackerInsertArray('ctracker_data', $rowData);
352 // Alerts the current user about malicious/suspicious traffic
353 function crackerTrackerAlertCurrentUser () {
354 // Is there some data?
355 if (isset($GLOBALS['ctracker_last_suspicious_entry'])) {
356 // Does the user have a ticket?
357 if (ifCrackerTrackerIpHasTicket()) {
358 // Load "Thank you" template
359 crackerTrackerLoadTemplate('add_ticket_thanks');
360 } elseif ((isset($_POST['ctracker_add_ticket'])) && (!empty($_POST['name'])) && (!empty($_POST['email']))) {
362 addCrackerTrackerTicket($_POST);
364 // Send the email out
365 sendCrackerTrackerTicketMails();
367 // Display the form for new ticket
368 crackerTrackerLoadTemplate('add_ticket');