SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编程时的疏忽,通过SQL语句,实现无帐号登录,甚至篡改数据库。

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions; namespace Core.Common
{
/// <summary>
/// SQL注入帮助
/// </summary>
public class SQLInjectionHelper
{
private const string StrKeyWord = @".*(select|insert|delete|from|count(|drop table|update|truncate|asc(|mid(|char(|xp_cmdshell|exec master|netlocalgroup administrators|:|net user|""|or|and).*";
private const string StrRegex = @"[-|;|,|/|(|)|[|]|}|{|%|@|*|!|']"; /// <summary>
/// 获取Post的数据
/// </summary>
public static bool ValidUrlPostData()
{
bool result = false; for (int i = ; i < HttpContext.Current.Request.Form.Count; i++)
{
result = ValidData(HttpContext.Current.Request.Form[i].ToString());
if (result)
{
LogHelper.Info("检测出POST恶意数据: 【" + HttpContext.Current.Request.Form[i].ToString() + "】 URL: 【" + HttpContext.Current.Request.RawUrl + "】来源: 【" + HttpContext.Current.Request.UserHostAddress + "】");
break;
}//如果检测存在漏洞
}
return result;
} /// <summary>
/// 获取QueryString中的数据
/// </summary>
public static bool ValidUrlGetData()
{
bool result = false; for (int i = ; i < HttpContext.Current.Request.QueryString.Count; i++)
{
result = ValidData(HttpContext.Current.Request.QueryString[i].ToString());
if (result)
{
LogHelper.Info("检测出GET恶意数据: 【" + HttpContext.Current.Request.QueryString[i].ToString() + "】 URL: 【" + HttpContext.Current.Request.RawUrl + "】来源: 【" + HttpContext.Current.Request.UserHostAddress + "】");
break;
}//如果检测存在漏洞
}
return result;
} /// <summary>
/// 验证是否存在注入代码
/// </summary>
/// <param name="inputData"></param>
public static bool ValidData(string inputData)
{
//里面定义恶意字符集合
//验证inputData是否包含恶意集合
if (Regex.IsMatch(inputData.ToLower(), GetRegexString()))
{
return true;
}
else
{
return false;
}
} /// <summary>
/// 获取正则表达式
/// </summary>
/// <param name="queryConditions"></param>
/// <returns></returns>
private static string GetRegexString()
{
//构造SQL的注入关键字符
string[] strBadChar =
{
"and"
,"exec"
,"insert"
,"select"
,"delete"
,"update"
,"count"
,"from"
,"drop"
,"asc"
,"char"
,"or"
,"%"
,";"
,":"
,"\'"
,"\""
,"-"
,"chr"
,"mid"
,"master"
,"truncate"
,"char"
,"declare"
,"SiteName"
,"net user"
,"xp_cmdshell"
,"/add"
,"exec master.dbo.xp_cmdshell"
,"net localgroup administrators"
}; //构造正则表达式
string str_Regex = ".*(";
for (int i = ; i < strBadChar.Length - ; i++)
{
str_Regex += strBadChar[i] + "|";
}
str_Regex += strBadChar[strBadChar.Length - ] + ").*"; return str_Regex;
} /// <summary>
/// 检测是否有Sql危险字符
/// </summary>
/// <param name="str">要判断字符串</param>
/// <returns>判断结果</returns>
public static bool IsSafeSqlString(string str)
{
return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
} /// <summary> SQL注入等安全验证
/// 检测是否有危险的可能用于链接的字符串
/// </summary>
/// <param name="str">要判断字符串</param>
/// <returns>判断结果</returns>
public static bool IsSafeUserInfoString(string str)
{
return !Regex.IsMatch(str, @"^\s*$|^c:\\con\\con$|[%,\*" + "\"" + @"\s\t\<\>\&]|游客|^Guest");
} #region 检测客户的输入中是否有危险字符串
/// <summary>
/// 检测客户输入的字符串是否有效,并将原始字符串修改为有效字符串或空字符串。
/// 当检测到客户的输入中有攻击性危险字符串,则返回false,有效返回true。
/// </summary>
/// <param name="input">要检测的字符串</param>
public static bool IsValidInput(ref string input)
{
try
{ //替换单引号
input = input.Replace("'", "''").Trim(); //检测攻击性危险字符串
string testString = "and |or |exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ";
string[] testArray = testString.Split('|');
foreach (string testStr in testArray)
{
if (input.ToLower().IndexOf(testStr) != -)
{
//检测到攻击字符串,清空传入的值
input = "";
return false;
}
} //未检测到攻击字符串
return true; }
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion
}
}

SQLInjectionHelper

C#工具:防sql注入帮助类的更多相关文章

  1. mysql之数据库连接的方法封装及防sql注入

    一.定义数据库和表 create database animal; CREATE TABLE `pet` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `name ...

  2. SpringBoot微服务电商项目开发实战 --- api接口安全算法、AOP切面及防SQL注入实现

    上一篇主要讲了整个项目的子模块及第三方依赖的版本号统一管理维护,数据库对接及缓存(Redis)接入,今天我来说说过滤器配置及拦截设置.接口安全处理.AOP切面实现等.作为电商项目,不仅要求考虑高并发带 ...

  3. C#防SQL注入代码的实现方法

    对于网站的安全性,是每个网站开发者和运营者最关心的问题.网站一旦出现漏洞,那势必将造成很大的损失.为了提高网站的安全性,首先网站要防注入,最重要的是服务器的安全设施要做到位. 下面说下网站防注入的几点 ...

  4. Sqlparameter防SQL注入

    一.SQL注入的原因 随着B/S模式应用开发的发展,使用这种模式编写应用程序的程序员也越来越多.但是由于这个行业的入门门槛不高,程序员的水平及经验也参差不齐,相当大一部分程序员在编写代码的时候,没有对 ...

  5. 【转载】C#防SQL注入过滤危险字符信息

    不过是java开发还是C#开发或者PHP的开发中,都需要关注SQL注入攻击的安全性问题,为了保证客户端提交过来的数据不会产生SQL注入的风险,我们需要对接收的数据进行危险字符过滤来防范SQL注入攻击的 ...

  6. 回头探索JDBC及PreparedStatement防SQL注入原理

    概述 JDBC在我们学习J2EE的时候已经接触到了,但是仅是照搬步骤书写,其中的PreparedStatement防sql注入原理也是一知半解,然后就想回头查资料及敲测试代码探索一下.再有就是我们在项 ...

  7. Java 防SQL注入过滤器(拦截器)代码

    原文出自:https://blog.csdn.net/seesun2012 前言 浅谈SQL注入:        所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符 ...

  8. golang 防SQL注入 基于反射、TAG标记实现的不定参数检查器

    收到一个任务,所有http的handler要对入参检查,防止SQL注入.刚开始笨笨的,打算为所有的结构体写一个方法,后来统计了下,要写几十上百,随着业务增加,以后还会重复这个无脑力的机械劳作.想想就l ...

  9. .Net防sql注入的方法总结

    #防sql注入的常用方法: 1.服务端对前端传过来的参数值进行类型验证: 2.服务端执行sql,使用参数化传值,而不要使用sql字符串拼接: 3.服务端对前端传过来的数据进行sql关键词过来与检测: ...

随机推荐

  1. C# 异步编程之 Task 的使用

    (说明:随笔内容为学习task的笔记,资料来源:https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task?redi ...

  2. [LeetCode] Maximize Distance to Closest Person 离最近的人的最大距离

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  3. swust oj 1012

    哈希表(链地址法处理冲突) 1000(ms) 10000(kb) 2542 / 6517 采用除留余数法(H(key)=key %n)建立长度为n的哈希表,处理冲突用链地址法.建立链表的时候采用尾插法 ...

  4. C语言复习3_条件结构

    if条件结构 if else 结构一般处理区间情况 #include <stdio.h> #include <stdlib.h> int main() { //打印剧情 dou ...

  5. xgboost 多gpu支持 编译

    xgboost 多gpu支持 编译 Ubuntu 18.04.2Linux 4.15.0-46-genericgcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0 cuda ...

  6. Tomcat线程池配置

    简介  线程池作为提高程序处理数据能力的一种方案,应用非常广泛.大量的服务器都或多或少的使用到了线程池技术,不管是用Java还是C++实现,线程池都有如下的特点:线程池一般有三个重要参数: 最大线程数 ...

  7. idea 启动调试模式总提示端口58346被占用问题

    在开发的时候,莫名其妙没法用jrebel调试模式启动了tomcat了,最开始以为是后台端口占用问题,然而把后台java程序全部关了都没用.仔细排查,根据提示发现是端口58346被占用了, 于是便在 w ...

  8. [Swift]LeetCode459. 重复的子字符串 | Repeated Substring Pattern

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  9. Storm学习笔记 - 消息容错机制

    Storm学习笔记 - 消息容错机制 文章来自「随笔」 http://jsynk.cn/blog/articles/153.html 1. Storm消息容错机制概念 一个提供了可靠的处理机制的spo ...

  10. 【Spark篇】---Spark中Action算子

    一.前述 Action类算子也是一类算子(函数)叫做行动算子,如foreach,collect,count等.Transformations类算子是延迟执行,Action类算子是触发执行.一个appl ...