본문 바로가기
Hack/Mobile

[Android] java.net.URL Parsing 취약점

by Becoming a Hacker 2022. 12. 16.
반응형

java.net.URL 클래스를 사용하는 경우 Parsing된 Host브라우저를 통하여 접속되는 Host가 달라 Bypass가 가능한 취약점이 존재합니다.

 

Sample Code

import java.net.URL;

public String url_bypass(HttpServletRequest request) throws Exception{
    String url = request.getParameter("url");
    System.out.println("url:  " + url);
    URL u = new URL(url);
    
    if (!u.getProtocol().startsWith("http") && !u.getProtocol().startsWith("https")) {
        return "Url is not http or https";
    }
    String host = u.getHost().toLowerCase();
    System.out.println("host:  " + host);

    if (host.endsWith("." + "naver.com")) {
        return "good url";
    } else {
        return "bad url";
    }
}

 

만약 위와 같은 Code가 존재할 경우, getHost()로 Parsing하는 Host와 Chrome을 통하여 접속하는 Host가 달라지는 URL이 존재하여 검증 로직을 Bypass할 수 있습니다.

  • 정상으로 표시된 URL들도 환경에 따라 Bypass할 수 있는 공격 구문들임
URL  Host Parsing 결과 Chrome 접속 결과 결과
hacksms.tistory.com hacksms.tistory.com 정상
hacksms.tistroy.com\www.naver.com hacksms.tistory.com 정상
www.naver.com hacksms.tistory.com 취약
hacksms.tistory.com hacksms.tistory.com 정상

 

보통 이런 Code는 WebView load() 이전에 Host를 검증하는 로직에 존재하는데, 해당 취약점으로 인하여 임의의 URL을 Redirect 시킬 수 있게 됩니다.

 

조치 방안

가장 쉬운 조치 방안은 취약한 Class인 java.net.URLandroid.net.URI을 사용하지 않고, 안전한 Class인 java.net.URI을 사용하는 것 입니다.

반응형

댓글