大家都是如何防止SQL注入式攻击的?
做网站的同志都知道,网站的安全是非常重要的。大家都是如何来防止网站被黑的?
为了防止登录口令被SQL注入攻击,我是将输入的textbox中的文本
如果有“'”的全部替换成“¥”来处理的,不知道大家是怎么做的?
大家交流下。
[解决办法]
坐等高手.
[解决办法]
1.不要或尽量少拼接sql
2.注意对用户输入的数据进行检查
3.sql参数使用参数化形式(parameter)
[解决办法]
危险字符过滤的类(最新完善版) * 山哥的后台类 *
http://blog.csdn.net/johnsuna/archive/2004/12/05/205295.aspx
我看了一下,但是不知道好用吗,
我想做的是在前台作判断,至今还没有找到好的方法。
[解决办法]
public class ProcessRequest { public static void StartProcessRequest() { string sqlErrorPage = "ErrorPage.htm"; try { string getkeys = ""; if (System.Web.HttpContext.Current.Request.QueryString != null) { for (int i = 0; i < System.Web.HttpContext.Current.Request.QueryString.Count; i++) { getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i]; if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys])) { System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage + ""); System.Web.HttpContext.Current.Response.End(); } } } if (System.Web.HttpContext.Current.Request.Form != null) { for (int i = 0; i < System.Web.HttpContext.Current.Request.Form.Count; i++) { getkeys = System.Web.HttpContext.Current.Request.Form.Keys[i]; if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys])) { System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage + ""); System.Web.HttpContext.Current.Response.End(); } } } } catch { System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage + ""); System.Web.HttpContext.Current.Response.End(); } } public static bool ProcessSqlStr(string Str) { bool ReturnValue = true; try { if (Str != "") { string SqlStr = ";|and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare"; string[] anySqlStr = SqlStr.Split('|'); foreach (string s in anySqlStr) { if (Str.IndexOf(s) > -1) { ReturnValue = false; } } } } catch { ReturnValue = false; } return ReturnValue; } }
[解决办法]
1.使用双引号
2.避免动态的SQL语句
3.验证所有的输入
在global.asax里
void Application_BeginRequest(Object sender, EventArgs e)
{
StartProcessRequest();
}
#region
private void StartProcessRequest()
{
try
{
string getkeys = "";
string sqlErrorPage = "index.aspx";
if (System.Web.HttpContext.Current.Request.QueryString != null)
{
for (int i = 0; i < System.Web.HttpContext.Current.Request.QueryString.Count; i++)
{
getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i];
if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys]))
{
System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage);
System.Web.HttpContext.Current.Response.End();
}
}
}
if (System.Web.HttpContext.Current.Request.Form != null)
{
for (int i = 0; i < System.Web.HttpContext.Current.Request.Form.Count; i++)
{
getkeys = System.Web.HttpContext.Current.Request.Form.Keys[i];
if (getkeys == "__VIEWSTATE") continue;
if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys]))
{
System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage);
System.Web.HttpContext.Current.Response.End();
}
}
}
}
catch
{
// 错误处理: 处理用户提交信息!
}
}
private bool ProcessSqlStr(string Str)
{
bool ReturnValue = true;
try
{
if (Str.Trim() != "")
{
string SqlStr = "exec¦insert¦select¦delete¦master¦update¦truncate¦declare";
string[] anySqlStr = SqlStr.Split('¦');
foreach (string ss in anySqlStr)
{
if(!Str.ToLower().Contains("updatepanel"))
{
if (Str.ToLower().IndexOf(ss) >= 0)
{
ReturnValue = false;
break;
}
}
}
}
}
catch
{
ReturnValue = false;
}
return ReturnValue;
}
#endregion
http://www.cnblogs.com/xinyuxin912/archive/2006/09/01/492655.aspx
[解决办法]
过滤 参数化
[解决办法]
CREATE PROCEDURE [dbo].[sp_us_CheckUserInfo] @LoginName varchar(30), @Password varchar(60)AS Select * from us_User LoginName=@LoginName and Password=@PasswordGO
[解决办法]
如果是过滤的话,会把一些有用的字符也过滤掉,很麻烦
[解决办法]
不要拼sql.
使用存储过程或者使用参数化的sql语句.
对输入的数据进行有效性验证.
控制输入
[解决办法]
楼上的兄弟姐妹们,说了这么多,其实就是就通用的sql防注入方法
就是
参数化数据变量
[解决办法]
我是这样写的:
private const string S_tib = "select count(Tib_Code) from web_Tibetan where Tib_Name='{0}'";
再这样调用:string.Format(D_tib, uLogin.Name);
[解决办法]
支持存储过程!
[解决办法]