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

    /// <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. Confluence 6 服务器硬件要求指南

    服务器管理员可以通过本页面的指南来对在运行 Confluence 评估版本的最小服务器硬件需求进行评估.应为实际的服务器负载是很难进行预测的,所以最好的办法是通过实际运行一个 Confluence 实 ...

  2. 创建spark_读取数据

    在2.0版本之前,使用Spark必须先创建SparkConf和SparkContext,不过在Spark2.0中只要创建一个SparkSession就够了,SparkConf.SparkContext ...

  3. 第7天【find命令、if语句、磁盘管理、文件系统管理】

    find命令详解(01)_recv Find及文件特殊权限: 文件查找工具: Find Find [OPTON]…[查找路径] [查找条件] [处理动作] 查找路径:指定具体目标路径,默认为当前路径 ...

  4. ES6学习笔记(二)—— 通过ES6 Module看import和require区别

    前言 说到import和require,大家平时开发中一定不少见,尤其是需要前端工程化的项目现在都已经离不开node了,在node环境下这两者都是大量存在的,大体上来说他们都是为了实现JS代码的模块化 ...

  5. WebSphere,WebLogic,Tomcat,IIS

    Weblogic.Tomcat 都基于java的基础架构 Weblogic是一个企业级的应用服务器,其中包括j2ee中的各类应用如jsp,servlet,ejb等, Tomcat是一个初级的应用服务器 ...

  6. vue项目中的tab页实现

    //需要自己弄雪碧图 <template> <div class="tab" id="tab"> <router-link to= ...

  7. PHP写的验证码类

    class Captcha { private $width; private $height; private $codeNum; private $code; private $im; funct ...

  8. Delphi 10.3.1 Secure File Sharing解决应用间文件共享

    Delphi 10.3.1 为Android项目提供了Secure File Sharing选择项,默认是False.这一项是设置什么呢? 原来,Android 7及以后的版本,为了加强OS的安全性, ...

  9. mac搭配Nginx服务器常见问题

    推流服务器主要是使用了开源的nginx和rtmp模块,网上也有很多资料,不过对有些可能出现的问题没有很好的总结. 安装brew 使用Mac进行开发很久的老司机应该对这个工具很熟悉了.brew是一个第三 ...

  10. Python全栈之路----常用模块----os模块

    os模块提供了很多允许你的程序和操作系统直接交互的功能 得到当前工作目录,即当前Python脚本工作的目录路径:os.getcwd() #test.py import os print(os.getc ...