人人商城

scan.ctrl.php 7.1KB

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