解决方案是:
1、首先在UI录入时,要控制数据的类型和长度、防止SQL注入式攻击,系统提供检测注入式攻击的函数,一旦检测出注入式攻击,该数据即不能提交;
2、业务逻辑层控制,通过在方法内部将SQL关键字用一定的方法屏蔽掉,然后检查数据长度,保证提交SQL时,不会有SQL数据库注入式攻击代码;但是这样处理后,要求UI输出时将屏蔽的字符还原。因此系统提供屏蔽字符 的函数和还原字符的函数。
3、在数据访问层,绝大多数采用存储过程访问数据,调用时以存储过程参数的方式访问,也会很好的防止注入式攻击。

/**////
/// 判断字符串中是否有SQL攻击代码
///
/// 传入用户提交数据
/// true-安全;false-有注入攻击现有;
public bool ProcessSqlStr(string inputString)
{
string SqlStr = "and|or|exec|execute|insert|select|delete|update|alter|create|drop|count|\*|chr|char|asc|mid|substring|master|truncate|declare|xp_cmdshell|restore|backup|net +user|net +localgroup +administrators";
try
{
if ((inputString != null) && (inputString != String.Empty))
{
string str_Regex = @"\b(" + SqlStr + @")\b"; Regex Regex = new Regex(str_Regex, RegexOptions.IgnoreCase);
//string s = Regex.Match(inputString).Value;
if (true == Regex.IsMatch(inputString))
return false;
}
}
catch
{
return false;
}
return true;
}
  /**////
/// 处理用户提交的请求,校验sql注入式攻击,在页面装置时候运行
/// System.Configuration.ConfigurationSettings.AppSettings["ErrorPage"].ToString(); 为用户自定义错误页面提示地址,
/// 在Web.Config文件时里面添加一个 ErrorPage 即可
///
///
///
public void ProcessRequest()
{
try
{
string getkeys = "";
string sqlErrorPage = System.Configuration.ConfigurationSettings.AppSettings["ErrorPage"].ToString();
if (System.Web.HttpContext.Current.Request.QueryString != null)
{ for (int i = ; 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 + "?errmsg=" + getkeys + "有SQL攻击嫌疑!");
System.Web.HttpContext.Current.Response.End();
}
}
}
if (System.Web.HttpContext.Current.Request.Form != null)
{
for (int i = ; 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]))
{
System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage + "?errmsg=" + getkeys + "有SQL攻击嫌疑!");
System.Web.HttpContext.Current.Response.End();
}
}
}
}
catch
{
// 错误处理: 处理用户提交信息!
}
}
#endregion
  //转换sql代码(也防止sql注入式攻击,可以用于业务逻辑层,但要求UI层输入数据时候进行解码)#region // 转换sql代码(也防止sql注入式攻击,可以用于业务逻辑层,但要求UI层输入数据时候进行解码)
/**////
/// 提取字符固定长度
///
///
///
///
public string CheckStringLength(string inputString, Int32 maxLength)
{
if ((inputString != null) && (inputString != String.Empty))
{
inputString = inputString.Trim(); if (inputString.Length > maxLength)
inputString = inputString.Substring(, maxLength);
}
return inputString;
}
 /**////
/// 将输入字符串中的sql敏感字,替换成"[敏感字]",要求输出时,替换回来
///
///
///
public string MyEncodeInputString(string inputString)
{
//要替换的敏感字
string SqlStr = @"and|or|exec|execute|insert|select|delete|update|alter|create|drop|count|\*|chr|char|asc|mid|substring|master|truncate|declare|xp_cmdshell|restore|backup|net +user|net +localgroup +administrators";
try
{
if ((inputString != null) && (inputString != String.Empty))
{
string str_Regex = @"\b(" + SqlStr + @")\b"; Regex Regex = new Regex(str_Regex, RegexOptions.IgnoreCase);
//string s = Regex.Match(inputString).Value;
MatchCollection matches = Regex.Matches(inputString);
for (int i = ; i < matches.Count; i++)
inputString = inputString.Replace(matches[i].Value, "[" + matches[i].Value + "]"); }
}
catch
{
return "";
}
return inputString; }
 /**////
/// 将已经替换成的"[敏感字]",转换回来为"敏感字"
///
///
///
public string MyDecodeOutputString(string outputstring)
{
//要替换的敏感字
string SqlStr = @"and|or|exec|execute|insert|select|delete|update|alter|create|drop|count|\*|chr|char|asc|mid|substring|master|truncate|declare|xp_cmdshell|restore|backup|net +user|net +localgroup +administrators";
try
{
if ((outputstring != null) && (outputstring != String.Empty))
{
string str_Regex = @"\[\b(" + SqlStr + @")\b\]";
Regex Regex = new Regex(str_Regex, RegexOptions.IgnoreCase);
MatchCollection matches = Regex.Matches(outputstring);
for (int i = ; i < matches.Count; i++)
outputstring = outputstring.Replace(matches[i].Value, matches[i].Value.Substring(, matches[i].Value.Length - )); }
}
catch
{
return "";
}
return outputstring;
}
#endregion

防止sql注入式攻击 SQL注入学习——三层架构的更多相关文章

  1. SQL注入详细介绍及如何防范SQL注入式攻击

    一. SQL注入攻击的简单示例. statement := "SELECT * FROM Users WHERE Value= " + a_variable + " 上面 ...

  2. 什么是SQL注入式攻击

    什么是SQL注入式攻击? 所谓SQL注入式攻击,就是攻击者把SQL命令插入到Web表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的SQL命令.在某些表单中,用户输入的内容直接用来构造(或者影响 ...

  3. 什么是SQL注入式攻击和如何防范?

    什么是SQL注入式攻击? 所谓SQL注入式攻击,就是攻击者把SQL命令插入到Web表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的SQL命令.在某些表单中,用户输入的内容直接用来构造(或者影响 ...

  4. 浅谈C#.NET防止SQL注入式攻击

    1#region 防止sql注入式攻击(可用于UI层控制)  2  3///   4/// 判断字符串中是否有SQL攻击代码  5///   6/// 传入用户提交数据  7/// true-安全:f ...

  5. 什么是SQL注入式攻击?

    什么是SQL注入式攻击? 所谓SQL注入式攻击,就是攻击者把SQL命令插入到Web表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的SQL命令.在某些表单中,用户输入的内容直接用来构造(或者影响 ...

  6. 如何防范SQL注入式攻击

    一.什么是SQL注入式攻击? 所谓SQL注入式攻击,就是攻击者把SQL命令插入到Web表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的SQL命令.在某些表单中,用户输入的内容直接用来构造(或者 ...

  7. 如何防止SQL注入式攻击

    一.什么是SQL注入式攻击?  所谓SQL注入式攻击,就是攻击者把SQL命令插入到Web表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的SQL命令.在某些表单中,用户输入的内容直接用来构造(或 ...

  8. SQL注入式攻击

    百度百科:http://baike.baidu.com/link?url=GQbJ2amTzTahZA7XJSBDLYYkN3waQ9JCoJ0l--tCWlvKQibe0YaH4hpmgEnLyn0 ...

  9. ADO.NET复习总结(3)--参数化SQL语句--防止sql注入式攻击

    1.SQL 注入 2.使用参数化的方式,可以有效防止SQL注入,使用类parameter的实现类SqlParameter Command的属性parameters是一个参数集合. 3.举例<查询 ...

随机推荐

  1. List<T>的IndexOf方法和Remove方法

    Microsoft地址 List<T>的IndexOf()方法 如果T是值类型的,就按照比较值的方法从列表的第一个元素开始逐个匹配,如果T是引用类型,就比较引用是否相同 举例如下: cla ...

  2. [CODEVS1294]全排列

    题目描述 Description 给出一个n, 请输出n的所有全排列 输入描述 Input Description 读入仅一个整数n   (1<=n<=10) 输出描述 Output De ...

  3. lecode Interleaving String

    这个问题,前面思考过,当时就是用搜索的方法,此处又遇到一次,发现自己理解的太浅了 Given s1, s2, s3, find whether s3 is formed by the interlea ...

  4. 2013 ACM区域赛长沙 A Alice’s Print Service HDU 4791

    题意:就是一个打印分段收费政策,印的越多,单张价格越低,输入需要印刷的数量,求最小印刷费用一个细节就是,比当前还小的状态可能是最后几个. #include<stdio.h> #includ ...

  5. 转 wordpress搭建

    新建一个博客文件 在搭建博客的过程中,我们需要创建一个文件,用来存储博客的相关信息.这些信息包括域名的信息,主机空间服务器ip,FTP登录名和密码,空间面板登录信息等等,只要是与该博客有关的信息,全部 ...

  6. 排列的Java递归语言实现

    在做算法题的时候,发现排列经常被使用到,是一个重要的知识点, 下面是博主修改过的代码,初学者,如有不足,欢迎指出 import java.util.ArrayList; import java.uti ...

  7. Yii简单的基于角色的访问控制

    public function filters() { return array( 'accessControl', // perform access control for CRUD operat ...

  8. Yii 将对象转化成数组

    将从数据库查找的对象,转换成数组,并且以设定属性键名称,用到ArrayHelper::toArray $posts = Post::find()->limit(10)->all(); $d ...

  9. SampleManager(赛默飞)

  10. leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

    Climbing Stairs  You are climbing a stair case. It takes n steps to reach to the top. Each time you ...