存储过程真的可以防sql注入攻击吗??

qingYun1029 2010-11-04 03:34:31
//下面是我写的一个关于GridView的例子;
int rid = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
string rtitle = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
string rauthor = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
string rtime = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
//我写的一条sql执行语句,里面的rid 、rtitle 、rauthor 、rtime 是从GridView控件里面获取的,
string sqlCon = "update news set ntitle='" + rtitle + "',nauthor='" + rauthor + "',ntime='" + rtime + "' where nid=" + rid;
//如果用户在rtitle输入 '--' 的话就变成
[code=SQL]update news set ntitle=''--'',nauthor='--',ntime='2007/8/15 0:00:00' where nid=31

//这样的话数据库就彻底崩溃了



//现在我把他改成调用存储过程的方式来修改,
int rid = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
string rtitle = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
string rauthor = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
string rtime = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
using (SqlConnection con = new SqlConnection(conStr))
{
string procName = "proc_updatenews";
SqlCommand com = new SqlCommand(procName, con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@rid", rid);
com.Parameters.AddWithValue("@rtitle", rtitle);
com.Parameters.AddWithValue("@rauthor", rauthor);
com.Parameters.AddWithValue("@rtime", rtime);

con.Open();
result = com.ExecuteNonQuery();
}
//这样执行的结果却是好的。。[/code]

问题一:存储过程可以防止SQL注入攻击吗???
问题二:在调用存储过程时如何能看到执行的SQl语句(在直接使用sql语句时,调试就可以看到了。)???

...全文
993 31 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
qingYun1029 2010-11-05
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 begintransaction 的回复:]

存诸过程还有个好处呀,可以减少服务器编译.
[/Quote]

嘿嘿,你可爱!!!
madelaop5566 2010-11-04
  • 打赏
  • 举报
回复
直接在代码里防不就完了么
snowmagic 2010-11-04
  • 打赏
  • 举报
回复
两件事, 一个是如何向数据库传参数,一个是sql注入
对于传参,可以在code里把参数填进sql,数据库直接执行sql
还有就是把sql和参数一起传给数据库,类似这样"select * from tbl where id=@id, @id=123"

对于sql注入,是要避免动态拼sql,即使用参数化调用sp,如果在sp里有拼sql和exec(sql),一样有可能受到sql注入攻击
SK_Aqi 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 qingyun1029 的回复:]
大家可以去看看,这是我想要的意思,也是我想要的例子!!!
谢谢各位了!!!

http://hi.baidu.com/ncheng/blog/item/e62e7d519f600a14367abed8.html
[/Quote]
不是一样的吗?没有什么不同吧!
qingYun1029 2010-11-04
  • 打赏
  • 举报
回复
大家可以去看看,这是我想要的意思,也是我想要的例子!!!
谢谢各位了!!!

http://hi.baidu.com/ncheng/blog/item/e62e7d519f600a14367abed8.html
qingYun1029 2010-11-04
  • 打赏
  • 举报
回复
找到符合自己意思的方法了,分享一下,谢谢支持!!!
int rid = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
string rtitle = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
string rauthor = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
string rtime = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text;

string sqlCon = @"update news set ntitle=@rtitle,nauthor=@rauthor,ntime=@rtime where nid=@rid";
string conStr = GetSqlconStr();
int result = 0;
using (SqlConnection con = new SqlConnection(conStr))
{
SqlCommand com = new SqlCommand(sqlCon, con);
SqlParameter[] paras = { new SqlParameter("@rtitle", rtitle), new SqlParameter("@rauthor", rauthor), new SqlParameter("@rtime", rtime), new SqlParameter("@rid", rid) };
foreach (SqlParameter para in paras)
{
com.Parameters.Add(para);
}
con.Open();
result = com.ExecuteNonQuery();
}
TITI_Yu 2010-11-04
  • 打赏
  • 举报
回复
不要搞成一条SQL传入存储过程,如果只是把参数直接传进去存储过程,我记得是可以防止SQL注入攻击的,所有传入的数据,不管你是不是单引号,都会作为参数处理。
qingYun1029 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 yubanzhi 的回复:]

参数化可以防注入攻击. 不要把问题搞混淆了。
存储过程是否防注入。就看你的参数如何传。

第二个, 你需要看存储过程执行的SQl 语句, 如果你用的是SQl Server 2005 以上版本的话, 其有个SQL Server Porfier ,可以监控。(SQL Server 菜单“工具”第一项)
[/Quote]

对不住了,没有用过这个工具,还需要研究研究!!!
qingYun1029 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 xjq198510258 的回复:]

参数化才是防止SQL注入的关键,而不是存储过程。

拼接SQL也是可以防止SQL的,只要注意数据类型和单引号即可
[/Quote]

什么叫参数化,就是上面那种用存储过程的参数设置形式吗

using (SqlConnection con = new SqlConnection(conStr))
{
string procName = "update news set ntitle=@rtitle,nauthor=@rauthor,ntime=@rtime where nid=@rid";
SqlCommand com = new SqlCommand(procName, con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@rid", rid);
com.Parameters.AddWithValue("@rtitle", rtitle);
com.Parameters.AddWithValue("@rauthor", rauthor);
com.Parameters.AddWithValue("@rtime", rtime);

con.Open();
result = com.ExecuteNonQuery();
}

是这样子的吗??
但是上面主要提到的是参数化,而不是存储过程,所以有另外一种参数化的例子没?就是不用存储过程的参数化形式???

参数化是什么意思 ???
发挥着什么样的作用???

另外即便是参数化还是过滤单引号好像也不好过滤那种 where 1=1 的那种吧???


paual779 2010-11-04
  • 打赏
  • 举报
回复
参数化可以防注入攻击. 不要把问题搞混淆了。
存储过程是否防注入。就看你的参数如何传。

第二个, 你需要看存储过程执行的SQl 语句, 如果你用的是SQl Server 2005 以上版本的话, 其有个SQL Server Porfier ,可以监控。(SQL Server 菜单“工具”第一项)
xjq198510258 2010-11-04
  • 打赏
  • 举报
回复
参数化才是防止SQL注入的关键,而不是存储过程。

拼接SQL也是可以防止SQL的,只要注意数据类型和单引号即可
qingYun1029 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 ly_longyue 的回复:]

KarasCanvas(乌鸦) :
比如参数是数组,用的时候强制转成INT 型。过滤还是比较有效的
此消息通过 【CSDN论坛 Winform测试版】 回复!
[/Quote]

被人输入的是字符串类型,怎么可能都改成int类型呢???
begintransaction 2010-11-04
  • 打赏
  • 举报
回复
存诸过程还有个好处呀,可以减少服务器编译.
龍月 2010-11-04
  • 打赏
  • 举报
回复
KarasCanvas(乌鸦) : 比如参数是数组,用的时候强制转成INT 型。过滤还是比较有效的 此消息通过 【CSDN论坛 Winform测试版】 回复!
种草德鲁伊 2010-11-04
  • 打赏
  • 举报
回复
字符串过滤什么的可以不用考虑了,其实那样比参数化要麻烦的多...
龍月 2010-11-04
  • 打赏
  • 举报
回复
关键是 过滤 此消息通过 【CSDN论坛 Winform测试版】 回复!
qingYun1029 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 karascanvas 的回复:]

悲剧...
[/Quote]

是 啊。。

请帮忙解答我的疑问,谢谢!!!
qingYun1029 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hztltgg 的回复:]

C# code

int rid = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
string rtitle = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Replace("'","''");
st……
[/Quote]

但是还有那种1=1、-- 等能这样过滤吗??
种草德鲁伊 2010-11-04
  • 打赏
  • 举报
回复
悲剧...
qingYun1029 2010-11-04
  • 打赏
  • 举报
回复
不好意思啊,各位,回答晚了。。

刚才我的电脑经历了一场浩劫——一大杯茶被我不小心撞到了,眼疾手快救了我的点奥,但是鼠标牺牲了。。
加载更多回复(10)

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧