目前只支持单个对象,不支持对象中包含对象

    /// <summary>
/// 检查SQL中的非法字符
/// </summary>
public class SQLInjectionScanner
{
private static string sqlCheck; static SQLInjectionScanner()
{
if (string.IsNullOrEmpty(sqlCheck))
{
sqlCheck = "declare|exec|varchar|cursor|begin|open|drop|creat|select|truncate";
}
} /// <summary>
///
/// </summary>
/// <param name="functionName"></param>
/// <param name="args"></param>
public static void CheckForSQLInjection(string functionName, params object[] args)
{
int argIndex = -;
try
{
foreach (object item in args)
{
argIndex++;
Type T = item.GetType();
if (T.Name == "String")
{
CheckForSQLInjectionString(item.ToString());
}
else if (T.IsClass && !T.IsValueType)
{
CheckForSQLInjectionObject(item);
}
}
}
catch (ArgumentException ex)
{
string msg = string.Format("方法{0},{1}", functionName, ex.Message); //记录sql注入的日志
throw new ArgumentException(msg);
}
catch (Exception ex)
{
//记录日志
}
} /// <summary>
/// 字符串验证
/// </summary>
/// <param name="inputString"></param>
private static void CheckForSQLInjectionString(string inputString)
{
bool isSQLInjection = CheckForSQLInjectionProcess(inputString, sqlCheck);
if (isSQLInjection)
{
string msg = string.Format("参数有SQL攻击嫌疑,参数值:{0}", inputString);
throw new ArgumentException(msg);
}
} public static void CheckForSQLInjectionObject(object input)
{
Type t = input.GetType();
var ps = t.GetProperties();
//字段处理
//字段处理
FieldInfo[] fields = t.GetFields(BindingFlags.Public | BindingFlags.Instance);
foreach (FieldInfo fi in fields)
{
string temp = string.Empty;
if (fi.FieldType.Name == "String")
{
object userInput = fi.GetValue(input);
if (userInput != null)
{
bool isSQLInjection = CheckForSQLInjectionProcess(userInput.ToString(), sqlCheck); if (isSQLInjection)
{
string msg = string.Format("字段{0},参数有SQL攻击嫌疑,参数值:{1}", string.Concat(t.Name, ".", fi.Name), userInput.ToString());
throw new ArgumentException(msg);
}
}
}
} //属性处理
foreach (var pi in ps)
{
if (pi.PropertyType.Name == "String")
{
object userInput = pi.GetValue(input, null);
if (userInput != null)
{ bool isSQLInjection = CheckForSQLInjectionProcess(userInput.ToString(), sqlCheck); if (isSQLInjection)
{
string msg = string.Format("字段{0},参数有SQL攻击嫌疑,参数值:{1}", string.Concat(t.Name, ".", pi.Name), userInput.ToString());
throw new ArgumentException(msg);
}
}
}
Type tItem = pi.GetType(); }
} private static bool CheckForSQLInjectionProcess(string userInput, string sqlCheck)
{
bool isSQLInjection = false;
try
{
string[] sqlCheckList = sqlCheck.Split('|'); string CheckString = userInput.Replace("'", "''");
for (int i = ; i <= sqlCheckList.Length - ; i++)
{
if ((CheckString.IndexOf(sqlCheckList[i].Trim(),
StringComparison.OrdinalIgnoreCase) >= ))
{
isSQLInjection = true;
}
}
}
catch
{
isSQLInjection = false;
}
return isSQLInjection;
}
}

SQL注入检测的更多相关文章

  1. 常见的SQL注入检测语句(转载)

    0x00 前言 现在很多WAF都能拦截sqlmap.havij 等注入工具的发包注入,所以这时我们需要在浏览器上使用hackerbar 进行手工注入,或者说是手工绕过注入攻击 0x01 发现SQL 注 ...

  2. sqlmap和burpsuite绕过csrf token进行SQL注入检测

    利用sqlmap和burpsuite绕过csrf token进行SQL注入 转载请注明来源:http://www.cnblogs.com/phoenix--/archive/2013/04/12/30 ...

  3. SQL注入检测方法

    private bool CheckParams(params object[] args){ string[] Lawlesses={"=","'"}; if ...

  4. 利用sqlmap对网站进行sql注入检测

    1.下载sqlmap github地址:https://github.com/sqlmapproject/sqlmap/zipball/master 2.sqlmap的运行环境需要python,这个网 ...

  5. 防止sql注入和sqlmap介绍

    sql注入问题从WEB诞生到现在也一直没停过,各种大小公司都出现过sql注入问题,导致被拖库,然后存在社工库撞库等一系列影响. 防止sql注入个人理解最主要的就一点,那就是变量全部参数化,能根本的解决 ...

  6. 电子商务网站SQL注入项目实战一例

    故事A段:发现整站SQL对外输出: 有个朋友的网站,由于是外包项目,深圳某公司开发的,某天我帮他检测了一下网站相关情况. 我查看了页面源代码,发现了个惊人的事情,竟然整站打印SQL到Html里,着实吓 ...

  7. MySQL SQL 注入

    如果您通过网页获取用户输入的数据并将其插入一个MySQL数据库,那么就有可能发生SQL注入安全的问题. 本博文将为大家介绍如何防止SQL注入,并通过脚本来过滤SQL中注入的字符. 所谓SQL注入,就是 ...

  8. Go--避免SQL注入

    避免SQL注入 什么是SQL注入 SQL注入攻击(SQL Injection),简称注入攻击,是Web开发中最常见的一种安全漏洞.可以用它来从数据库获取敏感信息,或者利用数据库的特性执行添加用户,导出 ...

  9. mysql sql注入

    防止SQL注入,我们需要注意以下几个要点: 1.永远不要信任用户的输入.对用户的输入进行校验,可以通过正则表达式,或限制长度:对单引号和 双"-"进行转换等. 2.永远不要使用动态 ...

随机推荐

  1. CAD{绘制坡道)(绘制楼梯)5.26

    “楼梯其他”“坡道”编辑坡道的各项数据, 三维图中坡道反了.在平面图中镜像,“MI"镜像坡道.给坡道一个箭头引注, 绘制楼梯:”楼梯其他“”双跑楼梯“编辑参数,绘制楼梯,双击楼梯,改变成首层 ...

  2. 微信小程序中对于变量的定义

    在页面对应的js文件中: page顶部使用let定义变量,这是定义的全局变量,在当前脚本页面,任何函数中都可以直接使用变量名调用.如果做修改,就直接使用变量等于要更改的值. 使用const定义变量,就 ...

  3. 6——ThinkPhp中的请求:

    <?php namespace app\index\controller; use think\console\Input; use think\Controller; use think\Db ...

  4. C++解析十-数据封装

    数据封装 所有的 C++ 程序都有以下两个基本要素: 程序语句(代码):这是程序中执行动作的部分,它们被称为函数. 程序数据:数据是程序的信息,会受到程序函数的影响.封装是面向对象编程中把数据和操作数 ...

  5. python之路-----网络编程

    一:客户端/服务端j架构(C/S架构) 在生活中,最常见的C/S架构即是APP和网页服务器这个架构.Client和Server之间利用网络来进行通信,客户端和服务器端的程序不同,用户的程序主要在客户端 ...

  6. eclipse工具类及插件(设置注释模板)

    (摘抄原地址)https://blog.csdn.net/xb12369/article/details/79850476(设置注释模板) 首先打开Eclipse配置选项:Window->Pre ...

  7. LINK : fatal error LNK1104: cannot open file .exe' 重开application Experience 服务即可

    这是一个坑, , 答案五花八门这个解决了我的痛点.  就这样了.

  8. java中的线程问题(二)——线程的创建和用法。

    在java中一个类要当作线程来使用有两种方法. 1.继承Thread类,并重写run函数 2.实现Runnable接口,并重写run函数 因为java是单继承的,在某些情况下一个类可能已经继承了某个父 ...

  9. 面向对象编程其实很简单--python面向对象(初级篇)

    出处:http://www.cnblogs.com/wupeiqi/ 概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函 ...

  10. springcloud Eureka学习笔记

    最近在学习springcloud,抽空记录下学习笔记;主要记录Eureka的实现过程和高可用性的实现 Eureka是一个服务治理框架,它提供了Eureka Server和Eureka Client两个 ...