You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
5.6 KiB
142 lines
5.6 KiB
9 months ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
using System.Web;
|
||
|
|
||
|
namespace FangYar.Common
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 防SQL注入检查器
|
||
|
/// </summary>
|
||
|
public static class SqlChecker
|
||
|
{
|
||
|
//安全Url,当出现Sql注入时,将导向到的安全页面,如果没赋值,则停留在当前页面
|
||
|
private static string safeUrl = String.Empty;
|
||
|
|
||
|
private const string StrRegex = @"=|!|'";
|
||
|
static SqlChecker()
|
||
|
{
|
||
|
//
|
||
|
// TODO: 在此处添加构造函数逻辑
|
||
|
//
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 当出现Sql注入时需要提示的错误信息(主要是运行一些客户端的脚本)
|
||
|
/// </summary>
|
||
|
public static string Msg(HttpRequest request)
|
||
|
{
|
||
|
string msg = "<script type='text/javascript'> "
|
||
|
+ " alert('请勿输入非法字符!'); ";
|
||
|
|
||
|
if (safeUrl == String.Empty)
|
||
|
msg += " window.location.href = '" + request.RawUrl + "'";
|
||
|
else
|
||
|
msg += " window.location.href = '" + safeUrl + "'";
|
||
|
|
||
|
msg += "</script>";
|
||
|
msg = "{\"code\":-10,\"msg\":\"检测到您提交信息中有非法字符,请勿输入非法字符\"}";
|
||
|
return msg;
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 检查URL参数中是否带有SQL注入的可能关键字。
|
||
|
/// </summary>
|
||
|
/// <returns>存在SQL注入关键字时返回 true,否则返回 false</returns>
|
||
|
public static bool CheckRequestQuery(string StrKeyWord,HttpRequest request)
|
||
|
{
|
||
|
bool result = false;
|
||
|
if (request.QueryString.Count != 0)
|
||
|
{
|
||
|
//若URL中参数存在,则逐个检验参数。
|
||
|
foreach (string queryName in request.QueryString)
|
||
|
{
|
||
|
//过虑一些特殊的请求状态值,主要是一些有关页面视图状态的参数
|
||
|
if (queryName == "__VIEWSTATE" || queryName == "__EVENTVALIDATION")
|
||
|
continue;
|
||
|
//开始检查请求参数值是否合法
|
||
|
if (CheckKeyWord(StrKeyWord,request.QueryString[queryName]))
|
||
|
{
|
||
|
//只要存在一个可能出现Sql注入的参数,则直接退出
|
||
|
result = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 检查提交表单中是否存在SQL注入的可能关键字
|
||
|
/// </summary>
|
||
|
/// <returns>存在SQL注入关键字时返回 true,否则返回 false</returns>
|
||
|
public static bool CheckRequestForm(string StrKeyWord, HttpRequest request)
|
||
|
{
|
||
|
bool result = false;
|
||
|
if (request.Form.Count > 0)
|
||
|
{
|
||
|
//若获取提交的表单项个数不为0,则逐个比较参数
|
||
|
foreach (string queryName in request.Form)
|
||
|
{
|
||
|
//过虑一些特殊的请求状态值,主要是一些有关页面视图状态的参数
|
||
|
if (queryName == "__VIEWSTATE" || queryName == "__EVENTVALIDATION")
|
||
|
continue;
|
||
|
//开始检查提交的表单参数值是否合法
|
||
|
if (CheckKeyWord(StrKeyWord,request.Form[queryName]))
|
||
|
{
|
||
|
//只要存在一个可能出现Sql注入的参数,则直接退出
|
||
|
result = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 检查_sword是否包涵SQL关键字
|
||
|
/// </summary>
|
||
|
/// <param name="_sWord">需要检查的字符串</param>
|
||
|
/// <returns>存在SQL注入关键字时返回 true,否则返回 false</returns>
|
||
|
public static bool CheckKeyWord(string StrKeyWord, string _sWord)
|
||
|
{
|
||
|
bool result = false;
|
||
|
//模式1 : 对应Sql注入的可能关键字
|
||
|
string[] patten1 = StrKeyWord.Split('|');
|
||
|
//模式2 : 对应Sql注入的可能特殊符号
|
||
|
string[] patten2 = StrRegex.Split('|');
|
||
|
//开始检查 模式1:Sql注入的可能关键字 的注入情况
|
||
|
foreach (string sqlKey in patten1)
|
||
|
{
|
||
|
if (_sWord.ToLower().IndexOf(" " + sqlKey.ToLower()) >= 0 || _sWord.ToLower().IndexOf(sqlKey.ToLower() + " ") >= 0)
|
||
|
{
|
||
|
//只要存在一个可能出现Sql注入的参数,则直接退出
|
||
|
result = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
//开始检查 模式1:Sql注入的可能特殊符号 的注入情况
|
||
|
foreach (string sqlKey in patten2)
|
||
|
{
|
||
|
if (_sWord.ToLower().IndexOf(sqlKey.ToLower()) >= 0)
|
||
|
{
|
||
|
//只要存在一个可能出现Sql注入的参数,则直接退出
|
||
|
result = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 执行Sql注入验证
|
||
|
/// </summary>
|
||
|
public static void Check(string StrKeyWord,HttpRequest request, HttpResponse response)
|
||
|
{
|
||
|
if (CheckRequestQuery(StrKeyWord,request) || CheckRequestForm(StrKeyWord,request))
|
||
|
{
|
||
|
response.ContentType = "text/json";
|
||
|
response.Write(Msg(request));
|
||
|
response.End();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|