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.永远不要使用动态 ...
随机推荐
- Orchard是如何呈现内容的
首先Orchard是一个建立在ASP.NET MVC框架上的CMS应用框架.Orchard在呈现内容的时候也遵循MVC的规律,也是通过Controller来处理Url请求并决定用那个View来呈现那种 ...
- hihocoder #1044 : 状态压缩·一 状压DP
http://hihocoder.com/problemset/problem/1044 可以看出来每一位的选取只与前m位有关,我们把每个位置起始的前m位选取状态看出01序列,就可以作为一个数字来存储 ...
- web 服务发布注意事项
1.在发布的时候首先查看服务器对外开放的端口,如果没有最好和客户进行沟通需要开放那些对应的端口,要不外界无法访问发布的站点. 2.在oracle需要远程控制服务器的数据库的时候需要开发1521端口.
- 基于Python——实现远程下载sftp文件(只下载.zip文件)
[背景]远程下载发布包等文件时,总是要使用WinSCP等工具登陆拖动.今天就介绍一种使用python下载文件到本地的方法. [代码实现] import paramiko # paramiko模块,基于 ...
- MySQL 排名、分组后组内排名、取各组的前几名 及排名后更新插入数据表中
一.排名 /*普通排名:从1开始,顺序往下排*/ AS rank ) r ORDER BY score; /*并列排名:相同的值是相同的排名*/ SELECT cs.* , CASE WHEN @p= ...
- Android 音视频深入 十五 FFmpeg 推流mp4文件(附源码下载)
源码地址https://github.com/979451341/Rtmp 1.配置RTMP服务器 这个我不多说贴两个博客分别是在mac和windows环境上的,大家跟着弄 MAC搭建RTMP服务器h ...
- Django的下载与项目的创建
一.Django的下载安装 Django官网下载页面 二.DOS窗口下的django项目从创建和启动 1. DOS窗口下载Django pip3 install django==1.11.9 2.DO ...
- LeetCode 47 全排列II
题目: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 与上一题相比,这题多了一 ...
- 野(wild)指针与悬空(dangling)指针
1. 什么是野指针(wild pointer)? A pointer in c which has not been initialized is known as wild pointer. 野指针 ...
- mysql中describe关键字
今天写代码的时候,有一个类的数据始终不能插入,老是提示在You have an error in your SQL syntax; check the manual that corresponds ...