反SQL注入攻击,特殊字符与特殊关键字过滤。

说到SQL注入攻击,我想大家都并不陌生,甚至深爱其害.那怎么才避免遭受攻击,减少伤害呢.在此我提出几点建议:
一.尽量使用参数形式,少用拼凑型SQL语句.这不但安全,还增加代码可读性,何乐不为呢!
ExpandedBlockStart.gif ContractedBlock.gif SqlParameter[] parms  =   new  SqlParameter[]  dot.gif {
InBlock.gif                    
new SqlParameter(PARM_EMAIL, SqlDbType.VarChar, 80),
InBlock.gif                    
new SqlParameter(PARM_FIRST_NAME, SqlDbType.VarChar, 80),
InBlock.gif                    
new SqlParameter(PARM_LAST_NAME, SqlDbType.VarChar, 80),
InBlock.gif                    
new SqlParameter(PARM_ADDRESS1, SqlDbType.VarChar, 80),
InBlock.gif                    
new SqlParameter(PARM_ADDRESS2, SqlDbType.VarChar, 50),
InBlock.gif                    
new SqlParameter(PARM_CITY, SqlDbType.VarChar, 80),
InBlock.gif                    
new SqlParameter(PARM_STATE, SqlDbType.VarChar, 80),
InBlock.gif                    
new SqlParameter(PARM_ZIP, SqlDbType.VarChar, 50),
InBlock.gif                    
new SqlParameter(PARM_COUNTRY, SqlDbType.VarChar, 50),
InBlock.gif                    
new SqlParameter(PARM_PHONE, SqlDbType.VarChar, 80),
ExpandedBlockEnd.gif                    
new SqlParameter(PARM_USER_ID, SqlDbType.VarChar, 80)}
;
二.转换参数类型.如是参数是数字型,则转换成数字型的才操作.
三.替换特殊字符.如将"'"替换成""".
四.条件可以的话,请使用存储过程操作数据库.
五.等等,当然安全要做的事,远远不止这些..

在这里我还要再发布自已用过的,用于参数及表单过滤的一个类.请大家自已扩展
None.gif using  System;
None.gif
using  System.Text.RegularExpressions;
None.gif
using  Microsoft.VisualBasic;
None.gif
None.gif
namespace  zhang.Common
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class AntiSqlInAttack
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public System.Web.HttpRequest request;
InBlock.gif
InBlock.gif        
public AntiSqlInAttack(System.Web.HttpRequest request)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.request = request;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool CheckBadQuery()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//整串字符对比方法
InBlock.gif            
//string badword = ";|'|*|%| and |20%and20%| master |20%master20%|exec|insert|select|delete|count|chr|mid|truncate|char|declare|update";
InBlock.gif            
//string query = request.ServerVariables["Query_String"].ToString();
InBlock.gif            
//string[] badwordArry = badword.Split(new char[] { '|' });
InBlock.gif            
//for (int i = 0; i < badwordArry.Length; i++)
InBlock.gif            
//{
InBlock.gif            
//    string tempWord = badwordArry[i].Trim();
InBlock.gif            
//    if (query.IndexOf(tempWord) >= 1)
InBlock.gif            
//        return true;
InBlock.gif            
//}
InBlock.gif            
//return false;
InBlock.gif
            if (request.QueryString.Count != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for (int i = 0; i < request.QueryString.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (CheckBadWord(request.QueryString[i].ToString()))
InBlock.gif                        
return true;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool CheckBadForm()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (request.Form.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for (int i = 0; i < request.Form.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (CheckBadWord(request.Form[i]))
InBlock.gif                        
return true;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool CheckBadWord(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string pattern = @"select|insert|delete|from|count\(|drop table|update|truncate|asc\(|mid\(|char\(|xp_cmdshell|exec   master|netlocalgroup administrators|:|net user|""|or|and";
InBlock.gif            
if (Regex.IsMatch(str, pattern, RegexOptions.IgnoreCase) || Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']"))
InBlock.gif                
return true;
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 反SQL注入
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void AntiSqlInjectionAttack()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (CheckBadQuery() || CheckBadForm())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string msg = string.Empty;
InBlock.gif                msg 
+= "<span style='font-size:12px;'>非法操作!系统做了如下记录!<br>";
InBlock.gif                msg 
+= "操作IP:" +Utils.GetRealIP()+ "<br>";
InBlock.gif                msg 
+= "操作时间:" + DateTime.Now + "<br>";
InBlock.gif                msg 
+= "页面:" + request.ServerVariables["URL"].ToLower() + "<br>";
InBlock.gif                msg 
+= "<a href=\"#\" οnclick=\"history.back()\">返回上一页</a></span>";
InBlock.gif                MessageBox.ResponseWrite(msg, 
true);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

至强工作室 http://www.haotaoci.com

转载于:https://www.cnblogs.com/zhang/archive/2007/07/25/830267.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值