黑色中长开衫搭配图片:网站安全性:C#防SQL注入代码的实现方法 - 51CTO.COM
来源:百度文库 编辑:九乡新闻网 时间:2024/04/29 19:57:31
-
网站安全性:C#防SQL注入代码的实现方法
- http://developer.51cto.com 2009-08-31 14:44 bjdren hi.baidu 我要评论(0)
- 为了提高网站的安全性,首先网站要防注入。本文就讲解了C#防SQL注入代码实现方法,供大家参考。
对于网站的安全性,是每个网站开发者和运营者最关心的问题。网站一旦出现漏洞,那势必将造成很大的损失。为了提高网站的安全性,首先网站要防注入,最重要的是服务器的安全设施要做到位。
下面说下网站防注入的几点要素。
一:丢弃SQL语句直接拼接,虽然这个写起来很快很方便。
二:如果用SQL语句,那就使用参数化,添加Param
三:尽可能的使用存储过程,安全性能高而且处理速度也快
四:屏蔽SQL,javascript等注入(很是主要的),对于每个文件写是不太可能的。所以要找到对所有文件起作用的办法。我在网上收集了以下3种方法
C#防SQL注入方法一
在Web.config文件中, < appSettings>下面增加一个标签:如下
- < appSettings>
- < add key="safeParameters" value="OrderID-int32,CustomerEmail-email,ShippingZipcode-USzip" />
- < /appSettings>
其中key是 < saveParameters>后面的值为"OrderId-int32"等,其中"-"前面表示参数的名称比如:OrderId,后面的int32表示数据类型。
C#防SQL注入方法二
在Global.asax中增加下面一段:
- protected void Application_BeginRequest(Object sender, EventArgs e){
- String[] safeParameters = System.Configuration.ConfigurationSettings.AppSettings["safeParameters"].ToString().Split(',');
- for(int i= 0 ;i < safeParameters.Length; i++){
- String parameterName = safeParameters[i].Split('-')[0];
- String parameterType = safeParameters[i].Split('-')[1];
- isValidParameter(parameterName, parameterType);
- }
- }
- public void isValidParameter(string parameterName, string parameterType){
- string parameterValue = Request.QueryString[parameterName];
- if(parameterValue == null) return;
- if(parameterType.Equals("int32")){
- if(!parameterCheck.isInt(parameterValue)) Response.Redirect("parameterError.aspx");
- }
- else if (parameterType.Equals("USzip")){
- if(!parameterCheck.isUSZip(parameterValue)) Response.Redirect("parameterError.aspx");
- }
- else if (parameterType.Equals("email")){
- if(!parameterCheck.isEmail(parameterValue)) Response.Redirect("parameterError.aspx");
- }
- }
C#防SQL注入方法三
使用字符串过滤类
- using System;
- namespace web.comm
- {
- /**//// < summary>
- /// ProcessRequest 的摘要说明。
- /// < /summary>
- public class ProcessRequest
- {
- public ProcessRequest()
- {
- //
- // TODO: 在此处添加构造函数逻辑
- //
- }
- SQL注入式攻击代码分析#region SQL注入式攻击代码分析
- /**//// < summary>
- /// 处理用户提交的请求
- /// < /summary>
- public static void StartProcessRequest()
- {
- // System.Web.HttpContext.Current.Response.Write("< script>alert('dddd');< /script>");
- try
- {
- string getkeys = "";
- //string sqlErrorPage = System.Configuration.ConfigurationSettings.AppSettings["CustomErrorPage"].ToString();
- if (System.Web.HttpContext.Current.Request.QueryString != null)
- {
- for(int i=0;i< System.Web.HttpContext.Current.Request.QueryString.Count;i++)
- {
- getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i];
- if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys],0))
- {
- //System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");
- System.Web.HttpContext.Current.Response.Write("< script>alert('请勿非法提交!');history.back();< /script>");
- System.Web.HttpContext.Current.Response.End();
- }
- }
- }
- if (System.Web.HttpContext.Current.Request.Form != null)
- {
- for(int i=0;i< System.Web.HttpContext.Current.Request.Form.Count;i++)
- {
- getkeys = System.Web.HttpContext.Current.Request.Form.Keys[i];
- if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys],1))
- {
- //System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");
- System.Web.HttpContext.Current.Response.Write("< script>alert('请勿非法提交!');history.back();< /script>");
- System.Web.HttpContext.Current.Response.End();
- }
- }
- }
- }
- catch
- {
- // 错误处理: 处理用户提交信息!
- }
- }
- /**//// < summary>
- /// 分析用户请求是否正常
- /// < /summary>
- /// < param name="Str">传入用户提交数据< /param>
- /// < returns>返回是否含有SQL注入式攻击代码< /returns>
- private static bool ProcessSqlStr(string Str,int type)
- {
- string SqlStr;
- if(type == 1)
- SqlStr = "exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ";
- else
- SqlStr = "'|and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare";
- bool ReturnValue = true;
- try
- {
- if (Str != "")
- {
- string[] anySqlStr = SqlStr.Split('|');
- foreach (string ss in anySqlStr)
- {
- if (Str.IndexOf(ss)>=0)
- {
- ReturnValue = false;
- }
- }
- }
- }
- catch
- {
- ReturnValue = false;
- }
- return ReturnValue;
- }
- #endregion
- }
- }
【编辑推荐】
网站安全性:C#防SQL注入代码的实现方法 - 51CTO.COM
C#发送Email邮件的方法解析 - 51CTO.COM
C#接口的作用实例解析 - 51CTO.COM
精确实现C# form与form之间通信 - 51CTO.COM
sql 防注入
探寻C# Button双击事件 - 51CTO.COM
简析散列算法在C# 加密中的应用 - 51CTO.COM
代码注入博客首页的方法:
技巧:在VirtualBox中实现主机文件的共享 - 51CTO.COM
C#实现汉字转换为拼音缩写的代码
1.6 新建Web网站与新建Web应用程序的区别 - 51CTO.COM
详解Java解析XML的四种方法 - 51CTO.COM
企业局域网本地路由配置的五个方法 - 51CTO.COM
SQL 的注入式攻击 1
Unix操作系统下的LD_PRELOAD环境变量 - 51CTO.COM
J2EE平台的13种核心技术 - 51CTO.COM
解读数据存储ODS的概念 - 51CTO.COM
C/C++中Static的作用详述 - 51CTO.COM
25个最棒最耀眼的Eclipse开发项目- 51CTO.COM
微软还将买谁:诺基亚还是愤怒的小鸟? - 51CTO.COM
防止SQL注入
SQL注入天书
asp代码实现EXCEL数据导入到SQL数据库2
asp代码实现EXCEL数据导入到SQL数据库