-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
290 lines (267 loc) · 8.36 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
* Malicious
*
* A simple, lightweight framework to detect potential suspicious/malicious PHP Code and few other
* commons sources of problems. Fully extensible with plugins (Check & Report) support...
*
* @version 0.1
* @copyright Ackwa.fr - 2014
*
* Load configuration file
*/
require(dirname(__FILE__).'/config.php');
/**
* Some local definition
*/
define('MCS_VERSION', '0.1-beta');
define('MCS_START' , _now());
register_shutdown_function('_stop');
spl_autoload_register('autoloadPlugins');
/*
* Create log directory if needed
*/
if (!file_exists(MCS_LOGS)) mkdir(MCS_LOGS);
/**
* Initialization
*/
$aOptions = getopt("c:r:s:");
$aOptions = (is_array($aOptions) ? $aOptions : $_GET);
$iCheck = (isset($aOptions['c']) ? $aOptions['c'] : 0) + 0;
$iReport = (isset($aOptions['r']) ? $aOptions['r'] : 0) + 0;
$sSecret = (isset($aOptions['s']) ? $aOptions['s'] : '');
$sRoot = (isset($aOptions['d']) ? $aOptions['d'] : MCS_ROOT);
/*
* Log some informations
*/
$aPUID = array('uid' => getmyuid(), 'name' => get_current_user());
$aPGID = array('gid' => getmygid(), 'name' => '???');
_log(str_repeat('-', 80));
_log('Malicious '.MCS_VERSION.' started... PHP '.phpversion().' / '.php_sapi_name().' on '.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost').' for '.(isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '???'));
_log('Process is owned by '.$aPUID['name'].' ('.$aPUID['uid'].') / '.$aPGID['name'].' ('.$aPGID['gid'].') with umask '.sprintf('%04o', umask()).' running in '.realpath(getcwd()));
/*
* Check Authorisation
*/
if (MCS_SECRET == $sSecret) {
/*
* Try to avoid memory & timeout errors (i.e in foreachloop)
*/
if (MCS_NOLIMIT) {
if (ini_get('safe_mode')) {
_log('In safe mode we can\'t change the maximum execution time. Timeout may occur...');
}
else {
ini_set('memory_limit', '-1');
@set_time_limit(0);
}
}
/*
* Get "check plugins" configuration
*/
if ($iCheck && defined('MCS_PLUGINS_'.$iCheck)) {
$sPlugins = constant('MCS_PLUGINS_'.$iCheck);
}
else {
$sPlugins = MCS_PLUGINS;
}
/*
* Get "report plugins" configuration
*/
if ($iReport && defined('MCS_REPORTS_'.$iReport)) {
$sReports = constant('MCS_REPORTS_'.$iReport);
}
else {
$sReports = MCS_REPORTS;
}
/*
* Load "Check plugins"...
*/
$lChecks = loadPlugins($sPlugins, 'Check');
$sLastP = $sLastC = '';
$iCount = $iCheck = 0;
/*
* Start scanner...
*/
_log('Scan ['.realpath($sRoot).'] directory with : '.implode(', ', array_keys($lChecks)).' plugins.');
scan($sRoot, $lChecks);
/*
* Load and execute "Report plugins"...
*/
$lReports = loadPlugins($sReports, 'Report');
foreach($lReports as $oO) $oO->report($lChecks);
}
else {
_log('Unauthorized client request: ['.$sSecret.']');
}
define('MCS_STOP', _now());
/**
* Recursive Directory Scan & file check function
*
* @param string $sDir Directory to scan
* @param array $aPlugins Plugins to execute
*/
function scan($sDir, $aPlugins = array()) {
global $sLastP, $sLastC, $iCount, $iCheck;
if (is_readable($sDir)) {
$lFiles = scandir(realpath($sDir));
foreach($lFiles as $sFile) {
if (('.' != $sFile) && ('..' != $sFile)) {
/*
* Update counter & marker
*/
$sPath = realpath($sDir.'/'.$sFile);
$sLastP = $sPath;
$iCount++;
/*
* A tick...
*/
if ($iCheck && !($iCheck % 10000)) _tick();
/*
* Check directory or file
*/
if (is_dir($sPath)) {
foreach($aPlugins as $oPlugin) {
$sLastC = get_class($oPlugin);
if ($oPlugin->checkDirectories()) {
$iCheck++;
if ($oPlugin->check($sPath, null)) break;
}
}
scan($sPath, $aPlugins);
}
else {
//echo '<pre>['.$sPath."]\n";
$sContent = null;
foreach($aPlugins as $oPlugin) {
if ($oPlugin->filter($sPath)) {
$sLastC = get_class($oPlugin);
if ($oPlugin->needFileContent() && !$sContent) $sContent = file_get_contents($sPath);
$iCheck++;
if ($oPlugin->check($sPath, $sContent)) break; // If file "checked" do not continue testing...
}
}
}
}
}
}
}
/**
* Dummy plugins loader
*/
function loadPlugins($sPlugins, $sKind) {
$lOO = array();
foreach(explode(',', $sPlugins) as $sPlugin) {
$sPlugin = trim($sPlugin);
if ($sPlugin) {
$sClass = trim($sPlugin.$sKind);
$lOO[$sClass] = new $sClass();
}
}
return $lOO;
}
function autoloadPlugins($sClass) {
$sPath = dirname(__FILE__).'/plugins/check/'.$sClass.'.class.php';
if (file_exists($sPath) && is_readable($sPath)) {
include_once($sPath);
}
else {
$sPath = dirname(__FILE__).'/plugins/report/'.$sClass.'.class.php';
if (file_exists($sPath) && is_readable($sPath)) include_once($sPath);
}
}
/**
* Trace Malicious stop delay / status
*/
function _stop() {
global $sLastP, $sLastC, $iCount, $iCheck;
_log(sprintf('Malicious stop after %2.4fs. Completed status is %s. Last file (#%d) checked (#%d) : [%s] by %s', (_now() - MCS_START), (defined('MCS_STOP') ? 'OK.' : 'KO!'), $iCount, $iCheck, $sLastP, $sLastC));
}
function _tick() {
global $sLastP, $sLastC, $iCount, $iCheck;
_log(sprintf('Malicious is running since %2.4fs. Last file (#%d) checked (#%d) : [%s] by %s', (_now() - MCS_START), $iCount, $iCheck, $sLastP, $sLastC));
}
/**
* Current time in usec
*
* @return float Current time in usec
* @see http://fr2.php.net/manual/fr/function.microtime.php
*/
function _now() {
list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
/**
* Log message to file
*
* @param string $sMsg Message to log
* @param string $sName Log name (.log will be appended)
*/
function _log($sMsg = '', $sName = 'malicious') {
static $fd = null, $inc = 0;
$sName = (trim($sName) ? $sName : md5(date('Ymd')));
$sPath = MCS_LOGS.'/'.$sName.'.log';
$fd[$sPath] = (isset($fd[$sPath]) ? $fd[$sPath] : fopen($sPath, 'a'));
if ($fd[$sPath]) fwrite($fd[$sPath], sprintf("%04d|%s|%s\n", $inc++, date('d/m/y|H:i:s'), ($sMsg ? $sMsg : '!!!')));
}
/**
* @param integer $sSize Size of file
* @return int
*/
function bytes($sSize) {
switch (substr($sSize, -1)) {
case 'M': case 'm': return (int)$sSize * 1048576;
case 'K': case 'k': return (int)$sSize * 1024;
case 'G': case 'g': return (int)$sSize * 1073741824;
default: return $sSize;
}
}
/**
* Malicious "Check plugin" model
*/
class maliciousCheck {
public $iCount = 0; // Number of files checked
public $iSize = 0; // Bytes read
public $lFiles = array(); // List of selected files
public $lMore = array(); // More information about selected files
function __construct() {
//echo 'Load : '.get_class($this)."\n";
}
function description() {
return 'Check "plugin" Model';
}
function check($sPath, $sContent = null) {
$this->iCount++;
return false; // By default we'll continue testing...
}
function filter($sPath) {
return true;
}
function warn() {
return true;
}
function needFileContent() {
return false;
}
function checkDirectories() {
return false;
}
protected function extension($sPath) {
return strtolower(pathinfo($sPath, PATHINFO_EXTENSION));
}
function __destruct() {
//echo 'Unload : '.get_class($this)."\n";
}
}
/**
* Malicious "Check plugin" model
*/
class maliciousReport {
function __construct() {
//echo 'Load : '.get_class($this)."\n";
}
function report($lChecks) {
}
function __destruct() {
//echo 'Unload : '.get_class($this)."\n";
}
}