您的位置:首页 > 数据库

最近sql注入数据库被更改泛滥,以下提供一个.net程序防止sql注入的方法

2009-07-09 13:02 851 查看
最近sql注入数据库被更改泛滥:状况如下:“ </title> </pre>> <script src=http://sb.5252.ws:88/107/1.js> </script> <”,
以下提供一个.net程序防止sql注入的方法(过滤敏感语句的仅供参考)方式如下:在Global.asax文件下面加入如下代码:
void Application_BeginRequest(Object sender, EventArgs e)
{
StartProcessRequest();

}

#region SQL注入式攻击代码分析
/// <summary>
/// 处理用户提交的请求
/// </summary>
private void StartProcessRequest()
{
try
{
string getkeys = "";
string sqlErrorPage = "../default.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
{
// 错误处理: 处理用户提交信息!
}
}
/// <summary>
/// 分析用户请求是否正常
/// </summary>
/// <param name="Str">传入用户提交数据 </param>
/// <returns>返回是否含有SQL注入式攻击代码 </returns>
private bool ProcessSqlStr(string Str)
{
bool ReturnValue = true;
try
{
if (Str.Trim() != "")
{
string SqlStr = "and ¦exec ¦insert ¦select ¦delete ¦update ¦count ¦* ¦chr ¦mid ¦master ¦truncate ¦char ¦declare";

string[] anySqlStr = SqlStr.Split(' ¦');
foreach (string ss in anySqlStr)
{
if (Str.ToLower().IndexOf(ss) >= 0)
{
ReturnValue = false;
break;
}
}
}
}
catch
{
ReturnValue = false;
}
return ReturnValue;
}
#endregion

今天看到这么多兄弟也中了这个,我补充一下,这个是我们实际中用的方法.发出来分享一下:
1,以下代码,用txt编辑器保存为lobal.asax
//从这里开始
<%@ Application Language="C#" %>

<script RunAt="server">

void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码

}

void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码

}

void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码

}

void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码

}

void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。

}

void Application_BeginRequest(Object sender, EventArgs e)
{
StartProcessRequest();

}

#region SQL注入式攻击代码分析
/// <summary>
/// 处理用户提交的请求
/// </summary>
private void StartProcessRequest()
{
try
{
string getkeys = "";
string sqlErrorPage = "/ErrorLog/Log.aspx";
if (System.Web.HttpContext.Current.Request.QueryString != null)
{

//具有针对性的注入:
string url = System.Web.HttpContext.Current.Request.Url.ToString();
string sInject = "http://%66%75%63%6B%75%75%2E%75%73/1.js";
string s2 = "fuckuu.us";
if (url.IndexOf(sInject) >= 0 || url.IndexOf(s2) >=0 )
{
System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage);
System.Web.HttpContext.Current.Response.End();
}

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]))
{
sqlErrorPage += "?preUrl=" + HttpUtility.UrlEncode(Request.Url.ToString());//转向的错误提示页面
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]))
{
sqlErrorPage += "?preUrl=" + HttpUtility.UrlEncode(System.Web.HttpContext.Current.Request.Form[getkeys]);
System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage);
System.Web.HttpContext.Current.Response.End();
}
}
}

}
catch
{
// 错误处理: 处理用户提交信息!
}
}
/// <summary>
/// 分析用户请求是否正常
/// </summary>
/// <param name="Str">传入用户提交数据 </param>
/// <returns>返回是否含有SQL注入式攻击代码 </returns>
private bool ProcessSqlStr(string Str)
{
bool ReturnValue = true;
try
{
if (Str.Trim() != "")
{
string SqlStr = "and |exec |insert |select |delete |update |count |* |chr |mid |master |truncate |char |declare ";

string[] anySqlStr = SqlStr.Split('|');
foreach (string ss in anySqlStr)
{
if (Str.ToLower().IndexOf(ss) >= 0)
{
ReturnValue = false;
break;
}
}
}
}
catch
{
ReturnValue = false;
}
return ReturnValue;
}
#endregion
</script>
//到这里结束
2,新建一文件夹ErrorLog,
复制以下代码用txt编辑器保存为Log.aspx,
//从这里开始
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Log.aspx.cs" Inherits="ErrorLog_Log" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>

//到这里结束
复制以下代码用txt编辑器保存为Log.aspx.cs
//从这里开始
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class ErrorLog_Log : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Log(HttpUtility.UrlDecode(Request["PreUrl"]));
}

//把注入操作记录在本地文件中,每天写一个文件
private void Log(string url)
{
DateTime dt = DateTime.Now;
string fileName = Server.MapPath("/ErrorLog") + "//Log" + dt.ToString("yyyy_MM_dd") + ".log";
string content = dt.ToString() + "," + Request.UserHostAddress + "," + url + "/r/n";
File.AppendAllText(fileName, content);
//Response.Redirect("/errorPage.html");
}
}
//到这里结束
把Log.aspx.cs与Log.aspx放在ErrorLog文件夹中,接着把ErrorLog文件夹与Global.asax文件放在根目录下,这样就可以记录下攻击的日志.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐