.net 通用防注入代码

简介: using System;namespace web.comm{    /**////     /// ProcessRequest 的摘要说明。    ///     public class ProcessRequest    {        public ProcessRequest...

 

img_a6339ee3e57d1d52bc7d02b338e15a60.gif using  System;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
namespace  web.comm
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif {
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
/**//// <summary>
img_33d02437d135341f0800e3d415312ae8.gif    
/// ProcessRequest 的摘要说明。
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
/// </summary>

img_33d02437d135341f0800e3d415312ae8.gif    public class ProcessRequest
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif        
public ProcessRequest()
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            
//
img_33d02437d135341f0800e3d415312ae8.gif            
// TODO: 在此处添加构造函数逻辑
img_33d02437d135341f0800e3d415312ae8.gif            
//
img_105a1e124122b2abcee4ea8e9f5108f3.gif
        }

img_33d02437d135341f0800e3d415312ae8.gif
img_7a2b9a960ee9a98bfd25d306d55009f8.gifimg_2887d91d0594ef8793c1db92b8a1d545.gif        
SQL注入式攻击代码分析#region SQL注入式攻击代码分析
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
img_33d02437d135341f0800e3d415312ae8.gif        
/// 处理用户提交的请求
img_105a1e124122b2abcee4ea8e9f5108f3.gif        
/// </summary>

img_33d02437d135341f0800e3d415312ae8.gif        public static void StartProcessRequest()
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            
img_33d02437d135341f0800e3d415312ae8.gif
//            System.Web.HttpContext.Current.Response.Write("<script>alert('dddd');</script>");
img_33d02437d135341f0800e3d415312ae8.gif
            try
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                
string getkeys = "";
img_33d02437d135341f0800e3d415312ae8.gif                
//string sqlErrorPage = System.Configuration.ConfigurationSettings.AppSettings["CustomErrorPage"].ToString();
img_33d02437d135341f0800e3d415312ae8.gif
                if (System.Web.HttpContext.Current.Request.QueryString != null)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif    
img_33d02437d135341f0800e3d415312ae8.gif                    
for(int i=0;i<System.Web.HttpContext.Current.Request.QueryString.Count;i++)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                    
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                        getkeys 
= System.Web.HttpContext.Current.Request.QueryString.Keys[i];
img_33d02437d135341f0800e3d415312ae8.gif                        
if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys],0))
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                            
//System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");
img_33d02437d135341f0800e3d415312ae8.gif
                            System.Web.HttpContext.Current.Response.Write("<script>alert('请勿非法提交!');history.back();</script>");
img_33d02437d135341f0800e3d415312ae8.gif                            System.Web.HttpContext.Current.Response.End();
img_105a1e124122b2abcee4ea8e9f5108f3.gif                        }

img_105a1e124122b2abcee4ea8e9f5108f3.gif                    }

img_105a1e124122b2abcee4ea8e9f5108f3.gif                }

img_33d02437d135341f0800e3d415312ae8.gif                
if (System.Web.HttpContext.Current.Request.Form != null)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                    
for(int i=0;i<System.Web.HttpContext.Current.Request.Form.Count;i++)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                    
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                        getkeys 
= System.Web.HttpContext.Current.Request.Form.Keys[i];
img_33d02437d135341f0800e3d415312ae8.gif                        
if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys],1))
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                            
//System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");
img_33d02437d135341f0800e3d415312ae8.gif
                            System.Web.HttpContext.Current.Response.Write("<script>alert('请勿非法提交!');history.back();</script>");
img_33d02437d135341f0800e3d415312ae8.gif                            System.Web.HttpContext.Current.Response.End();
img_105a1e124122b2abcee4ea8e9f5108f3.gif                        }

img_105a1e124122b2abcee4ea8e9f5108f3.gif                    }

img_105a1e124122b2abcee4ea8e9f5108f3.gif                }

img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
catch
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                
// 错误处理: 处理用户提交信息!
img_105a1e124122b2abcee4ea8e9f5108f3.gif
            }

img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
img_33d02437d135341f0800e3d415312ae8.gif        
/// 分析用户请求是否正常
img_33d02437d135341f0800e3d415312ae8.gif        
/// </summary>
img_33d02437d135341f0800e3d415312ae8.gif        
/// <param name="Str">传入用户提交数据</param>
img_105a1e124122b2abcee4ea8e9f5108f3.gif        
/// <returns>返回是否含有SQL注入式攻击代码</returns>

img_33d02437d135341f0800e3d415312ae8.gif        private static bool ProcessSqlStr(string Str,int type)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            
string SqlStr;
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            
if(type == 1)
img_33d02437d135341f0800e3d415312ae8.gif                SqlStr 
= "exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ";
img_33d02437d135341f0800e3d415312ae8.gif            
else
img_33d02437d135341f0800e3d415312ae8.gif                SqlStr 
= "'|and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare";
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            
bool ReturnValue = true;
img_33d02437d135341f0800e3d415312ae8.gif            
try
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                
if (Str != "")
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                    
string[] anySqlStr = SqlStr.Split('|');
img_33d02437d135341f0800e3d415312ae8.gif                    
foreach (string ss in anySqlStr)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                    
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                        
if (Str.IndexOf(ss)>=0)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                            ReturnValue 
= false;
img_105a1e124122b2abcee4ea8e9f5108f3.gif                        }

img_105a1e124122b2abcee4ea8e9f5108f3.gif                    }

img_105a1e124122b2abcee4ea8e9f5108f3.gif                }

img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
catch
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                ReturnValue 
= false;
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
return ReturnValue;
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_105a1e124122b2abcee4ea8e9f5108f3.gif        
#endregion

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif
img_105a1e124122b2abcee4ea8e9f5108f3.gif    }

img_05dd8d549cff04457a6366b0a7c9352a.gif}

img_a6339ee3e57d1d52bc7d02b338e15a60.gif
目录
相关文章
|
6月前
|
JSON IDE 前端开发
[.NET开发者的福音]一个方便易用的在线.NET代码编辑工具.NET Fiddle
[.NET开发者的福音]一个方便易用的在线.NET代码编辑工具.NET Fiddle
|
网络协议 算法 Shell
来我们探究一下net/http 的代码流程
来我们探究一下net/http 的代码流程
|
3月前
|
API
【Azure 媒体服务】Media Service的编码示例 -- 创建缩略图子画面的.NET代码调试问题
【Azure 媒体服务】Media Service的编码示例 -- 创建缩略图子画面的.NET代码调试问题
|
3月前
|
C# 开发者 Windows
在VB.NET项目中使用C#编写的代码
在VB.NET项目中使用C#编写的代码
57 0
|
1月前
|
前端开发 JavaScript C#
CodeMaid:一款基于.NET开发的Visual Studio代码简化和整理实用插件
CodeMaid:一款基于.NET开发的Visual Studio代码简化和整理实用插件
|
3月前
|
Kubernetes 监控 Devops
【独家揭秘】.NET项目中的DevOps实践:从代码提交到生产部署,你不知道的那些事!
【8月更文挑战第28天】.NET 项目中的 DevOps 实践贯穿代码提交到生产部署全流程,涵盖健壮的源代码管理、GitFlow 工作流、持续集成与部署、容器化及监控日志记录。通过 Git、CI/CD 工具、Kubernetes 及日志框架的最佳实践应用,显著提升软件开发效率与质量。本文通过具体示例,助力开发者构建高效可靠的 DevOps 流程,确保项目成功交付。
77 0
|
3月前
|
XML 开发框架 .NET
.NET框架:软件开发领域的瑞士军刀,如何让初学者变身代码艺术家——从基础架构到独特优势,一篇不可错过的深度解读。
【8月更文挑战第28天】.NET框架是由微软推出的统一开发平台,支持多种编程语言,简化应用程序的开发与部署。其核心组件包括公共语言运行库(CLR)和类库(FCL)。CLR负责内存管理、线程管理和异常处理等任务,确保代码稳定运行;FCL则提供了丰富的类和接口,涵盖网络、数据访问、安全性等多个领域,提高开发效率。此外,.NET框架还支持跨语言互操作,允许开发者使用C#、VB.NET等语言编写代码并无缝集成。这一框架凭借其强大的功能和广泛的社区支持,已成为软件开发领域的重要工具,适合初学者深入学习以奠定职业生涯基础。
105 1
|
3月前
|
API
【Azure Key Vault】.NET 代码如何访问中国区的Key Vault中的机密信息(Get/Set Secret)
【Azure Key Vault】.NET 代码如何访问中国区的Key Vault中的机密信息(Get/Set Secret)
|
3月前
|
存储 Linux 网络安全
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)
|
3月前
|
网络安全 API 数据安全/隐私保护
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)