求助.net防注入代码

网站经常被注入,请问谁有实用点的防注入代码呢?.NET和SQLSERVER数据库的,本人不懂,望哪位朋友指点下我啊,万分感谢啊,我都愁死了


在线等


没人帮我啊??


帮你顶
我也遇到过
等待解决

我目前的改版是
所有的数据库操作都使用 存储过程 或者 参数传递
再就是基本的字符过滤


在进行数据库操作的时候,比如像这样做就不容易被注入了:

......int id = 值;
SqlCommand cmd = new SqlCommand ("Select * From Table Where id = @id",con);
cmd.Parameters.AddWithValue("@id",id);
......

主要就是要:cmd.Parameters.AddWithValue("@id",id);这样的形式比较安全。。。


其实它就是字符串的过滤。。。这样比较好。。。


还有就是传入数据库时对数据库操作时把一些数据库操作的关键字如“Select”这些替换掉。。。这样就更好些了


up


有整段的过滤代码吗?一个个修改我还真不会啊


引用 4 楼 Schwann520 的回复:
在进行数据库操作的时候,比如像这样做就不容易被注入了: 

......int id = 值; 
SqlCommand cmd = new SqlCommand ("Select * From Table Where id = @id",con); 
cmd.Parameters.AddWithValue("@id",id); 
...... 

主要就是要:cmd.Parameters.AddWithValue("@id",id);这样的形式比较安全。。。


存储过程是最理想的。赞!


求助啊


简单一句话,参数,
呵呵,’
不过具体代码忘了



去论坛搜搜,之前有好几个帖子是关于这个的,在这里就不多说了。


1.字符过滤,如'过滤为''
2.参数化 即向数据库传递值时全部参数化,这样.net会为你做判断你传过来的值是否安全,并作相关操作
3.数据库使用存储过程


我就不信用三层结构,全存储过程,如何注入?
解放前的故事了,还注入!!


三层结构和注入没什么关系吧


嘿嘿..
sql语句使用参数了


各位高手,能不能具体点的代码有吗?我上找的三种不知大能不能用?具体怎么用,谢谢.Net相对以前的ASP程序而言,在防注入上其实还是有很大提高的,因为他有了数据类型之分,当你以数字类型做参数的时候,那么用户传递非数字的变量,系统直接就出错了。奈何一些新手们为了贪图方便,所有的类型都以String定义,以String定义也就罢了,为何查询数据库的时候还用字符串连接的方式呐?

  难怪那么多.Net写的网站还是被一次又一次的注入,站长们还一次又一次的头疼。下面这个代码我是临时想到的一个办法,一个可以暂时拦截初级Script Boy们用在Aspx网站的防注入代码,代码内有提示该加强的地方:


void Application_Beginrequest(object sender, EventArgs e)
{
string sqlstr = @"(and|exec|insert|select|delete|update|chr|mid|master|or|truncate|char|declare|join)".Replace("|",")|("); //这个地方明显的过滤不严格,可以很多方式绕过的。
for (int i = 0; i < Request.QueryString.Count; i++)
{
string str = Request.QueryString[i].ToString().ToLower();

if (System.Text.RegularExpressions.Regex.IsMatch(str, sqlstr))
{
Response.Redirect("/sqlerr.aspx");
}
}
for (int i = 0; i < Request.Form.Count; i++)
{
string str = Request.Form[i].ToString().ToLower();

if (System.Text.RegularExpressions.Regex.IsMatch(str, sqlstr))
{
Response.Redirect("/sqlerr.aspx");
}
}
}


本文来自: (yw7788.blog.hexun.com) 详细出处参考:http://yw7788.blog.hexun.com/27498821_d.html

是.NET的吗? 
和我之前遇到的问题一样,主要是程序上有漏洞, 
尽可能全的过滤SQL敏感的语句, 
先把数据库里面注入的代码替换掉, 
再在Global文件里里加入 
protected void Application_BeginRequest(Object sender, EventArgs e) 

//SQL防注入 
string Sql_1 = "exec|insert+|select+|delete+|update+|count|chr|mid|master+|truncate|char|declare|drop+|drop+table|creat+|creat+table";
string Sql_2 = "exec+|insert|insert+|delete+|update+|count(|count+|chr+|+mid(|+mid+|+master+|truncate+|char+|+char(|declare+|drop+|creat+|drop+table|creat+table";
string[] sql_c = Sql_1.Split('|'); 
string[] sql_c1 = Sql_2.Split('|'); 

if (Request.QueryString != null) 

foreach (string sl in sql_c) 

if (Request.QueryString.ToString().ToLower().IndexOf(sl.Trim()) >= 0) 

Response.Write("警告!你的IP已经被记录!不要使用敏感字符!");// 
Response.Write(sl); 
Response.Write(Request.QueryString.ToString()); 
Response.End(); 
break; 




if (Request.Form.Count > 0) 

string s1 = Request.ServerVariables["SERVER_NAME"].Trim();//服务器名称 
if (Request.ServerVariables["HTTP_REFERER"] != null) 

string s2 = Request.ServerVariables["HTTP_REFERER"].Trim();//http接收的名称 
string s3 = ""; 
if (s1.Length > (s2.Length - 7)) 

s3 = s2.Substring(7); 

else 

s3 = s2.Substring(7, s1.Length); 

if (s3 != s1) 

Response.Write("警告!你的IP已经被记录!不要使用敏感字符!");// 
Response.End(); 











防止sql注入,通常一个一个文件修改不仅麻烦而且还有漏掉的危险,下面我说一上如何从整个系统防止注入。

做到以下三步,相信的程序将会比较安全了,而且对整个网站的维护也将会变的简单。

一、数据验证类:
parameterCheck.cs 


public class parameterCheck{
public static bool isEmail(string emailString){
return System.Text.RegularExpressions.Regex.IsMatch(emailString, "['//w_-]+(//.['//w_-]+)*@['//w_-]+(//.['//w_-]+)*//.[a-zA-Z]{2,4}");
}
public static bool isInt(string intString){
return System.Text.RegularExpressions.Regex.IsMatch(intString ,"^(//d{5}-//d{4})|(//d{5})$"); //我这边是用"^(//d*)$"通过
}
public static bool isUSZip(string zipString){
return System.Text.RegularExpressions.Regex.IsMatch(zipString ,"^-[0-9]+$|^[0-9]+$");
}




二、Web.config

在你的Web.config文件中,在<appSettings>下面增加一个标签:如下

<appSettings>
<add key="safeParameters" value="OrderID-int32,CustomerEmail-email,ShippingZipcode-USzip" />
</appSettings> 

其中key是<saveParameters>后面的值为"OrderId-int32"等,其中"-"前面表示参数的名称比如:OrderId,后面的int32表示数据类型。

三、Global.asax

在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");
}



以后需要修改的时候我们只需要修改以上三个文件,对整个系统的维护将会大大提高效率,当然你可以根据自己的需要增加其它的变量参数和数据类型。



(1)使用储过程
(2)过虑一些特别字符.例如:',insert ,update,delete,turn等..
(3)最好也在数据库加一个安全的执行权限..在数据库做权限限制..
其实1和2已经可以了,如果你想做到更加安全,也加上3..


给你看代码吧:
public int Check(string username, string password)
{
int flag = 0;
Connection.Open();
SqlCommand sqlComm = new SqlCommand("select BlogID from Blog where Username = @Username and Password = @Password", Connection);
sqlComm.Parameters.Add(new SqlParameter("@Username", SqlDbType.NVarChar, 50));
sqlComm.Parameters.Add(new SqlParameter("@Password", SqlDbType.NVarChar, 50));
sqlComm.Parameters["@UserName"].Value = username;
sqlComm.Parameters["@Password"].Value = password;
SqlDataReader tmpReader = sqlComm.ExecuteReader();
if (tmpReader.Read())
{
flag = Convert.ToInt32(tmpReader["BlogID"].ToString());
}
tmpReader.Close();
Connection.Close();
return flag;
}



使用储过程 
过虑一些特别字符.例如:',insert ,update,delete,turn等.. 


引用 16 楼 H79469992 的回复:
if (Request.Form.Count > 0) 

string s1 = Request.ServerVariables["SERVER_NAME"].Trim();//服务器名称 
if (Request.ServerVariables["HTTP_REFERER"] != null) 

string s2 = Request.ServerVariables["HTTP_REFERER"].Trim();//http接收的名称 
string s3 = ""; 
if (s1.Length > (s2.Length - 7)) 

s3 = s2.Substring(7); 

else 

s3 = s2.Substring(7, s1.Length); 

if (s3 != s1) 

Response.Write("警告!你的IP已经被记录!不要使用敏感字符!");// 
Response.End(); 






为什么form提交还要用这种拦截方式呢?用替换不是更好吗?
我的做法是,get提交的用这种栏截方式,form提交的用替换,就是把危险字符替换掉,比如select我就换成SELECT。
我这 种做法有无不妥,其实我也不太确定,希望有高手能解答?


C# code

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





}

}






好好看看DBhelp去 
传参
传参传参传参传参
哪还有人写三层不用传参的啊~~~


1.查看和修改等的权限分离
2.过滤所有用户输入
3.把所有单独出现的单引号改成两个单引号,防止攻击者修改SQL命令
4.用存储过程来执行所有的查询
5.完善用户输入的的长度和类型等验证
6.检查用户输入的合法性
7.将用户登录名称、密码等数据加密保存


用Parameters方法最好。这方面的资料多的很。


1.推荐使用Parameter

2.如果要自己来做这件事,只需要做两步:1)如果是数字类型,则判断其是否为数字

2)如果是字符串,则替换一个单引号为两个单引号


1。建议使用sqlhelper.cs,配合使用参数。
2。尽可能使用储存过程。
3。输入验证,不要让用户随意乱输入。
4。页面访问时验证是否已经是合法用户,是否有权限。


推荐使用Parameter 




学习


SQL语句不要用拼接的方式,用传参数的方式


url 伪地址
sql语句 用存储过程


sql语句不要用拼接字符串 用传参的方式
例如
select name from stuInfo where stuId=@id
用这种形式


传参和替换敏感字符


学习来了


引用 21 楼 qq196260188 的回复:
C# codeusing System;

namespace web.comm
{
/** <summary>
/// ProcessRequest 的摘要说明。
/// </summary>
public class ProcessRequest
{
public ProcessRequest()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

SQL注入式攻击代码分析#region SQL注入式攻击代码分析
/** <summary>
//…

好东西,收藏了


UP


1:UI层过滤
2:DAL数据层过滤
3:全站(GLOABEL)


学习了...


16楼的我感觉比较全面的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值