动态代码加载

OWASP 类别: MASVS-CODE:代码质量

概览

将代码动态加载到应用中会带来需要缓解的风险。攻击者可能会篡改或替换代码,以访问敏感数据或执行恶意操作。

许多形式的动态代码加载,尤其是使用远程来源的代码,违反 Google Play 政策,并可能导致您的应用被 Google Play 下架。

影响

如果攻击者设法访问将加载到应用中的代码,他们可以修改代码以达到其目的。这可能导致数据泄露和代码执行攻击。即使攻击者无法修改代码以执行其选择的任意操作,他们仍然可能损坏或移除代码,从而影响应用的可用性。

缓解措施

避免使用动态代码加载

除非有业务需要,否则应避免动态代码加载。尽可能将所有功能直接包含在应用中。

使用可信来源

将加载到应用中的代码应存储在可信位置。对于本地存储,建议使用应用内部存储或分区存储(适用于 Android 10 及更高版本)。这些位置采取了措施,可避免其他应用和用户直接访问。

从网址等远程位置加载代码时,应尽可能避免使用第三方,并遵循安全最佳实践将代码存储在自己的基础设施中。如果需要加载第三方代码,请确保提供方是可信的。

执行完整性检查

建议执行完整性检查,以确保代码未被篡改。在将代码加载到应用中之前,应执行这些检查。

加载远程资源时,可以使用子资源完整性来验证所访问资源的完整性。

从外部存储空间加载资源时,使用完整性检查来验证没有其他应用篡改过此数据或代码。文件的哈希值应以安全的方式存储,最好加密并存储在内部存储空间中。

Kotlin

package com.example.myapplication

import java.io.BufferedInputStream
import java.io.FileInputStream
import java.io.IOException
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException

object FileIntegrityChecker {
    @Throws(IOException::class, NoSuchAlgorithmException::class)
    fun getIntegrityHash(filePath: String?): String {
        val md = MessageDigest.getInstance("SHA-256") // You can choose other algorithms as needed
        val buffer = ByteArray(8192)
        var bytesRead: Int
        BufferedInputStream(FileInputStream(filePath)).use { fis ->
            while (fis.read(buffer).also { bytesRead = it } != -1) {
                md.update(buffer, 0, bytesRead)
            }

    }

    private fun bytesToHex(bytes: ByteArray): String {
        val sb = StringBuilder(bytes.length * 2)
        for (b in bytes) {
            sb.append(String.format("%02x", b))
        }
        return sb.toString()
    }

    @Throws(IOException::class, NoSuchAlgorithmException::class)
    fun verifyIntegrity(filePath: String?, expectedHash: String): Boolean {
        val actualHash = getIntegrityHash(filePath)
        return actualHash == expectedHash
    }

    @Throws(Exception::class)
    @JvmStatic
    fun main(args: Array<String>) {
        val filePath = "/path/to/your/file"
        val expectedHash = "your_expected_hash_value"
        if (verifyIntegrity(filePath, expectedHash)) {
            println("File integrity is valid!")
        } else {
            println("File integrity is compromised!")
        }
    }
}

Java

package com.example.myapplication;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class FileIntegrityChecker {

    public static String getIntegrityHash(String filePath) throws IOException, NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA-256"); // You can choose other algorithms as needed
        byte[] buffer = new byte[8192];
        int bytesRead;

        try (BufferedInputStream fis = new BufferedInputStream(new FileInputStream(filePath))) {
            while ((bytesRead = fis.read(buffer)) != -1) {
                md.update(buffer, 0, bytesRead);
            }
        }

        byte[] digest = md.digest();
        return bytesToHex(digest);
    }

    private static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        for (byte b : bytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }

    public static boolean verifyIntegrity(String filePath, String expectedHash) throws IOException, NoSuchAlgorithmException {
        String actualHash = getIntegrityHash(filePath);
        return actualHash.equals(expectedHash);
    }

    public static void main(String[] args) throws Exception {
        String filePath = "/path/to/your/file";
        String expectedHash = "your_expected_hash_value";

        if (verifyIntegrity(filePath, expectedHash)) {
            System.out.println("File integrity is valid!");
        } else {
            System.out.println("File integrity is compromised!");
        }
    }
}

签署代码

确保数据完整性的另一个选项是签署代码并在加载前验证其签名。这种方法的优势在于也能确保哈希码的完整性,而不仅仅是代码本身的完整性,这提供了额外的防篡改保护。

尽管代码签名提供了额外的安全层,但务必考虑到它是一个更复杂的过程,可能需要额外的努力和资源才能成功实现。

本文档的“资源”部分提供了一些代码签名的示例。

资源