首页 新闻 赞助 找找看

.NET 防注入

0
悬赏园豆:10 [已关闭问题] 关闭于 2013-09-11 08:56

我在使用一个通用防注入的类。这个类就是过滤一些关键字,但是我新闻里面如果有这些关键字就会“误杀”。请问有没有办法解决这个问题!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


public class cedar : IHttpModule
{
public void Dispose()
{
throw new NotImplementedException();
}

public void Init(HttpApplication context)
{
context.AcquireRequestState += new EventHandler(application_AcquireRequestState);
}
private void application_AcquireRequestState(object sender, EventArgs e)
{
HttpContext content = ((HttpApplication)sender).Context;
try
{
string sqlErrorPage = "sqlErrorPage.html";//转到默认页面
string keyValue = string.Empty;
string requestUrl = content.Request.Path.ToString();
if (content.Request.QueryString != null)
{
foreach (string val in content.Request.QueryString)
{
keyValue = content.Server.UrlDecode(content.Request.QueryString[val]);
if (!processSqlStr(keyValue))
{
content.Response.Write("<script>alert('请不要提交非法字符')</script>");
content.Response.End();
break;
}
}
}
if (content.Request.Form != null)
{
foreach (string val in content.Request.Form)
{
keyValue = content.Server.HtmlDecode(content.Request.Form[val]);
if (keyValue == "_ViEWSTATE") continue;
if (!processSqlStr(keyValue))
{
content.Response.Write("<script>alert('请不要提交非法字符')</script>");
content.Response.End();
break;
}
}
}
}
catch (Exception ex)
{
}
}

private bool processSqlStr(string str)
{
bool returnValue = true;
try
{
if (str.Trim() != "")
{
//取得webconfig中过滤字符串
// string sqlStr = ConfigurationManager.AppSettings["FilterSql"].Trim();
string sqlStr = "'|declare|exec|varchar|cursor|begin|open|drop|creat|select|truncate|Execute";
string[] sqlStrs = sqlStr.Split('|');
foreach (string ss in sqlStrs)
{
if (str.ToLower().IndexOf(ss) >= 0)
{
sqlStr = ss;
returnValue = false;
break;
}
}
}
}
catch
{
returnValue = false;
}
return returnValue;
}
}

hamigua的主页 hamigua | 初学一级 | 园豆:4
提问于:2013-09-04 20:25
< >
分享
所有回答(6)
0

把文本中使用的这些字符转义以后再存到数据库去

sinhbv | 园豆:2579 (老鸟四级) | 2013-09-04 20:37
0

你过滤单引号和--就OK了。。

56180825 | 园豆:1756 (小虾三级) | 2013-09-04 20:43

但是比如你一篇文章里面有 单引号 咋办呢?

支持(0) 反对(0) hamigua | 园豆:4 (初学一级) | 2013-09-04 20:46

@hamigua: 替换为2个单引号,你还不如直接用参数方式

支持(0) 反对(0) 56180825 | 园豆:1756 (小虾三级) | 2013-09-04 21:20
0

下载cyq.data的开源版本v4.55里面就有防sql注入函数~~~

路过秋天 | 园豆:4787 (老鸟四级) | 2013-09-04 21:15
0

直接参数化sql语句,何必去过滤呢

幻天芒 | 园豆:37175 (高人七级) | 2013-09-04 23:12

参数化查询能够过滤<div>这种样式注入吗?

支持(0) 反对(0) hamigua | 园豆:4 (初学一级) | 2013-09-05 10:55

@hamigua: 你这算是注入吗?你是指XSS吧,这个用编码就可以搞定了。

支持(0) 反对(0) 幻天芒 | 园豆:37175 (高人七级) | 2013-09-05 13:22
0

同意楼上的,建议使用ORM或参数化SQL,不要拼字符串,拼字符串不可能完全避免SQL注入

空明流光 | 园豆:106 (初学一级) | 2013-09-05 08:42

参数化查询能够过滤<div>这种样式注入吗?

支持(0) 反对(0) hamigua | 园豆:4 (初学一级) | 2013-09-05 10:55
0

<div>这种 要转意,不是过滤,HttpServerUtility.HtmlEncode 

geass.. | 园豆:1821 (小虾三级) | 2013-09-05 15:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册