师哥的StringBuilder防注入

che2piaopiao 2009-03-31 09:29:04
这是一位师哥写的类.哈哈. 之前我是一直用参数化的,看这个写起来比较快.

大家感觉怎么样? 有没有其他的BUG呢


public static void Insert(ServiceInfo entity)
{
DBLayer.ExeSql(GetInsertSql(entity));
return;
}

public static string GetInsertSql(ServiceInfo entity)
{
StringBuilder strSql = new StringBuilder("Insert ServiceInfo (");


strSql.Append("userid,");
strSql.Append("serviceid,");
strSql.Append("servicename,");
strSql.Append("companyid,");
strSql.Append("companyname,");
strSql.Append("worktime,");
strSql.Append("tellphone,");
strSql.Append("address,");
strSql.Append("consume,");
strSql.Append("busLine,");
strSql.Append("carbit,");
strSql.Append("img,");
strSql.Append("contest,");
strSql.Append("province,");
strSql.Append("city,");
strSql.Append("country,");
strSql.Append("support,");
strSql.Append("demand,");
strSql.Append("price,");
strSql.Append("Mark,");
strSql.Append("shenhe,");
strSql.Append("imgmark)");
strSql.Append(" values (");

strSql.Append("'" + entity.userid + "',");
strSql.Append("'" + entity.serviceid + "',");
strSql.Append("'" + entity.servicename + "',");
strSql.Append("'" + entity.companyid + "',");
strSql.Append("'" + entity.companyname + "',");
strSql.Append("'" + entity.worktime + "',");
strSql.Append("'" + entity.tellphone + "',");
strSql.Append("'" + entity.address + "',");
strSql.Append("'" + entity.consume + "',");
strSql.Append("'" + entity.busLine + "',");
strSql.Append("'" + entity.carbit + "',");
strSql.Append("'" + entity.img + "',");
strSql.Append("'" + entity.contest + "',");
strSql.Append("'" + entity.province + "',");
strSql.Append("'" + entity.city + "',");
strSql.Append("'" + entity.country + "',");
strSql.Append("'" + entity.support + "',");
strSql.Append("'" + entity.demand + "',");
strSql.Append("'" + entity.price + "',");
strSql.Append("'" + entity.Mark + "',");
strSql.Append("'" + entity.shenhe + "',");
strSql.Append("'" + entity.imgmark + "')");
return strSql.ToString();
}



public static void ExeSql(string sql)
{
if (Func.filterSql(sql) == 1)
{
return ;
}
SqlConnection conn = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}

public static int filterSql(string sSql)
{
int srcLen, decLen = 0;
sSql = sSql.ToLower().Trim();
srcLen = sSql.Length;
sSql = sSql.Replace("exec ", "");
sSql = sSql.Replace("drop ", "");
sSql = sSql.Replace("master ", "");
sSql = sSql.Replace("truncate ", "");
sSql = sSql.Replace("declare ", "");
sSql = sSql.Replace("create ", "");
sSql = sSql.Replace("xp_", "no");
sSql = sSql.Replace("net user", "");
sSql = sSql.Replace("xp_cmdshell", "");
sSql = sSql.Replace("net localgroup administrators", "");
sSql = sSql.Replace("master.dbo.xp_cmdshell", "");
sSql = sSql.Replace("/add", "");
decLen = sSql.Length;
if (srcLen == decLen) return 0; else return 1;
}
...全文
420 42 打赏 收藏 转发到动态 举报
写回复
用AI写文章
42 条回复
切换为时间正序
请发表友善的回复…
发表回复
che2piaopiao 2009-09-06
  • 打赏
  • 举报
回复
[Quote=引用 41 楼 liuguobin_999 的回复:]
拼的太离谱了,速度慢、效率低、易注入、难维护

——————————————————————————————————————
上帝啊,太感谢你了,我终于找到比我还笨的人了,感动啊,呜呜~~~~~~~~~~
[/Quote]


------------------

现在我变了
liuguobin_999 2009-09-04
  • 打赏
  • 举报
回复
拼的太离谱了,速度慢、效率低、易注入、难维护

——————————————————————————————————————
上帝啊,太感谢你了,我终于找到比我还笨的人了,感动啊,呜呜~~~~~~~~~~
junon 2009-04-03
  • 打赏
  • 举报
回复
存储过程参数的个数受限制,参数的类型受限制,如果同时使用SqlParameter
是能够防止SQL注入的,再说使用类,对传入的数据惊醒类型匹配,更能防止非法数据了。
che2piaopiao 2009-04-01
  • 打赏
  • 举报
回复
传入参数有两种:int, string

int型参数拼接:
string sql = "select * from table1 where id=" + int.Parse(Request["id"]);

string型参数拼接:
string sql = "select * from table1 where name='" + Request["name"].Replace("'","''") + "'";

========
请问谁可以注入?
junon 2009-03-31
  • 打赏
  • 举报
回复
单引号的问题都还没考虑,难道没问题!
如果是插入操作,最好还是用存储过程或者Parameter

public override void ExeSql(string strSql, List<string> paramNames, List<object> objParamValues)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = this._conn;
cmd.CommandText = strSql;
if ((paramNames != null) && (paramNames.Count != objParamValues.Count))
{
throw new DataFactory.GoberlDataDll.MyException.ParamValueNotMatchParamNameException();
}


//添加ParamItem
if (paramNames != null)
{
for (int i = 0; i < paramNames.Count; i++)
{
if (objParamValues[i].GetType().ToString() == "System.DateTime")
{
SqlParameter p = new SqlParameter();
p.ParameterName = paramNames[i];
p.SqlDbType = SqlDbType.Date;
p.Value = objParamValues[i];
cmd.Parameters.Add(p);
}
else
cmd.Parameters.AddWithValue(paramNames[i], objParamValues[i]);
}
}
else
{
throw new DataFactory.GoberlDataDll.MyException.ParamValueNotMatchParamNameException();
}

try
{
this.OpenCon();
this.BeginTrans();
cmd.Transaction = this._trans;
cmd.ExecuteNonQuery();
this.CommitTrans();
}
catch (Exception exp)
{
this.RollbackTrans();
throw exp;
}
finally
{

cmd.Dispose();
this.CloseCon();
this._conn.Dispose();
}

}
NqIceCoffee 2009-03-31
  • 打赏
  • 举报
回复
不知有没有人回答一下22楼的问题

以前写ASP的时候,用这个,没发现问题

但现在太多版本的防注入代码,对这个又有点怀疑了。不知这个方式能不能被绕过
xsm545 2009-03-31
  • 打赏
  • 举报
回复
这方法看起来有点麻烦
jiang_jiajia10 2009-03-31
  • 打赏
  • 举报
回复

可能还是会被注入的
bj890 2009-03-31
  • 打赏
  • 举报
回复
存储过程就一定 不被sql注入吗???????????????????????
NqIceCoffee 2009-03-31
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 zzxap 的回复:]
只要你是拼SQL的,无论的玩什么花样,都会被注入。
[/Quote]

请教一下,我拼的时候,如果参数是数字,则判断类型,如果是字符,替换一个'为两个(必要的时候判断字符长度)

这种情况如何注入?
sumaoyi 2009-03-31
  • 打赏
  • 举报
回复
老虎 老鼠 傻傻分不清楚
che2piaopiao 2009-03-31
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zzxap 的回复:]
还是会被注入
[/Quote]

不赞同
leehong0704 2009-03-31
  • 打赏
  • 举报
回复
用传参数好了。
zgke 2009-03-31
  • 打赏
  • 举报
回复
很好很难读...
xuan.ye 2009-03-31
  • 打赏
  • 举报
回复
好帅的代码
lovehongyun 2009-03-31
  • 打赏
  • 举报
回复
zhxhdean 2009-03-31
  • 打赏
  • 举报
回复
sp是什么?
heyihong0208 2009-03-31
  • 打赏
  • 举报
回复
这么写能起到一定的效果,但不能完全达到防注入的效果。
建议还是用存储过程吧
dzswej 2009-03-31
  • 打赏
  • 举报
回复
没有必要的,好比画蛇添足
用储存语句
angellan 2009-03-31
  • 打赏
  • 举报
回复
我用也是传sql语句,但不是你那种写法,我的写法如下:
SqlCommand cmd = new SqlCommand();
photoParms[0].Value = photo.Pho_Code;
photoParms[1].Value = photo.Pho_UserId;
photoParms[2].Value = photo.Pho_Time;
photoParms[3].Value = photo.Pho_ID;


     parms = new SqlParameter[] {
new SqlParameter(PARM_Pho_Code, SqlDbType.BigInt),
new SqlParameter(PARM_Pho_UserId, SqlDbType.VarChar,20),
new SqlParameter(PARM_Pho_Time, SqlDbType.DateTime),
new SqlParameter(PARM_Pho_ID, SqlDbType.BigInt)};
加载更多回复(22)

62,268

社区成员

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

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

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

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