관리 메뉴

웹개발자의 기지개

[ASP] SQL injection 방지 본문

ASP

[ASP] SQL injection 방지

http://portfolio.wonpaper.net 2022. 9. 6. 18:12

sql injection  방지 소스이다.

 

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
<%
' ===========================
' injection 처리
' ===========================
injection_filter  = "<script|delete from|drop table|drop column|alter table|alter column|;--|declare @|exec(|set @|; --|char(|varchar("
 
 
 
Function f_injection(query_string)
 
      query_string = LCase(query_string)
      f_injection = false
 
      injection_filter_arr = split(injection_filter,"|")
      injection_filter_cnt = Ubound(injection_filter_arr)
 
      for j = 0 to injection_filter_cnt
 
       if InStr(1,query_string,injection_filter_arr(j),1> 0 Then
 
            f_injection = true
 
                'Response.Write "<font color=red>죄송합니다.<br>"
            'Response.Write "특수문자나 명령어들은 서버에 전달할 수 없습니다. (필터링된 문자 : <font color=blue>"& injection_filter_arr(j) &"</font>)<br>"
            'Call objXML_Log(injection_filter_arr(j),query_string)
         '   Response.End
 
        exit for
       end if
      next
End function
 
 
    'post체크
    For each item in REQUEST.FORM
 
        For i=1 to REQUEST.FORM(item).Count
 
            If  REQUEST.FORM(item)(i) <> "" Then
                If f_injection(REQUEST.FORM(item)(i)) = true then
                    post_check = true
                    exit for
                End if
            End if
        Next
    Next
 
'cookie체크
    For each item in REQUEST.COOKIES
        For i=1 to REQUEST.COOKIES(item).Count
 
            If  REQUEST.COOKIES(item)(i) <> "" Then
'                Response.Write REQUEST.COOKIES(item)(i)
                If f_injection(REQUEST.COOKIES(item)(i)) = true then
                    cookie_check = true
                    exit for
                End if
            End if
        Next
    Next
 
'get체크
     inj_qs = Request.ServerVariables("QUERY_STRING")
 
    If inj_qs <> "" Then
        get_check = f_injection(unescape(inj_qs))
    End if
 
    if post_check = true or get_check = True Or cookie_check = True Then
    '    Response.Write("<script language=""javascript"" type=""text/javascript""> alert(""SQL INJECTION으로 의심되는 문장이 삽입되어 있습니다.\n\n이전페이지로 이동합니다.""); history.go (-1); </script>")
        Response.End
    End if
 
 
 
%>
cs

위의 소스를 sql_injection.asp 파일로 만들고,

 

DB 연결 파일 제일 하단에 아래와 같이 소스를 붙여주자.

 

'----------- SQL Injection 방지--------------
Server.Execute("/Inc/Inc_Function/sql_injection.asp")
'----------- SQL Injection 방지--------------

 

참고 : https://copyrightyoon.tistory.com/1387

 

 

 

 

Comments