架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 21110|回复: 2

[接口] c#和asp.net防止SQL注入过滤方法

[复制链接]
发表于 2015-4-19 23:54:16 | 显示全部楼层 |阅读模式
QQ截图20150419235310.jpg QQ截图20150419235330.jpg

我用asp.net来做演示,嘿嘿

前台代码:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="sqltest.aspx.cs" Inherits="TestWeb.sqltest" %>

  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5.     <title></title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <div>
  10.         <asp:TextBox ID="txtsql" runat="server"></asp:TextBox>
  11.         <asp:Button ID="btnsql" runat="server" Text="注入" onclick="btnsql_Click" />
  12.     </div>
  13.     </form>
  14. </body>
  15. </html>
复制代码
后台代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;

  7. namespace TestWeb
  8. {
  9.     public partial class sqltest : System.Web.UI.Page
  10.     {
  11.         protected void Page_Load(object sender, EventArgs e)
  12.         {

  13.         }

  14.         protected void btnsql_Click(object sender, EventArgs e)
  15.         {
  16.             
  17.             if (zhuru.ProcessSqlStr(txtsql.Text))
  18.             {
  19.                 Response.Write("安全");
  20.             }
  21.             else {
  22.                 Response.Write("危险");
  23.             }
  24.         }
  25.     }
  26. }
复制代码
zhuru.cs类代码:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Text.RegularExpressions;

  6. namespace TestWeb
  7. {
  8.     public class zhuru
  9.     {
  10.         
  11.         public static bool ProcessSqlStr(string inputString)
  12.         {
  13.             string SqlStr = @"and|or|exec|execute|insert|select|delete|update|alter|create|drop|count|\*|chr|char|asc|mid|substring|master|truncate|declare|xp_cmdshell|restore|backup|net +user|net +localgroup +administrators";
  14.             try
  15.             {
  16.                 if ((inputString != null) && (inputString != String.Empty))
  17.                 {
  18.                     string str_Regex = @"\b(" + SqlStr + @")\b";

  19.                     Regex Regex = new Regex(str_Regex, RegexOptions.IgnoreCase);
  20.                     //string s = Regex.Match(inputString).Value;
  21.                     if (true == Regex.IsMatch(inputString))
  22.                         return false;

  23.                 }
  24.             }
  25.             catch
  26.             {
  27.                 return false;
  28.             }
  29.             return true;
  30.         }

  31.     }
  32. }
复制代码






上一篇:C#.NET防止SQL注入式攻击
下一篇:已知局域网IP地址,如何查主机名
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
 楼主| 发表于 2022-2-7 19:33:01 | 显示全部楼层
Java 动态拼接 SQL 语句防止数据库注入
https://www.itsvse.com/thread-10249-1-1.html
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2022-3-6 10:40:43 | 显示全部楼层
学习下               
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2025-6-17 20:39

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表