查看: 3028|回复: 6
收起左侧

[求助] 如何利用mcafee防止SQL注入!!!

[复制链接]
wy1981910
发表于 2012-9-27 14:23:03 | 显示全部楼层 |阅读模式
应该配置什么样的规则,请高手协助解决!!!
WEI.ER
发表于 2012-9-27 15:06:43 | 显示全部楼层
装McAfee HIP和Firewall吧,VSE规则只能防止恶意修改、中毒等。


以下内容来自网络:

1.asp.net防SQLJS注入攻击:过滤标记

/// <summary>
/// 过滤标记
/// </summary>
/// <param name="NoHTML">包括HTML,脚本,数据库关键字,特殊字符的源码 </param>
/// <returns>已经去除标记后的文字</returns>
public static string NoHTML(string Htmlstring)
{
if (Htmlstring == null)
{
return "";
}
else
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);

Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "xp_cmdshell", "", RegexOptions.IgnoreCase);

//删除与数据库相关的词
Htmlstring = Regex.Replace(Htmlstring, "select", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "insert", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "delete from", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "count''", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "drop table", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "truncate", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "asc", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "mid", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "char", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "xp_cmdshell", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "exec master", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "net localgroup administrators", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "and", "", RegexOptions.IgnoreCase);


return Htmlstring ;

      }

}


2.js 防止sql注入

js 防止sql注入
    式攻击是利用是指利用设计上的漏洞,在目标服务器上运行Sql命令以及进行其他方式的攻击

    动态生成Sql命令时没有对用户输入的数据进行验证是Sql注入攻击得逞的主要原因。

    比如:
    如果你的查询语句是select * from admin where username=" user " and password=" pwd ""

    那么,如果我的用户名是:1 or 1=1
    那么,你的查询语句将会变成:
    select * from admin where username=1 or 1=1 and password=" pwd ""

    这样你的查询语句就通过了,从而就可以进入你的管理界面。

    所以防范的时候需要对用户的输入进行检查。特别式一些特殊字符,比如单引号,双引号,分号,逗号,冒号,连接号等进行转换或者过滤。

    需要过滤的特殊字符及字符串有:
    net localgroup administrators
    下面是我写的两种关于解决注入式攻击的防范代码,供大家学习参考!

    js版的防范式攻击代码~:
    [CODE START]
    script language="javascript"
    var url = location.search;
    var re=/^\?(.*)(select |insert |delete from |count\(|drop table|update truncate |asc\(|mid\(|char\(|xp_cmdshell|exec master|net localgroup administrators|\"|:|net user|\| or )(.*)$/gi;

    alert("地址中含有非法字符~");
    [CODE END]
    asp版的防范式攻击代码~:
    [CODE START]
    On Error Resume Next
    If LCase(Request.ServerVariables("HTTPS")) = "off" Then

    strTemp = strTemp   Request.ServerVariables("SERVER_NAME")

    If Request.ServerVariables("SERVER_PORT")    80 Then strTemp = strTemp   ":"   Request.ServerVariables("SERVER_PORT")

    strTemp = strTemp   Request.ServerVariables("URL")
    If Trim(Request.QueryString)    "" Then strTemp = strTemp   "?"   Trim(Request.QueryString)

    strTemp = LCase(strTemp)
    If Instr(strTemp,"select ") or Instr(strTemp,"insert ") or Instr(strTemp,"delete from") or Instr(strTemp,"count(") or Instr(strTemp,"drop table") or Instr(strTemp,"update ") or Instr(strTemp,"truncate ") or Instr(strTemp,"asc(") or Instr(strTemp,"mid(") or Instr(strTemp,"char(") or Instr(strTemp,"xp_cmdshell") or Instr(strTemp,"exec master") or Instr(strTemp,"net localgroup administrators")  or Instr(strTemp,":") or Instr(strTemp,"net user") or Instr(strTemp,"") or Instr(strTemp," or ") then

    Response.Write " script language=javascript "
    Response.Write "alert(非法地址!!);"

3.JS过滤SQL注入字符

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>标题页</title>
<script LANGUAGE="JavaScript">
function check(inputStr) {
   if (typeof(inputStr) != "string") { return inputStr; } //判断是否是字符串类型
   var tmpValue = inputStr;
    //以下搜索字符串中的特殊字符,如果存在,则替换成""
    while (tmpValue.indexOf(';') > -1) {tmpValue = tmpValue.replace(';',''); }
    while (tmpValue.indexOf('<') > -1) {tmpValue = tmpValue.replace('<',''); }
    while (tmpValue.indexOf('>') > -1) {tmpValue = tmpValue.replace('>',''); }
    while (tmpValue.indexOf('--') > -1) {tmpValue = tmpValue.replace('--',''); }
   while (tmpValue.indexOf(",") > -1) {tmpValue = tmpValue.replace(",",""); }
   while (tmpValue.indexOf("'") > -1) {tmpValue = tmpValue.replace("'",""); }
   while (tmpValue.indexOf("?") > -1) {tmpValue = tmpValue.replace("?",""); }
   document.getElementByIdx_x("txt1").value = tmpValue; //重新显示更改后的变量
}
</script>
</head>
<body>
<input type=text id="txt1" value="select * from userinfo where username=zhang' and passwrod=2" style="width: 392px">
<input type=button value="提交" onClick="check(txt1.value)">
</body>
</html>

4.防止sql注入式攻击(可用于UI层控制)

#region  防止sql注入式攻击(可用于UI层控制)
  
   ///
   ///  判断字符串中是否有SQL攻击代码
   ///
   ///  传入用户提交数据
   ///  true-安全;false-有注入攻击现有;
   public   bool  ProcessSqlStr( string  inputString)
     {
      string  SqlStr  =   @"
and|or|exec|execute|insert|select|delete|update|alter|create|drop|count|\*|chr|char|asc|mid|substring|master|truncate|declare|xp_cmdshell|restore|backup|net +user|net
+localgroup +administrators " ;
        try
          {
            if  ((inputString  !=   null )  &&  (inputString  !=  String.Empty))
              {
                string  str_Regex  =   @" \b( "   +  SqlStr  +   @" )\b " ;
  
               Regex Regex  =   new  Regex(str_Regex, RegexOptions.IgnoreCase);
                // string s = Regex.Match(inputString).Value;
                if  ( true   ==  Regex.IsMatch(inputString))
                    return   false ;
  
          }
      }
       catch
         {
           return   false ;
      }
       return   true ;
  }


   ///
  ///  处理用户提交的请求,校验sql注入式攻击,在页面装置时候运行
  ///  System.Configuration.ConfigurationSettings.AppSettings["ErrorPage"].ToString(); 为用户自定义错误页面提示地址,
  ///  在Web.Config文件时里面添加一个 ErrorPage 即可
  ///
  ///     
  ///  
  public   void  ProcessRequest()
    {
       try
         {
           string  getkeys  =   "" ;
           string  sqlErrorPage  =  System.Configuration.ConfigurationSettings.AppSettings[ " ErrorPage " ].ToString();
          if  (System.Web.HttpContext.Current.Request.QueryString  !=   null )
            {

               for  ( int  i  =   0 ; i  <  System.Web.HttpContext.Current.Request.QueryString.Count; i ++ )
                {
                  getkeys  =  System.Web.HttpContext.Current.Request.QueryString.Keys;
                    if  ( ! ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys]))
                      {
                       System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage  +   " ?errmsg= "   +  getkeys  +   " 有SQL攻击嫌疑! " );
                     System.Web.HttpContext.Current.Response.End();
                 }
             }
         }
          if  (System.Web.HttpContext.Current.Request.Form  !=   null )
            {
               for  ( int  i  =   0 ; i  <  System.Web.HttpContext.Current.Request.Form.Count; i ++ )
                  {
                   getkeys  =  System.Web.HttpContext.Current.Request.Form.Keys;
                    if  ( ! ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys]))
                      {
                       System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage  +   " ?errmsg= "   +  getkeys  +   " 有SQL攻击嫌疑! " );
                       System.Web.HttpContext.Current.Response.End();
                   }
               }
           }
       }
        catch
          {
            //  错误处理: 处理用户提交信息!
       }
   }
   #endregion
  
  
  
  
    转换sql代码(也防止sql注入式攻击,可以用于业务逻辑层,但要求UI层输入数据时候进行解码) #region  转换sql代码(也防止sql注入式攻击,可以用于业务逻辑层,但要求UI层输入数据时
候进行解码)
    ///
   ///  提取字符固定长度
   ///
   ///
   ///
   ///  
   public   string  CheckStringLength( string  inputString, Int32 maxLength)
     {
        if  ((inputString  !=   null )  &&  (inputString  !=  String.Empty))
          {
           inputString  =  inputString.Trim();
  
            if  (inputString.Length  >  maxLength)
               inputString  =  inputString.Substring( 0 , maxLength);
       }
        return  inputString;
   }
  
  ///
///  将输入字符串中的sql敏感字,替换成"[敏感字]",要求输出时,替换回来
///
///
///  
public   string  MyEncodeInputString( string  inputString)
   {
      // 要替换的敏感字
      string  SqlStr  =   @"
and|or|exec|execute|insert|select|delete|update|alter|create|drop|count|\*|chr|char|asc|mid|substring|master|truncate|declare|xp_cmdshell|restore|backup|net +user|net
+localgroup +administrators " ;
      try
        {
          if  ((inputString  !=   null )  &&  (inputString  !=  String.Empty))
            {
              string  str_Regex  =   @" \b( "   +  SqlStr  +   @" )\b " ;

             Regex Regex  =   new  Regex(str_Regex, RegexOptions.IgnoreCase);
              // string s = Regex.Match(inputString).Value;
             MatchCollection matches  =  Regex.Matches(inputString);
              for  ( int  i  =   0 ; i  <  matches.Count; i ++ )
                  inputString  =  inputString.Replace(matches.Value,  " [ "   +  matches.Value  +   " ] " );

          }
      }
       catch
         {
           return   "" ;
      }
       return  inputString;

  }

   ///
  ///  将已经替换成的"[敏感字]",转换回来为"敏感字"
  ///
  ///
  ///  
  public   string  MyDecodeOutputString( string  outputstring)
    {
       // 要替换的敏感字
       string  SqlStr  =   
@"and|or|exec|execute|insert|select|delete|update|alter|create|drop|count|\*|chr|char|asc|mid|substring|master|truncate|declare|xp_cmdshell|restore|backup|net +user|net
+localgroup +administrators " ;
      try
        {
          if  ((outputstring  !=   null )  &&  (outputstring  !=  String.Empty))
            {
              string  str_Regex  =   @" \[\b( "   +  SqlStr  +   @" )\b\] " ;
             Regex Regex  =   new  Regex(str_Regex, RegexOptions.IgnoreCase);
             MatchCollection matches  =  Regex.Matches(outputstring);
              for  ( int  i  =   0 ; i  <  matches.Count; i ++ )
                  outputstring  =  outputstring.Replace(matches.Value, matches.Value.Substring( 1 , matches .Value.Length  -   2 ));

         }
     }
      catch
         {
           return   "" ;
      }
       return  outputstring;
  }
  #endregion

我们的解决方式是:
1、首先在UI录入时,要控制数据的类型和长度、防止SQL注入式攻击,系统提供检测注入式攻击的函数,一旦检测出注入式攻击,该数据即不能提交;
2、业务逻辑层控制,通过在方法内部将SQL关键字用一定的方法屏蔽掉,然后检查数据长度,保证提交SQL时,不会有SQL数据库注入式攻击代码;但是这样处理后,要求UI输出时将屏蔽的字符还原。因此系统提供屏蔽字符的函数和还原字符的函数。
3、在数据访问层,绝大多数采用存储过程访问数据,调用时以存储过程参数的方式访问,也会很好的防止注入式攻击。

评分

参与人数 1经验 +5 收起 理由
心跳回忆 + 5 感谢解答: )

查看全部评分

shiyuelaohu
发表于 2012-9-27 20:53:40 | 显示全部楼层
VSE不能拦截SQL注入,就连很多的防火墙也都束手无策。
lookf
发表于 2012-9-28 07:59:42 | 显示全部楼层
咖啡无法实现该功能。你可以在服务器上安装一个iis防火墙,百度搜索 智创防火墙,这个就是iis防火墙了,可以过滤sql注入

评分

参与人数 1经验 +3 收起 理由
心跳回忆 + 3 感谢解答: )

查看全部评分

hblf
发表于 2012-9-28 17:11:23 | 显示全部楼层
本帖最后由 hblf 于 2012-9-28 17:12 编辑

1.sql注入这种应用层攻击用杀毒软件过滤,效果恐怕要打个问号,攻击者只要经验足够丰富,可以任意构造、编码出注入语句,不是简单的过滤下select、order就可以有效的。
2.针对web攻击,各家安全厂商推出有web防火墙,一个1U的硬件设备,里面有各家公司的攻防高手积累的攻击代码特征,进而过滤阻拦web攻击,不只是sql注入,还有xss、csrf等等等等,即便是这样,web安全爱好者仍旧找到了一些行之有效的绕过waf(web application firewall)的办法和技巧,可见,不从代码本身入手,从通信角度是不可能彻底封堵web攻击的。设备和软件是死的,攻击是活的。
3.我认为用杀软规则、用安全宝、用安全狗这些产品可以一定程度上防御非顶级web攻击,那些只会用havij、明小子或者啊d扫一扫网站的脚本小子,的确对这些安全措施束手无策。
4.要真正防御web攻击,代码本身的安全加固才是唯一王道。

评分

参与人数 1经验 +5 收起 理由
蓝核 + 5 感谢解答: )

查看全部评分

不和陌生人聊
发表于 2012-9-30 18:32:41 | 显示全部楼层
防SQL注入用网站安全狗,有IIS,APACHE,Linux版本,免费的好用,你可试试,百度一下就能找到,我就不给网址了
vino
发表于 2012-10-1 14:49:24 | 显示全部楼层
web server 如果是微軟的 IIS + asp.net ...
用微軟的 urlscan 來防禦也是多少有用處的...
您需要登录后才可以回帖 登录 | 快速注册

本版积分规则

手机版|杀毒软件|软件论坛| 卡饭论坛

Copyright © KaFan  KaFan.cn All Rights Reserved.

Powered by Discuz! X3.4( 沪ICP备2020031077号-2 ) GMT+8, 2025-5-17 15:28 , Processed in 0.143504 second(s), 18 queries .

卡饭网所发布的一切软件、样本、工具、文章等仅限用于学习和研究,不得将上述内容用于商业或者其他非法用途,否则产生的一切后果自负,本站信息来自网络,版权争议问题与本站无关,您必须在下载后的24小时之内从您的电脑中彻底删除上述信息,如有问题请通过邮件与我们联系。

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