大家都是如何防止SQL注入式攻击的?
做网站的同志都知道,网站的安全是非常重要的。大家都是如何来防止网站被黑的?
为了防止登录口令被SQL注入攻击,我是将输入的textbox中的文本
如果有“'”的全部替换成“¥”来处理的,不知道大家是怎么做的?
大家交流下。
[解决办法]
坐等高手.
[解决办法]
1.不要或尽量少拼接sql
2.注意对用户输入的数据进行检查
3.sql参数使用参数化形式(parameter)
[解决办法]
危险字符过滤的类(最新完善版) * 山哥的后台类 *
http://blog.csdn.net/johnsuna/archive/2004/12/05/205295.aspx
我看了一下,但是不知道好用吗,
我想做的是在前台作判断,至今还没有找到好的方法。
[解决办法]
[解决办法]
[解决办法]
[解决办法]
http://blog.csdn.net/dengwei007/articles/30712.aspx 看看这个sql注入天书
[解决办法]
做到两点,可保你万无一失
1、对string参数进行Replace("'","''") or Replace("'","")
示例sql语句:string sql = "select * from [users] where [name]='"+ name.Replace("'","") +"'";
注意sql语句中[name]=''两个单引号不可缺
2、对int参数进行int.Parse()
示例sql语句:string sql = "select * from [users] where [id]="+ int.Parse(id).ToString();
出错说名参数不对,跳转到默认错误页面
当然也可以通过含蓄的方式验证,比如 int.TryParse 或者 try{}catch{}
[解决办法]
另外,如果考虑插入<script>标签的话,可以在显示的时候进行 Server.HtmlEncode(row["title"].ToString())
[解决办法]
[解决办法]
学习
[解决办法]
- C# code
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
[解决办法]
过滤 参数化
[解决办法]
[解决办法]
推荐使用存储过程
[解决办法]
[解决办法]
可以在前台把几个重要的符号设上禁用,他在文本里就输入不上',之类的符号。
------解决方案--------------------
SqlCommand cmda = new SqlCommand(sqlu, scn);
cmda.Parameters.Add(new SqlParameter("@Pass", SqlDbType.NVarChar,20));
cmda.Parameters["@pass"].Value = TextBox3.Text.Trim().ToString();
cmda.ExecuteNonQuery();
[解决办法]
不要或尽量少拼接sql
sql语句都写成存储过程
查询变量在传入过程中必须通过参数传递
[解决办法]
使用存储过程的例子.
存储过程如下:
- SQL code
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);
[解决办法]
支持存储过程!
[解决办法]
[解决办法]
[解决办法]
不要想的那么难, 看看microsoft的最佳实践。
照着做就好。
[解决办法]
当然用存储过程了,又快又安全哦~
[解决办法]
我用存储过程
[解决办法]
1指定数据类型
2 过滤单引号
足够了
[解决办法]
最好的方法,使用SqlParameter