SQL防漏洞注入攻击小结
3
///
4
/// 判断字符串中是否有SQL攻击代码
5
///
6
/// 传入用户提交数据
7
/// true-安全;false-有注入攻击现有;
8
public 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
///
39
public 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
///
88
public 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
///
105
public 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
///
136
public 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代码 $ ...
随机推荐
- java String源码学习
public final class String implements java.io.Serializable, Comparable<String>, CharSequence { ...
- ruby操作mongo DB
web自动化中,对操作日志功能进行验证(操作日志存在mongoDB中). 为了避免前面操作产生的日志影响结果校验,我们需要先清除之前的所有操作日志. require 'mongo' host = '1 ...
- Java入门第二季第一章类和对象知识点
Java 中的 static 使用之静态方法 1. 静态方法中可以直接调用同类中的静态成员,但不能直接调用非静态成员.如: 如果希望在静态方法中调用非静态变量,可以通过创建类的对象,然后通过对象来访问 ...
- iOS设置UITableView中Cell被默认选中后怎么触发didselect事件
//默认选中某个cell [self.searchResultTV selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] a ...
- IntelliJ IDEA新建JAVA WEB项目(转载)
IntelliJ IDEA是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支持.各类版本控制工具(git.svn ...
- 关于对HandlerThread的了解
在Android中经常需要创建一个循环线程,有耗时操作时候,放到里面去操作,如果没有耗时操作,就让该线程处于等待,但是不要杀死它, 最好不要一旦有耗时任务,就立刻创建一个新线程,因为会有性能问题. H ...
- 【css2、css3】css改变select选择框的样式
效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- CoreJavaE10V1P3.1 第3章 Java的基本编程结构-3.1 Java 最简程序
3.1Java最简程序 FirstSample.java public class FirstSample { public static void main(String[] args) { Sys ...
- ECStore图片存储采用阿里云OSS(图片存储)服务
主要功能:ECStore图片存储采用阿里云OSS(图片存储)服务 适用版本:ECStore 授权方式:授权域名使用,付费插件 联系方式: QQ 275553385 mail: jimingson ...
- PHP不使用递归的无限级分类
不用递归实现无限级分类,简单测试了下性能比递归稍好一点点点,但写得太复杂了,还是递归简单方便点 代码: <?php $list = array( array('id'=>1, 'pid'= ...