ASP.NET過濾類SqlFilter,防止SQL注入 原創(chuàng)
什么是SQL注入?
我理解的sql注入就是一些人可以通過惡意的參數(shù)輸入,讓后臺(tái)執(zhí)行這段SQL,然后達(dá)到獲取數(shù)據(jù)或者破壞數(shù)據(jù)庫(kù)的目的!
舉個(gè)簡(jiǎn)單的查詢例子,后臺(tái)sql是拼接的:select * from Test where name='+參數(shù)傳遞+';前臺(tái)頁(yè)面要求輸入name,那么黑客可以輸入: ';DROP TABLE Test;-- 不要小瞧這一段SQL代碼:
select * from Test where name=' ';DROP TABLE Test;--';在SQL中是正確的,可執(zhí)行的,但是執(zhí)行后整個(gè)Test表都刪除了,網(wǎng)站崩潰!
最好的解決方法
最好的辦法就是不寫拼接SQL,改用參數(shù)化SQL,推薦新項(xiàng)目使用。這里不做介紹,感興趣的朋友可以自行搜索一下,本文介紹的方法適合老項(xiàng)目,就是沒有使用參數(shù)化SQL開發(fā)的程序。
使用過濾函數(shù)來過濾
將SQL一些危險(xiǎn)的關(guān)鍵字,還有注釋百分號(hào)以及分號(hào)這些根本在我們正常寫代碼的時(shí)候根本不會(huì)出現(xiàn)的字符都過濾掉,這樣能最大限度的保證SQL執(zhí)行是安全的,代碼如下:
public class SqlFilter
{
public static void Filter()
{
string fileter_sql = "execute,exec,select,insert,update,delete,create,drop,alter,exists,table,sysobjects,truncate,union,and,order,xor,or,mid,cast,where,asc,desc,xp_cmdshell,join,declare,nvarchar,varchar,char,sp_oacreate,wscript.shell,xp_regwrite,',%,;,--";
try
{
// -----------------------防 Post 注入-----------------------
if (HttpContext.Current.Request.Form != null)
{
PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
//把 Form 屬性改為可讀寫
isreadonly.SetValue(HttpContext.Current.Request.Form, false, null);
for (int k = 0; k < System.Web.HttpContext.Current.Request.Form.Count; k++)
{
string getsqlkey = HttpContext.Current.Request.Form.Keys[k];
string sqlstr = HttpContext.Current.Request.Form[getsqlkey];
string[] replace_sqls = fileter_sql.Split(',');
foreach (string replace_sql in replace_sqls)
{
sqlstr = Regex.Replace(sqlstr, replace_sql, "", RegexOptions.IgnoreCase);
}
HttpContext.Current.Request.Form[getsqlkey] = sqlstr;
}
}
// -----------------------防 GET 注入-----------------------
if (HttpContext.Current.Request.QueryString != null)
{
PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
//把 QueryString 屬性改為可讀寫
isreadonly.SetValue(HttpContext.Current.Request.QueryString, false, null);
for (int k = 0; k < System.Web.HttpContext.Current.Request.QueryString.Count; k++)
{
string getsqlkey = HttpContext.Current.Request.QueryString.Keys[k];
string sqlstr = HttpContext.Current.Request.QueryString[getsqlkey];
string[] replace_sqls = fileter_sql.Split(',');
foreach (string replace_sql in replace_sqls)
{
sqlstr = Regex.Replace(sqlstr, replace_sql, "", RegexOptions.IgnoreCase);
}
HttpContext.Current.Request.QueryString[getsqlkey] = sqlstr;
}
}
// -----------------------防 Cookies 注入-----------------------
if (HttpContext.Current.Request.Cookies != null)
{
PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
//把 Cookies 屬性改為可讀寫
isreadonly.SetValue(HttpContext.Current.Request.Cookies, false, null);
for (int k = 0; k < System.Web.HttpContext.Current.Request.Cookies.Count; k++)
{
string getsqlkey = HttpContext.Current.Request.Cookies.Keys[k];
string sqlstr = HttpContext.Current.Request.Cookies[getsqlkey].Value;
string[] replace_sqls = fileter_sql.Split(',');
foreach (string replace_sql in replace_sqls)
{
sqlstr = Regex.Replace(sqlstr, replace_sql, "", RegexOptions.IgnoreCase);
}
HttpContext.Current.Request.Cookies[getsqlkey].Value = sqlstr;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
相關(guān)文章
解決 .NET Core 中 GetHostAddressesAsync 引起的 EnyimMemcached 死鎖問題
這篇文章主要介紹了解決 .NET Core 中 GetHostAddressesAsync 引起的 EnyimMemcached 死鎖問題的相關(guān)資料,需要的朋友可以參考下2016-09-09
Net Core Web Api項(xiàng)目與在NginX下發(fā)布的方法
這篇文章主要介紹了Net Core Web Api項(xiàng)目與在NginX下發(fā)布的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
asp.net水晶報(bào)表參數(shù)字段在代碼中賦值的方法
這篇文章主要介紹了asp.net水晶報(bào)表參數(shù)字段在代碼中賦值的方法,實(shí)例分析了asp.net中水晶報(bào)表的使用技巧,需要的朋友可以參考下2015-05-05
ASP.NET中DES加密與解密MD5加密幫助類的實(shí)現(xiàn)代碼
這篇文章主要介紹了ASP.NET中DES加密與解密MD5加密幫助類的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-07-07
實(shí)現(xiàn).Net7下數(shù)據(jù)庫(kù)定時(shí)檢查的方法詳解
在軟件開發(fā)過程中,有時(shí)候我們需要定時(shí)地檢查數(shù)據(jù)庫(kù)中的數(shù)據(jù),并在發(fā)現(xiàn)新增數(shù)據(jù)時(shí)觸發(fā)一個(gè)動(dòng)作。為了實(shí)現(xiàn)這個(gè)需求,本文我們?cè)?.Net?7?下進(jìn)行一次簡(jiǎn)單的演示。感興趣的可以了解一下2022-12-12
ASP.NET用DataSet導(dǎo)出到Excel的方法
ASP.NET用DataSet導(dǎo)出到Excel的方法,需要的朋友可以參考一下2013-03-03
.Net?Core使用Logger實(shí)現(xiàn)log寫入本地文件系統(tǒng)
這篇文章介紹了.Net?Core使用Logger實(shí)現(xiàn)log寫入本地文件系統(tǒng)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
.NET?API?接口數(shù)據(jù)傳輸加密最佳實(shí)踐記錄
這篇文章主要介紹了.NET?API?接口數(shù)據(jù)傳輸加密最佳實(shí)踐記錄,我們?cè)谧?Api?接口時(shí),相信一定會(huì)有接觸到要給傳輸?shù)恼?qǐng)求?body?的內(nèi)容進(jìn)行加密傳輸。其目的就是為了防止一些敏感的內(nèi)容直接被?UI?層查看或篡改,需要的朋友可以參考下2022-10-10

