人人商城

tools.ctrl.php 7.9KB

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