본문 바로가기

Security Study/Android

[InsecureBankv2] 취약한 암호화 실행

취약점 개요

취약점 설명

암호화 알고리즘에 대한 취약점으로, 중요 데이터 전송시 취약한 암호화 알고리즘을 이용하는 것을 의미한다. 취약한 알고리즘으로는 RC2, RC4, RC5, MD4, MD5, SHA1, DES, 3DES 등이 있다.

보안 위협

키값을 소지하지 않으므로 공격자가 똑같은 데이터를 만들고 변조가 가능하다.

발생 위치

LoginActivity.java

 

취약점 진단 과정

Step 1) LoginActivity.java 확인

username, password에 각각 아이디와 패스워드를 받아 저장한다. 아래의 if문을 보면, username은 Base64로 인코딩하여 저장하고, password는 aes 암호화를 진행하는 것을 확인할 수 있다. Base64는 암호화가 아니라 인코딩이므로, 쉽게 원래의 값을 알 수 있어 취약하다.

protected void fillData() throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {

    SharedPreferences settings = getSharedPreferences(MYPREFS, 0);
    final String username = settings.getString("EncryptedUsername", null);
    final String password = settings.getString("superSecurePassword", null);

    if(username!=null && password!=null)
    {
        byte[] usernameBase64Byte = Base64.decode(username, Base64.DEFAULT);
        try {
            usernameBase64ByteString = new String(usernameBase64Byte, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Username_Text = (EditText) findViewById(R.id.loginscreen_username);
        Password_Text = (EditText) findViewById(R.id.loginscreen_password);
        Username_Text.setText(usernameBase64ByteString);
        CryptoClass crypt = new CryptoClass();
        String decryptedPassword = crypt.aesDeccryptedString(password);
        Password_Text.setText(decryptedPassword);
    }

}

Step 2) CryptoClass.java 확인

AES 암호화에 사용되는 key가 하드 코딩 되어 있는 것을 확인할 수 있다. 키 값은 안전하게 외부 서버에 저장하여 보관해야 하고, 필요할 때 서버로부터 전달 받아 사용해야 한다. 또한, 초기화 벡터는 모두 0으로 설정되어 있는데, 암호화 알고리즘이 수행되기 전에 생성하고 수행한 후에는 0으로 설정해야 한다.

public class CryptoClass {

    String key = "This is the super secret key 123";

    byte[] ivBytes = {
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};
    String plainText;
    byte[] cipherData;
    String base64Text;
    String cipherText;

    public static byte[] aes256decrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes)
    throws UnsupportedEncodingException,
    NoSuchAlgorithmException,
    NoSuchPaddingException,
    InvalidKeyException,
    InvalidAlgorithmParameterException,
    IllegalBlockSizeException,
    BadPaddingException {

        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
        return cipher.doFinal(textBytes);
		
    }

}

 

대응 방안

  • 검증된 안전한 암호화 알고리즘을 사용해야 한다.
  • 키, 초기화 벡터는 하드 코딩하여 사용하지 않아야 한다.
반응형