scan.ctrl.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. set_time_limit(0);
  8. load()->func('file');
  9. $dos = array('post', 'count', 'filter_func', 'filter_code', 'encode', 'display','view');
  10. $do = in_array($do, $dos) ? $do : 'post';
  11. if ($do == 'post') {
  12. $config = iunserializer(cache_read(cache_system_key('scan_config')));
  13. $list = glob(IA_ROOT.'/*', GLOB_NOSORT);
  14. $ignore = array('data','attachment');
  15. foreach ($list as $key => $li) {
  16. if (in_array(basename($li), $ignore)) {
  17. unset($list[$key]);
  18. }
  19. }
  20. $safe = array (
  21. 'file_type' => 'php|js',
  22. 'code' => 'weidongli|sinaapp|safedog',
  23. 'func' => 'com|system|exec|eval|escapeshell|cmd|passthru|base64_decode|gzuncompress',
  24. 'dir' => '',
  25. );
  26. if (checksubmit('submit')) {
  27. if (empty($_GPC['dir'])) {
  28. itoast('请选择要扫描的目录', referer(), 'success');
  29. }
  30. foreach ($_GPC['dir'] as $k => $v) {
  31. if (in_array(basename($v), $ignore)) {
  32. unset($_GPC['dir'][$k]);
  33. }
  34. }
  35. $info['file_type'] = 'php|js';
  36. $info['func'] = trim($_GPC['func']) ? trim($_GPC['func']) : 'com|system|exec|eval|escapeshell|cmd|passthru|base64_decode|gzuncompress';
  37. $info['code'] = trim($_GPC['code']) ? trim($_GPC['code']) : 'weidongli|sinaapp';
  38. $info['md5_file'] = trim($_GPC['md5_file']);
  39. $info['dir'] = $_GPC['dir'];
  40. cache_delete(cache_system_key('scan_file'));
  41. cache_write(cache_system_key('scan_config'), iserializer($info));
  42. itoast("配置保存完成,开始文件统计。。。", url('system/scan', array('do' => 'count')), 'success');
  43. }
  44. }
  45. if ($do == 'count') {
  46. $files = array();
  47. $config = iunserializer(cache_read(cache_system_key('scan_config')));
  48. if (empty($config)) {
  49. itoast('获取扫描配置失败', url('system/scan'), 'error');
  50. }
  51. $config['file_type'] = explode('|', $config['file_type']);
  52. $list_arr = array();
  53. foreach ($config['dir'] as $v) {
  54. if (is_dir($v)) {
  55. if (!empty($config['file_type'])) {
  56. foreach ($config['file_type'] as $k) {
  57. $list_arr = array_merge($list_arr, file_lists($v . '/', 1, $k, 0, 1, 1));
  58. }
  59. }
  60. } else {
  61. $list_arr = array_merge($list_arr, array(str_replace(IA_ROOT . '/', '', $v) => md5_file($v)));
  62. }
  63. }
  64. unset($list_arr['data/config.php']);
  65. $list_arr = iserializer($list_arr);
  66. cache_write(cache_system_key('scan_file'), $list_arr);
  67. itoast("文件统计完成,进行特征函数过滤。。。", url('system/scan', array('do' => 'filter_func')), 'success');
  68. }
  69. if ($do == 'filter_func') {
  70. $config = iunserializer(cache_read(cache_system_key('scan_config')));
  71. $file = iunserializer(cache_read(cache_system_key('scan_file')));
  72. if (isset($config['func']) && !empty($config['func'])) {
  73. foreach ($file as $key => $val) {
  74. $html = file_get_contents(IA_ROOT . '/' . $key);
  75. if (stristr($key, '.php.') != false || preg_match_all('/[^a-z]?('.$config['func'].')\s*\(/i', $html, $state, PREG_SET_ORDER)) {
  76. $badfiles[$key]['func'] = $state;
  77. }
  78. }
  79. }
  80. if (!isset($badfiles)) $badfiles = array();
  81. cache_write(cache_system_key('scan_badfile'), iserializer($badfiles));
  82. itoast("特征函数过滤完成,进行特征代码过滤。。。", url('system/scan', array('do' => 'filter_code')), 'success');
  83. }
  84. if ($do == 'filter_code') {
  85. $config = iunserializer(cache_read(cache_system_key('scan_config')));
  86. $file = iunserializer(cache_read(cache_system_key('scan_file')));
  87. $badfiles = iunserializer(cache_read(cache_system_key('scan_badfile')));
  88. if (isset($config['code']) && !empty($config['code'])) {
  89. foreach ($file as $key => $val) {
  90. if (!empty($config['code'])) {
  91. $html = file_get_contents(IA_ROOT . '/' . $key);
  92. if (stristr($key, '.php.') != false || preg_match_all('/[^a-z]?('.$config['code'].')/i', $html, $state, PREG_SET_ORDER)) {
  93. $badfiles[$key]['code'] = $state;
  94. }
  95. }
  96. if (strtolower(substr($key, -4)) == '.php' && function_exists('zend_loader_file_encoded') && zend_loader_file_encoded(IA_ROOT . '/' . $key)) {
  97. $badfiles[$key]['zend'] = 'zend encoded';
  98. }
  99. $html = '';
  100. }
  101. }
  102. cache_write(cache_system_key('scan_badfile'), iserializer($badfiles));
  103. itoast("特征代码过滤完成,进行加密文件过滤。。。", url('system/scan', array('do' => 'encode')), 'success');
  104. }
  105. if ($do == 'encode') {
  106. $file = iunserializer(cache_read(cache_system_key('scan_file')));
  107. $badfiles = iunserializer(cache_read(cache_system_key('scan_badfile')));
  108. foreach ($file as $key => $val) {
  109. if (strtolower(substr($key, -4)) == '.php') {
  110. $html = file_get_contents(IA_ROOT . '/' . $key);
  111. $token = token_get_all($html);
  112. $html = '';
  113. foreach ($token as $to) {
  114. if (is_array($to) && $to[0] == T_VARIABLE) {
  115. $pre = preg_match("/([".chr(0xb0)."-".chr(0xf7)."])+/", $to[1]);
  116. if (!empty($pre)) {
  117. $badfiles[$key]['danger'] = 'danger';
  118. break;
  119. }
  120. }
  121. }
  122. }
  123. }
  124. cache_write(cache_system_key('scan_badfile'), iserializer($badfiles));
  125. itoast("扫描完成。。。", url('system/scan', array('do' => 'display')), 'success');
  126. }
  127. if ($do == 'display') {
  128. $badfiles = iunserializer(cache_read(cache_system_key('scan_badfile')));
  129. if (empty($badfiles)) {
  130. itoast('没有找到扫描结果,请重新扫描', url('system/scan'), 'error');
  131. }
  132. unset($badfiles['data/config.php']);
  133. foreach ($badfiles as $k => &$v) {
  134. $v['func_count'] = 0;
  135. if (isset($v['func'])) {
  136. $v['func_count'] = count($v['func']);
  137. foreach ($v['func'] as $k1 => $v1) {
  138. $d[$k1] = strtolower($v1[1]);
  139. }
  140. $d = array_unique($d);
  141. $v['func_str'] = implode(', ', $d);
  142. }
  143. $v['code_count'] = 0;
  144. if (isset($v['code'])) {
  145. $v['code_count'] = count($v['code']);
  146. foreach ($v['code'] as $k2 => $v2) {
  147. $d1[$k2] = strtolower($v2[1]);
  148. }
  149. $d1 = array_unique($d1);
  150. $v['code_str'] = implode(', ', $d1);
  151. }
  152. }
  153. }
  154. if ($do == 'view') {
  155. $file = authcode(trim($_GPC['file'], 'DECODE'));
  156. $file_tmp = $file;
  157. $file = str_replace('//','',$file);
  158. if (empty($file) || ! parse_path($file) || $file == 'data/config.php') {
  159. itoast('文件不存在', referer(), 'error');
  160. }
  161. $file_arr = explode('/', $file);
  162. $ignore = array('payment');
  163. if (is_array($file_arr) && in_array($file_arr[0], $ignore)) {
  164. itoast('系统不允许查看当前文件', referer(), 'error');
  165. }
  166. $file = IA_ROOT . '/' . $file;
  167. if (!is_file($file)) {
  168. itoast('文件不存在', referer(), 'error');
  169. }
  170. $badfiles = iunserializer(cache_read(cache_system_key('scan_badfile')));
  171. $info = $badfiles[$file_tmp];
  172. unset($badfiles);
  173. if (!empty($info)) {
  174. $info['func_count'] = 0;
  175. if (isset($info['func'])) {
  176. $info['func_count'] = count($info['func']);
  177. foreach ($info['func'] as $k1 => $v1) {
  178. $d[$k1] = strtolower($v1[1]);
  179. }
  180. $d = array_unique($d);
  181. $info['func_str'] = implode(', ', $d);
  182. }
  183. $info['code_count'] = 0;
  184. if (isset($info['code'])) {
  185. $info['code_count'] = count($info['code']);
  186. foreach ($info['code'] as $k2 => $v2) {
  187. $d1[$k2] = strtolower($v2[1]);
  188. }
  189. $d1 = array_unique($d1);
  190. $info['code_str'] = implode(', ', $d1);
  191. }
  192. }
  193. $data = file_get_contents($file);
  194. }
  195. template('system/scan');