차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판이전 판다음 판 | 이전 판다음 판양쪽 다음 판 | ||
guide:ajax_개발_보안_가이드 [2013/11/26 06:33] – 121.140.124.172 | guide:ajax_개발_보안_가이드 [2013/11/29 08:36] – 121.140.124.172 | ||
---|---|---|---|
줄 133: | 줄 133: | ||
\\ | \\ | ||
지금 당면한 진짜 과제는 어떻게 정상적인 javascript에서 공격자 javascript를 확인하는가 하는 것이다. | 지금 당면한 진짜 과제는 어떻게 정상적인 javascript에서 공격자 javascript를 확인하는가 하는 것이다. | ||
+ | <code html> | ||
+ | 1 :< | ||
+ | 2 :< | ||
+ | 3 :<? | ||
+ | 4 ://our signature will be a random number generated by the server | ||
+ | 5 :$signature = rand(); | ||
+ | 6 :?> | ||
+ | 7 :<!-- here is our legitimate script with the signature as its element id --> | ||
+ | 8 :<script id="<? | ||
+ | 9 : | ||
+ | 10 :</ | ||
+ | 11 :<!-- here is the injected attacker script that doesn' | ||
+ | 12 :< | ||
+ | 13 : | ||
+ | 14 :</ | ||
+ | 15 :<!-- here is a more evil script where the attacker will try to imitate the signature --> | ||
+ | 16 :<script id=" | ||
+ | 17 : | ||
+ | 18 :</ | ||
+ | 19 :<!-- here is the script that will do the check and of course it have the signature too --> | ||
+ | 20 :<script id="<? | ||
+ | 21 ://here we gather all the script tags elements in one array | ||
+ | 22 :var scripts = document.getElementsByTagName(" | ||
+ | 23 :for(var i = 0; i < scripts.length; | ||
+ | 24 : if(scripts[i].id != null) | ||
+ | 25 : { | ||
+ | 26 : //then we compare it with our signature if it have one, if it’s invalid we warn the user/ | ||
+ | 27 : if(scripts[i].id != <? echo $signature ?>) | ||
+ | 28 : warn(scripts[i].innerHTML) | ||
+ | 29 : } | ||
+ | 30 : else //else if there is no signature in the 1st place we warn the user/ | ||
+ | 31 : warn(scripts[i].innerHTML) | ||
+ | 32 : | ||
+ | 33 :function warn(attackscript) | ||
+ | 34 :{ | ||
+ | 35 : //here we create our XMLHttpRequest object | ||
+ | 36 : xmlHttp=GetXmlHttpObject() | ||
+ | 37 : //and here we create a request string to our logger script then send the attacker script | ||
+ | 38 : //to be logged for later analysis so we can tell what exactly happened | ||
+ | 39 : var url=" | ||
+ | 40 : xmlHttp.open(" | ||
+ | 41 : xmlHttp.send(null) | ||
+ | 42 : //then we warn the user about what is going on and advice him/her to change his/her password | ||
+ | 43 : alert(" | ||
+ | 44 :} | ||
+ | 45 ://the rest of this code is the code that is responsible of creating | ||
+ | 46 ://the XMLHttpRequest object for different browsers | ||
+ | 47 :function GetXmlHttpObject() | ||
+ | 48 :{ | ||
+ | 49 : var xmlHttp=null; | ||
+ | 50 : try | ||
+ | 51 : { | ||
+ | 52 : // Firefox, Opera 8.0+, Safari | ||
+ | 53 : xmlHttp=new XMLHttpRequest(); | ||
+ | 54 : } | ||
+ | 55 : catch (e) | ||
+ | 56 : { | ||
+ | 57 : // Internet Explorer | ||
+ | 58 : try | ||
+ | 59 : { | ||
+ | 60 : xmlHttp=new ActiveXObject(" | ||
+ | 61 : } | ||
+ | 62 : catch (e) | ||
+ | 63 : { | ||
+ | 64 : xmlHttp=new ActiveXObject(" | ||
+ | 65 : } | ||
+ | 66 : } | ||
+ | 67 : return xmlHttp; | ||
+ | 68 :} | ||
+ | 69 :</ | ||
+ | 70 :</ | ||
+ | 71 :</ | ||
+ | </ | ||
+ | 위의 예제에서 모든 javascript에 사용되는 랜덤 값을 발생 시키기 위해 rand()함수를 사용했다. 사용자 악성 스크립트가 아래와 같이 보이는 키로그 스크립트에 보내지는 동안 그의 패스워드를 변경하는 것은 경고 될 수 있다. 이런, rand()함수는 다른 악성 script로부터 정상적인 모든 javascript를 서명하기 위해 사용된다. | ||
+ | < | ||
+ | <? | ||
+ | $file = fopen(" | ||
+ | $timestamp = date(" | ||
+ | fwrite($file, | ||
+ | fwrite($file, | ||
+ | fclose($file); | ||
+ | ?> | ||
+ | </ | ||
+ | 악성 스크립트 컨텐츠들은 로그를 남길수 있는데, 후에 사이트 관리자는 이 공격을 분석 할 수 있다. 로그 파일은 다음과 같다. | ||
+ | < | ||
+ | -------------------- | ||
+ | Fri 20th Jul,2007 12:38 am | ||
+ | alert(\" | ||
+ | -------------------- | ||
+ | Fri 20th Jul,2007 12:41 am | ||
+ | alert(\" | ||
+ | -------------------- | ||
+ | </ | ||
+ | 또한 우리는 사용자가 타고 온 링크에 referral URL 더 중요한 정보를 남길 수 있다. 그래서 우리는 어떻게 공격자가 공격했는지 메일이나 다른 방법으로 알 수 있을 것이며, 사용자 이름을 기록할 수 있고, 도움을 요청하기 위해 연락하거나 또한 그 공격에 대해 더 많은 정보를 얻을 수도 있다. | ||
+ | \\ | ||
+ | \\ | ||
+ | Ajax 코딩 기법을 사용해서 사용자나 운영자에게 미리 경고하는것이 가능하고, | ||
+ | \\ | ||
+ | \\ | ||
+ | 그러나 이 기법은 큰 과제를 남기는데, | ||
+ | \\ | ||
+ | \\ | ||
+ | JavaScript가 sleep()함수를 지원한다면 이는 아주 좋은 해결책이 될 것이다. 그러나 JavaScript는sleep함수를 지원하지 않는다. | ||
+ | \\ | ||
+ | \\ | ||
+ | 다른 해결책은 완전한 Ajax 웹 페이지에 XMLHttpRequest 객체를 사용해서 HTML 컨텐츠를 요청하는 것이다. 그리고 사용자가 클릭할 때마다 그 페이지를 검증 JavaScript가 담긴 새로운 링크로 업데이트 하는 것이다. 그러나 이 작업은 많은 부하가 걸릴 것이다. | ||
+ | === 나. SQL Injection in Ajax === | ||
+ | 일반적인 웹 어플리케이션은 클라이언트 측의 쿼리 요청에 대해 서버에서 결과 데이터를 파싱하여 사용자가 보는 HTML에서 결과를 출력해 준다. 하지만 Ajax 어플리케이션은 서버에서 원시 데이터의 결과를 클라이언트에게 돌려주고 클라이언트는 받은 원시 데이터를 분석하여 HTML로 변형시키도록 설계하는 경우가 있다. | ||
+ | \\ | ||
+ | \\ | ||
+ | 성능 측면에서 이런 접근 방법은 상당히 좋다. 데이터 변형 루틴은 시간이 걸리므로, |