脏字过滤问题,求高手

microsoftAnson 2010-04-13 01:08:45
比如在QQ空间,博客,发表日志,怎么过滤脏字?

写一个方法 把文本框进行绑定。

<asp:TextBox ID="TextBox1" onFocus='$(this).animate({ height: "65px"}, 200);$("#imgchange").fadeIn("fast");' Style="behavior: url(#default#savehistory); overflow:auto;" Width="400px"
Height="40" runat="server" TextMode="MultiLine" Text='<%# FilterBadWords(Eval("dynamicContent").ToString())%>' onkeyup="checkinputtext();"></asp:TextBox>

求高手,详解。急...
...全文
574 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
BEN254422571 2010-04-14
  • 打赏
  • 举报
回复

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Kingar.Web.Control
{
/// <summary>
/// 防SQL注入检查器
/// </summary>
public class SqlChecker
{
//当前请求对象
private HttpRequest request;
//当前响应对象
private HttpResponse response;
//安全Url,当出现Sql注入时,将导向到的安全页面,如果没赋值,则停留在当前页面
private string safeUrl = String.Empty;
//Sql注入时,可能出现的sql关键字,可根据自己的实际情况进行初始化,每个关键字由'|'分隔开来
private const string StrKeyWordAdd = @"select|insert|delete|from|count(|drop table|update|truncate|asc(|mid(|char(|xp_cmdshell|exec master|<script|netlocalgroup administrators|net user|""|or|and";
private const string StrKeyWord = @"* from|drop table|truncate|exec master|netlocalgroup administrators|net user";
//Sql注入时,可能出现的特殊符号,,可根据自己的实际情况进行初始化,每个符号由'|'分隔开来
private const string StrRegexAdd = @"-|;|,|/|(|)|[|]|}|{|%|@|*|!|'";
private const string StrRegex = @"'1= 1|'--|'=|'or 1=1|'select|'insert|'delete|'update|'exec|' 1= 1|' --|' =|' or 1=1|' select|' insert|' delete|' update|' exec|'| '|script";



//select|insert|delete|union |union%20|union%09|join%09|join%20

public SqlChecker()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

/// <summary>
/// 由此构造函数创建的对象,在验证Sql注入之后将停留在原来页面上
/// </summary>
/// <param name="_request">当前请求的 Request 对象</param>
/// <param name="_response">当前请求的 Response 对象</param>
public SqlChecker(HttpRequest _request, HttpResponse _response)
{
this.request = _request;
this.response = _response;
}

/// <summary>
/// 由此构造函数创建的对象,在验证Sql注入之后将请求将导向由 _safeUrl 指定的安全url页面上
/// </summary>
/// <param name="_request">当前请求的 Request 对象</param>
/// <param name="_response">当前请求的 Response 对象</param>
/// <param name="_safeUrl">验证Sql注入之后将导向的安全 url</param>
public SqlChecker(HttpRequest _request, HttpResponse _response, string _safeUrl)
{
this.request = _request;
this.response = _response;
this.safeUrl = _safeUrl;
}

/// <summary>
/// 只读属性 SQL关键字
/// </summary>
public string KeyWord
{
get
{
return StrKeyWord;
}
}
/// <summary>
/// 只读属性过滤特殊字符
/// </summary>
public string RegexString
{
get
{
return StrRegex;
}
}
/// <summary>
/// 当出现Sql注入时需要提示的错误信息(主要是运行一些客户端的脚本)
/// </summary>
public string Msg
{
get
{
string msg = "<script type='text/javascript'> "
+ " alert('请勿输入非法字符!'); ";

if (this.safeUrl == String.Empty)
msg += " window.location.href = '" + request.RawUrl + "'";
else
msg += " window.location.href = '" + safeUrl + "'";

msg += "</script>";

return msg;
}
}
/// <summary>
/// 检查URL参数中是否带有SQL注入的可能关键字。
/// </summary>
/// <returns>存在SQL注入关键字时返回 true,否则返回 false</returns>
public bool CheckRequestQuery(int flag)
{
bool result = false;

if (request.QueryString.Count != 0)
{
//若URL中参数存在,则逐个检验参数。
foreach (string queryName in this.request.QueryString)
{
//过虑一些特殊的请求状态值,主要是一些有关页面视图状态的参数
if (queryName == "__VIEWSTATE" || queryName == "__EVENTVALIDATION")
continue;

//开始检查请求参数值是否合法
if (CheckKeyWord(request.QueryString[queryName], flag))
{
//只要存在一个可能出现Sql注入的参数,则直接退出
result = true;
break;
}
}
}

return result;
}

/// <summary>
/// 检查提交表单中是否存在SQL注入的可能关键字
/// </summary>
/// <returns>存在SQL注入关键字时返回 true,否则返回 false</returns>
public bool CheckRequestForm(int flag)
{
bool result = false;

if (request.Form.Count > 0)
{
//若获取提交的表单项个数不为0,则逐个比较参数
foreach (string queryName in this.request.Form)
{
//过虑一些特殊的请求状态值,主要是一些有关页面视图状态的参数
if (queryName == "__VIEWSTATE" || queryName == "__EVENTVALIDATION")
continue;

//开始检查提交的表单参数值是否合法
if (CheckKeyWord(request.Form[queryName], flag))
{
//只要存在一个可能出现Sql注入的参数,则直接退出
result = true;
break;
}
}
}

return result;
}

/// <summary>
/// 检查_sword是否包涵SQL关键字
/// </summary>
/// <param name="_sWord">需要检查的字符串</param>
/// <returns>存在SQL注入关键字时返回 true,否则返回 false</returns>
public bool CheckKeyWord(string _sWord, int flag)
{
bool result = false;
//模式1 : 对应Sql注入的可能关键字
string[] patten1 = null;
//模式2 : 对应Sql注入的可能特殊符号
string[] patten2 = null;
if (flag == 1)
{
patten1 = StrKeyWord.Split('|');
patten2 = StrRegex.Split('|');
}
else if (flag == 2)
{
patten1 = StrKeyWordAdd.Split('|');
patten2 = StrRegexAdd.Split('|');
}
//开始检查 模式1:Sql注入的可能关键字 的注入情况
foreach (string sqlKey in patten1)
{
if (_sWord.IndexOf(" " + sqlKey) >= 0 || _sWord.IndexOf(sqlKey + " ") >= 0)
{
//只要存在一个可能出现Sql注入的参数,则直接退出
result = true;
break;
}
}

//开始检查 模式1:Sql注入的可能特殊符号 的注入情况
foreach (string sqlKey in patten2)
{
if (_sWord.IndexOf(sqlKey) >= 0)
{
//只要存在一个可能出现Sql注入的参数,则直接退出
result = true;
break;
}
}

return result;
}

/// <summary>
/// 执行Sql注入验证
/// </summary>
/// <param name="flag">检测模式,1普通检测,2高级检察</param>
public void Check(int flag)
{
if (CheckRequestQuery(flag) || CheckRequestForm(flag))
{
response.Write(Msg);
response.End();
}
}
}

}



使用 SqlChecker.Check(1); 最好在Global.asax文件的
protected void Application_BeginRequest(object sender, EventArgs e)
{
SqlChecker.Check(1);
}
方法中使用
blank223 2010-04-14
  • 打赏
  • 举报
回复
马上要用 留以后看 谢谢
wanghui0380 2010-04-14
  • 打赏
  • 举报
回复
过滤的了吗??

如果现在的技术能过滤,那么工信部就不会再花了4000w买了一个笑话以后,有继续搞了一个白名单笑话

然后紧跟着他google逼走了(google和政府明面上主要矛盾就是这个“过滤”)
xupeihuagudulei 2010-04-13
  • 打赏
  • 举报
回复
这个还是没有很好的方法,
只有在一些中衡量。
微工程 2010-04-13
  • 打赏
  • 举报
回复
网站加过滤是一种方法而已
在过滤中,也可以考虑在空间中加过滤。
一品梅 2010-04-13
  • 打赏
  • 举报
回复
正则过滤词多就要慢啊,呵呵
pdsnet 2010-04-13
  • 打赏
  • 举报
回复


/// <summary>
/// 脏字符过滤
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public static string FilterBadWords(string msg)
{
string badwords = "妈妈的|我靠|操|fuck|sb|bitch 边有很多发布上去...";
string[] tempstr = badwords.Split('|');
string finalstr = msg;
for (int i = 0; i < tempstr.Length; i++)
{
finalstr = finalstr.Replace(tempstr[i], new String('*', tempstr[i].Length));
}
return finalstr;

}

您的回复正文中有非法词或词组!
参考
focuswe 2010-04-13
  • 打赏
  • 举报
回复
确定了脏字库后,需要的就只是判断了,判断是否在脏字库中存在~~~~
躲奶 2010-04-13
  • 打赏
  • 举报
回复
比如过滤“中国”

var text = $('#<%= TextBox1.ClientID %>').val();
$('#<%= TextBox1.ClientID %>').val(text.replace('中国','*'))

躲奶 2010-04-13
  • 打赏
  • 举报
回复
还要加上

var text = $('#<%= TextBox1.ClientID %>').val();
$('#<%= TextBox1.ClientID %>').val(text.replace(表达式,'*'))
microsoftAnson 2010-04-13
  • 打赏
  • 举报
回复
// 过滤脏字代码

//验证脏字
public class BadWordsFilter
{
private HashSet<string> hash = new HashSet<string>();
private byte[] fastCheck = new byte[char.MaxValue];
private byte[] fastLength = new byte[char.MaxValue];
private BitArray charCheck = new BitArray(char.MaxValue);
private BitArray endCheck = new BitArray(char.MaxValue);
private int maxWordLength = 0;
private int minWordLength = int.MaxValue;

public BadWordsFilter()
{

}

public void Init(string[] badwords)
{
foreach (string word in badwords)
{
maxWordLength = Math.Max(maxWordLength, word.Length);
minWordLength = Math.Min(minWordLength, word.Length);

for (int i = 0; i < 7 && i < word.Length; i++)
{
fastCheck[word[i]] |= (byte)(1 << i);
}

for (int i = 7; i < word.Length; i++)
{
fastCheck[word[i]] |= 0x80;
}

if (word.Length == 1)
{
charCheck[word[0]] = true;
}
else
{
fastLength[word[0]] |= (byte)(1 << (Math.Min(7, word.Length - 2)));
endCheck[word[word.Length - 1]] = true;

hash.Add(word);
}
}
}

public string Filter(string text, string mask)
{
throw new NotImplementedException();
}

public bool HasBadWord(string text)
{
int index = 0;

while (index < text.Length)
{
int count = 1;

if (index > 0 || (fastCheck[text[index]] & 1) == 0)
{
while (index < text.Length - 1 && (fastCheck[text[++index]] & 1) == 0) ;
}

char begin = text[index];

if (minWordLength == 1 && charCheck[begin])
{
return true;
}

for (int j = 1; j <= Math.Min(maxWordLength, text.Length - index - 1); j++)
{
char current = text[index + j];

if ((fastCheck[current] & 1) == 0)
{
++count;
}

if ((fastCheck[current] & (1 << Math.Min(j, 7))) == 0)
{
break;
}

if (j + 1 >= minWordLength)
{
if ((fastLength[begin] & (1 << Math.Min(j - 1, 7))) > 0 && endCheck[current])
{
string sub = text.Substring(index, j + 1);

if (hash.Contains(sub))
{
return true;
}
}
}
}

index += count;
}

return false;
}
}


上面是代码 就是 需要把 方法 绑定在 那个文本框上面去,但是 就是不行 不执行..
vip__888 2010-04-13
  • 打赏
  • 举报
回复
在提交的时候匹配你的关键字
如果包含 就提示

关键字可以使用xml来保存。
躲奶 2010-04-13
  • 打赏
  • 举报
回复

function checkinputtext()
{
var inputChars=$('#<%= TextBox1.ClientID %>').val().length;
if(148>=inputChars)
{
$('#<%= Button1.ClientID %>')[0].disabled=false;
$('#<%= Button1.ClientID %>').val("发布");
}
}

应该是这样,但怎么看都没有关于过滤的代码呀!!
microsoftAnson 2010-04-13
  • 打赏
  • 举报
回复
有高手吗? 如果能解决此问题 我愿意在加100分,急...
microsoftAnson 2010-04-13
  • 打赏
  • 举报
回复
我现在做的这个项目 有一个是写随想的部分,所以 不能出现一些 脏字,和一些敏感字,如果 不山寨的话 我觉得还是很复杂的,所以,头疼
microsoftAnson 2010-04-13
  • 打赏
  • 举报
回复
function checkinputtext()
{
var inputChars=$('#<%= TextBox1.ClientID %>').val().length;
if(148>=inputChars)
$('#<%= Button1.ClientID %>')[0].disabled=false;
$('#<%= Button1.ClientID %>').val("发布");
}
躲奶 2010-04-13
  • 打赏
  • 举报
回复
请把关键内容的代码发出来

就是checkinputtext里面的代码
huankfy 2010-04-13
  • 打赏
  • 举报
回复
如果是敏感词过滤,难度会很大,因为你不知道什么是敏感词(上面说是就是,不能问为什么)
要考虑划词的问题,比如“十二口交换机”,划词错了,就过改变意思了
bo_301 2010-04-13
  • 打赏
  • 举报
回复
29037453
.net技术群,欢迎加入~~~
huankfy 2010-04-13
  • 打赏
  • 举报
回复
敏感词过滤 ?
加载更多回复(6)

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧