实例代码:可以有效防止sql注入

  编程之家 jb51.cc:我们先来看一段代码:

  /// <summary>
/// 移除字符串中的可能引起危险Sql字符
/// </summary>
/// <param name=str></param>
/// <returns></returns>
public static string RemoveSqlUnsafeString(string str)
{
string p = @[-|;|,|/|(|)|[|]|}|{|%|@|*|!|'];
return Regex.Replace(str,p,);
}
/// <summary>
/// 检测是否有Sql危险字符
/// </summary>
/// <param name=str>要判断字符串</param>
/// <returns>判断结果</returns>
public static bool IsSafeSqlString(string str)
{

return !Regex.IsMatch(str,@[-|;|,|/|(|)|[|]|}|{|%|@|*|!|']);
}

/// <summary>
/// 替换sql语句中的有问题符号
/// </summary>
public static string ChkSQL(string str)
{
string str2;

if (str == null)
{
str2 = ;
}
else
{
str = str.Replace(','');
str2 = str;
}
return str2;
}
#region 过滤攻击性字符
/// <summary>
/// 过滤攻击性字符
/// </summary>
/// <param name=str></param>
/// <returns></returns>
public static string ReplaceBadChar(string str)
{
if (!string.IsNullOrEmpty(str))
{
str = Regex.Replace(str,@(?s)/*.*?*/,,RegexOptions.IgnoreCase); //删除注释:/* */
str = Regex.Replace(str,@(?s)<script.*?>.*?</script>,RegexOptions.IgnoreCase); //删除脚本
str = Regex.Replace(str,@(?s)<style.*?>.*?</style>,RegexOptions.IgnoreCase);
//需要把用户自己添加的样式都删除
//<link href=/scripts/PopBox/stylesheets/Styles.css rel=stylesheet type=text/css />
str = Regex.Replace(str,@(?s)<link[^>]+href+([^>]+?)>,RegexOptions.IgnoreCase);

//替换一些比较特殊的字符
// str = str.Replace(&nbsp;, ); //将&nbsp;替换为一个空格
str = str.Replace(&mdash;,-);//将&mdash;替换为-
str = str.Replace(&rdquo;,”);
str = str.Replace(&ldquo;,“);
str = str.Replace(&le;,<=);
str = str.Replace(&ne;,!=);
str = str.Replace(&ge;,>=);

//<img src= onerror= /> <([^>|^<]+?on)([w]+[^=]+?)=([^>]+?)>
str = Regex.Replace(str,@<([^>|^<]+?on)([a-z|A-Z]+[^=]+?)=([^>]+?)>,
<$1_$2=$3>,RegexOptions.IgnoreCase);//过滤可能的XSS攻击,脚本事件

//javascript:
str = str.Replace(javascript:,javascript:);//过滤<img src=javascript:alert(/xss/) />

str = str.Replace(vbscrript:,vbscript:);//过滤vbscript

str = str.Replace(script,script);//过滤所有可能的脚本 liehuo.net

//style=XSS:expression(alert(/xss/))
str = str.Replace(expression,Expression);//过滤所有可能的脚本
//str=Regex.Replace(str,@(style(.*))=(.*)(expression),$1=$3,
RegexOptions.IgnoreCase); //过滤样式中,可能带有的脚本事件
//<iframe src=


str = Regex.Replace(str,(?s)<iframe.*?>.*?</iframe>,
RegexOptions.IgnoreCase);//过滤Ifrmae;网

//防止转码XSS攻击:<img src=&#106&#97&#118&#97&#115&#99&#114&#105&#112&#116&#58&#97
&#108&#101&#114&#116&#40&#39&#88&#83&#83&#39&#41&#59>
str = str.Replace(#,#);//过滤#
// str = str.Replace(&,&);//过滤&
str = str.Replace(%,%);//过滤%

//<img STYLE=background-image: 75726c286a61766173
63726970743a616c6572742827585353272929>
str = str.Replace(,/);//过滤 防止连接16进制的攻击

if (str.IndexOf(<script) >= 0)
str = str.Replace(<,&lt;--script);

if (str.IndexOf(') > 0)
str = str.Replace(',’);

//str = str.Replace(<,&lt;);
//str = str.Replace(>,&gt;);

}
return str;
}
#endregion

相关文章

本篇内容主要讲解“sqlalchemy的常用数据类型怎么使用”,感...
今天小编给大家分享一下sqlServer实现分页查询的方式有哪些的...
这篇文章主要介绍“sqlmap之osshell怎么使用”,在日常操作中...
本篇内容介绍了“SQL注入的知识点有哪些”的有关知识,在实际...
1. mssql权限sa权限:数据库操作,文件管理,命令执行,注册...
sql执行计划如何查看?在SPL庞大的数据中我们不知道如何查看...