]> git.mxchange.org Git - ctracker.git/blob - libs/lib_detector.php
703e69cf80d7759fbc86d0718e6b3878e3c47a88
[ctracker.git] / libs / lib_detector.php
1 <?php
2 /**
3  * Detector library
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             3.0.0
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
10  *
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.
15  *
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.
20  *
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/>.
23  */
24
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);
31         } else {
32                 // No output
33                 @error_reporting(0);
34         }
35
36         // Whitelist some absolute query strings (see below)
37         $GLOBALS['ctracker_whitelist'] = array(
38                 'cmd=new',   // LinPHA
39                 'cmd=edit',  // LinPHA
40                 'cmd=lostpw' // LinPHA
41         );
42
43         // Attacks we should detect and block
44         $GLOBALS['ctracker_get_blacklist'] = array(
45                 'chr(', 'chr=', 'chr%20', '%20chr', 'wget%20', '%20wget', 'wget(',
46                 'cmd.exe', '%20cmd', 'cmd%20', 'rush=', '%20rush', 'rush%20',
47                 'union%20', '%20union', 'union(', 'union=', 'echr(', '%20echr', 'echr%20', 'echr=',
48                 'esystem(', 'esystem%20', 'cp%20', '%20cp', 'cp(', 'mdir%20', '%20mdir', 'mdir(',
49                 'mcd%20', 'mrd%20', 'rm%20', '%20mcd', '%20mrd', '%20rm',
50                 'mcd(', 'mrd(', 'rm(', 'mcd=', 'mrd=', 'mv%20', 'rmdir%20', 'mv(', 'rmdir(',
51                 'chmod(', 'chmod%20', '%20chmod', 'chmod(', 'chmod=', 'chown%20', 'chgrp%20', 'chown(', 'chgrp(',
52                 'locate%20', 'grep%20', 'locate(', 'grep(', 'diff%20', 'kill%20', 'kill(', 'killall',
53                 'passwd%20', '%20passwd', 'passwd(', 'telnet%20', 'vi(', 'vi%20', 'cgi-', '.eml',
54                 'insert%20into', 'select%20', 'nigga(', '%20nigga', 'nigga%20', 'fopen', 'fwrite', '%20like', 'like%20',
55                 '$_request', '$_get', '$request', '$get', '.system', 'HTTP_PHP', '&aim', '%20getenv', 'getenv%20',
56                 'new_password', '&icq','/etc/passwd','/etc/shadow', '/etc/groups', '/etc/gshadow',
57                 'HTTP_USER_AGENT', 'HTTP_HOST', 'wget%20', 'uname\x20-', 'uname%20-', 'bin/id', '/bin/', '/chgrp',
58                 '/chown', '/usr/bin', 'g\+\+', 'bin/python', 'bin/tclsh', 'bin/nasm', 'perl%20', 'traceroute%20',
59                 'ping%20', 'bin/xterm', 'lsof%20', '.conf', 'motd%20', 'HTTP/1.', '.inc.php', '.lib.php', '.class.php',
60                 'config.php', 'file\://', 'window.open', '<SCRIPT>', 'javascript\://', 'img src', 'img%20src', '.jsp',
61                 'ftp.exe', 'xp_enumdsn', 'xp_availablemedia', 'xp_filelist', 'xp_cmdshell', 'nc.exe', '.htpasswd',
62                 'servlet', '/etc/passwd', 'wwwacl', '~root', '~ftp', '.js', '.jsp', '.history', 'bash_history',
63                 '.bash_history', '~nobody', 'server-info', 'server-status', 'reboot%20', 'halt%20',
64                 'powerdown%20', '/home/ftp', '/home/www', 'secure_site, ok', 'chunked', 'org.apache', '/servlet/con',
65                 '<script', '/robot.txt' ,'/perl' ,'mod_gzip_status', 'db_mysql.inc', '.inc', 'select%20from',
66                 'select from', 'drop%20', '.system', 'getenv', 'http_', '_php', 'php_', 'phpinfo()', '\<?php', '?\>', 'sql=',
67                 'div style=', 'overflow: auto', 'height: 1px', 'cc%20', 'admin_action=', 'path=', 'action=http',
68                 'page=http', 'module=http', 'op=http', 'id=http', 'id%3Dhttp', 'action%3Dhttp', 'page%3Dhttp',
69                 'module%3Dhttp', 'op%3Dhttp', 'starhack', '../../','..//', 'directory=http', 'dir=http', 'busca',
70                 'uol.com', '=http://', '=https://','=ftp://','=file://','_SESSION','CFG_ROOT','/proc/', ',0x', '(0x',
71                 '=%7BQUOT%7D', '=%5C', 'DOCUMENT_ROOT', '_SERVER','=%22http','=%22ftp','=%22file','=%27http','=%27ftp',
72                 '=%27file'
73         );
74
75         // Block these words found in POST requests
76         $GLOBALS['ctracker_post_blacklist'] = array(
77                 // This line is for detecting hidden link spam in wikis, forums, guestbooks, etc.
78                 'div style=', 'overflow:auto', 'height:1px', 'width:1px', 'display:hidden',
79                 // "Common" login name from VHCS exploiters ;-)
80                 'starhack', 'DeLiMehmet', 'hisset', 'Hisset', 'delimert', 'MecTruy',
81                 'busca'
82         );
83
84         // Load email header
85         $GLOBALS['ctracker_header'] = crackerTrackerLoadEmailTemplate('header');
86
87         // Init more elements
88         $GLOBALS['ctracker_post_track'] = '';
89         $GLOBALS['ctracker_checkworm']  = '';
90         $GLOBALS['ctracker_check_post'] = '';
91 }
92
93 // Checks for worms
94 function isCrackerTrackerWormDetected () {
95         // Check against the whole list
96         $GLOBALS['ctracker_checkworm'] = str_replace($GLOBALS['ctracker_get_blacklist'], '*', crackerTrackerQueryString());
97
98         // If it differs to original and the *whole* request string is not in whitelist
99         // then blog the attempt
100         return ($GLOBALS['ctracker_checkworm'] != crackerTrackerQueryString() && (!in_array(crackerTrackerQueryString(), $GLOBALS['ctracker_whitelist'])));
101 }
102
103 // Checks POST data
104 function isCrackerTrackerPostAttackDetected () {
105         // Implode recursive the whole $_POST array
106         $GLOBALS['ctracker_post_track'] = implode_r('', $_POST);
107
108         // Check for suspicious POST data
109         $GLOBALS['ctracker_check_post'] = str_replace($GLOBALS['ctracker_post_blacklist'], '*', $GLOBALS['ctracker_post_track']);
110
111         // Is it detected?
112         return ((isCrackerTrackerWormDetected()) || ($GLOBALS['ctracker_check_post'] != $GLOBALS['ctracker_post_track']));
113 }
114
115 // Prepares a mail and send it out
116 function sendCrackerTrackerMail () {
117         // Log the attack
118         crackerTrackerLogAttack();
119
120         // Mail content
121         $mail = "Attack detected:
122 -----------------------------------------------------
123 Remote-IP       : ".determineCrackerTrackerRealRemoteAddress()."
124 User-Agent      : ".crackerTrackerUserAgent()."
125 Request-string  : ".crackerTrackerQueryString()."
126 Filtered string : ".$GLOBALS['ctracker_checkworm']."
127 Server          : ".crackerTrackerServerName()."
128 Script          : ".crackerTrackerScriptName()."
129 Referrer        : ".crackerTrackerReferer()."
130 -----------------------------------------------------
131 ";
132
133         // Send it out
134         crackerTrackerSendMail($mail);
135
136         // And die here
137         crackerTrackerDie();
138 }
139
140 // Sends the ticket emails out
141 function sendCrackerTrackerTicketMails () {
142         // Load user template
143         $mail = crackerTrackerLoadEmailTemplate('user_add_ticket', $GLOBALS['ctracker_last_ticket']);
144
145         // Send email to the user
146         crackerTrackerSendMail($mail, $GLOBALS['ctracker_last_ticket']['ctracker_ticket_email'], getCrackerTrackerLocalized('user_add_ticket_subject'));
147
148         // Load webmaster template
149         $mail = crackerTrackerLoadEmailTemplate('webmaster_add_ticket', $GLOBALS['ctracker_last_ticket'], getCrackerTrackerConfig('ctracker_language'));
150
151         // Send email to the user
152         crackerTrackerSendMail($mail, null, getCrackerTrackerLocalized('webmaster_add_ticket_subject'));
153 }
154
155 // Sends a mail out
156 function crackerTrackerSendMail ($mail, $recipient = null, $subject = null) {
157         // Construct dummy array
158         $rowData = array(
159                 'remote_addr' => determineCrackerTrackerRealRemoteAddress(),
160                 'check_worm'  => $GLOBALS['ctracker_checkworm'],
161                 'server_name' => crackerTrackerServerName()
162         );
163
164         // Only send email if not yet found
165         if (!isCrackerTrackerEntryFound($rowData)) {
166                 // Send the email out only in non-debug mode
167                 if (isCrackerTrackerDebug()) {
168                         // Output message
169                         print 'Recipient=' . $recipient . '<br />Subject=' . $subject . '<br />Text=<pre>' . $mail . '</pre>';
170
171                         // All fine
172                         return true;
173                 } elseif (!is_null($recipient)) {
174                         // Recipient specified
175                         return mail($recipient, $subject, $mail, $GLOBALS['ctracker_header']);
176                 } elseif (isset($GLOBALS['ctracker_email'])) {
177                         // Send it
178                         return mail($GLOBALS['ctracker_email'], 'CTracker: Attack detected!', $mail, $GLOBALS['ctracker_header']);
179                 } else {
180                         // Send it the deprecated way with constant
181                         return mail(constant('__CTRACKER_EMAIL'), 'CTracker: Attack detected!', $mail, $GLOBALS['ctracker_header']);
182                 }
183         } elseif (isCrackerTrackerDebug()) {
184                 // Output message
185                 print 'Recipient=' . $recipient . '<br />Subject=' . $subject . '<br />Text=<pre>' . $mail . '</pre>';
186
187                 // All fine
188                 return true;
189         }
190 }
191
192 // Sends a detected POST attack mail
193 function sendCrackerTrackerPostMail () {
194         // Log the attack
195         crackerTrackerLogAttack();
196
197         // Mail text
198         $mail = "POST-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 -----------------------------------------------------
208 POST string          : ".$GLOBALS['ctracker_post_track']."
209 Filtered POST string : ".$GLOBALS['ctracker_check_post']."
210 -----------------------------------------------------
211 ";
212
213         // Send it out
214         crackerTrackerSendMail($mail);
215
216         // And die here
217         crackerTrackerDie();
218 }
219
220 // Sleeps for a random time and aborts the script
221 function crackerTrackerDie () {
222         // Close database link
223         crackerTrackerCloseDatabaseLink();
224
225         // Do only sleep if debug/developer mode is not enabled
226         if (!isCrackerTrackerDebug()) {
227                 // Sleep a little to waste the attacker's time
228                 sleep(mt_rand(10,30));
229         } // END - if
230
231         // Bye, bye...
232         if (isCrackerTrackerDebug()) {
233                 // With debug backtrace in debug/developer mode
234                 print '<pre>';
235                 debug_print_backtrace();
236                 die('</pre>');
237         } else {
238                 // Simple die() call
239                 die();
240         }
241 }
242
243 // Logs the attack attempt
244 function crackerTrackerLogAttack () {
245         // Aquire database link
246         aquireCrackerTrackerDatabaseLink();
247
248         // By default no proxy is used
249         $proxyUsed = 'N';
250
251         // Did the attacker use a proxy?
252         if (isCrackerTrackerProxyUsed()) {
253                 // Set it
254                 $proxyUsed = 'Y';
255         } // END - if
256
257         // Prepare array for database insert
258         $rowData = array(
259                 'remote_addr'   => determineCrackerTrackerRealRemoteAddress(),
260                 'user_agent'    => crackerTrackerUserAgent(),
261                 'get_data'      => crackerTrackerQueryString(),
262                 'post_data'     => $GLOBALS['ctracker_post_track'],
263                 'check_worm'    => $GLOBALS['ctracker_checkworm'],
264                 'check_post'    => $GLOBALS['ctracker_check_post'],
265                 'server_name'   => crackerTrackerServerName(),
266                 'script_name'   => crackerTrackerScriptName(),
267                 'referer'       => crackerTrackerReferer(),
268                 'proxy_used'    => $proxyUsed,
269                 'first_attempt' => 'NOW()'
270         );
271
272         // Insert the array in database
273         crackerTrackerInsertArray('ctracker_data', $rowData);
274 }
275
276 // Alerts the current user about malicious/suspicious traffic
277 function crackerTrackerAlertCurrentUser () {
278         // Is there some data?
279         if (isset($GLOBALS['ctracker_last_suspicious_entry'])) {
280                 // Does the user have a ticket?
281                 if (ifCrackerTrackerIpHasTicket()) {
282                         // Should we continue?
283                         if (isset($_POST['ctracker_continue'])) {
284                                 // Set cookie
285                                 sendCrackerTrackerCookie();
286
287                                 // And redirect to same URL
288                                 crackerTrackerRedirectSameUrl();
289                         } elseif (ifCrackerTrackerCookieIsSet()) {
290                                 // Return here to normal program
291                                 return;
292                         } else {
293                                 // Load "Thank you" template
294                                 crackerTrackerLoadTemplate('add_ticket_thanks');
295                         }
296                 } elseif ((isset($_POST['ctracker_add_ticket'])) && (!empty($_POST['name'])) && (!empty($_POST['email']))) {
297                         // Add the ticket
298                         addCrackerTrackerTicket($_POST);
299
300                         // Send the email out
301                         sendCrackerTrackerTicketMails();
302                 } else {
303                         // Display the form for new ticket
304                         crackerTrackerLoadTemplate('add_ticket');
305                 }
306         } // END - if
307
308         // And stop here
309         die();
310 }
311
312 // [EOF]
313 ?>