天天看點

危險字元過濾的類

危險字元過濾的類

using System;

危險字元過濾的類

using System.IO;

危險字元過濾的類

using System.Text;

危險字元過濾的類

using System.Text.RegularExpressions;

危險字元過濾的類

using System.Runtime.Remoting;

危險字元過濾的類

using System.Runtime.Remoting.Proxies;

危險字元過濾的類

using System.Runtime.Remoting.Messaging;

危險字元過濾的類

using System.Reflection;

危險字元過濾的類
危險字元過濾的類

namespace FilterRealProxy

危險字元過濾的類

{

危險字元過濾的類

 /// <summary>

危險字元過濾的類

 ///  FilterRealProxy類:一個真實代理, 攔截它所代理對象中方法的傳回值,并對需要過濾的傳回值進行過濾。

危險字元過濾的類

 /// </summary>

危險字元過濾的類

 public class FilterRealProxy:RealProxy

危險字元過濾的類

 {

危險字元過濾的類

  private MarshalByRefObject target;

危險字元過濾的類

  public FilterRealProxy(MarshalByRefObject target):base(target.GetType())

危險字元過濾的類

  {

危險字元過濾的類

   this.target=target;    

危險字元過濾的類

  }

危險字元過濾的類

  public override IMessage Invoke(IMessage msg)

危險字元過濾的類
危險字元過濾的類

   IMethodCallMessage callMsg=msg as IMethodCallMessage;

危險字元過濾的類

   IMethodReturnMessage returnMsg = RemotingServices.ExecuteMessage(target,callMsg);

危險字元過濾的類

   //檢查傳回值是否為String,如果不是String,就沒必要進行過濾

危險字元過濾的類

   if(this.IsMatchType(returnMsg.ReturnValue))

危險字元過濾的類

   {

危險字元過濾的類

    string returnValue=this.Filter(returnMsg.ReturnValue.ToString(),returnMsg.MethodName);            

危險字元過濾的類

    return new ReturnMessage(returnValue,null,0,null,callMsg);

危險字元過濾的類

   }

危險字元過濾的類

   return returnMsg;

危險字元過濾的類

     }

危險字元過濾的類

  protected string Filter(string ReturnValue,string MethodName)

危險字元過濾的類
危險字元過濾的類

   MethodInfo methodInfo=target.GetType().GetMethod(MethodName);

危險字元過濾的類

   object[] attributes=methodInfo.GetCustomAttributes(typeof(StringFilter),true);

危險字元過濾的類

   foreach (object attrib in attributes)

危險字元過濾的類
危險字元過濾的類

    return FilterHandler.Process(((StringFilter)attrib).FilterType,ReturnValue);

危險字元過濾的類
危險字元過濾的類

   return ReturnValue;

危險字元過濾的類
危險字元過濾的類

  protected bool IsMatchType(object obj)

危險字元過濾的類
危險字元過濾的類

   return obj is System.String;

危險字元過濾的類
危險字元過濾的類

 }

危險字元過濾的類
危險字元過濾的類

 ///<summary>

危險字元過濾的類

 ///  StringFilter類:自定義屬性類, 定義目标元素的過濾類型 

危險字元過濾的類

 ///</summary>

危險字元過濾的類

 public class StringFilter:Attribute

危險字元過濾的類
危險字元過濾的類

  protected FilterType _filterType;

危險字元過濾的類
危險字元過濾的類

  public StringFilter(FilterType filterType)

危險字元過濾的類
危險字元過濾的類

   this._filterType=filterType;

危險字元過濾的類
危險字元過濾的類

  public FilterType FilterType

危險字元過濾的類
危險字元過濾的類

   get

危險字元過濾的類
危險字元過濾的類

    return _filterType;

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類
危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

 /// 枚舉類:用于指定過濾類型,例如:對script過濾還是對html進行過濾?

危險字元過濾的類
危險字元過濾的類

 [Flags()]

危險字元過濾的類

 public enum FilterType

危險字元過濾的類
危險字元過濾的類

  Script = 1,

危險字元過濾的類

  Html =2,

危險字元過濾的類

  Object=3,

危險字元過濾的類

  AHrefScript=4,

危險字元過濾的類

  Iframe=5,

危險字元過濾的類

  Frameset=6,

危險字元過濾的類

  Src=7,

危險字元過濾的類

  BadWords=8,

危險字元過濾的類

  //Include=9,

危險字元過濾的類

  All=16

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

 /// 過濾處理類:根據過濾類型,調用相應的過濾處理方法。

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

 public class FilterHandler

危險字元過濾的類
危險字元過濾的類

  private FilterHandler()

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

  public static string Process(FilterType filterType,string filterContent)

危險字元過濾的類
危險字元過濾的類

   switch(filterType)

危險字元過濾的類
危險字元過濾的類

    case FilterType.Script:

危險字元過濾的類

     filterContent=FilterScript(filterContent);

危險字元過濾的類

     break;

危險字元過濾的類

    case FilterType.Html:

危險字元過濾的類

     filterContent=FilterHtml(filterContent);

危險字元過濾的類
危險字元過濾的類

    case FilterType.Object:

危險字元過濾的類

     filterContent=FilterObject(filterContent);

危險字元過濾的類
危險字元過濾的類

    case FilterType.AHrefScript:

危險字元過濾的類

     filterContent=FilterAHrefScript(filterContent);

危險字元過濾的類
危險字元過濾的類

    case FilterType.Iframe:

危險字元過濾的類

     filterContent=FilterIframe(filterContent);

危險字元過濾的類
危險字元過濾的類

    case FilterType.Frameset:

危險字元過濾的類

     filterContent=FilterFrameset(filterContent);

危險字元過濾的類
危險字元過濾的類

    case FilterType.Src:

危險字元過濾的類

     filterContent=FilterSrc(filterContent);

危險字元過濾的類
危險字元過濾的類

    //case FilterType.Include:

危險字元過濾的類

    // filterContent=FilterInclude(filterContent);

危險字元過濾的類

    // break;

危險字元過濾的類

    case FilterType.BadWords:

危險字元過濾的類

     filterContent=FilterBadWords(filterContent);

危險字元過濾的類
危險字元過濾的類

    case FilterType.All:

危險字元過濾的類

     filterContent=FilterAll(filterContent);

危險字元過濾的類
危險字元過濾的類

    default:

危險字元過濾的類

     //do nothing

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

   return filterContent;

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

  public static string FilterScript(string content)

危險字元過濾的類
危險字元過濾的類

   string commentPattern = @"(?'comment'<!--.*?--[ \n\r]*>)" ;

危險字元過濾的類

   string embeddedScriptComments = @"(\/\*.*?\*\/|\/\/.*?[\n\r])" ;

危險字元過濾的類

   string scriptPattern = String.Format(@"(?'script'<[ \n\r]*script[^>]*>(.*?{0}?)*<[ \n\r]*/script[^>]*>)", embeddedScriptComments ) ;

危險字元過濾的類

   // 包含注釋和Script語句

危險字元過濾的類

   string pattern = String.Format(@"(?s)({0}|{1})", commentPattern, scriptPattern) ;

危險字元過濾的類
危險字元過濾的類

   return StripScriptAttributesFromTags(Regex.Replace(content,pattern,string.Empty,RegexOptions.IgnoreCase));

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

  private static string StripScriptAttributesFromTags( string content )

危險字元過濾的類
危險字元過濾的類

   string eventAttribs = @"on(blur|c(hange|lick)|dblclick|focus|keypress|(key|mouse)(down|up)|(un)?load

危險字元過濾的類

                    |mouse(move|o(ut|ver))|reset|s(elect|ubmit))" ;

危險字元過濾的類
危險字元過濾的類

   string pattern = String.Format(@"(?inx)

危險字元過濾的類

        \<(\w+)\s+

危險字元過濾的類

            (

危險字元過濾的類

                (?'attribute'

危險字元過濾的類

                (?'attributeName'{0})\s*=\s*

危險字元過濾的類

                (?'delim'['""]?)

危險字元過濾的類

                (?'attributeValue'[^'"">]+)

危險字元過濾的類

                (\3)

危險字元過濾的類

            )

危險字元過濾的類

            |

危險字元過濾的類

            (?'attribute'

危險字元過濾的類

                (?'attributeName'href)\s*=\s*

危險字元過濾的類
危險字元過濾的類

                (?'attributeValue'javascript[^'"">]+)

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

            [^>]

危險字元過濾的類

        )*

危險字元過濾的類

    \>", eventAttribs ) ;

危險字元過濾的類

   Regex re = new Regex( pattern ) ;

危險字元過濾的類

   // 使用MatchEvaluator的委托

危險字元過濾的類

   return re.Replace( content, new MatchEvaluator( StripAttributesHandler ) ) ;

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

  private static string StripAttributesHandler( Match m )

危險字元過濾的類
危險字元過濾的類

   if( m.Groups["attribute"].Success  )

危險字元過濾的類
危險字元過濾的類

    return m.Value.Replace( m.Groups["attribute"].Value, "") ;

危險字元過濾的類
危險字元過濾的類

   else

危險字元過濾的類
危險字元過濾的類

    return m.Value ;

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

  public static string FilterAHrefScript(string content)

危險字元過濾的類
危險字元過濾的類

   string newstr=FilterScript(content);

危險字元過濾的類

   string regexstr=@" href[ ^=]*= *[\s\S]*script *:";

危險字元過濾的類

   return Regex.Replace(newstr,regexstr,string.Empty,RegexOptions.IgnoreCase);

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

  public static string FilterSrc(string content)

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

   string regexstr=@" src *= *['""]?[^\.]+\.(js|vbs|asp|aspx|php|jsp)['""]";

危險字元過濾的類

   return Regex.Replace(newstr,regexstr,@"",RegexOptions.IgnoreCase);

危險字元過濾的類
危險字元過濾的類

/*

危險字元過濾的類

  public static string FilterInclude(string content)

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

   string regexstr=@"<[\s\S]*include *(file|virtual) *= *[\s\S]*\.(js|vbs|asp|aspx|php|jsp)[^>]*>";

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

*/

危險字元過濾的類

  public static string FilterHtml(string content)

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

   string regexstr=@"<[^>]*>";

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

  public static string FilterObject(string content)

危險字元過濾的類
危險字元過濾的類

   string regexstr=@"(?i)<Object([^>])*>(\w|\W)*</Object([^>])*>";

危險字元過濾的類

   return Regex.Replace(content,regexstr,string.Empty,RegexOptions.IgnoreCase);

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

  public static string FilterIframe(string content)

危險字元過濾的類
危險字元過濾的類

   string regexstr=@"(?i)<Iframe([^>])*>(\w|\W)*</Iframe([^>])*>";

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

  public static string FilterFrameset(string content)

危險字元過濾的類
危險字元過濾的類

   string regexstr=@"(?i)<Frameset([^>])*>(\w|\W)*</Frameset([^>])*>";

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

  //移除非法或不友好字元

危險字元過濾的類

  private static string FilterBadWords(string chkStr)

危險字元過濾的類
危險字元過濾的類

    //這裡的非法和不友好字元由你任意加,用“|”分隔,支援正規表達式,由于本Blog禁止貼非法和不友好字元,是以這裡無法加上。

危險字元過濾的類

string BadWords=@"

危險字元過濾的類

";

危險字元過濾的類

   if (chkStr == "")

危險字元過濾的類
危險字元過濾的類

    return "";

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

   string[] bwords = BadWords.Split('#');

危險字元過濾的類

   int i,j;

危險字元過濾的類

   string str;

危險字元過濾的類

   StringBuilder sb = new StringBuilder();

危險字元過濾的類

   for(i = 0; i< bwords.Length; i++)

危險字元過濾的類
危險字元過濾的類

    str=bwords[i].ToString().Trim();

危險字元過濾的類

    string regStr,toStr;

危險字元過濾的類

    regStr=str;

危險字元過濾的類

    Regex r=new Regex(regStr,RegexOptions.IgnoreCase | RegexOptions.Singleline| RegexOptions.Multiline);

危險字元過濾的類

    Match m=r.Match(chkStr);

危險字元過濾的類

    if(m.Success)

危險字元過濾的類

    {

危險字元過濾的類

     j=m.Value.Length;

危險字元過濾的類

     sb.Insert(0,"*",j);

危險字元過濾的類

     toStr=sb.ToString();

危險字元過濾的類

     chkStr=Regex.Replace(chkStr,regStr,toStr,RegexOptions.IgnoreCase | RegexOptions.Singleline| RegexOptions.Multiline);  

危險字元過濾的類

    }

危險字元過濾的類

    sb.Remove(0,sb.Length);

危險字元過濾的類
危險字元過濾的類

   return chkStr;

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

  public static string FilterAll(string content)

危險字元過濾的類
危險字元過濾的類

   content = FilterHtml(content);

危險字元過濾的類

   content = FilterScript(content);

危險字元過濾的類

   content = FilterAHrefScript(content);

危險字元過濾的類

   content = FilterObject(content);

危險字元過濾的類

   content = FilterIframe(content);

危險字元過濾的類

   content = FilterFrameset(content);

危險字元過濾的類

   content = FilterSrc(content);

危險字元過濾的類

   content = FilterBadWords(content);

危險字元過濾的類

   //content = FilterInclude(content);

危險字元過濾的類

   return content;

危險字元過濾的類
危險字元過濾的類
危險字元過濾的類

}

危險字元過濾的類

本文轉自高海東部落格園部落格,原文連結:http://www.cnblogs.com/ghd258/archive/2006/03/21/354970.html,如需轉載請自行聯系原作者