这是我的代码中的sql漏洞吗?因为我已经参数化了SQL,所以没有sql注入?任何人给出一些建议都会很感谢!如果有,如何修复?
ASP.NET代码:
public DataTable CompanySearchUser(int pageSize, int currentPage, string whereCondition)
{
DbParameter[] parms = {
DbHelper.MakeInParam("@PageSize",(DbType)SqlDbType.Int,4,pageSize),
DbHelper.MakeInParam("@PageNumber",(DbType)SqlDbType.Int,4,currentPage),
DbHelper.MakeInParam("@where",(DbType)SqlDbType.NVarChar,500,whereCondition)
};
DataTable userlist = DbHelper.ExecuteDataset(CommandType.StoredProcedure, "spCompanySearchUser", parms).Tables[0];
return userlist;
}SQL代码:
ALTER PROC [dbo].[spCompanySearchUser]
@PageSize INT
@PageNumber INT,
@where nvarchar(550)--like 'and a=1 '
AS
DECLARE @RowStart INT
DECLARE @RowEnd INT
DECLARE @SQL NVARCHAR(4000)
IF @PageNumber > 0
BEGIN
SET @PageNumber = @PageNumber - 1
SET @RowStart = @PageSize * @PageNumber + 1;
SET @RowEnd = @RowStart + @PageSize - 1;
SET @SQL='
WITH AllUsers
AS (SELECT
UB.UserBaicInfoID,
UB.UserName,
UB.HighestEducation,
UB.Age,
UB.Sex,
UB.WorkExperience,
UB.PositionDesired,
UB.UpdateTime,
Row_number() OVER (ORDER BY UB.UpdateTime DESC) AS RowNumber
From UserBasicInfo UB
WHERE ResumeState=1 '+@where+')
SELECT * FROM AllUsers WHERE RowNumber >=' + Str(@RowStart) + ' AND RowNumber <= ' + Str(@RowEnd) + ''
EXEC sp_executesql @SQL
END 这是我的代码中的sql漏洞吗?因为我已经参数化了SQL,所以没有sql注入?任何人给点建议都会很感谢!如果有,怎么修复?
发布于 2013-07-07 09:35:47
现在不能测试它,但我会说是的。
传入@where并进行连接,@where执行的是实际的sql代码,而不是真正的"where“子句
然后对最终的sql代码执行exec
这是可以被利用的
不确定你的whereCondition是什么样子,但试试这个
public DataTable CompanySearchUser(int pageSize, int currentPage, string whereCondition)
{
/**** add this to test ****/
whereCondition += " or 1=1";
/**** add this to test ****/
DbParameter[] parms = {
DbHelper.MakeInParam("@PageSize",(DbType)SqlDbType.Int,4,pageSize),
DbHelper.MakeInParam("@PageNumber",(DbType)SqlDbType.Int,4,currentPage),
DbHelper.MakeInParam("@where",(DbType)SqlDbType.NVarChar,500,whereCondition)
};
DataTable userlist = DbHelper.ExecuteDataset(CommandType.StoredProcedure, "spCompanySearchUser", parms).Tables[0];
return userlist;
}发布于 2013-07-07 09:38:42
它仍然是不安全和没有性能的。
您应该动态地执行SQL并在其中添加参数,而不是依赖于sp_executeSQL。
使用SQL占位符项目SQL值构建字符串,然后添加这些参数,通常会更好、更安全、更易于管理,而且不会浪费cmd.Parameters.AddWithValue("@placeholder", value)的时间,因为它无法优化语句。
实际上,与在存储过程中相比,在代码中动态执行可能会获得更好的性能,因为SQL将缓存这些语句中的每一个语句,并在出现相同的语句时重新执行它们。
https://stackoverflow.com/questions/17508508
复制相似问题