浅谈 C# SQL防注入
1
#region 防止sql注入式攻击(可用于UI层控制)
2
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
}
158
#endregion
我们的解决方式是:
1、首先在UI录入时,要控制数据的类型和长度、防止SQL注入式攻击,系统提供检测注入式攻击的函数,一旦检测出注入式攻击,该数据即不能提交;
2、业务逻辑层控制,通过在方法内部将SQL关键字用一定的方法屏蔽掉,然后检查数据长度,保证提交SQL时,不会有SQL数据库注入式攻击代码;但是这样处理后,要求UI输出时将屏蔽的字符还原。因此系统提供屏蔽字符 的函数和还原字符的函数。
3、在数据访问层,绝大多数采用存储过程访问数据,调用时以存储过程参数的方式访问,也会很好的防止注入式攻击。
浅谈 C# SQL防注入的更多相关文章
- SQL防注入程序 v1.0
/// ***************C#版SQL防注入程序 v1.0************ /// *使用方法: /// 一.整站防注入(推荐) /// 在Global.asax.cs中查找App ...
- PHP之SQL防注入代码集合(建站常用)
SQL防注入代码一 <?php if (!function_exists (quote)) { function quote($var) { if (strlen($var)) { $var=! ...
- sql 防注入 维基百科
http://zh.wikipedia.org/wiki/SQL%E8%B3%87%E6%96%99%E9%9A%B1%E7%A2%BC%E6%94%BB%E6%93%8A SQL攻击(SQL inj ...
- 浅谈.NET编译时注入(C#-->IL)
原文:浅谈.NET编译时注入(C#-->IL) .NET是一门多语言平台,这是我们所众所周知的,其实现原理在于因为了MSIL(微软中间语言)的一种代码指令平台.所以.NET语言的编译就分为了两部 ...
- 特殊字符的过滤方法,防sql防注入代码的过滤方法
特殊字符的过滤方法 function strFilter($str){ //特殊字符的过滤方法 $str = str_replace('`', '', $str); $str = str_replac ...
- SQL防注入程序
1.在Global.asax.cs中写入: protected void Application_BeginRequest(Object sender,EventArgs e){ SqlIn ...
- PHP SQL防注入
过年前后在做一个抽奖的东西,需要用户填写中奖信息,为了防止非法用户对数据库进行入侵神马的,于是写下基本的防注入语句,需要用的可以自己封装成一个function. $str = str_replace( ...
- 360提供的SQL防注入
<?php class sqlsafe { private $getfilter = "'|(and|or)\\b.+?(>|<|=|in|like)|\\/\\*.+?\ ...
- .net sql 防注入 httpmodule
1 新建一个类,实现IHttpModule接口 using System; using System.Collections.Generic; using System.Linq; using Sys ...
随机推荐
- CTF取证方法大汇总,建议收藏!
站在巨人的肩头才会看见更远的世界,这是一篇来自技术牛人的神总结,运用多年实战经验总结的CTF取证方法,全面细致,通俗易懂,掌握了这个技能定会让你在CTF路上少走很多弯路,不看真的会后悔! 本篇文章大约 ...
- 微信公众号支付提示mch_id参数格式错误
背景: .Net MVC微信公众号支付功能 问题: 今天在做网站微信支付的时候,一直提示“微信公众号支付提示mch_id参数格式错误” ! 解决方法: 其实这个问题一般并不是说你配置有错,首先它提示你 ...
- 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 二十五║初探SSR服务端渲染(个人博客二)
缘起 时间真快,现在已经是这个系列教程的下半部 Vue 第 12 篇了,昨天我也简单思考了下,可能明天再来一篇,Vue 就基本告一段落了,因为什么呢,这里给大家说个题外话,当时写博文的时候,只是想给大 ...
- U盘重装Win10系统视频教程
编程行业中,如你遇到非常奇怪.无法解释的问题时,通常会使用这三步骤:重启电脑.重装软件.重装系统: 作为终极大法重装系统在我们日常使用电脑中也可以说是不可避免的,比如你电脑无故运行非常卡.下软件可能中 ...
- RecyclerFlexboxLayoutManagerDemo【使用FlexboxLayoutManager实现流式布局】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 FlexboxLayout是一个Google 开源的库项目,它将CSS Flexible Box Layout Module的类似功 ...
- 数据库~dotnetcore连接Mysql插入中文失败
到目录 在dotnetcore里,连接mysql数据,插入中文时出现无法识别,并提示插入失败的情况,分析后得知它是编码问题,即数据库编码问题,你的中文在数据表里无法被识别! 解决方法(一) 进行mys ...
- SpringBoot启动原理及相关流程
一.springboot启动原理及相关流程概览 springboot是基于spring的新型的轻量级框架,最厉害的地方当属自动配置.那我们就可以根据启动流程和相关原理来看看,如何实现传奇的自动配置 二 ...
- 微信公众号开发C#系列-4、获取接口调用凭证
概述 获取接口调用凭证实质就是获取access_token.在微信接口开发中,许多服务的使用都离不开Access Token,Access Token相当于打开这些服务的钥匙,正常情况下会在7200秒 ...
- JavaScript引擎浅析
前言 该文章是为大家整理一个关于js的知识网络,重点是知识的罗列及之间的联系,所以实例可能会有所不足,导致可能没有对应知识的人看不懂,希望大家能够结合其他资料来学习这篇文章,并整理出自己的知识体系. ...
- C# 反射 判断类的延伸类型
判断类型是否被继承.实现 1.判断是否实现了指定接口 添加测试类: public class TestClass2 : TestClass1 { } public class TestClass1 : ...