找回密码
 开放注册

QQ登录

只需一步,快速开始

微信登录

微信扫码,快速开始

搜索
查看: 968|回复: 2

一个半成品-Webshell Scan

[复制链接]

735

主题

1102

回帖

3076

牛毛

二级牛人

积分
3096
QQ
发表于 2009-7-12 09:28:26 | 显示全部楼层 |阅读模式 来自 山东省威海市
一个半成品-Webshell Scan
经常看到很多站长说,我站被入侵了,首页被替换了,数据库被插马了.
可是还原之后,还是被替换首页..
这个时候,你的服务器肯能被黑客植入了 WebShell
所以,我自己就写了个扫描webshell的脚本..
经过简单的测试,能扫描出常用的webshell..
缺点:我随便写的,没经过仔细验证,比如windows跟linux的路径问题
以及那些变异的webshell(就是加密或者修改其他信息),都是存在问题的
我测试的平台的linux服务器,所以大家有兴趣可以自己去改改..
代码如下
[php]
<?php
session_start();
  $passwd = 'pass';    //设置管理密码
  
  $type = array(     //扫描文件类型
   'aspx','cerx','asax','asp','cer','asa','cdx',
   'jsp','php','phps','php3','php4','php5',
   );
   
  $code = array(       //危险代码
   'clsid:72C24DD5-D70A-438B-8A42-98424B88AFB8',
   'clsid:13709620-C279-11CE-A49E-444553540000',
   'WScript.Shell',
   'Shell.Application',
   'eval',
   'Execute',
   'CreateTextFile',
   'OpenTextFile',
   'SaveToFile',
   'CreateObject',
   );
  $meta = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf8\">";
  
$title = "<title>Scan WebShell 测试版-- Powered by Baddie</title>";
if ( $_GET[act] == "login" ) {
   ( $_POST[pwd] == $passwd ) ? $_SESSION[name] = $passwd : $_SESSION[name] = NULL;
  }
  
  if ( $_SESSION[name] == NULL ) {
   echo $meta.$title;
   echo <<< html_login
    <form method="POST" action="?act=login">
     <div align="center">Password:
      <input name="pwd" type="password" size="15"/>
      <input type="submit" name="Submit" value="提交"/>
     </div><p>
     <div align="center">powered by <a href="http://passwd.cc" target=_blank>Baddie</a></div>
   </form>
html_login;
} else {
  echo $meta.$title;
  echo <<< html_set
   <form action="?act=scan" method="POST">
    <b>确认你要检查的路径:</b>
    <input name="path" type="text"" value="/" size="30" /><br>
      填"<b><font color="red">/</font></b>"即检查当前目录<br>
      填"<b><font color="red">*</font></b>"则根目录形式扫描整个网站<br>
    <b>选择操作</b>
    <input type="radio" name="scanshell" value="ss" checked>
    查找Webshel木马
    <input type="radio" name="searchfile" value="sf">
    搜索符合条件之文件<b>(搜索功能下版本看情况发布)</b><br>
    <input type="submit" value=" 开始扫描 "/>
   </form>
html_set;
  }
//扫描木马开始
if ( $_GET[act] == "scan" && $_SESSION[name] != NULL ) {
  set_time_limit(120);
  $starttime = microtime();
  echo $meta.$title;
  echo <<< html_result0
  <table width="100%" border="0" cellpadding="0" cellspacing="0" class="CContent">
  <table width="100%" border="0" cellpadding="0" cellspacing="0">
   <tr>
  <td valign="top">
  <table width="100%" border="1" cellpadding="0" cellspacing="0" style="padding:5px;line-height:170%;clear:both;font-size:12px">
  <tr>
  <td width="20%">文件相对路径</td>
  <td width="20%">特征码</td>
  <td width="40%">描述</td>
  <td width="20%">创建/修改时间</td>
  </tr>
html_result0;
  
  $path = GetPath($_POST[path]);
  OpenFile($path,$type,$code);
  
echo <<< html_result
  </table>
  </td>
   </tr>
  </table>
  </td></tr>
  </table>
html_result;
  $time = $endtime - $starttime;
  echo "<p>执行共用了 <font color=\"#FF0000\">".$time."</font> 秒";
}
  
//获取扫描路径函数
function GetPath($path)
{
  if ( $path == "/" ) {
   $tmp = realpath(dirname(__FILE__));
    }
    elseif ( $path == "*" ) {
     $tmp = realpath($_SERVER['DOCUMENT_ROOT']);
     } else {
      $tmp = realpath($_POST[path]);
      }
  return $tmp;
}
//递归函数读取目录下文件
  function OpenFile($path,$type,$code)
  {
   $handle = opendir($path);
   while ( $file = readdir($handle) ) {
     $opendir = $path.'/'.$file;
     if ( $file != '.' && $file != '..' && is_dir($opendir) ) {
      OpenFile($path .'/'. $file,$type,$code);
      }
      elseif( $file != '.' && $file != '..' && $file != basename(__FILE__)) {
       $ext = explode('.',$file);
       $ext = strtolower($ext[count($ext)-1]);
      if ( in_array($ext,$type) ) {
       CheckFile($path,$file,$code);
       }
     }
     }
   closedir($handle);
  }
  
//检测危险代码
function CheckFile($path,$file,$code)
{
  $cfile = $path.'/'.$file;
  $fres = fopen($cfile,'r');
    $res = fread($fres,filesize($cfile));
    for ( $i = 0 ; $i < sizeof($code) ; $i ++ ) {
     if ( preg_match_all("/('".$code[$i]."')/isU" , $res , $result ) ) {
      foreach($result[1] as $v) {
     echo "<tr><td><a href=\"[url=http://".$_SERVER[]http://".$_SERVER['SERVER_NAME'].RootPath($cfile)."\[/url]" target=\"_blank\">".$file."</a></td><td>".$code[$i]."</td><td>".CodeDescribe($i)."</td><td>".date('Y-m-d H:i:s',filectime($cfile))."<br>".date('Y-m-d H:i:s',filemtime($cfile))."</td>";
     }
      }
     }
    fclose($fres);
}
//定义相对路径
function RootPath($path)
{
  $path = str_replace('\\','/',$path);
  $path = str_replace('//','/',$path);
  $real_path = str_replace($_SERVER['DOCUMENT_ROOT'],'',$path);
  return $real_path;
  }
//关联危险代码说明
function CodeDescribe($i)
{
  switch ( $i ) {
   case '4' : $describe = 'eval()函数可以执行任意代码,被一些后门利用.其形式一般是:eval(*)
但是javascript代码中也可以使用,有可能是误报.';
   break;
   case '5' : $describe = '<font color=red>execute()函数可以执行任意代码.被一些后门利用.其形式一般是:execute(*)</font>';
   break;
   case '6' : $describe = '使用了FSO的CreateTextFile|OpenTextFile函数读写文件';
   break;
   case '7' : $describe = '使用了FSO的CreateTextFile|OpenTextFile函数读写文件';
   break;
   case '8' : $describe = '使用了Stream的SaveToFile函数写文件';
   break;
   case '9' : $describe = 'CreateObject函数使用了变形技术.可能是误报';
   break;
   default : $describe = '<font color=red>危险组件,一般被木马利用</font>';
   }
  return $describe;
}
?>
[/php]

56

主题

693

回帖

1015

牛毛

二级牛人

,,,,

积分
1015
发表于 2009-7-12 14:04:50 | 显示全部楼层 来自 福建省三明市
有点看不懂...

56

主题

693

回帖

1015

牛毛

二级牛人

,,,,

积分
1015
发表于 2009-7-12 14:05:11 | 显示全部楼层 来自 福建省三明市
但还是谢谢了.....
您需要登录后才可以回帖 登录 | 开放注册

本版积分规则

帮助|Archiver|小黑屋|通信管理局专项备案号:[2008]238号|NB5社区 ( 皖ICP备08004151号;皖公网安备34010402700514号 )

GMT+8, 2025-4-28 09:42 , Processed in 0.152558 second(s), 42 queries .

Powered by Discuz! X3.5

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