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',
92 // PHP commands/scripts
93 'fopen', 'fwrite', 'phpinfo()', '\<?', '?\>',
95 // Typical PHP script remote-inclusions and typical include file names
96 '.inc.php', '.lib.php', '.class.php', 'config.php', '.inc', '_php',
97 'php_', 'class_', '_class.php', 'db_mysql.inc',
99 // Generic remote inclusion
100 '=http://', '=https://',
102 '=%7BQUOT%7D', '=%5C', '=%22http','=%22ftp','=%22file','=%27http','=%27ftp',
106 'data://', 'tcp://', 'udp://', 'raw://', 'javascript://', 'file://', 'ftp://',
109 'nigga(', ' nigga', 'nigga ', 'starhack', 'busca',
112 ' style=', 'style =',
113 'overflow:auto', 'overflow: auto',
114 'overflow :auto', 'overflow : auto',
115 'display:hidden', 'display: hidden',
116 'display :hidden', 'display : hidden',
117 'height:0px', 'height: 0px','height:1px', 'height: 1px',
118 'width:0px', 'width: 0px','width:1px', 'width: 1px',
120 // Uncommon user websites
121 '~root', '~ftp', '~nobody',
123 // Windows XP (?) hacks
124 'xp_enumdsn', 'xp_availablemedia', 'xp_filelist', 'xp_cmdshell',
126 // Attempts to insert links into a badly secured URL
129 // @TODO Misc/unsorted
130 'cgi-', '.eml', '$_request', '$_get', '$request', '$get', '.system',
131 '&aim', 'new_password', '&icq', '.conf', 'motd ', 'HTTP/1.',
132 'window.open', 'img src', 'img src', '.jsp', 'servlet',
133 'wwwacl', '.js', '.jsp', 'server-info', 'server-status',
134 'secure_site, ok', 'chunked', 'org.apache', '/servlet/con',
135 '<script', 'mod_gzip_status', '.system', 'http_',
136 'uol.com', ',0x', '(0x',
139 // Block these words found in POST requests
140 $GLOBALS['ctracker_post_blacklist'] = array(
141 // This line is for detecting hidden link spam in wikis, forums, guestbooks, etc.
142 ' style=', 'overflow:auto', 'height:1px', 'width:1px', 'display:hidden', 'style.display',
144 // "Common" login names from VHCS exploiters ;-)
145 'starhack', 'DeLiMehmet', 'hisset', 'Hisset', 'delimert', 'MecTruy'
149 $GLOBALS['ctracker_header'] = crackerTrackerLoadEmailTemplate('header');
151 // Init more elements
152 $GLOBALS['ctracker_post_track'] = '';
153 $GLOBALS['ctracker_checkworm'] = '';
154 $GLOBALS['ctracker_check_post'] = '';
158 function isCrackerTrackerWormDetected () {
159 // Check against the whole list
160 $GLOBALS['ctracker_checkworm'] = urldecode(str_ireplace($GLOBALS['ctracker_get_blacklist'], '*', crackerTrackerQueryString()));
162 // If it differs to original and the *whole* request string is not in whitelist
163 // then blog the attempt
164 return ($GLOBALS['ctracker_checkworm'] != crackerTrackerQueryString() && (!in_array(crackerTrackerQueryString(), $GLOBALS['ctracker_whitelist'])));
168 function isCrackerTrackerPostAttackDetected () {
169 // Implode recursive the whole $_POST array
170 $GLOBALS['ctracker_post_track'] = urldecode(implode_r('', $_POST));
172 // Check for suspicious POST data
173 $GLOBALS['ctracker_check_post'] = str_ireplace($GLOBALS['ctracker_post_blacklist'], '*', $GLOBALS['ctracker_post_track']);
176 return ((isCrackerTrackerWormDetected()) || ($GLOBALS['ctracker_check_post'] != $GLOBALS['ctracker_post_track']));
179 // Prepares a mail and send it out
180 function sendCrackerTrackerMail () {
182 crackerTrackerLogAttack();
185 $mail = 'Attack detected:
186 -----------------------------------------------------
187 Remote-IP : ' . determineCrackerTrackerRealRemoteAddress() . '
188 User-Agent : ' . crackerTrackerUserAgent() . '
189 Request-string : ' . crackerTrackerQueryString() . '
190 Filtered string : ' . $GLOBALS['ctracker_checkworm'] . '
191 Server : ' . crackerTrackerServerName() . '
192 Script : ' . crackerTrackerScriptName() . '
193 Referrer : ' . crackerTrackerReferer() . '
194 -----------------------------------------------------
198 crackerTrackerSendMail($mail);
204 // Sends the ticket emails out
205 function sendCrackerTrackerTicketMails () {
206 // Load user template
207 $mail = crackerTrackerLoadEmailTemplate('user_add_ticket', $GLOBALS['ctracker_last_ticket']);
209 // Send email to the user
210 crackerTrackerSendMail($mail, $GLOBALS['ctracker_last_ticket']['ctracker_ticket_email'], getCrackerTrackerLocalized('user_add_ticket_subject'));
212 // Load webmaster template
213 $mail = crackerTrackerLoadEmailTemplate('webmaster_add_ticket', $GLOBALS['ctracker_last_ticket'], getCrackerTrackerConfig('ctracker_language'));
215 // Send email to the user
216 crackerTrackerSendMail($mail, NULL, getCrackerTrackerLocalized('webmaster_add_ticket_subject'));
220 function crackerTrackerSendMail ($mail, $recipient = NULL, $subject = NULL) {
221 // Construct dummy array
223 'remote_addr' => determineCrackerTrackerRealRemoteAddress(),
224 'proxy_addr' => getenv('REMOTE_ADDR'),
225 'check_worm' => $GLOBALS['ctracker_checkworm'],
226 'server_name' => crackerTrackerServerName()
229 // Only send email if not yet found
230 if (!isCrackerTrackerEntryFound($rowData)) {
231 // Send the email out only in non-debug mode
232 if (isCrackerTrackerDebug()) {
234 print 'Recipient=' . $recipient . '<br />Subject=' . $subject . '<br />Text=<pre>' . $mail . '</pre>';
238 } elseif (!is_null($recipient)) {
239 // Recipient specified
240 return mail($recipient, $subject, $mail, $GLOBALS['ctracker_header']);
241 } elseif (isset($GLOBALS['ctracker_email'])) {
243 return mail($GLOBALS['ctracker_email'], 'CTracker: Attack detected!', $mail, $GLOBALS['ctracker_header']);
245 // Send it the deprecated way with constant
246 return mail(constant('__CTRACKER_EMAIL'), 'CTracker: Attack detected!', $mail, $GLOBALS['ctracker_header']);
248 } elseif (isCrackerTrackerDebug()) {
250 print 'Recipient=' . $recipient . '<br />Subject=' . $subject . '<br />Text=<pre>' . $mail . '</pre>';
257 // Sends a detected POST attack mail
258 function sendCrackerTrackerPostMail () {
260 crackerTrackerLogAttack();
263 $mail = 'POST-Attack detected:
264 -----------------------------------------------------
265 Remote-IP : '.determineCrackerTrackerRealRemoteAddress().'
266 User-Agent : '.crackerTrackerUserAgent().'
267 Request-string : '.crackerTrackerQueryString().'
268 Filtered string : '.$GLOBALS['ctracker_checkworm'].'
269 Server : '.crackerTrackerServerName().'
270 Script : '.crackerTrackerScriptName().'
271 Referrer : '.crackerTrackerReferer().'
272 -----------------------------------------------------
273 POST string : '.$GLOBALS['ctracker_post_track'].'
274 Filtered POST string : '.$GLOBALS['ctracker_check_post'].'
275 -----------------------------------------------------
279 crackerTrackerSendMail($mail);
285 // Sleeps for a random time and aborts the script
286 function crackerTrackerDie () {
287 // Close database link
288 crackerTrackerCloseDatabaseLink();
290 // Do only sleep if debug/developer mode is not enabled
291 if (!isCrackerTrackerDebug()) {
292 // Sleep a little to waste the attacker's time
293 sleep(mt_rand(10,30));
297 if (isCrackerTrackerDebug()) {
298 // With debug backtrace in debug/developer mode
300 debug_print_backtrace();
308 // Logs the attack attempt
309 function crackerTrackerLogAttack () {
310 // Aquire database link
311 aquireCrackerTrackerDatabaseLink();
313 // By default no proxy is used
316 // Did the attacker use a proxy?
317 if (isCrackerTrackerProxyUsed()) {
322 // Prepare array for database insert
324 'remote_addr' => determineCrackerTrackerRealRemoteAddress(),
325 'proxy_addr' => getenv('REMOTE_ADDR'),
326 'user_agent' => crackerTrackerUserAgent(),
327 'get_data' => crackerTrackerQueryString(),
328 'post_data' => $GLOBALS['ctracker_post_track'],
329 'check_worm' => $GLOBALS['ctracker_checkworm'],
330 'check_post' => $GLOBALS['ctracker_check_post'],
331 'server_name' => crackerTrackerServerName(),
332 'script_name' => crackerTrackerScriptName(),
333 'referer' => crackerTrackerReferer(),
334 'proxy_used' => $proxyUsed,
335 'first_attempt' => 'NOW()'
338 // Insert the array in database
339 crackerTrackerInsertArray('ctracker_data', $rowData);
342 // Alerts the current user about malicious/suspicious traffic
343 function crackerTrackerAlertCurrentUser () {
344 // Is there some data?
345 if (isset($GLOBALS['ctracker_last_suspicious_entry'])) {
346 // Does the user have a ticket?
347 if (ifCrackerTrackerIpHasTicket()) {
348 // Should we continue?
349 if (isset($_POST['ctracker_continue'])) {
351 sendCrackerTrackerCookie();
353 // And redirect to same URL
354 crackerTrackerRedirectSameUrl();
355 } elseif (ifCrackerTrackerCookieIsSet()) {
356 // Return here to normal program
359 // Load "Thank you" template
360 crackerTrackerLoadTemplate('add_ticket_thanks');
362 } elseif ((isset($_POST['ctracker_add_ticket'])) && (!empty($_POST['name'])) && (!empty($_POST['email']))) {
364 addCrackerTrackerTicket($_POST);
366 // Send the email out
367 sendCrackerTrackerTicketMails();
369 // Display the form for new ticket
370 crackerTrackerLoadTemplate('add_ticket');