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

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

JAVA 보안 Download of Code Without Integrity Check 본문

DEV&OPS/Java

JAVA 보안 Download of Code Without Integrity Check

ALEPH.GEM 2022. 8. 19. 16:27

Download of Code Without Integrity Check 무결성 체크를 하지 않은 코드 다운로드

원격으로부터 소스 코드 또는 실행파일을 무결성 검사 없이 다운받고 실행하게되면 host 서버의 변조, DNS spoofing또는 전송시 코드 변조를 통해 악의적인 코드를 실행할 수 있습니다.

 

방어 방법

자동 업데이트 처럼 다운로드 될 코드를 제공할 때는 코드에 대한 암호화된 시그니처를 사용하고 클라이언트가 시그니처를 검증하도록 합니다.

 

 

안전하지 않은 코드의 예

URL[] classURLs= new URL[]{new URL("file:subdir/")};
URLClassLoader loader = new URLClassLoader(classURLs);
Class loadedClass = Class.forName("MyClass", true, loader);

URLClassLoader를 사용해 원격 파일을 다운로드 하지만 무결성 검사를 수행하지 않았습니다.

 

 

안전한 코드의 예

공개키 암호 방식으로 개인키로 암호화 하여 다운 받게 하고 공개키로 복호화 하게 하여 사용합니다.

//서버에서 개인키로 MyClass를 암호화
String jarFile = "./download/util.jar";
byte[] loadFile = FileManager.getBytes(jarFile);
loadFile = encrypt(loadFile,privateKey);

//암호화된 파일을 생성
FileManager.createFile(loadFile,jarFileName);

//클라이언트에서 파일을 다운 받은 후 public key로 복호화
URL[] classURLs= new URL[]{new URL("http://filesave.com/download/util.jar")};
URLConnection conn=classURLs.openConnection();
InputStream is = conn.getInputStream();

//입력 스트림을 읽어 서버의 jarFile 명으로 파일을 출력
FileOutputStream fos = new FileOutputStream(new File(jarFile));
while ( is.read(buf) != -1 ){
    //생략
}
byte[] loadFile = FileManager.getBytes(jarFile);
//복호화
loadFile = decrypt(loadFile,publicKey);
//복호화된 파일 생성
FileManager.createFile(loadFile,jarFile);
URLClassLoader loader = new URLClassLoader(classURLs);
Class loadedClass = Class.forName("MyClass", true, loader);

 

 

안전하지 않은 코드의 예

public class DownloadFile{
    public boolean download(String localPath, String remoteURL){
        try{
            URL url = new URL(remoteURL);
            url.openConnection();
            InputStream reader = url.openStream();
            FileOutputStream writer = new FileOutputStream(localPath);
            byte[] buffer = new byte[BUFFER_SIZE];
            int totalBytesRead = 0;
            int bytesRead = 0;
            
            while ((bytesRead = reader.read(buffer)) > 0){
                writer.write(buffer, 0, bytesRead);
                totalBytesRead += bytesRead;
            }
            
            writer.close();
            reader.close();
        }
//생략

파일 다운로드에 대하여 무결성 검사를 하고 있지 않습니다.

 

 

안전한 코드의 예

다운로드 파일에 대하여 체크섬 검사(해시값 체크)를 합니다.

파일이 변조되면 체크섬 값(해시값)이 달라지기 때문입니다.

public class DownloadFile {
    public boolean download(String localPath, String remoteURL, String checksumURL){
        _download(checksumFilePath, checksumURL);
        decrypt(checksumFilePath, publicKey);
        _download(localPath, remoteURL);
        return checkCheckSum(localPath, checksumFilePath);
    }
    
    private void _download(String localPath, String remoteURL){
        try{
            URL url = new URL(remoteURL);
            url.openConnection();
            InputStream reader = url.openStream();
            
            FileOutputStream writer = new FileOutputStream(localPath);
            byte[] buffer = new byte[BUFFER_SIZE];
            int totalBytesRead = 0;
            int bytesRead = 0;
            while ((bytesRead = reader.read(buffer)) > 0){
                writer.write(buffer, 0, bytesRead);
                totalBytesRead += bytesRead;
            }
            writer.close();
            reader.close();
        }
//생략

 

 

참고

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

 

CWE - CWE-494: Download of Code Without Integrity Check (4.8)

div.collapseblock { display:inline} CWE-494: Download of Code Without Integrity CheckWeakness ID: 494Abstraction: BaseStructure: Simple The product downloads source code or an executable from a remote location and executes the code without sufficiently ver

cwe.mitre.org

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90

'DEV&OPS > Java' 카테고리의 다른 글

Spring boot todo - Gradle 라이브러리 관리  (0) 2022.08.29
Spring boot todo 프로젝트 세팅  (0) 2022.08.26
JAVA 보안 Unsafe Reflection  (0) 2022.08.19
JAVA 보안 Process Control  (0) 2022.08.18
JAVA (javascript)보안 Eval Injection  (0) 2022.08.18