在android kitkat错误上将文件复制到系统

时间:2014-04-24 12:02:12

标签: java android file permissions android-4.4-kitkat

我一直在使用这种方法获得su权限,运行echo命令并在android 4.1.2上复制文件但是因为我切换到kitkat它不再工作了。

当我运行我的应用程序时,它会显示su提示符,我可以设置su权限,没有任何问题。 当我想将回显值应用于系统文件或者我想将资产从应用程序复制到系统时,我遇到了问题。

这里是代码:

 // SU
public static String SU_wop(String cmds) {
    String out = null;
    try {
        out = new String();
        Process p = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        os.writeBytes(cmds+"\n");
        os.writeBytes("exit\n");
        os.flush();
        p.waitFor();
        InputStream stdout = p.getInputStream();
        byte[] buffer = new byte[4096];
        int read;
        while (true) {
            read = stdout.read(buffer);
            out += new String(buffer, 0, read);
            if (read < 4096) {
                break;
            }
        }
    } catch (Exception e) {
        final Activity activity = new Main();
        activity.runOnUiThread(new Runnable() {
            public void run() {
                Toast.makeText(activity, "Error Occured...!", Toast.LENGTH_SHORT).show();
            }
        });
        Log.e("cocoremanager", "Error executing SU command, flag:0x2");
    }
    return out.substring(0,out.length()-1);
}

//Copy files
public static void copyAssets(String script,String path,int mode,Context context) {
    AssetManager assetManager = context.getAssets();
    InputStream in = null;
    OutputStream out = null;
    try {
        in = assetManager.open(script);
        File outFile = new File(Environment.getExternalStorageDirectory().getPath(), script);
        out = new FileOutputStream(outFile);
        copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch(IOException e) {
        Log.e("cocoremanager", "Failed to handle: " + script, e);
    }
    mRunAsSU("mkdir -p " + path, "cp -f "+Environment.getExternalStorageDirectory().getPath()+"/"+script+" "+path+"/"+script, "rm "+Environment.getExternalStorageDirectory().getPath()+"/"+script, "chmod "+Integer.toString(mode)+" "+path+"/"+script);
}

 public static void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
        out.write(buffer, 0, read);
    }
}

 public static void mRunAsSU(String... cmds) {
    Process process;
    try {
        process = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        for(int i=0;i<cmds.length;i++)
            os.writeBytes(cmds[i]+"\n");
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
    } catch(Exception e) {
        Log.e("cocoretest", "Error executing....");
    }
}
}

 class SU extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... cmds) {
    try {
        Process process = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        for(int i=0;i<cmds.length;i++)
            os.writeBytes(cmds[i]+"\n");
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
    } catch (Exception e) {}
    return null;
}

1 个答案:

答案 0 :(得分:0)

我认为问题是Android 4.3引入的SELinux。请参阅:http://source.android.com/devices/tech/security/enhancements43.html

我认为没有简单的方法可以绕过SELinux。即使是需要root的流行应用程序也存在很大的问题,因为4.3