이 글은 기계 번역의 미러 문서이며, 원본 기사로 바로 이동하려면 여기를 클릭해 주세요.

보기: 18374|회답: 0

[ASP.NET] SQL 위험 문자에 대한 사용자 입력 배치 탐지

[링크 복사]
게시됨 2015. 5. 8. 오후 12:29:34 | | |
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="testsql.aspx.cs" Inherits="TestWeb.testsql" %>

  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="TextBox1" runat="server"></asp:TextBox><asp:TextBox ID="TextBox2"
  11.             runat="server"></asp:TextBox><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
  12.             <asp:Button ID="Button1" runat="server" Text="Button"
  13.             onclick="Button1_Click" />
  14.     </div>
  15.    
  16.     </form>
  17. </body>
  18. </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 testsql : System.Web.UI.Page
  10.     {
  11.         protected void Page_Load(object sender, EventArgs e)
  12.         {

  13.         }

  14.         protected void Button1_Click(object sender, EventArgs e)
  15.         {
  16.             string[] userinput = new string[] { TextBox1.Text, TextBox2.Text, TextBox3.Text };
  17.             bool a=SQLinfo.sql(userinput);
  18.             Response.Write(a);
  19.         }
  20.     }
  21. }
코드 복사

  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 SQLinfo
  9.     {
  10.         public static bool sql(string[] inputString)
  11.         {
  12.             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";
  13.             try
  14.             {
  15.                 foreach (string sqls in inputString)
  16.                 {
  17.                     if ((sqls != null) && (sqls != String.Empty))
  18.                     {
  19.                         string str_Regex = @"\b(" + SqlStr + @")\b";

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

  24.                     }
  25.                 }
  26.             }
  27.             catch
  28.             {
  29.                 return false;
  30.             }
  31.             return true;
  32.         }
  33.     }
  34. }
코드 복사






이전의:ASP.NET 의 3단계 아키텍처 (DAL, BLL, UI)
다음:이 명령어와 연결된 데이터가 이미 열려 있다면 먼저 종료해야 합니다.
면책 조항:
Code Farmer Network에서 발행하는 모든 소프트웨어, 프로그래밍 자료 또는 기사는 학습 및 연구 목적으로만 사용됩니다; 위 내용은 상업적 또는 불법적인 목적으로 사용되지 않으며, 그렇지 않으면 모든 책임이 사용자에게 부담됩니다. 이 사이트의 정보는 인터넷에서 가져온 것이며, 저작권 분쟁은 이 사이트와는 관련이 없습니다. 위 내용은 다운로드 후 24시간 이내에 컴퓨터에서 완전히 삭제해야 합니다. 프로그램이 마음에 드신다면, 진짜 소프트웨어를 지원하고, 등록을 구매하며, 더 나은 진짜 서비스를 받아주세요. 침해가 있을 경우 이메일로 연락해 주시기 바랍니다.

Mail To:help@itsvse.com