안녕하세요

주인장입니다.


웹 해킹에서 제일 위협이 큰 SQL Injection 공격에 대한 예방코드를 아래와 같이 공유를 해 드립니다.


2009년도에 만들었던거라 최신화는 안되어 있는데 왠만한 공격은 어느정도 방어가 가능합니다.

GET, POST, Cookies 로 들어오는 SQL 구문을 필터링 하는 것으로 필요한 소스코드에 붙여 놓으면 됩니다. 전체적용은 공통으로 적용되는 헤더 파일에 포함을 시키시면 됩니다.


<%

' SQL Injection Protected
Dim sql_pattern, item, array_counter, item_position1, item_position2

sql_pattern = Array("<?", "fromcharcode", "charcodeat", "unescape", "script.encode", "cast(", "sp_", "xp_", "sysobject", "sysadmin", "syscol", "nchar", "varchar", "nvarchar", "theact=", "declare")

for each item in Request.QueryString

for array_counter = lbound(sql_pattern) to ubound(sql_pattern)
item_position1 = InStr(lcase(Request(item)), sql_pattern(array_counter))
item_position2 = InStr(lcase(Request.QueryString), sql_pattern(array_counter))
if (item_position1 > 0) or (item_position2 > 0) then
Response.Write("<xmp>Logging for" & vbcrlf & "Client:" & Request.ServerVariables("REMOTE_ADDR") & vbcrlf & "URL:" & request.servervariables("URL") & vbcrlf & Request.Form & vbcrlf & Request.QueryString & vbcrlf & Request.Cookies & vbcrlf & "</xmp><hr>잘못된 정보를 요청하셨습니다. 동일한 증상 반복 발생시 관리자에게 이 화면을 캡처하여 문의하세요.")
Response.End()
end if
next
next

for each item in Request.Form

for array_counter = lbound(sql_pattern) to ubound(sql_pattern)
item_position1 = InStr(lcase(Request(item)), sql_pattern(array_counter))
item_position2 = InStr(lcase(Request.QueryString), sql_pattern(array_counter))
if (item_position1 > 0) or (item_position2 > 0) then
Response.Write("<xmp>Logging for" & vbcrlf & "Client:" & Request.ServerVariables("REMOTE_ADDR") & vbcrlf & "URL:" & request.servervariables("URL") & vbcrlf & Request.Form & vbcrlf & Request.QueryString & vbcrlf & Request.Cookies & vbcrlf & "</xmp><hr>잘못된 정보를 요청하셨습니다. 동일한 증상 반복 발생시 관리자에게 이 화면을 캡처하여 문의하세요.")
Response.End()
end if
next
next

for each item in Request.Cookies

for array_counter = lbound(sql_pattern) to ubound(sql_pattern)
item_position1 = InStr(lcase(Request(item)), sql_pattern(array_counter))
item_position2 = InStr(lcase(Request.QueryString), sql_pattern(array_counter))
if (item_position1 > 0) or (item_position2 > 0) then
Response.Write("<xmp>Logging for" & vbcrlf & "Client:" & Request.ServerVariables("REMOTE_ADDR") & vbcrlf & "URL:" & request.servervariables("URL") & vbcrlf & Request.Form & vbcrlf & Request.QueryString & vbcrlf & Request.Cookies & vbcrlf & "</xmp><hr>잘못된 정보를 요청하셨습니다. 동일한 증상 반복 발생시 관리자에게 이 화면을 캡처하여 문의하세요.")
Response.End()
end if
next
next

%> 


적용후 사이트에 따라 오류가 발생 할 수 있으니 신중하시기 바랍니다~

그리고 퍼가시면 출처라도..^^;;

Posted by 비타민A
,