]> git.mxchange.org Git - ctracker.git/blob - libs/lib_detector.php
eb9ed1d8ad3e15cc9f7b3a5cc07a5a9e78416694
[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['whitelist'] = array(
38                 'cmd=new',   // LinPHA
39                 'cmd=edit',  // LinPHA
40                 'cmd=lostpw' // LinPHA
41         );
42
43         // Attacks we should detect and blok
44         $GLOBALS['wormprotector'] = array(
45                 'chr(', 'chr=', 'chr%20', '%20chr', 'wget%20', '%20wget', 'wget(',
46                 'cmd=', '%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-a', 'bin/id', '/bin/', '/chgrp',
58                 '/chown', '/usr/bin', 'g\+\+', 'bin/python', 'bin/tclsh', 'bin/nasm', 'perl%20', 'traceroute%20',
59                 'ping%20', '.pl', 'bin/xterm', 'lsof%20', '.conf', 'motd%20', 'HTTP/1.', '.inc.php', '.lib.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', 'uol.com'
70         );
71
72         // Block these words found in POST requests
73         $GLOBALS['post_blacklist'] = array(
74                 // These two lines are for detecting hidden link spam in wikis, forums, guestbooks, etc.
75                 'div style=', 'overflow:auto', 'height:1px', 'width:1px', 'display:hidden',
76                 'overflow: auto', 'height: 1px', 'display: hidden',
77                 // "Common" login name from VHCS exploiters ;-)
78                 'starhack', 'DeLiMehmet', 'hisset', 'Hisset', 'delimert', 'MecTruy',
79                 'busca'
80         );
81 }
82
83 // Checks for worms
84 function isCrackerTrackerWormDetected () {
85         // Check against the whole list
86         $GLOBALS['checkworm'] = str_replace($GLOBALS['wormprotector'], '*', $_SERVER['QUERY_STRING']);
87
88         // If it differs to original and the *whole* request string is not in whitelist
89         // then blog the attempt
90         return ($GLOBALS['checkworm'] != $_SERVER['QUERY_STRING'] && (!in_array($_SERVER['QUERY_STRING'], $GLOBALS['whitelist'])));
91 }
92
93 // Checks POST data
94 function isCrackerTrackerPostAttackDetected () {
95         // Implode recursive the whole $_POST array
96         $GLOBALS['post_track'] = implode_r('', $_POST);
97
98         // Check for suspicious POST data
99         $GLOBALS['check_post'] = str_replace($GLOBALS['post_blacklist'], '*', $GLOBALS['post_track']);
100
101         // Is it detected?
102         return ((isCrackerTrackerWormDetected()) || ($GLOBALS['check_post'] != $GLOBALS['post_track']));
103 }
104
105 // Prepares a mail and send it out
106 function sendCrackerTrackerMail () {
107         // Mail content
108         $mail = "Attack detected:
109 -----------------------------------------------------
110 Remote-IP: ".$_SERVER['REMOTE_ADDR']."
111 User-Agent: ".$_SERVER['HTTP_USER_AGENT']."
112 Request-string: ".$_SERVER['QUERY_STRING']."
113 Filtered string: ".$GLOBALS['checkworm']."
114 Server: ".$_SERVER['SERVER_NAME']."
115 Script: ".$_SERVER['SCRIPT_NAME']."
116 Referrer: ".$_SERVER['HTTP_REFERRER']."
117 -----------------------------------------------------
118 ";
119
120         // Log the attack
121         crackerTrackerLogAttack();
122
123         // Send it out
124         crackerTrackerSendMail($mail);
125
126         // And die here
127         crackerTrackerDie();
128 }
129
130 // Sends a mail out
131 function crackerTrackerSendMail ($mail) {
132         // Construct dummy array
133         $rowData = array(
134                 'remote_addr' => $_SERVER['REMOTE_ADDR'],
135                 'check_worm'  => $GLOBALS['checkworm']
136         );
137
138         // Only send email if not yet found
139         if (!isCrackerTrackerEntryFound($rowData)) {
140                 // Send the email out only in non-debug mode
141                 if (isCrackerTrackerDebug()) {
142                         // Output message
143                         print '<pre>' . $mail . '</pre>';
144
145                         // All fine
146                         return true;
147                 } else {
148                         // Send it
149                         return mail(constant('__CTRACKER_EMAIL'), 'CTracker: Attack detected!', $mail, 'From: ctracker@mxchange.org');
150                 }
151         } // END - if
152 }
153
154 // Sends a detected POST attack mail
155 function sendCrackerTrackerPostMail () {
156         // Mail text
157         $mail = "POST-Attack detected:
158 -----------------------------------------------------
159 Remote-IP: ".$_SERVER['REMOTE_ADDR']."
160 User-Agent: ".$_SERVER['HTTP_USER_AGENT']."
161 POST string: ".$GLOBALS['post_track']."
162 Filtered worm string: ".$GLOBALS['checkworm']."
163 Filtered POST string: ".$GLOBALS['check_post']."
164 Server: ".$_SERVER['SERVER_NAME']."
165 Script: ".$_SERVER['SCRIPT_NAME']."
166 -----------------------------------------------------
167 ";
168
169         // Log the attack
170         crackerTrackerLogAttack();
171
172         // Send it out
173         crackerTrackerSendMail($mail);
174
175         // And die here
176         crackerTrackerDie();
177 }
178
179 // Sleeps for a random time and aborts the script
180 function crackerTrackerDie () {
181         // Close database link
182         crackerTrackerCloseDatabaseLink();
183
184         // Sleep a little to waste the attacker's time
185         if (!isCrackerTrackerDebug()) sleep(mt_rand(10,30));
186
187         // Bye, bye...
188         die();
189 }
190
191 // Logs the attack attempt
192 function crackerTrackerLogAttack () {
193         // Aquire database link
194         aquireCrackerTrackerDatabaseLink();
195
196
197         // Prepare array for database insert
198         $rowData = array(
199                 'remote_addr'     => $_SERVER['REMOTE_ADDR'],
200                 'user_agent'      => $_SERVER['HTTP_USER_AGENT'],
201                 'post_data'       => $GLOBALS['post_track'],
202                 'check_worm'      => $GLOBALS['checkworm'],
203                 'check_post'      => $GLOBALS['check_post'],
204                 'server_name'     => $_SERVER['SERVER_NAME'],
205                 'script_name'     => $_SERVER['SCRIPT_NAME'],
206                 'referer'         => $_SERVER['HTTP_REFERER']
207         );
208
209         // Insert the array in database
210         crackerTrackerInsertArray($rowData);
211 }
212
213 // [EOF]
214 ?>