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代码 $ ...
随机推荐
- Python爬虫学习——使用Cookie登录新浪微博
1.首先在浏览器中进入WAP版微博的网址,因为手机版微博的内容较为简洁,方便后续使用正则表达式或者beautifulSoup等工具对所需要内容进行过滤 https://login.weibo.cn/l ...
- 史上最“脑残”的“抢火车票”程序(node.js版)
[背景] 快过年了,我妈一个电话打过来叫我给他买火车票,我到12306一查,硬座和硬卧基本没有了,高铁又太贵. 最后只抢了3张无座票,但是我妈说能不能买有座位的啊,我说没有了啊,我妈:你过两天再帮我看 ...
- 【Scala】Scala之String
一.前言 大数据领域的Spark.Kafka.Summingbird等都是由Scala语言编写而成,相比Java而言,Scala更精炼.由于笔者从事大数据相关的工作,所以有必要好好学习Scala语言, ...
- Python print报ascii编码异常的靠谱解决办法
之前遇到此异常UnicodeEncodeError: 'ascii' codec can't encode characters...,都是用这种方式解决:sys.setdefaultencoding ...
- 第六届全球游戏大会( GMGC 北京 2017 )有哪些看点?
全球游戏大会作为亚太地区极具影响力的全球性游戏行业盛会,本次第六届全球游戏大会( GMGC 北京 2017 )将力邀全球50个国家.500多位业界领袖.5000多家企业.50000多位业界精英参展参会 ...
- C#中的重写和覆盖的区别
#中重写(override)和覆盖(new)的区别 重写用关键字 virtual 修饰的方法,叫虚方法.可以在子类中用override 声明同名的方法,这叫“重写”.相应的没有用virtual修饰的方 ...
- Jquery 操作DropDownList 根据条件选中
$("#<%=DDLCounty.ClientID%> option").each(function () { if ($(this).text() == $(&quo ...
- 数据库索引的实现原理(笔记)详细http://www.linezing.com/blog/?p=798#nav-1
数据库索引,是数据库管理系统中一个排序的数据结构,以协助快速查询.更新数据库表中数据.索引的实现通常使用B树及其变种B+树. 在数据之外,数据库系统还维护着满足特定查找算法的数据结构,这些数据结构以某 ...
- AndroidStudio0.5.2 BUG 导致 menu 菜单键崩溃
郁闷了半天,今天发现一点击手机 menu 键应用就崩溃了,记得之前都是好好的,调试了半天代码还是搞不定,于是网上google了一番,发现仅国外有一两篇文章有提到类似问题,据说是 0.5.2 版本的 B ...
- Android使用Eclipse遇到"java.lang.ClassNotFoundException"
最近遇到个Android Jar的问题,找了几天才找到root cause. 在此记录下. 我们的Android项目需要使用一个供应商的Jar. 我们使用的开发环境为:Eclipse + ADT插件( ...