對于網(wǎng)站的安全性,是每個網(wǎng)站開發(fā)者和運營者最關(guān)心的問題。網(wǎng)站一旦出現(xiàn)漏洞,那勢必將造成很大的損失。為了提高網(wǎng)站的安全性,首先網(wǎng)站要防注入,最重要的是服務器的安全設(shè)施要做到位。
下面說下網(wǎng)站防注入的幾點要素。
一:丟棄SQL語句直接拼接,雖然這個寫起來很快很方便。
二:如果用SQL語句,那就使用參數(shù)化,添加Param
三:盡可能的使用存儲過程,安全性能高而且處理速度也快
四:屏蔽SQL,javascript等注入(很是主要的),對于每個文件寫是不太可能的。所以要找到對所有文件起作用的辦法。我在網(wǎng)上收集了以下3種方法
C#防SQL注入方法一
在Web.config文件中, < appSettings>下面增加一個標簽:如下
- < appSettings>
- < add key="safeParameters" value="OrderID-int32,CustomerEmail-email,ShippingZipcode-USzip" />
- < /appSettings>
其中key是 < saveParameters>后面的值為"OrderId-int32"等,其中"-"前面表示參數(shù)的名稱比如:OrderId,后面的int32表示數(shù)據(jù)類型。
C#防SQL注入方法二
在Global.asax中增加下面一段:
- protected void Application_BeginRequest(Object sender, EventArgs e){
- String[] safeParameters = System.Configuration.ConfigurationSettings.AppSettings["safeParameters"].ToString().Split(',');
- for(int i= 0 ;i < safeParameters.Length; i++){
- String parameterName = safeParameters[i].Split('-')[0];
- String parameterType = safeParameters[i].Split('-')[1];
- isValidParameter(parameterName, parameterType);
- }
- }
- public void isValidParameter(string parameterName, string parameterType){
- string parameterValue = Request.QueryString[parameterName];
- if(parameterValue == null) return;
- if(parameterType.Equals("int32")){
- if(!parameterCheck.isInt(parameterValue)) Response.Redirect("parameterError.aspx");
- }
- else if (parameterType.Equals("USzip")){
- if(!parameterCheck.isUSZip(parameterValue)) Response.Redirect("parameterError.aspx");
- }
- else if (parameterType.Equals("email")){
- if(!parameterCheck.isEmail(parameterValue)) Response.Redirect("parameterError.aspx");
- }
- }
C#防SQL注入方法三
使用字符串過濾類
- using System;
- namespace web.comm
- {
- /**//// < summary>
- /// ProcessRequest 的摘要說明。
- /// < /summary>
- public class ProcessRequest
- {
- public ProcessRequest()
- {
- //
- // TODO: 在此處添加構(gòu)造函數(shù)邏輯
- //
- }
- SQL注入式攻擊代碼分析#region SQL注入式攻擊代碼分析
- /**//// < summary>
- /// 處理用戶提交的請求
- /// < /summary>
- public static void StartProcessRequest()
- {
- // System.Web.HttpContext.Current.Response.Write("< script>alert('dddd');< /script>");
- try
- {
- string getkeys = "";
- //string sqlErrorPage = System.Configuration.ConfigurationSettings.AppSettings["CustomErrorPage"].ToString();
- 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],0))
- {
- //System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");
- System.Web.HttpContext.Current.Response.Write("< script>alert('請勿非法提交!');history.back();< /script>");
- 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],1))
- {
- //System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");
- System.Web.HttpContext.Current.Response.Write("< script>alert('請勿非法提交!');history.back();< /script>");
- System.Web.HttpContext.Current.Response.End();
- }
- }
- }
- }
- catch
- {
- // 錯誤處理: 處理用戶提交信息!
- }
- }
- /**//// < summary>
- /// 分析用戶請求是否正常
- /// < /summary>
- /// < param name="Str">傳入用戶提交數(shù)據(jù)< /param>
- /// < returns>返回是否含有SQL注入式攻擊代碼< /returns>
- private static bool ProcessSqlStr(string Str,int type)
- {
- string SqlStr;
- if(type == 1)
- SqlStr = "exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ";
- else
- SqlStr = "'|and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare";
- bool ReturnValue = true;
- try
- {
- if (Str != "")
- {
- string[] anySqlStr = SqlStr.Split('|');
- foreach (string ss in anySqlStr)
- {
- if (Str.IndexOf(ss)>=0)
- {
- ReturnValue = false;
- }
- }
- }
- }
- catch
- {
- ReturnValue = false;
- }
- return ReturnValue;
- }
- #endregion
- }
- }
【編輯推薦】