Skip to content

Commit

Permalink
Merge pull request #105 from w-shackleton/root-check
Browse files Browse the repository at this point in the history
Root check
  • Loading branch information
w-shackleton committed Oct 16, 2015
2 parents 887753a + 37d6f33 commit 5545477
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,9 @@ protected Dialog onCreateDialog(int id, final Bundle args) {
switch(id) {
case DIALOG_ROOT:
builder = new AlertDialog.Builder(this);
builder.setMessage("Please root your phone before using this application. Search the internet for instructions on how to do this for your phone.\nA custom firmware (such as CyanogenMod) is also recommended.\n" +
"If the 'su' application is somewhere else on your phone, please specify it in the settings.")
builder.setMessage(R.string.no_root)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { }
});
dialog = builder.create();
Expand Down Expand Up @@ -316,6 +315,7 @@ private void installFiles() {

fi.installScript("spoof", R.raw.spoof);
fi.installScript("collect_netconf", R.raw.collect_netconf);
fi.installScript("rootcheck", R.raw.rootcheck);

fi.installBinary("arp-scan");
fi.installBinary("arpspoof");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@
import uk.digitalsquid.netspoofer.config.LogConf;
import uk.digitalsquid.netspoofer.config.RunManager;
import uk.digitalsquid.netspoofer.misc.AsyncTaskHelper;
import uk.digitalsquid.netspoofer.root.RootChecker;
import uk.digitalsquid.netspoofer.servicemsg.ServiceMsg;
import uk.digitalsquid.netspoofer.servicemsg.SpoofStarter;
import uk.digitalsquid.netspoofer.servicestatus.InitialiseStatus;
import uk.digitalsquid.netspoofer.servicestatus.NewLogOutput;
import uk.digitalsquid.netspoofer.servicestatus.Notifyer;
import uk.digitalsquid.netspoofer.servicestatus.RootStatus;
import uk.digitalsquid.netspoofer.servicestatus.ServiceStatus;
import uk.digitalsquid.netspoofer.spoofs.Spoof;
import uk.digitalsquid.netspoofer.spoofs.SpoofData;
Expand All @@ -68,6 +70,8 @@ public class NetSpoofService extends Service implements LogConf {

private NotificationManager notificationManager;

private RootStatus mRootStatus;

public class NetSpoofServiceBinder extends Binder {
public NetSpoofService getService() {
return NetSpoofService.this;
Expand Down Expand Up @@ -161,6 +165,13 @@ private void sendLogOutput(NewLogOutput logOutput) {
sendBroadcast(intent);
}

/**
* Gets the current root status. Returns <code>null</code> if we haven't yet checked.
*/
public RootStatus getRootStatus() {
return mRootStatus;
}

private RunManager runner;

private final BlockingQueue<ServiceMsg> tasks = new LinkedBlockingQueue<ServiceMsg>();
Expand All @@ -170,13 +181,18 @@ private void sendLogOutput(NewLogOutput logOutput) {
@Override
protected Void doInBackground(HardwareConfig... params) {
Log.i(TAG, "Setting up system...");

// Check for root
publishProgress(mRootStatus =
new RootStatus(new RootChecker(NetSpoofService.this).check()));

publishProgress(new InitialiseStatus(STATUS_LOADED));
if(isCancelled()) {
Log.i(TAG, "Stop initiated, stopping...");
Log.i(TAG, "Done.");
return null;
}

// Main point. Process requests from task list.
boolean running = true;
while(running || !isCancelled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
import uk.digitalsquid.netspoofer.NetSpoofService.NetSpoofServiceBinder;
import uk.digitalsquid.netspoofer.config.LogConf;
import uk.digitalsquid.netspoofer.misc.CheckedLinearLayout;
import uk.digitalsquid.netspoofer.root.RootChecker;
import uk.digitalsquid.netspoofer.servicestatus.RootStatus;
import uk.digitalsquid.netspoofer.spoofs.Spoof;
import uk.digitalsquid.netspoofer.spoofs.Spoof.OnExtraDialogDoneListener;

Expand Down Expand Up @@ -154,6 +156,7 @@ public void onServiceConnected(ComponentName className, IBinder service) {
break;
case NetSpoofService.STATUS_LOADED:
setSpoofs(SpoofSelector.this.service.getSpoofs());
processRootStatus(SpoofSelector.this.service.getRootStatus());
break;
}
}
Expand All @@ -177,6 +180,7 @@ public void onReceive(Context context, Intent intent) {
if (!haveSpoofList && SpoofSelector.this.service != null) {
setSpoofs(SpoofSelector.this.service.getSpoofs());
}
processRootStatus(SpoofSelector.this.service.getRootStatus());
break;
case NetSpoofService.STATUS_FAILED:
try {
Expand Down Expand Up @@ -207,6 +211,14 @@ public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
});
startingDialog.show();
}

private void processRootStatus(RootStatus status) {
if (status.rootCheckResult != RootChecker.RootCheckResult.AVAILABLE) {
Bundle bundle = new Bundle();
bundle.putSerializable("status", status.rootCheckResult);
showDialog(DIALOG_NO_ROOT, bundle);
}
}

private SpoofListAdapter spoofListAdapter;

Expand Down Expand Up @@ -312,7 +324,8 @@ public void onDone() {
}

private static final int DIALOG_FAIL_LOAD = 1;

private static final int DIALOG_NO_ROOT = 2;

/**
* The ID used for result intents returned by custom spoofs.
*/
Expand All @@ -327,20 +340,51 @@ public void onDone() {
@Override
public Dialog onCreateDialog(int id, Bundle bundle) {
super.onCreateDialog(id, bundle);
Builder builder;
int msg = 0;
int title = 0;
switch(id) {
case DIALOG_FAIL_LOAD:
Builder builder = new Builder(this);
builder.setTitle(R.string.loadfailedtitle);
builder.setMessage(R.string.loadfailed);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
case DIALOG_FAIL_LOAD:
msg = R.string.loadfailed;
title = R.string.loadfailedtitle;
break;
case DIALOG_NO_ROOT:
RootChecker.RootCheckResult status =
(RootChecker.RootCheckResult) bundle.getSerializable("status");
title = R.string.no_root_title;
switch (status) {
case UNAVAILABLE:
msg = R.string.no_root;
break;
case AVAILABLE:
Log.e(TAG, "Hit AVAILABLE in error dialog");
break;
case BINARY_NO_EXEC:
msg = R.string.no_working_root;
break;
case ERROR:
default:
msg = R.string.loadfailed;
break;
}
});
return builder.create();
default: return null;
break;
default:
msg = 0;
title = 0;
}
if (title == 0 || msg == 0) {
return null;
}
builder = new Builder(this);
builder.setTitle(title);
builder.setMessage(msg);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
return builder.create();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* This file is part of Network Spoofer for Android.
* Network Spoofer lets you change websites on other people’s computers
* from an Android phone.
* Copyright (C) 2014 Will Shackleton <[email protected]>
*
* Network Spoofer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Network Spoofer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Network Spoofer, in the file COPYING.
* If not, see <http://www.gnu.org/licenses/>.
*/

package uk.digitalsquid.netspoofer.root;

import android.content.Context;
import android.util.Log;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import uk.digitalsquid.netspoofer.config.FileFinder;
import uk.digitalsquid.netspoofer.config.FileInstaller;
import uk.digitalsquid.netspoofer.config.LogConf;

/**
* Checks that the device has root access.
*/
public class RootChecker implements LogConf {
private Context mContext;

public RootChecker(Context context) {
mContext = context;
}

public enum RootCheckResult {
ERROR,
UNAVAILABLE,
BINARY_NO_EXEC,
AVAILABLE
}

public RootCheckResult check() {
try {
FileFinder.initialise(mContext);
} catch (FileNotFoundException e) {
// Handle error below
}
if (FileFinder.SU == null || FileFinder.SU == "") {
return RootCheckResult.UNAVAILABLE;
}

ProcessBuilder pb = new ProcessBuilder(FileFinder.SU, "-c",
FileInstaller.getScriptPath(mContext, "rootcheck"));

try {
Process p = pb.start();
BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stderr = new BufferedReader(new InputStreamReader(p.getInputStream()));
String idLine = stdout.readLine();

// Munch garbage
if (idLine != null) {
while (stdout.readLine() != null) ;
}
while (stderr.readLine() != null);

p.waitFor();

return idLine != null && idLine.contains("(root)") ?
RootCheckResult.AVAILABLE :
RootCheckResult.BINARY_NO_EXEC;
} catch (IOException e) {
Log.e(TAG, "Failed to run root check", e);
return RootCheckResult.ERROR;
} catch (InterruptedException e) {
Log.e(TAG, "Failed to run root check", e);
return RootCheckResult.ERROR;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* This file is part of Network Spoofer for Android.
* Network Spoofer lets you change websites on other people’s computers
* from an Android phone.
* Copyright (C) 2014 Will Shackleton <[email protected]>
*
* Network Spoofer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Network Spoofer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Network Spoofer, in the file COPYING.
* If not, see <http://www.gnu.org/licenses/>.
*/

package uk.digitalsquid.netspoofer.servicestatus;

import uk.digitalsquid.netspoofer.root.RootChecker;

public class RootStatus extends ServiceStatus {
public final RootChecker.RootCheckResult rootCheckResult;

public RootStatus(RootChecker.RootCheckResult rootCheckResult) {
this.rootCheckResult = rootCheckResult;
}
}
4 changes: 4 additions & 0 deletions androidnetspoof/src/main/res/raw/rootcheck
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/system/bin/sh
# Creates a file as root to verify that root access exists

id
6 changes: 6 additions & 0 deletions androidnetspoof/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
<string name="downloadingfiles">Downloading files needed to run&#8230;</string>
<string name="loadfailedtitle">Oops!</string>
<string name="loadfailed">Something went wrong whilst trying to load. Perhaps the SD card isn\'t plugged in, or you didn\'t accept the superuser command?\n\nCheck the log for more details.</string>
<string name="no_root_title">Unrooted device</string>
<string name="no_root">Please root your phone before using this application. Search the internet for instructions on how to do this for your phone.
A custom firmware (such as CyanogenMod) is also recommended.
If the \'su\' application is somewhere else on your phone, please specify it in the settings.
</string>
<string name="no_working_root">This device is capable of root access, but root access is not enabled. Please enable root access to use Network Spoofer.</string>
<string name="preferences">Preferences</string>
<string name="programPrefs">Programs</string>
<string name="downloadUnzipped">Download uncompressed installation files</string>
Expand Down

0 comments on commit 5545477

Please sign in to comment.