1: /// summary>
2: ///SqlInject 的摘要说明
3: /// /summary>
4: public class SqlInject : System.Web.UI.Page
5: {
6: //检测到注入后的处理方式: 0:仅警告;1:警告+记录;2:警告+自定义错误页面;3:警告+记录+自定义错误页面
7: private const int _type = 0;
8: private const string errRedirectPage = "/err.aspx";
9:
10: //如果记录注入信息,那么请设置:errMDBpath:数据库路径
11: private const string errMDBpath = "/SqlInject.mdb";
12:
13:
14: //过滤特征字符
15: //过滤特征字符
16: private static string StrKeyWord = ConfigurationManager.AppSettings["SqlKeyWord"]; //@"select|insert|delete|from|count(|drop table|update|truncate|asc(|mid(|char(|xp_cmdshell|exec|master|net local group administrators|net user|or|and";
17: private static string StrRegex = ConfigurationManager.AppSettings["SqlRegex"]; //@";|/|(|)|[|]|{|}|%|@|*|'|!"; // 原始过滤条件:【-|;|,|/|(|)|[|]|{|}|%|@|*|'|!】
18:
19: private HttpRequest request;
20: public SqlInject(System.Web.HttpRequest _request)
21: {
22: this.request = _request;
23: }
24: ///summary>
25: ///检测SQL注入及记录、显示出错信息
26: ////summary>
27: public void CheckSqlInject()
28: {
29: bool isInject = false;
30: if (CheckRequestQuery() || CheckRequestForm())
31: {
32: isInject = true;
33: }
34: else
35: {
36: return;
37: }
38:
39: switch (_type)
40: {
41: case 0:
42: ShowErr();
43: break;
44: case 1:
45: ShowErr();
46: SaveToMdb();
47: break;
48: case 2:
49: ShowErr();
50: string temp;
51: System.Web.HttpContext.Current.Response.Write("script>setTimeout(\"" + "location.href='" + errRedirectPage + "'" + "\",5000)/script>");
52: break;
53: case 3:
54: ShowErr();
55: SaveToMdb();
56: System.Web.HttpContext.Current.Response.Write("script>setTimeout(\"" + "location.href='" + errRedirectPage + "'" + "\",5000)/script>");
57: break;
58: default:
59: break;
60: }
61: System.Web.HttpContext.Current.Response.End();
62:
63: }
64: private void SaveToMdb()
65: {
66: OleDbConnection conn = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + Server.MapPath(errMDBpath));
67: conn.Open();
68: OleDbCommand cmd = conn.CreateCommand();
69:
70: cmd.CommandText = "insert into [Record] (sIP,sDate,sPath) values ('" +
71: request.ServerVariables["REMOTE_ADDR"].ToString() + "','" +
72: DateTime.Now + "','" + request.ServerVariables["URL"].ToLower() + RelaceSingleQuotes(request.QueryString.ToString()) + "')";
73: int code = cmd.ExecuteNonQuery();
74: if (code == 1)
75: System.Web.HttpContext.Current.Response.Write("br>****以上信息已记录至日志数据库****");
76: else
77: System.Web.HttpContext.Current.Response.Write("br>日志数据库出错");
78: conn.Close();
79:
80: }
81: private string RelaceSingleQuotes(string _url)
82: {
83: string URL = _url.Replace("'", "单引号");
84: return URL;
85: }
86: private void ShowErr()
87: {
88: //string msg = @"font color=red>请不要尝试未授权之入侵检测!/font>" + @"br>br>";
89: //msg += @"操作IP:" + request.ServerVariables["REMOTE_ADDR"] + @"br>";
90: //msg += @"操作时间:" + DateTime.Now + @"br>";
91: //msg += @"页面:" + request.ServerVariables["URL"].ToLower() + request.QueryString.ToString() + @"br>";
92: //msg += @"a href='#' onclick='javascript:window.close()'>关闭/a>";
93: //System.Web.HttpContext.Current.Response.Clear();
94: //System.Web.HttpContext.Current.Response.Write(msg);
95: System.Web.HttpContext.Current.Response.Write("script>alert('请不要尝试未授权之入侵检测!');javascript:history.go(-1);/script>");
96: }
97: ///summary>
98: /// 特征字符
99: ////summary>
100: public static string KeyWord
101: {
102: get
103: {
104: return StrKeyWord;
105: }
106: }
107: ///summary>
108: /// 特征符号
109: ////summary>
110: public static string RegexString
111: {
112: get
113: {
114: return StrRegex;
115: }
116: }
117:
118: ///summary>
119: ///检查字符串中是否包含Sql注入关键字
120: /// param name="_key">被检查的字符串/param>
121: /// returns>如果包含注入true;否则返回false/returns>
122: ////summary>
123: private static bool CheckKeyWord(string _key)
124: {
125: string[] pattenString = StrKeyWord.Split('|');
126: string[] pattenRegex = StrRegex.Split('|');
127: foreach (string sqlParam in pattenString)
128: {
129: if (_key.Contains(sqlParam + " ") || _key.Contains(" " + sqlParam))
130: {
131: return true;
132: }
133: }
134: foreach (string sqlParam in pattenRegex)
135: {
136: if (_key.Contains(sqlParam))
137: {
138: return true;
139: }
140: }
141: return false;
142:
143: }
144: ///summary>
145: ///检查URL中是否包含Sql注入
146: /// param name="_request">当前HttpRequest对象/param>
147: /// returns>如果包含注入true;否则返回false/returns>
148: ////summary>
149: public bool CheckRequestQuery()
150: {
151: if (request.QueryString.Count > 0)
152: {
153: foreach (string sqlParam in this.request.QueryString)
154: {
155: if (sqlParam == "__VIEWSTATE") continue;
156: if (sqlParam == "__EVENTVALIDATION") continue;
157: if (CheckKeyWord(request.QueryString[sqlParam].ToLower()))
158: {
159: return true;
160: }
161: }
162: }
163: return false;
164: }
165: ///summary>
166: ///检查提交的表单中是否包含Sql注入
167: /// param name="_request">当前HttpRequest对象/param>
168: /// returns>如果包含注入true;否则返回false/returns>
169: ////summary>
170: public bool CheckRequestForm()
171: {
172: if (request.Form.Count > 0)
173: {
174: foreach (string sqlParam in this.request.Form)
175: {
176: if (sqlParam == "__VIEWSTATE") continue;
177: if (sqlParam == "__EVENTVALIDATION") continue;
178: if (CheckKeyWord(request.Form[sqlParam]))
179: {
180: return true;
181: }
182: }
183: }
184: return false;
185: }
186: }