public static void Check(HttpApplication application, SQLInjectionParam param) { HttpContext context = application.Context; HttpRequest request = context.Request; string file = ""; #region 记录日志文件 if (param.SQLInjectionType % 2 == 1) //记录到日志 { try { file = context.Server.MapPath(param.SQLInjectionLogFile); FileInfo fi = new FileInfo(file); if (!fi.Exists) { Directory.CreateDirectory(fi.DirectoryName); } } catch { } } #endregion // -----------------------防 Post 注入----------------------- if (request.Form != null) { for (int k = 0; k < request.Form.Count; k++) { string getsqlkey = request.Form.Keys[k]; string formValue = request.Form[getsqlkey].ToLower(); Find(request, getsqlkey, formValue, (int)CheckItem.form, param.SQLInjectionLevel, param.SQLInjectionType, file); } } // -----------------------防 GET 注入----------------------- if (request.QueryString != null) { for (int k = 0; k < request.QueryString.Count; k++) { string getsqlkey = request.QueryString.Keys[k]; string queryValue = request.QueryString[getsqlkey].ToLower(); Find(request, getsqlkey, queryValue, (int)CheckItem.query, param.SQLInjectionLevel, param.SQLInjectionType, file); } } // -----------------------防 Cookies 注入----------------------- if (request.Cookies != null) { for (int k = 0; k < request.Cookies.Count; k++) { string getsqlkey = request.Cookies.Keys[k]; string cookieValue = request.Cookies[getsqlkey].Value.ToLower(); Find(request, getsqlkey, cookieValue, (int)CheckItem.cookie, param.SQLInjectionLevel, param.SQLInjectionType, file); } } }
void application_BeginRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; try { SQLInjectionParam param = new SQLInjectionParam(); if (ConfigurationManager.AppSettings.Count > 0) { string enableStr = ConfigurationManager.AppSettings["SQLInjectionEnable"]; //是否启用 if (enableStr!= null) { if (enableStr.ToLower() == "false" || enableStr.ToLower() == "0") { param.SQLInjectionEnable = false; } } param.SQLInjectionLevel = ConvertToInt(ConfigurationManager.AppSettings["SQLInjectionLevel"]); //过滤等级 param.SQLInjection = ConfigurationManager.AppSettings["SQLInjection"]; //追加的过滤关键词 param.SQLInjectionType = ConvertToInt(ConfigurationManager.AppSettings["SQLInjectionType"]); //过滤方法 string logFileName = ConfigurationManager.AppSettings["SQLInjectionLogFile"]; //日志记录文件 if (!string.IsNullOrEmpty(logFileName)) { param.SQLInjectionLogFile = logFileName; } } if (param.SQLInjectionEnable) { CheckSQLReg.Check(application, param); CheckSQL.Check(application,param); } } catch(Exception ex) { } }
public static void Check(HttpApplication application, SQLInjectionParam param ) { HttpContext context = application.Context; HttpRequest request = context.Request; string sqlkeywords = "select↓insert↓update↓delete↓drop↓create↓truncate↓join↓declare↓exists↓union↓order↓exec↓execute↓alter↓mid↓xp_cmdshell↓char↓sp_oacreate↓wscript.shell↓xp_regwrite↓waitfor↓delay"; string file = ""; if (!string.IsNullOrEmpty(param.SQLInjection)) { if (param.SQLInjection.StartsWith("↓")) { sqlkeywords += param.SQLInjection; } else { sqlkeywords += "↓" + param.SQLInjection; } } #region 记录日志文件 if (param.SQLInjectionType % 2 == 1) //记录到日志 { try { file = context.Server.MapPath(param.SQLInjectionLogFile); FileInfo fi = new FileInfo(file); if (!fi.Exists) { Directory.CreateDirectory(fi.DirectoryName); } } catch { } } #endregion //Stopwatch watch = new Stopwatch(); //watch.Start(); string[] sqlkeyword = sqlkeywords.Split('↓'); foreach (string keyword in sqlkeyword) { // -----------------------防 Post 注入----------------------- if (request.Form!= null) { for (int k = 0; k <request.Form.Count; k++) { string getsqlkey = request.Form.Keys[k]; string formValue= request.Form[getsqlkey].ToLower(); string getip = Find(request, keyword, getsqlkey, formValue, (int)CheckItem.form, param.SQLInjectionLevel, param.SQLInjectionType, file); } } // -----------------------防 GET 注入----------------------- if (request.QueryString != null) { for (int k = 0; k <request.QueryString.Count; k++) { string getsqlkey = request.QueryString.Keys[k]; string queryValue=request.QueryString[getsqlkey].ToLower(); string getip = Find(request, keyword, getsqlkey, queryValue, (int)CheckItem.query, param.SQLInjectionLevel, param.SQLInjectionType, file); } } // -----------------------防 Cookies 注入----------------------- if (request.Cookies != null) { for (int k = 0; k < request.Cookies.Count; k++) { string getsqlkey = request.Cookies.Keys[k]; string cookieValue=request.Cookies[getsqlkey].Value.ToLower(); string getip = Find(request, keyword, getsqlkey, cookieValue, (int)CheckItem.cookie, param.SQLInjectionLevel, param.SQLInjectionType, file); } } } //watch.Stop(); //TimeSpan ts = watch.Elapsed; //string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", // ts.Hours, ts.Minutes, ts.Seconds, // ts.Milliseconds / 10); //System.Web.HttpContext.Current.Response.Write(elapsedTime); //System.Web.HttpContext.Current.Response.End(); }