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

    /// <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. OpenStack笔记

    *********virsh xml文件解读****************************** https://libvirt.org/format.html https://libvirt ...

  2. ajax上传下载自定义圆形滚动条

    demo地址:https://pan.baidu.com/s/1qYNYCFE 因为项目需要在上传文件的时候,知道上传的进度,所以封装了一个方法,在上传文件的时候动态显示上传进度,主要根据XMLHtt ...

  3. mqtt------ mosca服务器端参数简介

    一:服务器端 为什么使用mosca:mosca是基于node.js开发,上手难度相对较小,其次协议支持完整,除了不支持Qos 2,其它的基本都支持.持久化支持redis以及mongo.二次开发接口简单 ...

  4. OpenRASP安装使用教程

    一.说明 1.1 RASP和WAF的区别 WAF,Web Application Firewall,应用防火墙.其原理是拦截原始http数据包,然后使用规则对数据包进行匹配扫描,如果没有规则匹配上那就 ...

  5. java子类继承父类的方法(代码简略版)

    父类:public class Subjects { public void b() { System.out.println("学科"); } public void a(){ ...

  6. sql 中取整,四舍五入取整,向下取整,向上取整。

    SELECT round(52.45, 0) AS round4, round(52.54, 0) AS round5, round(52.45, 1) AS round41, round(52.54 ...

  7. python批量插入mysql数据库(性能相关)以及反引号的使用

    参考link: https://blog.csdn.net/qq_35958094/article/details/78462800(插入相关) https://www.cnblogs.com/hya ...

  8. LeetCode 47 全排列II

    题目: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 与上一题相比,这题多了一 ...

  9. open live writer安装教程和账号配置

    第一步:Open Live Writer软件下载.官方地址:http://openlivewriter.org/ 第二步:双击安装文件(OpenLiveWriterSetup.exe),然后点击下一步 ...

  10. css 文本超出范围显示省略号

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...