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 'check_worm' => $GLOBALS['ctracker_checkworm'],
225 'server_name' => crackerTrackerServerName()
228 // Only send email if not yet found
229 if (!isCrackerTrackerEntryFound($rowData)) {
230 // Send the email out only in non-debug mode
231 if (isCrackerTrackerDebug()) {
233 print 'Recipient=' . $recipient . '<br />Subject=' . $subject . '<br />Text=<pre>' . $mail . '</pre>';
237 } elseif (!is_null($recipient)) {
238 // Recipient specified
239 return mail($recipient, $subject, $mail, $GLOBALS['ctracker_header']);
240 } elseif (isset($GLOBALS['ctracker_email'])) {
242 return mail($GLOBALS['ctracker_email'], 'CTracker: Attack detected!', $mail, $GLOBALS['ctracker_header']);
244 // Send it the deprecated way with constant
245 return mail(constant('__CTRACKER_EMAIL'), 'CTracker: Attack detected!', $mail, $GLOBALS['ctracker_header']);
247 } elseif (isCrackerTrackerDebug()) {
249 print 'Recipient=' . $recipient . '<br />Subject=' . $subject . '<br />Text=<pre>' . $mail . '</pre>';
256 // Sends a detected POST attack mail
257 function sendCrackerTrackerPostMail () {
259 crackerTrackerLogAttack();
262 $mail = 'POST-Attack detected:
263 -----------------------------------------------------
264 Remote-IP : '.determineCrackerTrackerRealRemoteAddress().'
265 User-Agent : '.crackerTrackerUserAgent().'
266 Request-string : '.crackerTrackerQueryString().'
267 Filtered string : '.$GLOBALS['ctracker_checkworm'].'
268 Server : '.crackerTrackerServerName().'
269 Script : '.crackerTrackerScriptName().'
270 Referrer : '.crackerTrackerReferer().'
271 -----------------------------------------------------
272 POST string : '.$GLOBALS['ctracker_post_track'].'
273 Filtered POST string : '.$GLOBALS['ctracker_check_post'].'
274 -----------------------------------------------------
278 crackerTrackerSendMail($mail);
284 // Sleeps for a random time and aborts the script
285 function crackerTrackerDie () {
286 // Close database link
287 crackerTrackerCloseDatabaseLink();
289 // Do only sleep if debug/developer mode is not enabled
290 if (!isCrackerTrackerDebug()) {
291 // Sleep a little to waste the attacker's time
292 sleep(mt_rand(10,30));
296 if (isCrackerTrackerDebug()) {
297 // With debug backtrace in debug/developer mode
299 debug_print_backtrace();
307 // Logs the attack attempt
308 function crackerTrackerLogAttack () {
309 // Aquire database link
310 aquireCrackerTrackerDatabaseLink();
312 // By default no proxy is used
315 // Did the attacker use a proxy?
316 if (isCrackerTrackerProxyUsed()) {
321 // Prepare array for database insert
323 'remote_addr' => determineCrackerTrackerRealRemoteAddress(),
324 'user_agent' => crackerTrackerUserAgent(),
325 'get_data' => crackerTrackerQueryString(),
326 'post_data' => $GLOBALS['ctracker_post_track'],
327 'check_worm' => $GLOBALS['ctracker_checkworm'],
328 'check_post' => $GLOBALS['ctracker_check_post'],
329 'server_name' => crackerTrackerServerName(),
330 'script_name' => crackerTrackerScriptName(),
331 'referer' => crackerTrackerReferer(),
332 'proxy_used' => $proxyUsed,
333 'first_attempt' => 'NOW()'
336 // Insert the array in database
337 crackerTrackerInsertArray('ctracker_data', $rowData);
340 // Alerts the current user about malicious/suspicious traffic
341 function crackerTrackerAlertCurrentUser () {
342 // Is there some data?
343 if (isset($GLOBALS['ctracker_last_suspicious_entry'])) {
344 // Does the user have a ticket?
345 if (ifCrackerTrackerIpHasTicket()) {
346 // Should we continue?
347 if (isset($_POST['ctracker_continue'])) {
349 sendCrackerTrackerCookie();
351 // And redirect to same URL
352 crackerTrackerRedirectSameUrl();
353 } elseif (ifCrackerTrackerCookieIsSet()) {
354 // Return here to normal program
357 // Load "Thank you" template
358 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');