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

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

JAVA 보안 Unsafe Reflection 본문

DEV&OPS/Java

JAVA 보안 Unsafe Reflection

ALEPH.GEM 2022. 8. 19. 09:29

Unsafe Reflection 안전하지 않은 리플렉션

동적 클래스 loading시 외부의 입력을 사용할 경우, 공격자가 외부 입력을 변조하여 의도하지 않은 클래스가 로딩되도록 할 수 있습니다.

 

방어 방법

외부의 입력을 직접 클래스 이름으로 사용하지 말고, white 리스트에서 클래스 이름을 선택하도록 합니다.

 

 

안전하지 않은 코드의 예

String type = props.getProperty("type");
Worker w;

try{
    Class workClass = Class.forName(type + "Worker");
    w = (Worker) workClass.newInstance();
    w.doAction();
}catch (ClassNotFoundException e) { …… }

5라인 에서 외부입력을 클래스 이름의 일부로 사용하여 안전하지 않습니다.

 

 

안전한 코드의 예

String type = props.getProperty("type");
Worker w;

//외부 입력값에 대해 유효성 검증
if (type == null || "".equals(type)) return;
if (type.equals("Slow")){
    w = new SlowWorker();
    w.doAction();
}else if (type.equals("Hard")){
    w = new HardWorker();
    w.doAction();
}else{
    System.err.printf("No propper class name!");
}

 

 

 

참고

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

 

CWE - CWE-470: Use of Externally-Controlled Input to Select Classes or Code ('Unsafe Reflection') (4.8)

div.collapseblock { display:inline} CWE-470: Use of Externally-Controlled Input to Select Classes or Code ('Unsafe Reflection')Weakness ID: 470Abstraction: BaseStructure: Simple The application uses external input with reflection to select which classes or

cwe.mitre.org

 

 

 

 

 

 

728x90