一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - .Net防sql注入的幾種方法

.Net防sql注入的幾種方法

2020-06-16 12:21willingtolove ASP.NET教程

這篇文章主要給大家總結介紹了關于.Net防sql注入的幾種方法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用.Net具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

防sql注入的常用方法:

1、服務端對前端傳過來的參數值進行類型驗證;

2、服務端執行sql,使用參數化傳值,而不要使用sql字符串拼接;

3、服務端對前端傳過來的數據進行sql關鍵詞過來與檢測;

著重記錄下服務端進行sql關鍵詞檢測:

1、sql關鍵詞檢測類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
public class SqlInjectHelper:System.Web.UI.Page
 {
  private static string StrKeyWord = "select|insert|delete|from|count(|drop table|update|truncate|asc(|mid(|char(|xp_cmdshell|exec|master|net local group administrators|net user|or|and";
  private static string StrSymbol = ";|(|)|[|]|{|}|%|@|*|'|!";
 
  private HttpRequest request;
  public SqlInjectHelper(System.Web.HttpRequest _request)
  {
   this.request = _request;
  }
  public bool CheckSqlInject()
  {
   return CheckRequestQuery() || CheckRequestForm();
  }
 
  ///<summary>
  ///檢查URL中是否包含Sql注入
  /// <param name="_request">當前HttpRequest對象</param>
  /// <returns>如果包含sql注入關鍵字,返回:true;否則返回:false</returns>
  ///</summary>
  public bool CheckRequestQuery()
  {
   if (request.QueryString.Count > 0)
   {
    foreach (string sqlParam in this.request.QueryString)
    {
     if (sqlParam == "__VIEWSTATE")
      continue;
     if (sqlParam == "__EVENTVALIDATION")
      continue;
     if (CheckKeyWord(request.QueryString[sqlParam].ToLower()))
     {
      return true;
     }
    }
   }
   return false;
  }
  ///<summary>
  ///檢查提交的表單中是否包含Sql注入關鍵字
  /// <param name="_request">當前HttpRequest對象</param>
  /// <returns>如果包含sql注入關鍵字,返回:true;否則返回:false</returns>
  ///</summary>
  public bool CheckRequestForm()
  {
   if (request.Form.Count > 0)
   {
    foreach (string sqlParam in this.request.Form)
    {
     if (sqlParam == "__VIEWSTATE")
      continue;
     if (sqlParam == "__EVENTVALIDATION")
      continue;
     if (CheckKeyWord(request.Form[sqlParam]))
     {
      return true;
     }
    }
   }
   return false;
  }
  ///<summary>
  ///檢查字符串中是否包含Sql注入關鍵字
  /// <param name="_key">被檢查的字符串</param>
  /// <returns>如果包含sql注入關鍵字,返回:true;否則返回:false</returns>
  ///</summary>
  private static bool CheckKeyWord(string _key)
  {
   string[] pattenKeyWord = StrKeyWord.Split('|');
   string[] pattenSymbol = StrSymbol.Split('|');
   foreach (string sqlParam in pattenKeyWord)
   {
    if (_key.Contains(sqlParam + " ") || _key.Contains(" " + sqlParam))
    {
     return true;
    }
   }
   foreach (string sqlParam in pattenSymbol)
   {
    if (_key.Contains(sqlParam))
    {
     return true;
    }
   }
   return false;
  }
 
 }

SqlInjectHelper類中,對request的query參數和form參數進行的檢測,沒有對cookie的檢測,如有需要,可自行加上;

2、SqlInjectHelper在哪調用呢?

1)、如果想對整個web站點的所有請求都做sql關鍵字檢測,那就在Global.asax 的 Application_BeginRequest方法中調用;

?
1
2
3
4
5
6
7
8
9
10
11
protected void Application_BeginRequest(object sender, EventArgs e)
  {
   SqlInjectHelper myCheck = new SqlInjectHelper(Request);
   bool result = myCheck.CheckSqlInject();
   if (result)
   {
    Response.ContentType = "text/plain";
    Response.Write("您提交的數據有惡意字符!");
    Response.End();
   }
  }

2)、如果只需對某個接口文件的接口進行sql關鍵字檢測,那只需在該文件開始處調用SqlInjectHelper類即可;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Handler1 : IHttpHandler
 {
  public void ProcessRequest(HttpContext context)
  {
   SqlInjectHelper myCheck = new SqlInjectHelper(context.Request);
   bool result = myCheck.CheckSqlInject();
   context.Response.ContentType = "text/plain";
   context.Response.Write(result?"您提交的數據有惡意字符!":"");
   context.Response.StatusCode = result ? 500 : 200;
  }
  public bool IsReusable
  {
   get
   {
    return false;
   }
  }
 }

上面的代碼就是對某個一般處理程序(ashx)添加了sql關鍵字檢測;

3、補充說明:asp.net中的 __VIEWSTATE、__EVENTVALIDATION、

  在sql關鍵字檢測方法中,排除了__VIEWSTATE、__EVENTVALIDATION這兩個參數;

1)、__VIEWSTATE

  ViewState是ASP.NET中用來保存WEB控件回傳時狀態值一種機制。在WEB窗體(FORM)的設置為runat="server",這個窗體(FORM)會被附加一個隱藏的屬性_VIEWSTATE。_VIEWSTATE中存放了所有控件在ViewState中的狀態值。

ViewState是類Control中的一個域,其他所有控件通過繼承Control來獲得了ViewState功能。它的類型是system.Web.UI.StateBag,一個名稱/值的對象集合。

  當請求某個頁面時,ASP.NET把所有控件的狀態序列化成一個字符串,然后做為窗體的隱藏屬性送到客戶端。當客戶端把頁面回傳時,ASP.NET分析回傳的窗體屬性,并賦給控件對應的值;

2)、__EVENTVALIDATION

  __EVENTVALIDATION只是用來驗證事件是否從合法的頁面發送,只是一個數字簽名,所以一般很短。

“id”屬性為“__EVENTVALIDATION”的隱藏字段是ASP.NET 2.0的新增的安全措施。該功能可以阻止由潛在的惡意用戶從瀏覽器端發送的未經授權的請求.;

4、sql關鍵詞檢測的另一個版本:該版本將所有危險字符都放在了一個正則表達式中;

該類不僅檢測了sql常用關鍵字還有xss攻擊的常用關鍵字

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class SafeHelper
 {
  private const string StrRegex = @"<[^>]+?style=[\w]+?:expression\(|\b(alert|confirm|prompt)\b|^\+/v(8|9)|<[^>]*?=[^>]*?&#[^>]*?>|\b(and|or)\b.{1,6}?(=|>|<|\bin\b|\blike\b)|/\*.+?\*/|<\s*script\b|<\s*img\b|\bEXEC\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\s+(TABLE|DATABASE)";
  public static bool PostData()
  {
   bool result = false;
   for (int i = 0; i < HttpContext.Current.Request.Form.Count; i++)
   {
    result = CheckData(HttpContext.Current.Request.Form[i].ToString());
    if (result)
    {
     break;
    }
   }
   return result;
  }
 
  public static bool GetData()
  {
   bool result = false;
   for (int i = 0; i < HttpContext.Current.Request.QueryString.Count; i++)
   {
    result = CheckData(HttpContext.Current.Request.QueryString[i].ToString());
    if (result)
    {
     break;
    }
   }
   return result;
  }
  public static bool CookieData()
  {
   bool result = false;
   for (int i = 0; i < HttpContext.Current.Request.Cookies.Count; i++)
   {
    result = CheckData(HttpContext.Current.Request.Cookies[i].Value.ToLower());
    if (result)
    {
     break;
    }
   }
   return result;
 
  }
  public static bool referer()
  {
   bool result = false;
   return result = CheckData(HttpContext.Current.Request.UrlReferrer.ToString());
  }
  public static bool CheckData(string inputData)
  {
   if (Regex.IsMatch(inputData, StrRegex))
   {
    return true;
   }
   else
   {
    return false;
   }
  }
 }

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。

原文鏈接:https://www.cnblogs.com/willingtolove/p/11069969.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 2022色婷婷综合久久久 | yy111111影院理论大片 | 日本中文字幕在线视频 | 欧美jjvideo | 天天做日日做天天添天天欢公交车 | 成人影院在线观看视频 | 91色在线观看国产 | 网站久久 | 午夜一区二区免费视频 | 久久国产加勒比精品无码 | 国产农村乱子伦精品视频 | 操破苍穹小说 | 黄片毛片 | 国产高清在线精品一区二区三区 | 免费看欧美一级特黄a大片一 | 2022最新a精品视频在线观看 | 鬼惨笑小说 | 四虎影院地址 | 国产精品欧美亚洲韩国日本99 | 母爱成瘾在线观看 | kisssis无减删全集在线观看 | 天天射寡妇射 | 国产婷婷综合丁香亚洲欧洲 | 91久久碰国产 | 日本一区二区免费在线观看 | 男生同性视频twink在线 | 日本在线小视频 | 欧美成人免费观看bbb | 欧美成人一区二区 | 亚洲第99页| 色久久一个亚洲综合网 | 日本一道本视频 | 女教师雪白老汉 | 丁香网五月天 | 4444www免费看| brazzers办公室| aaa黄色| 国产在视频线精品视频 | 成人私人影院www片免费高清 | 午夜爱情动作片P | 91小视频在线观看免费版高清 |