SQL注入检测
目前只支持单个对象,不支持对象中包含对象
/// <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注入检测的更多相关文章
- 常见的SQL注入检测语句(转载)
0x00 前言 现在很多WAF都能拦截sqlmap.havij 等注入工具的发包注入,所以这时我们需要在浏览器上使用hackerbar 进行手工注入,或者说是手工绕过注入攻击 0x01 发现SQL 注 ...
- sqlmap和burpsuite绕过csrf token进行SQL注入检测
利用sqlmap和burpsuite绕过csrf token进行SQL注入 转载请注明来源:http://www.cnblogs.com/phoenix--/archive/2013/04/12/30 ...
- SQL注入检测方法
private bool CheckParams(params object[] args){ string[] Lawlesses={"=","'"}; if ...
- 利用sqlmap对网站进行sql注入检测
1.下载sqlmap github地址:https://github.com/sqlmapproject/sqlmap/zipball/master 2.sqlmap的运行环境需要python,这个网 ...
- 防止sql注入和sqlmap介绍
sql注入问题从WEB诞生到现在也一直没停过,各种大小公司都出现过sql注入问题,导致被拖库,然后存在社工库撞库等一系列影响. 防止sql注入个人理解最主要的就一点,那就是变量全部参数化,能根本的解决 ...
- 电子商务网站SQL注入项目实战一例
故事A段:发现整站SQL对外输出: 有个朋友的网站,由于是外包项目,深圳某公司开发的,某天我帮他检测了一下网站相关情况. 我查看了页面源代码,发现了个惊人的事情,竟然整站打印SQL到Html里,着实吓 ...
- MySQL SQL 注入
如果您通过网页获取用户输入的数据并将其插入一个MySQL数据库,那么就有可能发生SQL注入安全的问题. 本博文将为大家介绍如何防止SQL注入,并通过脚本来过滤SQL中注入的字符. 所谓SQL注入,就是 ...
- Go--避免SQL注入
避免SQL注入 什么是SQL注入 SQL注入攻击(SQL Injection),简称注入攻击,是Web开发中最常见的一种安全漏洞.可以用它来从数据库获取敏感信息,或者利用数据库的特性执行添加用户,导出 ...
- mysql sql注入
防止SQL注入,我们需要注意以下几个要点: 1.永远不要信任用户的输入.对用户的输入进行校验,可以通过正则表达式,或限制长度:对单引号和 双"-"进行转换等. 2.永远不要使用动态 ...
随机推荐
- Windows 独立启动方式安装 Archiva
在 Windows 中以独立启动方式安装. 你可以将安装文件拷贝到任何你希望运行的目录中,下面的步骤中.我们没有将 Archiva 安装成服务,所以你需要通过控制台的方式来进行启动. Windows ...
- laravel 资源篇
转自:https://github.com/qianyugang/learn-laravel # Learn-Laravel — 学习资料和开源项目集 ## Laravel 学习资料 ### 官方网站 ...
- laravel 实现增 与查
//调用模型层 <?phpnamespace App;use Illuminate\Support\Facades\DB;use Illuminate\Database\Eloquent\Mod ...
- redis基础学习总结
学习目标: 1.redis特点及安装 2.redis键值操作 3.redis数据类型[string, link,set,orderset,hash] 4.事务 5.消息 ...
- 枚举Enum 的常用方法
一.枚举介绍 通常定义常量方法和枚举定义常量方法区别 public class State { public static final int ON = 1; public static final ...
- Matlab_audiowrite_音频生成
输出音频文件所需函数为 audiowrite .通过例程进行解释: % 生成时间序列 fs = 5000; % [Hz] 信号采样频率 T = 1; % [s] 信号长度 x = 0:1/fs:T; ...
- 网易2018校招内推编程题-堆棋子-C++实现
链接:https://www.nowcoder.com/questionTerminal/27f3672f17f94a289f3de86b69f8a25b来源:牛客网 [编程题]堆棋子 热度指数:14 ...
- 成员变量位置获取url
- L2-016. 愿天下有情人都是失散多年的兄妹(深搜)*
L2-016. 愿天下有情人都是失散多年的兄妹 参考博客 #include<iostream> #include<cstdio> #include<cstring> ...
- 【Java】字节数组转换工具类
import org.apache.commons.lang.ArrayUtils; import java.nio.charset.Charset; /** * 字节数组转换工具类 */ publi ...