More "Fixes" Read commit

Added a promt (POC) to select shell.

    All new intents are working fine so far.

    We have our own permissions for Kali Nethunter.

    The app now use to open the correct window.

    Any other app developer can send commands to kali if the app uses this new perms. (ill document something)

    The terminal still have tons of little bugs.

     Note:

     This is a testing repo.
This commit is contained in:
jmingov 2015-11-20 04:29:52 +01:00
parent ca9d5194ec
commit e7a4bff9b9
12 changed files with 187 additions and 79 deletions

View File

@ -107,7 +107,9 @@ class GenericTermSession extends TermSession {
@Override
protected void onProcessExit() {
if (mSettings.closeWindowOnProcessExit()) {
notifyUpdate();
finish();
} else if (mProcessExitMessage != null) {
try {
@ -144,7 +146,7 @@ class GenericTermSession extends TermSession {
if (title != null && title.length() > 0) {
return defaultTitle +" "+ title;
} else {
return "Window (" + defaultTitle;
return defaultTitle + " No title";
}
}
@ -212,7 +214,6 @@ class GenericTermSession extends TermSession {
private static void cacheDescField() throws NoSuchFieldException {
if (descriptorField != null)
return;
descriptorField = FileDescriptor.class.getDeclaredField("descriptor");
descriptorField.setAccessible(true);
}

View File

@ -118,11 +118,11 @@ public class RemoteInterface extends Activity {
String path = ((Uri) extraStream).getPath();
File file = new File(path);
String dirPath = file.isDirectory() ? path : file.getParent();
openNewWindow("cd " + quoteForBash(dirPath));
openNewWindow("cd " + quoteForBash(dirPath), "i action send");
}
} else {
// Intent sender may not have permissions, ignore any extras
openNewWindow(null);
openNewWindow(null, "i action send");
}
finish();
@ -147,22 +147,22 @@ public class RemoteInterface extends Activity {
return builder.toString();
}
protected String openNewWindow(String iInitialCommand) {
Log.d("initialCommand","OPENSESAME");
protected String openNewWindow(String iInitialCommand, String mShell) {
Log.d("initialCommand",iInitialCommand);
TermService service = getTermService();
String initialCommand = mSettings.getInitialCommand();
if (!iInitialCommand.equals("")) {
if (!initialCommand.equals("")) {
Log.d("initialCommand",initialCommand);
initialCommand += "\r" + iInitialCommand;
initialCommand = iInitialCommand;
} else {
initialCommand = iInitialCommand;
}
}
try {
TermSession session = Term.createTermSession(this, mSettings, initialCommand);
TermSession session = Term.createTermSession(this, mSettings, initialCommand, mShell);
session.setFinishCallback(service);
service.getSessions().add(session);
@ -181,9 +181,9 @@ public class RemoteInterface extends Activity {
}
}
protected String appendToWindow(String handle, String iInitialCommand) {
protected String appendToWindow(String handle, String iInitialCommand, String mShell) {
TermService service = getTermService();
Log.d("appendToWindow", iInitialCommand + " <> " + handle);
// Find the target window
SessionList sessions = service.getSessions();
GenericTermSession target = null;
@ -199,12 +199,12 @@ public class RemoteInterface extends Activity {
if (target == null) {
// Target window not found, open a new one
return openNewWindow(iInitialCommand);
return openNewWindow(iInitialCommand, mShell);
}
if (iInitialCommand != null) {
target.write(iInitialCommand);
target.write('\r');
target.write('\n');
}
Intent intent = new Intent(PRIVACT_SWITCH_WINDOW);

View File

@ -31,7 +31,7 @@ public final class RunScript extends RemoteInterface {
private static final String EXTRA_WINDOW_HANDLE = "com.offsec.nhterm.window_handle";
private static final String EXTRA_INITIAL_COMMAND = "com.offsec.nhterm.iInitialCommand";
private static final String ANDROID_SHELL = "/system/bin/sh -";
@Override
protected void handleIntent() {
TermService service = getTermService();
@ -43,7 +43,7 @@ public final class RunScript extends RemoteInterface {
Intent myIntent = getIntent();
String action = myIntent.getAction();
if (action.equals(ACTION_RUN_SCRIPT)) {
Log.d("ACTION_RUN_SCRIPT","HOLAAA");
Log.d("ACTION_RUN_SCRIPT","ACTION_RUN_SCRIPT");
/* Someone with the appropriate permissions has asked us to
run a script */
String handle = myIntent.getStringExtra(EXTRA_WINDOW_HANDLE);
@ -55,10 +55,10 @@ public final class RunScript extends RemoteInterface {
if (handle != null) {
// Target the request at an existing window if open
handle = appendToWindow(handle, command);
handle = appendToWindow(handle, command, ShellType.ANDROID_SHELL);
} else {
// Open a new window
handle = openNewWindow(command);
handle = openNewWindow(command, ShellType.ANDROID_SHELL);
}
Intent result = new Intent();
result.putExtra(EXTRA_WINDOW_HANDLE, handle);

View File

@ -55,13 +55,13 @@ public final class RunScriptNetHunter extends RemoteInterface {
if (handle != null) {
// Target the request at an existing window if open
handle = appendToWindow(handle, command);
handle = appendToWindow(handle, command, ShellType.KALI_SHELL);
} else {
// Open a new window
handle = openNewWindow(command);
handle = openNewWindow(command, ShellType.KALI_SHELL);
}
Intent result = new Intent();
result.putExtra(EXTRA_WINDOW_HANDLE, 3);
result.putExtra(EXTRA_WINDOW_HANDLE, handle);
setResult(RESULT_OK, result);
finish();

View File

@ -55,13 +55,13 @@ public final class RunScriptSu extends RemoteInterface {
if (handle != null) {
// Target the request at an existing window if open
handle = appendToWindow(handle, command);
handle = appendToWindow(handle, command, ShellType.ANDROID_SU_SHELL);
} else {
// Open a new window
handle = openNewWindow(command);
handle = openNewWindow(command, ShellType.ANDROID_SU_SHELL);
}
Intent result = new Intent();
result.putExtra(EXTRA_WINDOW_HANDLE, 3);
result.putExtra(EXTRA_WINDOW_HANDLE, handle);
setResult(RESULT_OK, result);
finish();

View File

@ -67,10 +67,11 @@ public final class RunShortcut extends RemoteInterface {
String handle = myIntent.getStringExtra(EXTRA_WINDOW_HANDLE);
if (handle != null) {
// Target the request at an existing window if open
handle = appendToWindow(handle, command);
handle =
appendToWindow(handle, command, "shortcut");
} else {
// Open a new window
handle = openNewWindow(command);
handle = openNewWindow(command, "shortcut");
}
Intent result = new Intent();
result.putExtra(EXTRA_WINDOW_HANDLE, handle);

View File

@ -37,29 +37,31 @@ public class ShellTermSession extends GenericTermSession {
private Thread mWatcherThread;
private String mInitialCommand;
private String mInitialShell;
private static final int PROCESS_EXITED = 1;
private Handler mMsgHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (!isRunning()) {
Log.d("norunning EXIT","???");
return;
}
if (msg.what == PROCESS_EXITED) {
Log.d("noning SI EXIT","???");
onProcessExit((Integer) msg.obj);
}
}
};
public ShellTermSession(TermSettings settings, String initialCommand) throws IOException {
public ShellTermSession(TermSettings settings, String initialCommand, String _mInitialShell) throws IOException {
super(ParcelFileDescriptor.open(new File("/dev/ptmx"), ParcelFileDescriptor.MODE_READ_WRITE),
settings, false);
initializeSession();
initializeSession(_mInitialShell);
setTermOut(new ParcelFileDescriptor.AutoCloseOutputStream(mTermFd));
setTermIn(new ParcelFileDescriptor.AutoCloseInputStream(mTermFd));
mInitialShell = _mInitialShell;
mInitialCommand = initialCommand;
mWatcherThread = new Thread() {
@ -72,9 +74,10 @@ public class ShellTermSession extends GenericTermSession {
}
};
mWatcherThread.setName("Process watcher");
Log.d("STS: ^^",mInitialShell + " cmd: " + mInitialCommand);
}
private void initializeSession() throws IOException {
private void initializeSession(String mShell) throws IOException {
TermSettings settings = mSettings;
String path = System.getenv("PATH");
@ -98,8 +101,8 @@ public class ShellTermSession extends GenericTermSession {
env[0] = "TERM=" + settings.getTermType();
env[1] = "PATH=" + path;
env[2] = "HOME=" + settings.getHomePath();
mProcId = createSubprocess(settings.getShell(), env);
Log.d("Initialize Sess", settings.getShell());
mProcId = createSubprocess(mShell, env);
}
private String checkPath(String path) {
@ -125,15 +128,17 @@ public class ShellTermSession extends GenericTermSession {
private void sendInitialCommand(String initialCommand) {
if (initialCommand.length() > 0) {
write(initialCommand + '\r');
Log.d("CS: InitialCmd", initialCommand);
write(initialCommand + '\n');
}
}
private int createSubprocess(String shell, String[] env) throws IOException {
Log.d("CS: shell", shell);
ArrayList<String> argList = parse(shell);
String arg0;
String[] args;
try {
arg0 = argList.get(0);
File file = new File(arg0);
@ -155,6 +160,7 @@ public class ShellTermSession extends GenericTermSession {
}
private ArrayList<String> parse(String cmd) {
Log.d("CS parse: ", cmd);
final int PLAIN = 0;
final int WHITESPACE = 1;
final int INQUOTE = 2;
@ -208,6 +214,7 @@ public class ShellTermSession extends GenericTermSession {
@Override
public void finish() {
Log.d("noning FINISH","???");
hangupProcessGroup();
super.finish();
}

View File

@ -0,0 +1,13 @@
package com.offsec.nhterm;
// 3 types of promt: good old android, su and kali
// WHICH SU
// todo: Find a good way to get the paths
public class ShellType {
public static final String ANDROID_SHELL = "/system/bin/sh -";
public static final String ANDROID_SU_SHELL = "/system/xbin/su";
public static final String KALI_SHELL = "/system/xbin/su -c /system/bin/bootkali";
}

View File

@ -156,13 +156,19 @@ public class Term extends Activity implements UpdateCallback, SharedPreferences.
TermService.TSBinder binder = (TermService.TSBinder) service;
mTermService = binder.getService();
if (mPendingPathBroadcasts <= 0) {
populateViewFlipper();
populateWindowList();
// if(mTermSessions.size() > 0){
Log.d("ssssssss","Tamano = " + curLength);
Log.d("sssssss","Tamano = " + oldLength);
populateViewFlipper();
populateWindowList();
// }
}
}
public void onServiceDisconnected(ComponentName arg0) {
mTermService = null;
Log.d("onServiceDisconnected","onServiceDisconnected");
}
};
@ -186,7 +192,8 @@ public class Term extends Activity implements UpdateCallback, SharedPreferences.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("new getView", String.valueOf(position));
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));
label.setText(title);
@ -205,7 +212,7 @@ public class Term extends Activity implements UpdateCallback, SharedPreferences.
}
public void onUpdate() {
Log.d("new eq or minor", String.valueOf(mViewFlipper.getDisplayedChild()));
Log.d("onUpdate", String.valueOf(mViewFlipper.getDisplayedChild()));
mActionBar.setSelectedNavigationItem(mViewFlipper.getDisplayedChild());
notifyDataSetChanged();
}
@ -451,41 +458,39 @@ public class Term extends Activity implements UpdateCallback, SharedPreferences.
throw new IllegalStateException("Failed to bind to TermService!");
}
}
private void end_populateViewFlipper(){
mTermSessions.addCallback(this);
for (TermSession _session : mTermSessions) {
EmulatorView view = createEmulatorView(_session);
mViewFlipper.addView(view);
}
updatePrefs();
if (onResumeSelectWindow >= 0) {
//mViewFlipper.setDisplayedChild(onResumeSelectWindow);
onResumeSelectWindow = -1;
}
mViewFlipper.onResume();
}
private void populateViewFlipper() {
if (mTermService != null) {
mTermSessions = mTermService.getSessions();
if (mTermSessions.size() == 0) {
try {
mTermSessions.add(createTermSession());
} catch (IOException e) {
Toast.makeText(this, "Failed to start terminal session", Toast.LENGTH_LONG).show();
finish();
return;
}
show_shell_dialog("populateViewFlipper");
} else{
end_populateViewFlipper();
}
mTermSessions.addCallback(this);
for (TermSession session : mTermSessions) {
EmulatorView view = createEmulatorView(session);
mViewFlipper.addView(view);
}
updatePrefs();
if (onResumeSelectWindow >= 0) {
mViewFlipper.setDisplayedChild(onResumeSelectWindow);
onResumeSelectWindow = -1;
}
mViewFlipper.onResume();
}
}
private void populateWindowList() {
if (mActionBar == null) {
Log.d("populateWindowList","in null");
Log.d("populateWindowList", "in null");
return;
}
if (mTermSessions != null) {
@ -496,7 +501,7 @@ public class Term extends Activity implements UpdateCallback, SharedPreferences.
Log.d("populateWindowList", "in mWinListAdapter = null");
mActionBar.setListNavigationCallbacks(mWinListAdapter, mWinListItemSelected);
//mActionBar.setSelectedNavigationItem(position);
// POC sometimes not workin?
if(mTermSessions.getSelectedSession() == 0){
Log.d("populateWindowList", "curLength == null");
selectedTab = 0;
@ -579,17 +584,23 @@ public class Term extends Activity implements UpdateCallback, SharedPreferences.
finish();
}
protected static TermSession createTermSession(Context context, TermSettings settings, String initialCommand) throws IOException {
GenericTermSession session = new ShellTermSession(settings, initialCommand);
protected static TermSession createTermSession(Context context, TermSettings settings, String initialCommand, String _mShell) throws IOException {
Log.d("MM createTermSession", _mShell + "cmd: " + initialCommand);
GenericTermSession session = new ShellTermSession(settings, initialCommand, _mShell); // called from intents
// XXX We should really be able to fetch this from within TermSession
session.setProcessExitMessage(context.getString(R.string.process_exit_message));
return session;
}
private TermSession createTermSession() throws IOException {
Log.d("MM createTermSession", "inthreow");
TermSettings settings = mSettings;
TermSession session = createTermSession(this, settings, settings.getInitialCommand());
TermSession session = createTermSession(this, settings, "", "/system/bin/sh -");
session.setFinishCallback(mTermService);
return session;
}
@ -784,26 +795,91 @@ public class Term extends Activity implements UpdateCallback, SharedPreferences.
}
return super.onOptionsItemSelected(item);
}
private void show_shell_dialog(final String from){
Log.d("doCreateWin", "creating");
final TermSettings settings = mSettings;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
//alertDialogBuilder.setView(promptsView);
//alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setTitle("Select shell:");
alertDialogBuilder.setNegativeButton("Android",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d("CANCELED", "CANCELED");
TermSession session = null;
try {
session = createTermSession(getBaseContext(), settings, "", ShellType.ANDROID_SHELL);
session.setFinishCallback(mTermService);
} catch (IOException e) {
e.printStackTrace();
}
mTermSessions.add(session);
if(from == "doCreateNewWindow"){
end_doCreateNewWindow(session);
}
if(from == "populateViewFlipper"){
end_populateViewFlipper();
}
}
})
.setPositiveButton("AndroidSu",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d("Su", "Su");
TermSession session = null;
try {
session = createTermSession(getBaseContext(), settings, "", ShellType.ANDROID_SU_SHELL);
session.setFinishCallback(mTermService);
} catch (IOException e) {
e.printStackTrace();
}
mTermSessions.add(session);
if(from == "doCreateNewWindow"){
end_doCreateNewWindow(session);
}
if(from == "populateViewFlipper"){
end_populateViewFlipper();
};
}
})
.setNeutralButton("Kali",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d("Kali", "Kali");
TermSession session = null;
try {
session = createTermSession(getBaseContext(), settings, "", ShellType.KALI_SHELL);
session.setFinishCallback(mTermService);
} catch (IOException e) {
e.printStackTrace();
}
mTermSessions.add(session);
if(from.equals("doCreateNewWindow")){
end_doCreateNewWindow(session);
}
if(from.equals("populateViewFlipper")){
end_populateViewFlipper();
}
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
private void end_doCreateNewWindow(TermSession session){
TermView view = createEmulatorView(session);
view.updatePrefs(mSettings);
mViewFlipper.addView(view);
mViewFlipper.setDisplayedChild(mViewFlipper.getChildCount() - 1);
}
private void doCreateNewWindow() {
if (mTermSessions == null) {
Log.w(TermDebug.LOG_TAG, "Couldn't create new window because mTermSessions == null");
return;
}
try {
TermSession session = createTermSession();
mTermSessions.add(session);
TermView view = createEmulatorView(session);
view.updatePrefs(mSettings);
mViewFlipper.addView(view);
mViewFlipper.setDisplayedChild(mViewFlipper.getChildCount()-1);
} catch (IOException e) {
Toast.makeText(this, "Failed to create a session", Toast.LENGTH_SHORT).show();
}
Log.d("doCreateWin", "creating");
show_shell_dialog("doCreateNewWindow");
}
private void confirmCloseWindow() {
@ -840,11 +916,15 @@ public class Term extends Activity implements UpdateCallback, SharedPreferences.
mViewFlipper.removeView(view);
if (mTermSessions.size() != 0) {
mViewFlipper.showNext();
}else {
Log.d("NOSCREENS?","?NOSCREENS??");
}
}
@Override
protected void onActivityResult(int request, int result, Intent data) {
Log.d("onActivityResult?","?onActivityResult??");
switch (request) {
case REQUEST_CHOOSE_WINDOW:
if (result == RESULT_OK && data != null) {
@ -860,6 +940,7 @@ public class Term extends Activity implements UpdateCallback, SharedPreferences.
// Close the activity if user closed all sessions
// TODO the left path will be invoked when nothing happened, but this Activity was destroyed!
if (mTermSessions == null || mTermSessions.size() == 0) {
Log.d("but this ?","?but this Activity was destroyed!??");
mStopServiceOnFinish = true;
finish();
}
@ -1011,13 +1092,16 @@ public class Term extends Activity implements UpdateCallback, SharedPreferences.
public void onUpdate() {
SessionList sessions = mTermSessions;
if (sessions == null) {
Log.d("onupdateeeeee","sessions == null");
return;
}
if (sessions.size() == 0) {
Log.d("onupdateeeeee","tamano 0");
mStopServiceOnFinish = true;
finish();
} else if (sessions.size() < mViewFlipper.getChildCount()) {
Log.d("onupdateeeeee",sessions.size() + "sessions en el if " + mViewFlipper.getChildCount());
for (int i = 0; i < mViewFlipper.getChildCount(); ++i) {
EmulatorView v = (EmulatorView) mViewFlipper.getChildAt(i);
if (!sessions.contains(v.getTermSession())) {

View File

@ -19,6 +19,7 @@ package com.offsec.nhterm;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
@ -99,6 +100,7 @@ public class WindowListAdapter extends BaseAdapter implements UpdateCallback {
public void onUpdate() {
notifyDataSetChanged();
Log.d("onupdaye winlis", "changed");
}
private static Activity findActivityFromContext(Context context) {

View File

@ -14,8 +14,8 @@
<string name="pref_fnkey_default" translatable="false">4</string>
<string name="pref_ime_default" translatable="false">0</string>
<bool name="pref_alt_sends_esc_default">false</bool>
<string name="pref_shell_default" translatable="false">/system/xbin/su</string>
<string name="pref_initialcommand_default" translatable="false">/system/bin/bootkali</string>
<string name="pref_shell_default" translatable="false">/system/bin/sh -</string>
<string name="pref_initialcommand_default" translatable="false"></string>
<string name="pref_termtype_default" translatable="false">screen-256color</string>
<bool name="pref_close_window_on_process_exit_default">true</bool>
<bool name="pref_verify_path_default">true</bool>

View File

@ -15,7 +15,7 @@
* limitations under the License.
-->
<resources>
<string name="application_terminal">NetHunter Termj</string>
<string name="application_terminal">NetHunter Terminal</string>
<string name="preferences">Preferences</string>
<string name="new_window">New window</string>
<string name="close_window">Close window</string>