PHP

PHP – XSS 관련 패키지 (AntiXSS)

  • AntiXSS 패키지란?
    • AntiXSS 패키지는 문자 내에서 스크립트를 제거 및 변환해주는 PHP 패키지로 사이트 간 스크립팅 방지를 위해 사용할 수 있습니다.
  • 패키지 설치
composer require voku/anti-xss
  • 스크립트 필터
// 스크립트 필터
$harm_string = "Hello, i try to <script>alert('Hack');</script> your site";

$harmless_string = $antiXss->xss_clean($harm_string);

// 16진수 스크립트 필터
$harm_string = 
"<IMG SRC=&#x6A&#x61&#x76&#x61&#x73&#x63&#x72&#x69&#x70&#x74&#x3A&#x61&#x6C&#x65&#x72&#x74&#x28&#x27&#x58&#x53&#x53&#x27&#x29>";

$harmless_string = $antiXss->xss_clean($harm_string);

// 유니코드 스크립트 필터
$harm_string = "<a href=\"javascript:alert(1)\">CLICK<a>";
$harmless_string = $antiXss->xss_clean($harm_string);

// 16진수 유니코드 스크립트 필터
$harm_string = "<a href='&#x2000;javascript:alert(1)'>CLICK</a>";
$harmless_string = $antiXss->xss_clean($harm_string);
  • inline style 구문 확인
    • removeEvilAttributes – style 구문은 허용하며 내부 스크립트만 제거
$harm_string = '<li style="list-style-image: url(javascript:alert(0))">';

$antiXss->removeEvilAttributes(array('style')); // allow style-attributes

$harmless_string = $antiXss->xss_clean($harm_string);
  • xss 공격 포함 여부
    • isXssFound – xss 공격 포함 여부 반환 (True/False)
$harm_string = "\x3cscript src=http://www.example.com/malicious-code.js\x3e\x3c/script\x3e";

$harmless_string = $antiXss->xss_clean($harm_string);

$antiXss->isXssFound();
  • iframe 태그 허용
    • removeEvilHtmlTags – 함수에 매개변수로 태그를 넣으면 해당 태그는 html 태그로서 동작
$harm_string = "<iframe width=\"1280\" height=\"720\" src=\"https://www.youtube.com/embed/play_code\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>";

$antiXss->removeEvilHtmlTags(array('iframe'));
    
$harmless_string = $antiXss->xss_clean($harm_string);

참고 자료

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 항목은 *(으)로 표시합니다

이 사이트는 스팸을 줄이는 아키스밋을 사용합니다. 댓글이 어떻게 처리되는지 알아보십시오.