C#防SQL注入代码的三种方法
对于网站的安全性,是每个网站开发者和运营者最关心的问题。网站一旦出现漏洞,那势必将造成很大的损失。为了提高网站的安全性,首先网站要防注入,最重要的是服务器的安全设施要做到位。
下面说下网站防注入的几点要素。
一:丢弃sql语句直接拼接,虽然这个写起来很快很方便。
二:如果用sql语句,那就使用参数化,添加param
三:尽可能的使用存储过程,安全性能高而且处理速度也快
四:屏蔽sql,javascript等注入(很是主要的),对于每个文件写是不太可能的。所以要找到对所有文件起作用的办法。我在网上收集了以下3种方法
c#防sql注入方法一
在web.config文件中, < appsettings>下面增加一个标签:如下
< appsettings>
< add key="safeparameters" value="orderid-int32,customeremail-email,shippingzipcode-uszip" />
< /appsettings>
其中key是 < saveparameters>后面的值为"orderid-int32"等,其中"-"前面表示参数的名称比如:orderid,后面的int32表示数据类型。
c#防sql注入方法二
在global.asax中增加下面一段:
protected void application_beginrequest(object sender, eventargs e){
string[] safeparameters = system.configuration.configurationsettings.appsettings["safeparameters"].tostring()。split(',');
for(int i= 0 ;i < safeparameters.length; i++){
string parametername = safeparameters[i].split('-')[0];
string parametertype = safeparameters[i].split('-')[1];
isvalidparameter(parametername, parametertype);
}
}
public void isvalidparameter(string parametername, string parametertype){
string parametervalue = request.querystring[parametername];
if(parametervalue == null) return;
if(parametertype.equals("int32")){
if(!parametercheck.isint(parametervalue)) response.redirect("parametererror.aspx");
}
else if (parametertype.equals("uszip")){
if(!parametercheck.isuszip(parametervalue)) response.redirect("parametererror.aspx");
}
else if (parametertype.equals("email")){
if(!parametercheck.isemail(parametervalue)) response.redirect("parametererror.aspx");
}
}
c#防sql注入方法三
使用字符串过滤类
using system;
namespace web.comm
{
/**//// < summary>
/// processrequest 的摘要说明。
/// < /summary>
public class processrequest
{
public processrequest()
{
//
// todo: 在此处添加构造函数逻辑
//
}
sql注入式攻击代码分析#region sql注入式攻击代码分析
/**//// < summary>
/// 处理用户提交的请求
/// < /summary>
public static void startprocessrequest()
{
// system.web.httpcontext.current.response.write("< script>alert('dddd');< /script>");
try
{
string getkeys = "";
//string sqlerrorpage = system.configuration.configurationsettings.appsettings["customerrorpage"].tostring();
if (system.web.httpcontext.current.request.querystring != null)
{
for(int i=0;i< system.web.httpcontext.current.request.querystring.count;i++)
{
getkeys = system.web.httpcontext.current.request.querystring.keys[i];
if (!processsqlstr(system.web.httpcontext.current.request.querystring[getkeys],0))
{
//system.web.httpcontext.current.response.redirect (sqlerrorpage+"?errmsg=sqlserver&sqlprocess=true");
system.web.httpcontext.current.response.write("< script>alert('请勿非法提交!');history.back();< /script>");
system.web.httpcontext.current.response.end();
}
}
}
if (system.web.httpcontext.current.request.form != null)
{
for(int i=0;i< system.web.httpcontext.current.request.form.count;i++)
{
getkeys = system.web.httpcontext.current.request.form.keys[i];
if (!processsqlstr(system.web.httpcontext.current.request.form[getkeys],1))
{
//system.web.httpcontext.current.response.redirect (sqlerrorpage+"?errmsg=sqlserver&sqlprocess=true");
system.web.httpcontext.current.response.write("< script>alert('请勿非法提交!');history.back();< /script>");
system.web.httpcontext.current.response.end();
}
}
}
}
catch
{
// 错误处理: 处理用户提交信息!
}
}
/**//// < summary>
/// 分析用户请求是否正常
/// < /summary>
/// < param name="str">传入用户提交数据< /param>
/// < returns>返回是否含有sql注入式攻击代码< /returns>
private static bool processsqlstr(string str,int type)
{
string sqlstr;
if(type == 1)
sqlstr = "exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ";
else
sqlstr = "'|and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare";
bool returnvalue = true;
try
{
if (str != "")
{
string[] anysqlstr = sqlstr.split('|');
foreach (string ss in anysqlstr)
{
if (str.indexof(ss)>=0)
{
returnvalue = false;
}
}
}
}
catch
{
returnvalue = false;
}
return returnvalue;
}
#endregion
}
}
上一篇: Java中从键盘输入多个整数的方法