asp.net防SQL注入 我做了一个网站,是用C#+SQL SERVER 2000做的。没用存储过程。。现在在数据库的一些表中的一些列中加了一段代码,我猜应该是SQL的注入,因为我在代码中没有加防注入的,而且我也不知道怎么加,加在哪??请各位大虾指点指点。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary> /// 该方法用来检测用户输入是否带有恶意 /// </summary> /// <param name="text">用户输入的文字</param> /// <param name="maxlength">最大的长度</param> /// <returns>返回验证后的文字</returns> public static string InputText(string text, int maxlength) { text = text.ToLower().Trim(); if (string.IsNullOrEmpty(text)) return string.Empty; if (text.Length > maxlength) text = text.Substring(0, maxlength); text = Regex.Replace(text, "[\\s]{2,{", " "); text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br> text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); // text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags text = Regex.Replace(text,"=", ""); text = Regex.Replace(text, "%", ""); text = Regex.Replace(text, "'", ""); text = Regex.Replace(text, "select", ""); text = Regex.Replace(text, "insert", ""); text = Regex.Replace(text, "delete", ""); text = Regex.Replace(text, "or", ""); text = Regex.Replace(text, "exec", ""); text = Regex.Replace(text, "--", ""); text = Regex.Replace(text, "and", ""); text = Regex.Replace(text, "where", ""); text = Regex.Replace(text, "update", ""); text = Regex.Replace(text, "script", ""); text = Regex.Replace(text, "iframe", ""); text = Regex.Replace(text, "master", ""); text = Regex.Replace(text, "exec", ""); text = Regex.Replace(text, "<", ""); text = Regex.Replace(text, ">", ""); text = Regex.Replace(text, "\r\n", ""); return text; } 有游标把库里所有char,varchar,nvarchar的字段都替换(前提是没修改你的数据,只是添加了字符)c#防注入的替换看看其他帖,到处都是 用参数,不要直接拼SQL字符串 参考下http://topic.csdn.net/u/20080521/09/dad3eaba-bfc7-483c-98cd-d310f9a76ff0.html?seed=596201967http://topic.csdn.net/u/20080510/16/29c515de-c4d2-41fe-a1dc-df84de18f9b7.html 用SqlParameter传参数就不会有SQL注入的问题了不要用拼接字符串 关键是我的网站已经做完了,不可能把所有的SQL语句全部整改吧 例如:"select * from table where name='" + Request.Query["ID"].ToString() + "'"这句话有可能被sql注入,只要在Url参数中写成"HTTP://xxx.com//xxx.aspx?ID=%' exec SavedProc1"那么sql语句展开后就成了:"select * from table where name='%' exec SavedProc1"从而使得SavedProc1存储过程被非预期的执行。系统中的存储过程能做很多事情,例如访问控制管理、系统shell获取等。上面这句话写成下面的带参数语句,就不会发生这种情况:sqlCmd.CommandText = "select * from table where name=@Name"sqlCmd.Parameters.AddWithValue("@Name", "myname");这样执行的时候无论"myname"是什么文字,都被仅仅看作是文字,而不会作为sql语句执行。 不用这么 麻烦 !!!!比如if(name.Indexof(" ")>0){MessageBox.Show("你输入的用户名有误!");} 那不会每条SQL 语句都加吧,,,,, /// 在Global.asax.cs文件protected void Application_BeginRequest(Object sender, EventArgs e)内加入 /// <summary> /// SQL脚本检测 /// </summary> /// <param name="FormNV">POST:Form键值对</param> /// <param name="QueryNV">GET:Query键值对</param> /// <returns></returns> public bool checksql(NameValueCollection FormNV, NameValueCollection QueryNV) { string Fy_getIn, Fy_postIn; int i; bool SqlIn=false; Fy_getIn = "#|exec|insert|select|delete|update|%|chr|mid|master|truncate|declare|*";//get方法要过滤的 Fy_postIn = "exec|insert|select|delete|update|master|truncate|declare";//post方法要过滤的 string[] Fy_postInf = Fy_postIn.Split('|');//分离攻击post字符 string[] Fy_getInf = Fy_getIn.Split('|');//分离攻击get字符 //=============================post================= if (FormNV.Count != 0) { foreach (string Fy_Post in FormNV.Keys) { for (i = 0; i < Fy_postInf.GetUpperBound(0); i++)//判断是否为sql注入单词 if (FormNV[Fy_Post].IndexOf(Fy_postInf[i].ToLower()) != -1) { SqlIn=true;break;//POST方法存在SQL关键字 } } } //================get==== if (QueryNV.Count != 0&&!SqlIn) { foreach (string Fy_Get in QueryNV.Keys) { for (i = 0; i < Fy_getInf.GetUpperBound(0); i++)//判断是否为sql注入单词,如果是则保存攻击信息 if (QueryNV[Fy_Get].IndexOf(Fy_getInf[i].ToLower()) != -1) { SqlIn=true;break;//GET方法存在SQL关键字 } } } return SqlIn; }bool sqlIn=checksql(Request.Form,Request.QueryString);if(sqlIn) Response.Redirect("/", true);--针对前面时间大面积SQL注入--遍历所有字段,查找以 @SearchString开头的内容,并删除以@SearchString直到该内容末尾USE dbDECLARE @fieldtype sysname DECLARE @SearchString varchar(500)SET @fieldtype='nvarchar' SET @SearchString = '</ti'--删除处理 DECLARE hCForEach CURSOR GLOBAL FOR SELECT N'update '+QUOTENAME(o.name) +N' set '+ QUOTENAME(c.name) + N' = stuff(' + QUOTENAME(c.name) + ',charindex(''' + @SearchString + ''',' + QUOTENAME(c.name) + '),len(' + QUOTENAME(c.name) + '),'''')' +N' where charindex(''' + @SearchString + ''',' + QUOTENAME(c.name) + ') > 0'FROM sysobjects o,syscolumns c,systypes t WHERE o.id=c.id AND OBJECTPROPERTY(o.id,N'IsUserTable')=1 AND c.xusertype=t.xusertype AND t.name=@fieldtype EXEC sp_MSforeach_Worker @command1=N'?' Aspx好像本身就有这些过滤功能吧如果觉得这个不安全就用LINQ,那个就根本没办法注入了。 你是要全站整改,最好对你每个页传入的参数进行判断,长度,类型等如果用参数传,就得改每个sql语句 很简单么,你对输入的值用验证正则表达式验证不就行了,传值也是,在你的redirect语句前判断下有没有不规矩的代码。 或者你要对他们进行URL或者html编码。 进行html编码是这样的,server.htmlencode(控件.text)这样的话它就不在服务器端运行了直接存储或者直接作为html发到浏览器上。如果要更复杂更严谨的。那就用正则表达式去判断那些敏感的信息,比如&字符等 。 ms专家给你的防SQL Injection的方法:http://blogs.technet.com/swi/archive/2008/05/29/sql-injection-attack.aspx 在ADO.NET中经常需要跟各种数据库打交道,在不实用存储过程的情况下,使用参数化SQL语句一定程度上可以防止SQL注入,同时对一些较难赋值的字段(如在SQL Server中Image字段,在Oracle中Clob字段等)使用参数化SQL语句很容易就能赋值,所以本人经常在ADO.NET中使用参数化SQL语句,近几年来陆续跟SQL Server/Oracle/ MySQL/Access打交道,积累了一些心得,现在整理出来供大家参考。http://blog.csdn.net/zhoufoxcn/archive/2008/03/19/2195618.aspx 使用正则表达式,要添加个USINGusing System.Text.RegularExpressions; smtp邮件发送HTML表格 求教以下winf源代码如何添加图片打印,贴出代码 new Bitmap 参数带有空格的问题 我要做个文本比较器,不知道C#里头,有没有这样现成的类可以使用的? ThreadPool使用问题 嗯,这个比较麻烦,高分。。 有谁用c#做过IM Server? 请问textbox的内容怎么以不同颜色的字体显示,在线等!!! 急!!!!!关于C#里定时启动的问题 C#执行mysql 插入语句时报错,看不懂? DevExpress.Xpo如何解并发处理 各位大侠帮下忙 关于条码软件barTender 调用的
/// 该方法用来检测用户输入是否带有恶意
/// </summary>
/// <param name="text">用户输入的文字</param>
/// <param name="maxlength">最大的长度</param>
/// <returns>返回验证后的文字</returns>
public static string InputText(string text, int maxlength)
{
text = text.ToLower().Trim();
if (string.IsNullOrEmpty(text))
return string.Empty;
if (text.Length > maxlength)
text = text.Substring(0, maxlength); text = Regex.Replace(text, "[\\s]{2,{", " ");
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags
text = Regex.Replace(text,"=", "");
text = Regex.Replace(text, "%", "");
text = Regex.Replace(text, "'", "");
text = Regex.Replace(text, "select", "");
text = Regex.Replace(text, "insert", "");
text = Regex.Replace(text, "delete", "");
text = Regex.Replace(text, "or", "");
text = Regex.Replace(text, "exec", "");
text = Regex.Replace(text, "--", "");
text = Regex.Replace(text, "and", "");
text = Regex.Replace(text, "where", "");
text = Regex.Replace(text, "update", "");
text = Regex.Replace(text, "script", "");
text = Regex.Replace(text, "iframe", "");
text = Regex.Replace(text, "master", "");
text = Regex.Replace(text, "exec", "");
text = Regex.Replace(text, "<", "");
text = Regex.Replace(text, ">", "");
text = Regex.Replace(text, "\r\n", ""); return text;
}
c#防注入的替换看看其他帖,到处都是
http://topic.csdn.net/u/20080521/09/dad3eaba-bfc7-483c-98cd-d310f9a76ff0.html?seed=596201967
http://topic.csdn.net/u/20080510/16/29c515de-c4d2-41fe-a1dc-df84de18f9b7.html
就不会有SQL注入的问题了
不要用拼接字符串
例如:
"select * from table where name='" + Request.Query["ID"].ToString() + "'"
这句话有可能被sql注入,只要在Url参数中写成"HTTP://xxx.com//xxx.aspx?ID=%' exec SavedProc1"
那么sql语句展开后就成了:"select * from table where name='%' exec SavedProc1"
从而使得SavedProc1存储过程被非预期的执行。系统中的存储过程能做很多事情,例如访问控制管理、系统shell获取等。上面这句话写成下面的带参数语句,就不会发生这种情况:sqlCmd.CommandText = "select * from table where name=@Name"
sqlCmd.Parameters.AddWithValue("@Name", "myname");这样执行的时候无论"myname"是什么文字,都被仅仅看作是文字,而不会作为sql语句执行。
比如
if(name.Indexof(" ")>0)
{
MessageBox.Show("你输入的用户名有误!");
}
那不会每条SQL 语句都加吧,,,,,
/// 在Global.asax.cs文件protected void Application_BeginRequest(Object sender, EventArgs e)内加入
/// <summary>
/// SQL脚本检测
/// </summary>
/// <param name="FormNV">POST:Form键值对</param>
/// <param name="QueryNV">GET:Query键值对</param>
/// <returns></returns>
public bool checksql(NameValueCollection FormNV, NameValueCollection QueryNV)
{
string Fy_getIn, Fy_postIn;
int i;
bool SqlIn=false;
Fy_getIn = "#|exec|insert|select|delete|update|%|chr|mid|master|truncate|declare|*";//get方法要过滤的
Fy_postIn = "exec|insert|select|delete|update|master|truncate|declare";//post方法要过滤的
string[] Fy_postInf = Fy_postIn.Split('|');//分离攻击post字符
string[] Fy_getInf = Fy_getIn.Split('|');//分离攻击get字符
//=============================post=================
if (FormNV.Count != 0)
{
foreach (string Fy_Post in FormNV.Keys)
{
for (i = 0; i < Fy_postInf.GetUpperBound(0); i++)//判断是否为sql注入单词
if (FormNV[Fy_Post].IndexOf(Fy_postInf[i].ToLower()) != -1)
{
SqlIn=true;break;//POST方法存在SQL关键字
}
}
}
//================get====
if (QueryNV.Count != 0&&!SqlIn)
{
foreach (string Fy_Get in QueryNV.Keys)
{
for (i = 0; i < Fy_getInf.GetUpperBound(0); i++)//判断是否为sql注入单词,如果是则保存攻击信息
if (QueryNV[Fy_Get].IndexOf(Fy_getInf[i].ToLower()) != -1)
{
SqlIn=true;break;//GET方法存在SQL关键字
}
}
}
return SqlIn;
}bool sqlIn=checksql(Request.Form,Request.QueryString);
if(sqlIn)
Response.Redirect("/", true);--针对前面时间大面积SQL注入
--遍历所有字段,查找以 @SearchString开头的内容,并删除以@SearchString直到该内容末尾
USE db
DECLARE @fieldtype sysname
DECLARE @SearchString varchar(500)
SET @fieldtype='nvarchar'
SET @SearchString = '</ti'--删除处理
DECLARE hCForEach CURSOR GLOBAL
FOR
SELECT N'update '+QUOTENAME(o.name)
+N' set '+ QUOTENAME(c.name) + N' = stuff(' + QUOTENAME(c.name) + ',charindex(''' + @SearchString + ''',' + QUOTENAME(c.name) + '),len(' + QUOTENAME(c.name) + '),'''')'
+N' where charindex(''' + @SearchString + ''',' + QUOTENAME(c.name) + ') > 0'
FROM sysobjects o,syscolumns c,systypes t
WHERE o.id=c.id
AND OBJECTPROPERTY(o.id,N'IsUserTable')=1
AND c.xusertype=t.xusertype
AND t.name=@fieldtype
EXEC sp_MSforeach_Worker @command1=N'?'
如果觉得这个不安全就用LINQ,那个就根本没办法注入了。
如果用参数传,就得改每个sql语句
那就用正则表达式去判断那些敏感的信息,比如&字符等 。
http://blogs.technet.com/swi/archive/2008/05/29/sql-injection-attack.aspx
http://blog.csdn.net/zhoufoxcn/archive/2008/03/19/2195618.aspx
使用正则表达式,要添加个USING
using System.Text.RegularExpressions;