惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - foson

ASP.NET网络编程中常用到的27个函数集 将 ASP 转换为 ASP.NET(转载) MIS和ERP的区别 ViewState 的工作原理 Ajax程序设计入门 AJAX技术浅谈(转载) Asp.net与SQL一起打包部署安装 ASP.NET验证码的实现 关于权限控制(转载) 新的起点----中润基业 ASP.NET中的身份验证方式 两道SQL面试题目 C#中接口与类的区别? ASP.NET的Application、Session、Cookie、ViewState和Cache等变量的区别是什么? ASP.NET两个页面之间传递值的几种方式 .net 基本概念 DataSet与DataReader的区别 ADO 与ADO.NET两种数据访问方式区别? c#.net常用函数和方法集
C# 检查字符串,防SQL注入攻击
foson · 2006-04-28 · via 博客园 - foson

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;