个人经验其实SQL注入主要2中地方进去:1是地址栏,2是用户文本提交(其实2种都是用户提交,但是还是有所区别)
1.地址栏:只要是URL参数攻击,这方面讲的最多
主要参数有2中,一种是数值型,这种好防,只要来个数值转换,转换不了转错误页,再配合参数化SQL语句,基本没法供,
一种是字符串型,只有过滤敏感字符+参数化,这里求一个好的齐全的过滤函数(hehe),
2.用户文本提交,主要是一些HTML标签特别是JS脚本的攻击例如文本里有如下字:
<script>alert("haha"); </script>
写个无限循环要你命,上次CSDN就出过这种事!
主要要把<>换成<>,
这里再求这方面好的过滤函数
大家讨论下!高手指点下
1.地址栏:只要是URL参数攻击,这方面讲的最多
主要参数有2中,一种是数值型,这种好防,只要来个数值转换,转换不了转错误页,再配合参数化SQL语句,基本没法供,
一种是字符串型,只有过滤敏感字符+参数化,这里求一个好的齐全的过滤函数(hehe),
2.用户文本提交,主要是一些HTML标签特别是JS脚本的攻击例如文本里有如下字:
<script>alert("haha"); </script>
写个无限循环要你命,上次CSDN就出过这种事!
主要要把<>换成<>,
这里再求这方面好的过滤函数
大家讨论下!高手指点下
------------------------------------------------------------最简单的办法:数据库操作使用参数传递, 还有个办法存储过程其他的就是数据过滤了,但是数据过滤操作比较繁琐,你转换后如果输出还的转换回来,效率上估计有些损失。代码这些就太多了 呵呵。愚人之见。见笑
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
/// <summary>
/// 对字符串进行检查和替换其中的特殊字符
/// </summary>
/// <param name="strHtml"></param>
/// <returns></returns>
public static string HtmlToTxt(string strHtml)
{
string[] aryReg ={
@"<script[^>]*?>.*?</script>",
@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
@"([\r\n])[\s]+",
@"&(quot|#34);",
@"&(amp|#38);",
@"&(lt|#60);",
@"&(gt|#62);",
@"&(nbsp|#160);",
@"&(iexcl|#161);",
@"&(cent|#162);",
@"&(pound|#163);",
@"&(copy|#169);",
@"&#(\d+);",
@"-->",
@"<!--.*\n"
};
string newReg = aryReg[0];
string strOutput = strHtml;
for (int i = 0; i < aryReg.Length; i++)
{
Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
strOutput = regex.Replace(strOutput, string.Empty);
}
strOutput.Replace("<", "");
strOutput.Replace(">", "");
strOutput.Replace("\r\n", ""); return strOutput;
}=================另外两个函数=====================
/// <summary>
/// 替换html中的特殊字符
/// </summary>
/// <param name="theString">需要进行替换的文本。</param>
/// <returns>替换完的文本。</returns>
public string HtmlEncode(string theString)
{
theString = theString.Replace(">", ">");
theString = theString.Replace("<", "<");
theString = theString.Replace(" ", " ");
theString = theString.Replace(" ", " ");
theString = theString.Replace("\"", """);
theString = theString.Replace("\'", "'");
theString = theString.Replace("\n", "<br/> ");
return theString;
}
/// <summary>
/// 恢复html中的特殊字符
/// </summary>
/// <param name="theString">需要恢复的文本。</param>
/// <returns>恢复好的文本。</returns>
public string HtmlDiscode(string theString)
{
theString = theString.Replace(">", ">");
theString = theString.Replace("<", "<");
theString = theString.Replace(" ", " ");
theString = theString.Replace(" ", " ");
theString = theString.Replace(""", "\"");
theString = theString.Replace("'", "\'");
theString = theString.Replace("<br/> ", "\n");
return theString;
}
---------------------------------------------------------------------------以上转自别人的
2,Server.HtmlEncode(提交的可能含有HTML的内容)就可以
如果是URL注入的话我们一般采用的方式是加密
这样的话可以防止一般的注入,但是对高级感觉不行,
2.用户文本提交
我曾经做的一个网站就是出现了文本注入(注入的是<a></a>)类的广告链接,
他们是通过留言版注入的,
我们采用的办法是用 “正则”进行过虑,即使他写了再多的脚本标签,也会被轻松过虑。
同时格式化数据
public bool CheckBadWord(string str)
{
string pattern = @"select|insert|delete|from|count\(|drop table|update|truncate|asc\(|mid\(|char\(|xp_cmdshell|exec master|netlocalgroup administrators|:|net user|""|or|and";
if (Regex.IsMatch(str, pattern, RegexOptions.IgnoreCase) || Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']"))
return true;
return false;
}
StringBuilder sb = new StringBuilder( HttpUtility.HtmlEncode(htmlInputTxt.Text));
sb.Replace("<b>", "<b>");
sb.Replace("</b>", "");
sb.Replace("<i>", "<i>");
sb.Replace("</i>", "");
Response.Write(sb.ToString());
参考