ASP.NET防注入代码【转】
2010-07-23 02:34
344 查看
一种:
最近很多网站数据库被挂马,出现类似 "</title> <script src=http://s.see9.us/s.js> </script>"的字符。
C#防注检查代码
/// <summary>
/// 防注入字符串检查
/// </summary>
/// <param name= "str "> 待检查的字符串 </param>
/// <returns> </returns>
public static bool StringCheck(string str)
{
if (str.Trim() == " " || str == null)
{
return true;
}
else
{
Regex re = new Regex(@ "\s ");
str = re.Replace(str.Replace( "%20 ", " "), " ");
string pattern = @ "select |insert |delete from |count\(|drop table|update |truncate |asc\(|mid\(|char\(|xp_cmdshell|exec master|net localgroup administrators|:|net user| " "|\ '| or ";
if (Regex.IsMatch(str, pattern))
{
return false;
}
else
{
return true;
}
}
}
/// <summary> /// 在 Application_BeginRequest中加入函数StartProcessRequest() /// </summary> protected void Application_BeginRequest(Object sender, EventArgs e) { StartProcessRequest(); } #region SQL注入式攻击代码分析 /// <summary> /// 处理用户提交的请求 /// </summary> private void StartProcessRequest() { try { string getkeys = ""; string sqlErrorPage = "/default.aspx";//如果有非法参数,转向的错误提示页面 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; if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys])) { System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage); 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; if (getkeys == "__VIEWSTATE") continue; if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys])) { System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage); System.Web.HttpContext.Current.Response.End(); } } } if (System.Web.HttpContext.Current.Request.Cookies != null) { for (int i = 0; i < System.Web.HttpContext.Current.Request.Cookies.Count; i++) { getkeys = System.Web.HttpContext.Current.Request.Cookies.Keys; if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Cookies[getkeys].ToString())) { System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage); System.Web.HttpContext.Current.Response.End(); } } } } catch { // 错误处理: 处理用户提交信息! } } /// <summary> /// 分析用户请求是否正常 /// </summary> /// <param name="Str">传入用户提交数据 </param> /// <returns>返回是否含有SQL注入式攻击代码 </returns> private bool ProcessSqlStr(string Str) { bool ReturnValue = true; t ry { if (Str.Trim() != "") { string SqlStr = "and |exec |insert |select |delete |update |count |* |chr |mid |master |truncate |char |declare"; string[] anySqlStr = SqlStr.Split('|'); foreach (string ss in anySqlStr) { if (Str.ToLower().IndexOf(ss) >= 0) { ReturnValue = false; break; } } } } catch { ReturnValue = false; } return ReturnValue; } #endregion [/pre] |
C#防注检查代码
/// <summary>
/// 防注入字符串检查
/// </summary>
/// <param name= "str "> 待检查的字符串 </param>
/// <returns> </returns>
public static bool StringCheck(string str)
{
if (str.Trim() == " " || str == null)
{
return true;
}
else
{
Regex re = new Regex(@ "\s ");
str = re.Replace(str.Replace( "%20 ", " "), " ");
string pattern = @ "select |insert |delete from |count\(|drop table|update |truncate |asc\(|mid\(|char\(|xp_cmdshell|exec master|net localgroup administrators|:|net user| " "|\ '| or ";
if (Regex.IsMatch(str, pattern))
{
return false;
}
else
{
return true;
}
}
}
相关文章推荐
- ASP.NET - 使用依赖关系注入在 ASP.NET Core 中编写干净代码
- asp,jsp,asp.net防注入代码
- Asp.Net程序开发中的代码与安全之SQL注入
- C#,Asp.net 防止Sql 注入代码
- [转]使用依赖关系注入在 ASP.NET Core 中编写干净代码
- 使用依赖关系注入在 ASP.NET Core 中编写干净代码
- asp.net防注入代码
- Asp.Net异常:"由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值"的解决方法
- asp.net 2.0常用代码集合
- ASP.NET中常用的三十三种代码第1/7页
- ASP.NET基础代码备忘
- 如何解决ASP.NET下载时的中文文件名乱码,与TXT文件中存在代码两个问题
- [原]ASP.NET中使用后端代码注册脚本 生成JQUERY-EASYUI 的界面错位
- (转)ASP.NET前台代码绑定后台变量方法总结
- ASP.NET中动态修改web.config中的设置项目(前台代码)
- 在ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串
- ASP.NET程序中常用代码汇总
- ASP.NET中常用的33种代码
- ASP.NET程序中常用的三十三种代码(2)
- ASP.NET程序中常用的三十三种代码