C# 检查字符串,防SQL注入攻击
来源:互联网 发布:linux如何查看网络连接 编辑:程序博客网 时间:2024/05/21 11:01
SQL注入式攻击是利用是指利用设计上的漏洞,在目标服务器上运行Sql命令以及进行其他方式的攻击
动态生成Sql命令时没有对用户输入的数据进行验证是Sql注入攻击得逞的主要原因。
比如:
如果你的查询语句是select * from admin where username='"&user&"' and password='"&pwd&"'"
那么,如果我的用户名是:1' or '1'='1
那么,你的查询语句将会变成:
select * from admin where username='1 or '1'='1' and password='"&pwd&"'"
这样你的查询语句就通过了,从而就可以进入你的管理界面。
所以防范的时候需要对用户的输入进行检查。特别式一些特殊字符,比如单引号,双引号,分号,逗号,冒号,连接号等进行转换或者过滤。
需要过滤的特殊字符及字符串有:
net user
xp_cmdshell
/add
exec master.dbo.xp_cmdshell
net localgroup administrators
select
count
Asc
char
mid
'
:
"
insert
delete from
drop table
update
truncate
from
%
C# 检查字符串,防SQL注入攻击
这个例子里暂定为=号和'号
bool CheckParams(params object[] args)
{
string[] Lawlesses={"=","'"};
if(Lawlesses==null||Lawlesses.Length<=0)return true;
//构造正则表达式,例:Lawlesses是=号和'号,则正则表达式为 .*[=}'].* (正则表达式相关内容请见MSDN)
//另外,由于我是想做通用而且容易修改的函数,所以多了一步由字符数组到正则表达式,实际使用中,直接写正则表达式亦可;
string str_Regex=".*[";
for(int i=0;i< Lawlesses.Length-1;i++)
str_Regex+=Lawlesses[i]+"|";
str_Regex+=Lawlesses[Lawlesses.Length-1]+"].*";
//
foreach(object arg in args)
{
if(arg is string)//如果是字符串,直接检查
{
if(Regex.Matches(arg.ToString(),str_Regex).Count>0)
return false;
}
else if(arg is ICollection)//如果是一个集合,则检查集合内元素是否字符串,是字符串,就进行检查
{
foreach(object obj in (ICollection)arg)
{
if(obj is string)
{
if(Regex.Matches(obj.ToString(),str_Regex).Count>0)
return false;
}
}
}
}
return true;
- C# 检查字符串,防SQL注入攻击
- C# 检查字符串,防SQL注入攻击
- C#检查字符串,防SQL注入攻击
- C# 检查字符串,防SQL注入攻击
- C# 检查字符串,防SQL注入攻击
- C# 检查字符串,防SQL注入攻击
- C# 检查字符串,防SQL注入攻击
- C# 检查字符串,防SQL注入攻击
- C# 检查字符串,防SQL注入攻击
- C# 检查字符串,防SQL注入攻击
- C# 检查字符串,防SQL注入攻击
- C# 检查字符串,防SQL注入攻击
- C# 检查字符串,防SQL注入攻击
- C# 检查字符串,防SQL注入攻击
- 防SQL注入攻击
- JSP防SQL注入攻击
- SQL 防注入式攻击
- 防SQL注入式攻击
- SQL实现分页
- 设置Ubuntu上的MySQL可以远程访问
- JS特效,让人叹为观止(特效一)
- 两道SQL面试题目
- 如何重起设备
- C# 检查字符串,防SQL注入攻击
- 真实解析IP存储
- JS特效,让人叹为观止(特效二)
- GridView 自动增加序号
- Sql 日期输出的格式样式
- Linux进程实际内存占用: 私有驻留内存数(Private RSS)介绍
- 用WebRequest登录网站
- C# 装箱和拆箱、理论概念
- Sun科技日2008——OpenSolaris