网站注入与防范的方法总结(二)

6.最重要的还是程序的写法,用参数化SQL或存储过程

protected void cmdok_Click(object sender, EventArgs e)
    {
       
//添加信息
        StringBuilder  sql = new StringBuilder( " insert into m_phone ( pid,PhoneName,num,price,phonetype,onSellTime,color,weight,Video,Camera,phoneSize,phoneSystem,Memorysize,PhoneDesc,Standbytime,ScreenSize,Frequency,InputMethod,Soundrecord,gps,fm,mp3,email,Infrared,game,clock,Calendar,Calculator,Bluetooth)  ");
        sql.Append(
" values (@pid,@TextPhoneName,@Textnum,@Textprice,@Dropphonetype2,@TextonSellTime,@Textcolor,@Textweight ");
        .................
        SqlParameter[] paras
= { new SqlParameter("@pid", SqlDbType.Int, 4) ,
           
new SqlParameter("@TextPhoneName", SqlDbType.NVarChar, 50) ,
           
new SqlParameter("@Textnum", SqlDbType.Int, 4) ,
           
new SqlParameter("@Textprice", SqlDbType.Int, 4) ,
           
new SqlParameter("@Dropphonetype2", SqlDbType.VarChar, 20) ,
           
new SqlParameter("@TextonSellTime", SqlDbType.DateTime, 8) ,
           
new SqlParameter("@Textcolor", SqlDbType.VarChar, 20) ,
           
new SqlParameter("@Textweight", SqlDbType.NVarChar, 50) ,
           ...........
        };
       
string[] stra = {Dropphonetype.SelectedValue,TextPhoneName.Text , Textnum.Text, Textprice.Text, Dropphonetype2.SelectedValue, TextonSellTime.Text, Textcolor.Text, Textweight.Text,
            .............};
       
int a=stra.Length;
       
int j;     
       
for ( j = 0; j < a; j++)
        {
            paras[j].Value
= stra[j];        
        }
       
int strpid = 0;
       
string sqla = sql.ToString();
       
try
        {
            SqlHelper.ExcuteNonQurey(sqla, CommandType.Text, paras);
//执行添加数据          
            strpid
= Convert.ToInt32(SqlHelper.ExcuteSclar(sqla, CommandType.Text, paras));  //获取刚才插入的id号
        }
       
catch (SqlException ex)
        {
            cmdreturn.Text
= ex.Message.ToString();
        }
        cmdreturn.Text
= strpid.ToString();
。。。。。。。。。

 

7.通过URL传递的参数要用加密解密

string szTmp = "safdsfdsafdsfytrsd";
szTmp
= Server.UrlEncode(szTmp);
接收
STRING STRA
=Server.UrlDecode(request.querystring(szTmp));

 

8.把要使用的参数处理一下单引号,再放到SQL里面  
  例如 string stra=aa.replace("'","''")
  用参数化SQL可以不用处理单引号
  指定参数类型和过滤掉单引号,就可以杜绝99.9%入侵了
另外说一句:网上那些被人奉如圣经的过滤 update insert 等关键字的程序是用处不大的 upupdatedate 过滤掉 update还是update
还会造成不必要的麻烦

 

 

posted on 2010-06-28 16:18  Alone★民  阅读(133)  评论(0)    收藏  举报