简单的ASP.net防SQL注入
来源:互联网 发布:win7查找网络打印机 编辑:程序博客网 时间:2024/06/10 03:32
asp.net有个Global.asax文件,有一个Application_BeginRequest方法(应用启动获取)
就是当获取到参数时触发的事件;这里就是网站页面每次提交时都要经过的事件;在这里做防注入就一下子卡住入口了
代码如下:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
//遍历Post参数,隐藏域除外
foreach (string i in this.Request.Form)
{
//输出
}
//遍历Get参数。
foreach (string i in this.Request.QueryString)
{
//输出
}
}
这样就可以遍历出页面提交上来的所有的参数;里面只写了一个“//输出”,没有写代码出来,大家可以写repsonse.write输出来看看,尝试一下;我们只需对这些参数进行过滤,如果存在不安全字符,直接跳转就走行了;
也可以加过滤
public static string DelSQLStr(string str)
{
if(str == null || str == "")
return "";
str = str.Replace(";","");
str = str.Replace("'","");
str= str.Replace("&","");
str= str.Replace("%20","");
str= str.Replace("--","");
str= str.Replace("==","");
str= str.Replace("<","");
str= str.Replace(">","");
str= str.Replace("%","");
return str;
}
// <summary>
//当有数据时交时,触发事件
// </summary>
// <param name="sender"> </param>
// <param name="e"> </param>
protected void Application_BeginRequest(Object sender, EventArgs e)
{
//遍历Post参数,隐藏域除外
foreach (string i in this.Request.Form)
{
if (i == "__VIEWSTATE") continue;
this.goErr(this.Request.Form[i].ToString());
}
//遍历Get参数。
foreach (string i in this.Request.QueryString)
{
this.goErr(this.Request.QueryString[i].ToString());
}
}
/// <summary>
///SQL注入过滤
/// </summary>
/// <param name="InText">要过滤的字符串 </param>
/// <returns>如果参数存在不安全字符,则返回true </returns>
public bool SqlFilter(string InText)
{
string word = "and|exec|insert|select|delete|update|chr|mid|master|or|truncate|char|declare|join|cmd";
if (InText == null)
return false;
foreach (string i in word.Split('|'))
{
if ((InText.ToLower().IndexOf(i + " ") > -1) || (InText.ToLower().IndexOf(" " + i) > -1))
{
return true;
}
}
return false;
}
/// <summary>
/// 校验参数是否存在SQL字符
/// </summary>
/// <param name="tm"> </param>
private void goErr(string tm)
{
if (SqlFilter(tm))
{
Response.Write(" <script>window.alert('您输入的数据存在有误参数!');" + " </" + "script>");
}
}
放在全局Global.asax里
- 简单的ASP.net防SQL注入
- 简单的ASP.net防SQL注入
- 简单的ASP.net防SQL注入
- 简单的ASP.net防SQL注入
- ASP.net防SQL注入(简单)
- ASP.net防SQL注入(简单)
- ASP.net防SQL注入(简单)
- ASP.net防SQL注入(简单)
- ASP.net简单的防SQL注入代码
- ASP.net简单的防SQL注入代码
- ASP.net简单的防SQL注入代码
- asp.net防SQL注入的安全措施
- asp.net的sql防注入
- asp.net防SQL注入的安全措施
- ASP.net防SQL注入
- ASP.net防SQL注入
- asp.net 防SQL注入
- (转)asp.net 防sql注入 .net防sql
- mib 库解析
- 新手入门:写Java程序的三十个基本规则
- java 事件原理(给初学者开窍)转自冯忠孝 http://hi.baidu.com/32647908
- javascript:void(0)
- c# Web Services学习笔记(四、SOAP协议绑定框架)
- 简单的ASP.net防SQL注入
- select 表单变化更改图片
- http error: 505
- C++ BUILDER中*256与 << 8 的效率,结论:一样
- 关于SkinMagic的破解(转)
- 数据库中存储过程和视图的区别
- mysql inner join性能
- Fc9中安装ATI驱动,实现XGL,Compiz/Beryl效果Howto for fglrx (Ati driver) , XGL and Compiz / Beryl
- GOOGLE女工程师的一篇文章-如何准备软件工程师的面试