防范qurestring方式的sql注入的一个方法

public static string safeRequest(string str)
    {
        
string outStr = null;
        
object querStr = HttpContext.Current.Request.QueryString[str];
        
if (querStr != null)
        {
            outStr 
= InputText(querStr.ToString(), 30);
            
return outStr;
        }
        
else
            
return outStr;
    }
    
public static string InputText(string inputString, int maxLength)
    {
        System.Text.StringBuilder retVal 
= new System.Text.StringBuilder();
        
// check incoming parameters for null or blank string
        if ((inputString != null&& (inputString != String.Empty))
        {
            inputString 
= inputString.Trim();
            
//op the string incase the client-side max length
            
//fields are bypassed to prevent buffer over-runs
            if (inputString.Length > maxLength)
                inputString 
= inputString.Substring(0, maxLength);
            
//convert some harmful symbols incase the regular
            
//expression validators are changed
            for (int i = 0; i < inputString.Length; i++)
            {
                
switch (inputString[i])
                {
                    
case '"':
                        retVal.Append(
"&quot;");
                        
break;
                    
case '<':
                        retVal.Append(
"&lt;");
                        
break;
                    
case '>':
                        retVal.Append(
"&gt;");
                        
break;
                    
default:
                        retVal.Append(inputString[i]);
                        
break;
                }
            }
            
// Replace single quotes with white space
            retVal.Replace("'"" ");
            retVal.Replace(
";"" ");
            retVal.Replace(
"insert""");
            retVal.Replace(
"select""");
            retVal.Replace(
"delete""");
            retVal.Replace(
"update""");
            retVal.Replace(
"drop""");
            retVal.Replace(
"create""");
            retVal.Replace(
"alter""");
            retVal.Replace(
" ""20%");
            retVal.Replace(
"xp_cmdshell""");
            retVal.Replace(
"xp_regaddmultistring""");
            retVal.Replace(
"xp_regdeletekey""");
            retVal.Replace(
"xp_regdeletevalue""");
            retVal.Replace(
"xp_regenumkeys""");
            retVal.Replace(
"xp_regenumvalues""");
            retVal.Replace(
"xp_regread""");
            retVal.Replace(
"xp_regremovemultistring""");
            retVal.Replace(
"xp_regwrite""");
            retVal.Replace(
"sp_OACreate""");
            retVal.Replace(
"sp_OADestroy""");
            retVal.Replace(
"sp_OAMethod""");
            retVal.Replace(
"sp_OAGetProperty""");
            retVal.Replace(
"sp_OASetProperty""");
            retVal.Replace(
"sp_OAGetErrorInfo""");
            retVal.Replace(
"sp_OAStop""");
        }
        
return retVal.ToString();
    }
posted @ 2008-05-07 14:31 zhangsir 阅读(21) 评论(1)  编辑 收藏

  回复  引用  查看    
#1楼 2008-05-07 14:33 | 丁学      
其实只要把空格全部过滤掉,就算是大罗神仙,也救不了他

标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
 
另存  打印