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代码 $ ...
随机推荐
- 基于node.js构建微服务中的mock服务
缘起 由于现在微服务越来越火了,越来越多的微服务融入到了日常开发当中.在开发微服务的时候,经常会遇到一个问题由于依赖于其他服务,导致你的进度受到阻碍.使你不得不先mock出你期望调用依赖服务的输出,来 ...
- hdu1040
#include<stdio.h>#include<stdlib.h>int a[100];int cmp(const void *a,const void *b){ retu ...
- JQuery基础知识(1)
JQuery基础知识(1) 对JQuery学习中遇到的小细节进行了整理和总结 1.JQuery hide()和show()方法,分别对选中的元素进行隐藏和显示,语法:hide()和show分别有对应的 ...
- [UWP-小白日记13]Composition动画
前言 首先,来对比下传统动画和Composition动画.看图就能明白composition动画的优势太明显就像官方说的大幅度的降低了动画的实现难度和代码量. 传统的动画,就拿最常见的就是过度动画:进 ...
- 关于对HandlerThread的了解
在Android中经常需要创建一个循环线程,有耗时操作时候,放到里面去操作,如果没有耗时操作,就让该线程处于等待,但是不要杀死它, 最好不要一旦有耗时任务,就立刻创建一个新线程,因为会有性能问题. H ...
- JavaEE XML DOM创建
DOM创建XML @author ixenos 1.思路: 先封装构建一颗DOM树,然后将DOM树转换成XML文件 2.三种写DOM树到XML文件的方式: 1)使用DOM(或DOM4J.JDOM) 2 ...
- js ajax 调试
谷歌浏览器 F12->network->()请求ajax)->出现ajax调用的方法名-->点击查看网站请求地址--返回所有的数据(preview面板中) success返回后 ...
- [转]学好Mac常用命令,助力iOS开发
转自:http://www.jianshu.com/p/d9ec00d28237 序言 在iOS开发的过程中,更多地注重iOS开发的效率,熟练使用Mac终端操作的常用命令,可以让你更好的游刃于iO ...
- HttpURLConnection传JSON数据
try { //创建连接 URL url = new URL(url); HttpURLConnection connection = (HttpURLConnection) url.openConn ...
- 随机love'...
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...