SQL防漏洞注入攻击小结
3///
4/// 判断字符串中是否有SQL攻击代码
5///
6/// 传入用户提交数据
7/// true-安全;false-有注入攻击现有;
8public bool ProcessSqlStr(string inputString)
9{
10 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";
11 try
12 {
13 if ((inputString != null) && (inputString != String.Empty))
14 {
15 string str_Regex = @"\b(" + SqlStr + @")\b";
16
17 Regex Regex = new Regex(str_Regex, RegexOptions.IgnoreCase);
18 //string s = Regex.Match(inputString).Value;
19 if (true == Regex.IsMatch(inputString))
20 return false;
21
22 }
23 }
24 catch
25 {
26 return false;
27 }
28 return true;
29}
30
31
32///
33/// 处理用户提交的请求,校验sql注入式攻击,在页面装置时候运行
34/// System.Configuration.ConfigurationSettings.AppSettings["ErrorPage"].ToString(); 为用户自定义错误页面提示地址,
35/// 在Web.Config文件时里面添加一个 ErrorPage 即可
36///
37///
38///
39public void ProcessRequest()
40{
41 try
42 {
43 string getkeys = "";
44 string sqlErrorPage = System.Configuration.ConfigurationSettings.AppSettings["ErrorPage"].ToString();
45 if (System.Web.HttpContext.Current.Request.QueryString != null)
46 {
47
48 for (int i = 0; i < System.Web.HttpContext.Current.Request.QueryString.Count; i++)
49 {
50 getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i];
51 if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys]))
52 {
53 System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage + "?errmsg=" + getkeys + "有SQL攻击嫌疑!");
54 System.Web.HttpContext.Current.Response.End();
55 }
56 }
57 }
58 if (System.Web.HttpContext.Current.Request.Form != null)
59 {
60 for (int i = 0; i < System.Web.HttpContext.Current.Request.Form.Count; i++)
61 {
62 getkeys = System.Web.HttpContext.Current.Request.Form.Keys[i];
63 if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys]))
64 {
65 System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage + "?errmsg=" + getkeys + "有SQL攻击嫌疑!");
66 System.Web.HttpContext.Current.Response.End();
67 }
68 }
69 }
70 }
71 catch
72 {
73 // 错误处理: 处理用户提交信息!
74 }
75}
76#endregion
77
78
79
80
81#region 转换sql代码(也防止sql注入式攻击,可以用于业务逻辑层,但要求UI层输入数据时候进行解码)
82///
83/// 提取字符固定长度
84///
85///
86///
87///
88public string CheckStringLength(string inputString, Int32 maxLength)
89{
90 if ((inputString != null) && (inputString != String.Empty))
91 {
92 inputString = inputString.Trim();
93
94 if (inputString.Length > maxLength)
95 inputString = inputString.Substring(0, maxLength);
96 }
97 return inputString;
98}
99
100///
101/// 将输入字符串中的sql敏感字,替换成"[敏感字]",要求输出时,替换回来
102///
103///
104///
105public string MyEncodeInputString(string inputString)
106{
107 //要替换的敏感字
108 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";
109 try
110 {
111 if ((inputString != null) && (inputString != String.Empty))
112 {
113 string str_Regex = @"\b(" + SqlStr + @")\b";
114
115 Regex Regex = new Regex(str_Regex, RegexOptions.IgnoreCase);
116 //string s = Regex.Match(inputString).Value;
117 MatchCollection matches = Regex.Matches(inputString);
118 for (int i = 0; i < matches.Count; i++)
119 inputString = inputString.Replace(matches[i].Value, "[" + matches[i].Value + "]");
120
121 }
122 }
123 catch
124 {
125 return "";
126 }
127 return inputString;
128
129}
130
131///
132/// 将已经替换成的"[敏感字]",转换回来为"敏感字"
133///
134///
135///
136public string MyDecodeOutputString(string outputstring)
137{
138 //要替换的敏感字
139 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";
140 try
141 {
142 if ((outputstring != null) && (outputstring != String.Empty))
143 {
144 string str_Regex = @"\[\b(" + SqlStr + @")\b\]";
145 Regex Regex = new Regex(str_Regex, RegexOptions.IgnoreCase);
146 MatchCollection matches = Regex.Matches(outputstring);
147 for (int i = 0; i < matches.Count; i++)
148 outputstring = outputstring.Replace(matches[i].Value, matches[i].Value.Substring(1, matches[i].Value.Length - 2));
149
150 }
151 }
152 catch
153 {
154 return "";
155 }
156 return outputstring;
157}
SQL防漏洞注入攻击小结的更多相关文章
- (非原)SQL注入专题--整理帖 && like 语句拼sql 如何防止注入攻击。
原地址:blog.csdn.net/lvjin110/article/details/28697695 like 语句拼sql 如何防止注入攻击?http://bbs.csdn.net/topics/ ...
- 关于在线文本编辑器防XSS注入攻击问题
跨站脚本攻击,又称XSS代码攻击,也是一种常见的脚本注入攻击.例如在下面的界面上,很多输入框是可以随意输入内容的,特别是一些文本编辑框里面,可以输入例如<script>alert('这是一 ...
- 织梦dedecms修改include和plus重命名提高安全性防漏洞注入挂马
织梦dedecms是新手站长使用得比较多的一个建站开源程序,正因如此,也是被被入侵挂马比较多的程序.下面就来跟大家说一下怎么重新命名dedecms的include文件夹以及plus文件夹来提高网站的安 ...
- SQL注入攻击及防范
一.什么是SQL注入1.SQL注入的定义 SQL注入(SQL Injection) 利用了程序中的SQL的漏洞,进行攻击的方法. 2.SQL注入举例 1)利用SQL语法错误获取数据库表的结构 ...
- 防止SQL注入攻击,数据库操作类
如果不规避,在黑窗口里面输入内容时利用拼接语句可以对数据进行攻击 如:输入Code值 p001' union select * from Info where '1'='1 //这样可以查询到所有数据 ...
- 防止SQL注入攻击的一些方法小结
SQL注入攻击的危害性很大.在讲解其防止办法之前,数据库管理员有必要先了解一下其攻击的原理.这有利于管理员采取有针对性的防治措施. 一. SQL注入攻击的简单示例. statement := &quo ...
- PHP漏洞全解(五)-SQL注入攻击
本文主要介绍针对PHP网站的SQL注入攻击.所谓的SQL注入攻击,即一部分程序员在编写代码的时候,没有对用户输入数据的合法性进行判断,使应用程序存在安全隐患.用户可以提交一段数据库查询代码,根据程序返 ...
- 《sql注入攻击与防御 第2版》的总结 之 如何确定有sql注入漏洞
看完<sql注入攻击与防御 第2版>后,发现原来自己也能黑网站了,就一个字:太爽了. 简单总结一下入侵步骤: 1.确定是否有sql注入漏洞 2.确定数据库类型 3.组合sql语句,实施渗透 ...
- PHP防SQL注入攻击
PHP防SQL注入攻击 收藏 没有太多的过滤,主要是针对php和mysql的组合. 一般性的防注入,只要使用php的 addslashes 函数就可以了. 以下是一段copy来的代码: PHP代码 $ ...
随机推荐
- 在html页面中展示JSON
背景: 有时候我们需要将json数据直接显示在页面上(比如在做一个接口测试的项目,需要将接口返回的结果直接展示),但是如果直接显示字符串,不方便查看.需要格式化一下. 解决方案: 其实JSON.str ...
- [ios2]iphone编程中使用封装的NSLog来打印调试信息 【转】
使用NSLog的一个风险是:它的运行会占用时间和设备资源. 简单而粗暴的解决方案是:在release前,将所有的NSLog注释掉.简单有效,但副作用是:下次你要调试时,又得将NSLog一个个取消注释. ...
- java基础练习 3
import java.util.Scanner; public class Third { /*计算字符串中子串出现的次数 (5 分数)*/ public static void main(Stri ...
- PSR
目前包括以下几个规范: PSR-0(弃用) PSR-1 PSR-2 PSR-3 PSR-4 1.PSR-0 自动加载规范,此规范已被启用-本规范已于2014年10月21日被标记为弃用,目前新的替代规范 ...
- shell 字符串包含
转自:Shell判断字符串包含关系的几种方法 现在每次分析网站日志的时候都需要判断百度蜘蛛是不是真实的蜘蛛,nslookup之后需要判断结果中是否包含"baidu"字符串 以下给出 ...
- [大山中学dp常练-4 Rounds]
Round#1 2016.9.28 这次晚练十分sb 有点浪费时间 全是dp题 先说过程 3分钟草了第一题之后感觉好像有点水 然后翻了翻题目 看了看第一第四题两颗星 其他三颗星 然后一下子第二题题目太 ...
- OSI模型第四层传输层--TCP协议
1.传输层2个协议tcp和udp 2.tcp的可靠性(挂号信). 面向链接的:类似寄挂号信,对方收到了并且能够确认.所以也是可靠的传输. 最大报文传输:两端可以协商传输报文大小.(协商一个报文的大小) ...
- C#-实验3
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- js在关闭页面前弹出确认提示【转载】
最近项目中出现个bug,就是导出数据后,会提示确认导航,其实实际需求并不需要这个提示,可能是之前遗留的问题.查了下资料是在触发了onbeforeunload事件,那么剩下的就是代码组织问题了. 众所周 ...
- openstack私有云布署实践【17 配置文件部份说明】
Nova部份 cpu_allocation_ratio = 4.0 物理 CPU 超售比例,默认是 16 倍,超线程也算作一个物理 CPU,需要根据具体负载和物理 CPU 能力进行综合判断后确定具体的 ...