Check for root before running kali/androidsu

We need to make sure we have root before trying to run kali or androidsu.  This provides more feedback to users if something is not working.

Also, make sure we have root before we check for symlinks.

Signed-off-by: binkybear <binkybear@nethunter.com>
This commit is contained in:
binkybear 2016-09-30 18:47:33 -05:00
parent c2b1d28f36
commit 2a5c389d04
3 changed files with 80 additions and 11 deletions

View File

@ -0,0 +1,35 @@
package com.offsec.nhterm;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
/** @author Kevin Kowalewski */
public class CheckRoot {
public static boolean isDeviceRooted() {
return checkRootMethod2() || checkRootMethod3();
}
private static boolean checkRootMethod2() {
String[] paths = { "/system/app/Superuser.apk", "/sbin/su", "/system/bin/su", "/system/xbin/su", "/data/local/xbin/su", "/data/local/bin/su", "/system/sd/xbin/su",
"/system/bin/failsafe/su", "/data/local/su", "/su/bin/su"};
for (String path : paths) {
if (new File(path).exists()) return true;
}
return false;
}
private static boolean checkRootMethod3() {
Process process = null;
try {
process = Runtime.getRuntime().exec(new String[] { "/system/xbin/which", "su" });
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
return in.readLine() != null;
} catch (Throwable t) {
return false;
} finally {
if (process != null) process.destroy();
}
}
}

View File

@ -1,20 +1,19 @@
package com.offsec.nhterm;
// 3 types of promt: good old android, su and kali
// 3 types of prompt: good old android, su and kali
// WHICH SU
// todo: Find a good way to get the paths
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
public class ShellType {
public static final String ANDROID_SHELL = whichCMD("sh") + " -";
public static final String ANDROID_SU_SHELL = whichCMD("su");
public static final String KALI_SHELL = whichCMD("su") + " -c /system/bin/bootkali";
public static final String KALI_LOGIN_SHELL = whichCMD("su") +" -c /system/bin/bootkali_login";
class ShellType {
static final String ANDROID_SHELL = whichCMD("sh") + " -";
static final String ANDROID_SU_SHELL = whichCMD("su");
static final String KALI_SHELL = whichCMD("su") + " -c /system/bin/bootkali";
static final String KALI_LOGIN_SHELL = whichCMD("su") +" -c /system/bin/bootkali_login";
private static String whichCMD(String theCmd){
String output = null;

View File

@ -200,7 +200,7 @@ public class Term extends Activity implements UpdateCallback, SharedPreferences.
Log.d("mPendingPathBroadcasts","Tamano = " + mTermService.getSessions().size());
Log.d("mPendingPathBroadcasts","Tamano = " + oldLength);
TextView label = new TextView(Term.this);
String title = getSessionTitle(position, getString(R.string.window_title, position + 1));
@SuppressLint("StringFormatInvalid") String title = getSessionTitle(position, getString(R.string.window_title, position + 1));
label.setText(title);
if (AndroidCompat.SDK >= 13) {
label.setTextAppearance(Term.this, TextAppearance_Holo_Widget_ActionBar_Title);
@ -811,6 +811,24 @@ public class Term extends Activity implements UpdateCallback, SharedPreferences.
}
return super.onOptionsItemSelected(item);
}
private void show_nosupersu(){
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setMessage("No SU binary found! Missing root!");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
private void show_shell_dialog(final String from){
Log.d("doCreateWin", "creating");
final TermSettings settings = mSettings;
@ -849,19 +867,28 @@ public class Term extends Activity implements UpdateCallback, SharedPreferences.
public void onClick(DialogInterface dialog, int id) {
Log.d("Su", "Su");
TermSession session = null;
if(CheckRoot.isDeviceRooted()){
Log.d("isDeviceRooted","Device is rooted!");
try {
session = createTermSession(getBaseContext(), settings, "", ShellType.ANDROID_SU_SHELL);
session.setFinishCallback(mTermService);
} catch (IOException e) {
e.printStackTrace();
}
}
mTermSessions.add(session);
if(Objects.equals(from, "doCreateNewWindow")){
end_doCreateNewWindow(session);
}
}
if(Objects.equals(from, "populateViewFlipper")){
end_populateViewFlipper();
};
}
} else {
// ALERT! WHY YOU NO ROOT!
Log.d("isDeviceRooted","Device is not rooted!");
show_nosupersu();
}
}
})
.setNeutralButton("Kali",
@ -869,6 +896,9 @@ public class Term extends Activity implements UpdateCallback, SharedPreferences.
public void onClick(DialogInterface dialog, int id) {
Log.d("Kali", "Kali");
if(CheckRoot.isDeviceRooted()){
Log.d("isDeviceRooted","Device is rooted!");
String filename = "/system/bin/bootkali_login";
String chroot_dir = "/data/local/nhsystem/kali-armhf"; // Not sure if I can wildcard this
@ -898,6 +928,11 @@ public class Term extends Activity implements UpdateCallback, SharedPreferences.
} catch (IOException e) {
e.printStackTrace();
}
} else {
// ALERT! WHY YOU NO ROOT!
Log.d("isDeviceRooted","Device is not rooted!");
show_nosupersu();
}
}
});