一、什么是SQL注入?
SQL注入即是指web應(yīng)用程序?qū)τ脩糨斎霐?shù)據(jù)的合法性沒有判斷或過濾不嚴,攻擊者可以在web應(yīng)用程序中事先定義好的查詢語句的結(jié)尾上添加額外的SQL語句,在管理員不知情的情況下實現(xiàn)非法操作,以此來實現(xiàn)欺騙數(shù)據(jù)庫服務(wù)器執(zhí)行非授權(quán)的任意查詢,從而進一步得到相應(yīng)的數(shù)據(jù)信息。
SQL案列
Stringsql="deletefromtable1whereid="+"id";
這個id從請求參數(shù)中獲取,若參數(shù)被拼接為:
1001 or 1 = 1
最執(zhí)行語句變?yōu)椋?/p>
Stringsql="deletefromtable1whereid=1001or1=1";
此時,數(shù)據(jù)庫的數(shù)據(jù)都會被清空掉,后果非常嚴重
二、Java項目防止SQL注入方式
這里總結(jié)4種:
PreparedStatement防止SQL注入
mybatis中#{}防止SQL注入
對請求參數(shù)的敏感詞匯進行過濾
nginx反向代理防止SQL注入
1、PreparedStatement防止SQL注入
PreparedStatement具有預(yù)編譯功能,以上述SQL為例
使用PreparedStatement預(yù)編譯后的SQL為:
deletefromtable1whereid=?
此時SQL語句結(jié)構(gòu)已固定,無論"?"被替換為任何參數(shù),SQL語句只認為where后面只有一個條件,當(dāng)再傳入 1001 or 1 = 1時,語句會報錯,從而達到防止SQL注入效果
2、mybatis中#{}防止SQL注入
mybatis中#{}表達式防止SQL注入與PreparedStatement類似,都是對SQL語句進行預(yù)編譯處理
注意:
#{} :參數(shù)占位符
${} :拼接替換符,不能防止SQL注入,一般用于
傳入數(shù)據(jù)庫對象(如:數(shù)據(jù)庫名稱、表名)
order by 后的條件
3、對請求參數(shù)的敏感詞匯進行過濾
這里是springboot的寫法,如下:
importorg.springframework.context.annotation.Configuration; importjavax.servlet.*; importjavax.servlet.annotation.WebFilter; importjava.io.IOException; importjava.util.Enumeration; /** *@Auther:睡竹 *@Date:2023/03/07 *@Description:sql防注入過濾器 */ @WebFilter(urlPatterns="/*",filterName="sqlFilter") @Configuration publicclassSqlFilterimplementsFilter{ @Override publicvoidinit(FilterConfigfilterConfig)throwsServletException{} /** *@descriptionsql注入過濾 */ @Override publicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,FilterChainfilterChain)throwsIOException,ServletException{ ServletRequestrequest=servletRequest; ServletResponseresponse=servletResponse; //獲得所有請求參數(shù)名 Enumerationnames=request.getParameterNames(); Stringsql=""; while(names.hasMoreElements()){ //得到參數(shù)名 Stringname=names.nextElement().toString(); //得到參數(shù)對應(yīng)值 String[]values=request.getParameterValues(name); for(inti=0;i
4、nginx反向代理防止SQL注入
越來越多網(wǎng)站使用nginx進行反向代理,該層我們也可以進行防止SQL注入配置。
將下面的Nginx配置文件代碼放入到server塊中,然后重啟Nginx即可
if($request_method!~*GET|POST){return444;} #使用444錯誤代碼可以更加減輕服務(wù)器負載壓力。 #防止SQL注入 if($query_string~*($|'|--|[+|(%20)]union[+|(%20)]|[+|(%20)]insert[+|(%20)]|[+|(%20)]drop[+|(%20)]|[+|(%20)]truncate[+|(%20)]|[+|(%20)]update[+|(%20)]|[+|(%20)]from[+|(%20)]|[+|(%20)]grant[+|(%20)]|[+|(%20)]exec[+|(%20)]|[+|(%20)]where[+|(%20)]|[+|(%20)]select[+|(%20)]|[+|(%20)]and[+|(%20)]|[+|(%20)]or[+|(%20)]|[+|(%20)]count[+|(%20)]|[+|(%20)]exec[+|(%20)]|[+|(%20)]chr[+|(%20)]|[+|(%20)]mid[+|(%20)]|[+|(%20)]like[+|(%20)]|[+|(%20)]iframe[+|(%20)]|[<|%3c]script[>|%3e]|javascript|alert|webscan|dbappsecurity|style|confirm(|innerhtml|innertext)(.*)$){return555;} if($uri~*(/~).*){return501;} if($uri~*(\x.)){return501;} #防止SQL注入 if($query_string~*"[;'<>].*"){return509;} if($request_uri~""){return509;} if($request_uri~(/.+)){return509;} if($request_uri~(.+/)){return509;} #if($uri~*(insert|select|delete|update|count|master|truncate|declare|exec|*|')(.*)$){return503;} #防止SQL注入 if($request_uri~*"(cost()|(concat()"){return504;} if($request_uri~*"[+|(%20)]union[+|(%20)]"){return504;} if($request_uri~*"[+|(%20)]and[+|(%20)]"){return504;} if($request_uri~*"[+|(%20)]select[+|(%20)]"){return504;} if($request_uri~*"[+|(%20)]or[+|(%20)]"){return504;} if($request_uri~*"[+|(%20)]delete[+|(%20)]"){return504;} if($request_uri~*"[+|(%20)]update[+|(%20)]"){return504;} if($request_uri~*"[+|(%20)]insert[+|(%20)]"){return504;} if($query_string~"(<|%3C).*script.*(>|%3E)"){return505;} if($query_string~"GLOBALS(=|[|\%[0-9A-Z]{0,2})"){return505;} if($query_string~"_REQUEST(=|[|\%[0-9A-Z]{0,2})"){return505;} if($query_string~"proc/self/environ"){return505;} if($query_string~"mosConfig_[a-zA-Z_]{1,21}(=|\%3D)"){return505;} if($query_string~"base64_(en|de)code(.*)"){return505;} if($query_string~"[a-zA-Z0-9_]=http://"){return506;} if($query_string~"[a-zA-Z0-9_]=(..//?)+"){return506;} if($query_string~"[a-zA-Z0-9_]=/([a-z0-9_.]//?)+"){return506;} if($query_string~"b(ultram|unicauca|valium|viagra|vicodin|xanax|ypxaieo)b"){return507;} if($query_string~"b(erections|hoodia|huronriveracres|impotence|levitra|libido)b"){return507;} if($query_string~"b(ambien|bluespill|cialis|cocaine|ejaculation|erectile)b"){return507;} if($query_string~"b(lipitor|phentermin|pro[sz]ac|sandyauer|tramadol|troyhamby)b"){return507;} #這里大家根據(jù)自己情況添加刪減上述判斷參數(shù),cURL、wget這類的屏蔽有點兒極端了,但要“寧可錯殺一千,不可放過一個”。 if($http_user_agent~*YisouSpider|ApacheBench|WebBench|Jmeter|JoeDog|Havij|GetRight|TurnitinBot|GrabNet|masscan|mail2000|github|wget|curl|Java|python){return508;} #同上,大家根據(jù)自己站點實際情況來添加刪減下面的屏蔽攔截參數(shù)。 if($http_user_agent~*"Go-Ahead-Got-It"){return508;} if($http_user_agent~*"GetWeb!"){return508;} if($http_user_agent~*"Go!Zilla"){return508;} if($http_user_agent~*"DownloadDemon"){return508;} if($http_user_agent~*"IndyLibrary"){return508;} if($http_user_agent~*"libwww-perl"){return508;} if($http_user_agent~*"NmapScriptingEngine"){return508;} if($http_user_agent~*"~17ce.com"){return508;} if($http_user_agent~*"WebBench*"){return508;} if($http_user_agent~*"spider"){ return 508;}#這個會影響國內(nèi)某些搜索引擎爬蟲,比如:搜狗 #攔截各惡意請求的UA,可以通過分析站點日志文件或者waf日志作為參考配置。 if($http_referer~*17ce.com){return509;} #攔截17ce.com站點測速節(jié)點的請求,所以明月一直都說這些測速網(wǎng)站的數(shù)據(jù)僅供參考不能當(dāng)真的。 if($http_referer~*WebBench*"){return509;} #攔截WebBench或者類似壓力測試工具,其他工具只需要更換名稱即可。
審核編輯:湯梓紅
-
Web
+關(guān)注
關(guān)注
2文章
1285瀏覽量
70960 -
JAVA
+關(guān)注
關(guān)注
20文章
2987瀏覽量
107300 -
SQL
+關(guān)注
關(guān)注
1文章
782瀏覽量
44908 -
應(yīng)用程序
+關(guān)注
關(guān)注
38文章
3324瀏覽量
58826
原文標題:值得推薦,JAVA中防止SQL注入的四種方案
文章出處:【微信號:magedu-Linux,微信公眾號:馬哥Linux運維】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
基于SQL注入攻擊檢測與防御的方法

sql注入攻擊實例講解

網(wǎng)絡(luò)環(huán)境的SQL注入行為檢測
如何使用Java Web防范SQL 注入攻擊的資料說明

一文帶你了解安全測試基礎(chǔ)之SQL注入
訓(xùn)練SQL注入的sqil-labs-master闖關(guān)游戲
SQL注入攻擊是什么 SQL注入會帶來哪些威脅
SQL注入到Getshell的教程
Mybatis的SQL注入審計的基本方法
超級SQL注入工具–SSQLInjection
sql注入漏洞解決方法有哪些?

評論