index.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. pc_base::load_app_class('admin', 'admin', 0);
  4. class index extends admin {
  5. protected $safe = array ('file_type' => 'php|js','code' => '','func' => 'com|system|exec|eval|escapeshell|cmd|passthru|base64_decode|gzuncompress','dir' => '', 'md5_file'=>'');
  6. public function __construct() {
  7. parent::__construct();
  8. }
  9. public function init() {
  10. $list = glob(PHPCMS_PATH.'*');
  11. if (file_exists(CACHE_PATH.'caches_scan'.DIRECTORY_SEPARATOR.'caches_data')) {
  12. $md5_file_list = glob(CACHE_PATH.'caches_scan'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR.'md5_*.php');
  13. if(is_array($md5_file_list)) {
  14. foreach ($md5_file_list as $k=>$v) {
  15. $md5_file_list[$v] = basename($v);
  16. unset($md5_file_list[$k]);
  17. }
  18. }
  19. }
  20. $scan = getcache('scan_config', 'scan');
  21. if (is_array($scan)) {
  22. $scan = array_merge($this->safe, $scan);
  23. } else {
  24. $scan = $this->safe;
  25. }
  26. $scan['dir'] = string2array($scan['dir']);
  27. pc_base::load_sys_class('form', '', 0);
  28. include $this->admin_tpl('scan_index');
  29. }
  30. //进行配置文件更新
  31. public function public_update_config() {
  32. $info = isset($_POST['info']) ? $_POST['info'] : showmessage(L('illegal_action'), HTTP_REFERER);
  33. $dir = isset($_POST['dir']) ? new_stripslashes($_POST['dir']) : '';
  34. if (empty($dir)) {
  35. showmessage(L('please_select_the_content'), '?m=scan&c=index&a=init');
  36. }
  37. $info['dir'] = var_export($dir, true);
  38. setcache('scan_config', $info, 'scan');
  39. showmessage(L('configuration_file_save_to_the'), '?m=scan&c=index&a=public_file_count');
  40. }
  41. //对要进行扫描的文件进行统计
  42. public function public_file_count() {
  43. $scan = getcache('scan_config', 'scan');
  44. pc_base::load_app_func('global');
  45. set_time_limit(120);
  46. $scan['dir'] = string2array($scan['dir']);
  47. $scan['file_type'] = explode('|', $scan['file_type']);
  48. $list = array();
  49. foreach ($scan['dir'] as $v) {
  50. if (is_dir($v)) {
  51. foreach ($scan['file_type'] as $k ) {
  52. $list = array_merge($list, scan_file_lists($v.DIRECTORY_SEPARATOR, 1, $k, 0, 1, 1));
  53. }
  54. } else {
  55. $list = array_merge($list, array(str_replace(PHPCMS_PATH, '', $v)=>md5_file($v)));
  56. }
  57. }
  58. setcache('scan_list', $list, 'scan');
  59. showmessage(L('documents_to_file_the_statistics'), '?m=scan&c=index&a=public_file_filter');
  60. }
  61. //对文件进行筛选
  62. public function public_file_filter() {
  63. $scan_list = getcache('scan_list', 'scan');
  64. $scan = getcache('scan_config', 'scan');
  65. if (file_exists($scan['md5_file'])) {
  66. $old_md5 = include $scan['md5_file'];
  67. foreach ($scan_list as $k=>$v) {
  68. if ($v == $old_md5[$k]) {
  69. unset($scan_list[$k]);
  70. }
  71. }
  72. }
  73. setcache('scan_list', $scan_list, 'scan');
  74. showmessage(L('file_through_a_feature_the_function_is'), '?m=scan&c=index&a=public_file_func');
  75. }
  76. //进行特征函数过滤
  77. public function public_file_func() {
  78. @set_time_limit(600);
  79. $file_list = getcache('scan_list', 'scan');
  80. $scan = getcache('scan_config', 'scan');
  81. if (isset($scan['func']) && !empty($scan['func'])) {
  82. foreach ($file_list as $key=>$val) {
  83. $html = file_get_contents(PHPCMS_PATH.$key);
  84. if(stristr($key,'.php.') != false || preg_match_all('/[^a-z]?('.$scan['func'].')\s*\(/i', $html, $state, PREG_SET_ORDER)) {
  85. $badfiles[$key]['func'] = $state;
  86. }
  87. }
  88. }
  89. if(!isset($badfiles)) $badfiles = array();
  90. setcache('scan_bad_file', $badfiles, 'scan');
  91. showmessage(L('feature_function_complete_a_code_used_by_filtration'), '?m=scan&c=index&a=public_file_code');
  92. }
  93. //进行特征代码过滤
  94. public function public_file_code() {
  95. @set_time_limit(600);
  96. $file_list = getcache('scan_list', 'scan');
  97. $scan = getcache('scan_config', 'scan');
  98. $badfiles = getcache('scan_bad_file', 'scan');
  99. if (isset($scan['code']) && !empty($scan['code'])) {
  100. foreach ($file_list as $key=>$val) {
  101. $html = file_get_contents(PHPCMS_PATH.$key);
  102. if(stristr($key, '.php.') != false || preg_match_all('/[^a-z]?('.$scan['code'].')/i', $html, $state, PREG_SET_ORDER)) {
  103. $badfiles[$key]['code'] = $state;
  104. }
  105. if(strtolower(substr($key, -4)) == '.php' && function_exists('zend_loader_file_encoded') && zend_loader_file_encoded(PHPCMS_PATH.$key)) {
  106. $badfiles[$key]['zend'] = 'zend encoded';
  107. }
  108. }
  109. }
  110. setcache('scan_bad_file', $badfiles, 'scan');
  111. showmessage(L('scan_completed'), '?m=scan&c=index&a=scan_report&menuid=1005');
  112. }
  113. public function scan_report() {
  114. $badfiles = getcache('scan_bad_file', 'scan');
  115. if (empty($badfiles)) {
  116. showmessage(L('scan_to_find_a_result_please_to_scan'), '?m=scan&c=index&a=init');
  117. }
  118. include $this->admin_tpl('scan_report');
  119. }
  120. public function view() {
  121. $url = isset($_GET['url']) && trim($_GET['url']) ? new_stripslashes(urldecode(trim($_GET['url']))) : showmessage(L('illegal_action'), HTTP_REFERER);
  122. $url = str_replace("..","",$url);
  123. if (!file_exists(PHPCMS_PATH.$url)) {
  124. showmessage(L('file_not_exists'));
  125. }
  126. $html = file_get_contents(PHPCMS_PATH.$url);
  127. //判断文件名,如果是database.php 对里面的关键字符进行替换
  128. $basename = basename($url);
  129. if($basename == "database.php" || $basename == "system.php"){
  130. //$html = str_replace();
  131. showmessage(L('重要文件,不允许在线查看!'));
  132. }
  133. $file_list = getcache('scan_bad_file', 'scan');
  134. if (isset($file_list[$url]['func']) && is_array($file_list[$url]['func']) && !empty($file_list[$url]['func'])) foreach ($file_list[$url]['func'] as $key=>$val)
  135. {
  136. $func[$key] = strtolower($val[1]);
  137. }
  138. if (isset($file_list[$url]['code']) && is_array($file_list[$url]['code']) && !empty($file_list[$url]['code'])) foreach ($file_list[$url]['code'] as $key=>$val)
  139. {
  140. $code[$key] = strtolower($val[1]);
  141. }
  142. if (isset($func)) $func = array_unique($func);
  143. if (isset($code)) $code = array_unique($code);
  144. $show_header = true;
  145. include $this->admin_tpl('public_view');
  146. }
  147. public function md5_creat() {
  148. set_time_limit(120);
  149. $pro = isset($_GET['pro']) && intval($_GET['pro']) ? intval($_GET['pro']) : 1;
  150. pc_base::load_app_func('global');
  151. switch ($pro) {
  152. case '1'://统计文件
  153. $msg = L('please_wait');
  154. ob_start();
  155. include $this->admin_tpl('md5_creat');
  156. ob_flush();
  157. ob_clean();
  158. $list = scan_file_lists(PHPCMS_PATH, 1, 'php', 0, 1);
  159. setcache('md5_'.date('Y-m-d'), $list, 'scan');
  160. echo '<script type="text/javascript">location.href="?m=scan&c=index&a=md5_creat&pro=2&pc_hash='.$_SESSION['pc_hash'].'"</script>';
  161. break;
  162. case '2':
  163. showmessage(L('viewreporttrue'),'?m=scan&c=index&a=init');
  164. break;
  165. }
  166. }
  167. }