Recent Posts
Recent Comments
Link
06-30 12:53
Today
Total
관리 메뉴

삶 가운데 남긴 기록 AACII.TISTORY.COM

JAVA 보안 Improper Neutralization of Script-Related HTML Tags in a Web Page 본문

DEV&OPS/Java

JAVA 보안 Improper Neutralization of Script-Related HTML Tags in a Web Page

ALEPH.GEM 2022. 8. 17. 17:24

Improper Neutralization of Script-Related HTML Tags in a Web Page 크로스 사이트 스크립트 공격 취약점

정의

외부에서 입력되는 스크립트 문자열이 웹 페이지 생성에 사용되면 생성된 웹페이지를 열람하는 사용자에게 피해를 입힐 수 있습니다.

 

방어 방법

jsp의 document.write() 같은 DOM 객체 출력을 수행하는 메소드의 인자 값으로 외부 입력을 사용하면 위험한 문자를 제거해야 합니다.
보안성이 검증된 API를 사용하여 위험 문자열을 제거하십시오.

 

안전하지 않은 코드의 예

<%
String name = request.getParameter("name");
%>
<script>
document.write("name:"+<%=name%>);

외부 입력을 그대로 이용하고 있습니다.

 

 

안전한 코드의 예

<%
String name = request.getParameter("name");
//입력값의 유효성 검사
if(name != null){
    name = name.replaceAll("<","&lt");
    name = name.replaceAll(">","&gt");
    name = name.replaceAll("&","&amp;");
    name = name.replaceAll("\"","&quot;");
    name = name.replaceAll("\'","&#x27;");
    name = name.replaceAll("/","&#x2F;");
}

입력값에 대한 유효성 검사를 합니다.

 

 

안전한 코드의 예

<%
String eid = request.getParameter("eid");
String safeEID =  ESAPI.encoder().encodeForHTMLAttribute(name);
%>
Employee ID: <%= safeEID %>
//생략

owasp에서 제공하는 esapi 함수중 ESAPI.encoder().encdeForHTML Attribute를 이용하여 입력값을 검증한 후 사용합니다.

 

 

안전한 코드의 예

import com.josephoconnell.html.HTMLInputFilter;
//생략
public class RequestWrapper extends HttpServletRequestWrapper{
//생략
    private String filter(String input){
        String clean = new HTMLInputFilter().filter(input);
        return clean;
    }
}

http://josephoconnell.com/java/xss-html-filter/

 

XSS HTML Filter: A Java library for protecting against cross site scripting

HTML filtering utility for Java This utility is a single class, HTMLInputFilter, which can be used to parse user-submitted input and sanitize it against potential cross site scripting attacks, malicious html, or simply badly formed html. This version, writ

josephoconnell.com

에서 제공하는 XSS Filter를 이용한 입력값을 필터링 합니다.

 

 

 

참고

http://cwe.mitre.org/data/definitions/79.html

 

CWE - CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting') (4.8)

div.collapseblock { display:inline} CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')Weakness ID: 79Abstraction: BaseStructure: Simple The software does not neutralize or incorrectly neutralizes user-controllable

cwe.mitre.org

http://cwe.mitre.org/data/definitions/80.html

 

CWE - CWE-80: Improper Neutralization of Script-Related HTML Tags in a Web Page (Basic XSS) (4.8)

div.collapseblock { display:inline} CWE-80: Improper Neutralization of Script-Related HTML Tags in a Web Page (Basic XSS)Weakness ID: 80Abstraction: VariantStructure: Simple The software receives input from an upstream component, but it does not neutralize

cwe.mitre.org

 

 

 

 

728x90