본문 바로가기
Web Security/SecureCoding

무결성 점검 없는 코드 다운로드

by KkingKkang 2022. 11. 21.

- 원격으로부터 소스 코드 또는 실행파일을 무결성 검사 없이 다운로드하고, 이를 실행하는 경우 host server의 변조, DNS spoofing 또는 전송 시의 코드 변조 등의 방법을 이용하여 공격자가 악의적인 코드를 실행할 수 있다.

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

 

안전하지 않은 코드 ▼

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

 

 

안전한 코드 ▼

//서버에서는 private key를 가지고 MyClass를 암호화한다.
String jarFile = "./download/util.jar";
byte[] loadFile = FileManager.getBytes(jarFile);
loadFile = encrypt(loadFile, privateKey);

//jarFile 명으로 암호화된 파일을 생성한다.
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);

 

-공개키 방식의 암호알고리즘과 메커니즘을 이용하여 전송파일에 대한 시그니처를 생성하고, 파일의 변조 유무를 판단한다.

 

 

반응형

댓글