Android

[Android]디바이스 루팅 체크

안드로이드 휴대폰 루팅 체크 방법

해마다 ISMS 인증 관련하여 보안업체에서 취약점에 대한 조사를 진행 합니다.

그런 후 회사에 통보합니다. 취약점 발생되었으니 수정해라.

안드로이드 디바이스 루팅 체크하는 로직입니다.

완벽하게 100% 체크한다고 확신할 수는 없습니다.

public class CellphoneRoutingCheck {

public static boolean checkSuperUser(){
        ULog.d(CellphoneRoutingCheck.class.getSimpleName(), "디바이스 루팅 체크 =========");
        return (checkRootedFiles() == true || checkSuperUserCommand() == true  || checkSuperUserCommand2() == true ||  checkTags() == true) ? true : false;
    }

private static boolean checkRootedFiles(){
final String[] files = {
"/sbin/su",
                "/system/su",
                "/system/bin/su",
                "/system/sbin/su",
                "/system/xbin/su",
                "/system/xbin/mu",
                "/system/bin/.ext/.su",
                "/system/usr/su-backup",
                "/data/data/com.noshufou.android.su",
                "/system/app/Superuser.apk",
                "/system/app/su.apk",
                "/system/bin/.ext",
                "/system/xbin/.ext",
                "/data/local/xbin/su",
                "/data/local/bin/su",
                "/system/sd/xbin/su",
                "/system/bin/failsafe/su",
                "/data/local/su",
                "/su/bin/su"};

        for(int i = 0; i<files.length; i++){
            File file = new File(files[i]);
            if(null != file && file.exists()){
                ULog.d(CellphoneRoutingCheck.class.getSimpleName(),"Rooted File : " + file.getAbsolutePath() + " : " + file.getName());
                return true;
            }
        }
return false;
    }

/*
    루팅이 된 기기는 일반적으로 Build.TAGS 값이 제조사 키값이 아닌 test 키 값을 가지고 있습니다.
    */
    private static boolean checkTags() {
        String buildTags = android.os.Build.TAGS;
        ULog.d("FROCS", "========== test-keys : " + buildTags.contains("test-keys"));
        if (buildTags != null && buildTags.contains("test-keys")) {
return true;
        }
return false;
    }

private static boolean checkSuperUserCommand(){
try {
            Runtime.getRuntime().exec("su");
            ULog.d(CellphoneRoutingCheck.class.getSimpleName(), "device has super user");
            return true;
        } catch (Error e){

        } catch (Exception e){

        }
return false;
    }

private static boolean checkSuperUserCommand2() {
        Process process = null;
        try {
            process = Runtime.getRuntime().exec(new String[] { "/system/xbin/which", "su" });
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            if (in.readLine() != null) return true;
            return false;
        } catch (Throwable t) {
return false;
        } finally {
if (process != null) process.destroy();
        }
    }

}
if(CellphoneRoutingCheck.checkSuperUser()) {}

빌드 태그 관련 참고하세요 : https://stackoverflow.com/questions/18808705/android-root-detection-using-build-tags

참고) 모바일기기 취약점진단 가이드 (M-T06 발췌)

※ 안드로이드에서 루팅을 감지하는 방법은 여러가지가 있다. 그중 파일 이름 기반의 탐지, su 명령어를 실행해서 확인하는 방법으로 감지하는데 일반적으로 su 파일이나 superuser.apk 등 루팅 시 생성되는 파일 이름을 기반으로 탐지한다.

Su 명령어의 경우 일반적으로 루팅되지 않은 안드로이드 디바이스에서는 su 명령어가 존재하지 않는다.

이때 su 명령을 직접 실행 시켜서 Exception이 발생여부에 따라 해당 디바이스의 루팅 여부를 감지할 수 있다.

Determine if running on a rooted device

Leave a Reply

error: Content is protected !!