Browse Source

improved settings; and fixed SMB

Alexander Brakowski 10 years ago
parent
commit
eca13999b5

+ 21 - 28
res/xml/settings_preferences.xml

@@ -17,39 +17,32 @@
 				android:title="@string/pref_alarm" />
 	</PreferenceCategory>
 
+	<PreferenceCategory android:title="@string/pref_storage" >
+		<EditTextPreference
+				android:key="pref_external_location"
+				android:defaultValue="/HosTaGe/LogFiles/"
+				android:title="@string/pref_external_location_title"
+				/>
+
+		<Preference
+				android:key="pref_external_export"
+				android:title="@string/export_to_location"
+				/>
+
+	</PreferenceCategory>
+	<PreferenceCategory android:title="@string/pref_upload" >
+		<EditTextPreference
+				android:key="pref_upload_server"
+				android:defaultValue="https://ssi.cased.de"
+				android:title="@string/pref_upload_server" />
+
+	</PreferenceCategory>
+
 	<PreferenceCategory android:title="@string/advanced_settings">
 		<PreferenceScreen
 				android:key="pref_advanced settings"
 				android:title="@string/advanced_settings"
 				android:persistent="false">
-			<PreferenceCategory android:title="@string/pref_storage" >
-				<CheckBoxPreference
-						android:defaultValue="false"
-						android:key="pref_external_storage"
-						android:summary="@string/pref_external_storage_summ"
-						android:title="@string/pref_external_storage_title" />
-
-				<EditTextPreference
-						android:key="pref_external_location"
-						android:dependency="pref_external_storage"
-						android:defaultValue="/HosTaGe/LogFiles/"
-						android:title="@string/pref_external_location_title"
-						/>
-
-				<Preference
-						android:key="pref_external_export"
-						android:dependency="pref_external_storage"
-				        android:title="@string/export_to_location"
-				        />
-
-			</PreferenceCategory>
-			<PreferenceCategory android:title="@string/pref_upload" >
-				<EditTextPreference
-						android:key="pref_upload_server"
-						android:defaultValue="https://ssi.cased.de"
-						android:title="@string/pref_upload_server" />
-
-			</PreferenceCategory>
 			<PreferenceCategory android:title="@string/pref_connection_settings" >
 				<EditTextPreference
 						android:key="pref_max_connections"

+ 31 - 5
src/de/tudarmstadt/informatik/hostage/Hostage.java

@@ -236,6 +236,16 @@ public class Hostage extends Service {
 		return isRunning(protocolName, port);
 	}
 
+	public boolean isRunningAnyPort(String protocolName){
+		for(Listener listener: listeners){
+			if(listener.getProtocolName().equals(protocolName)){
+				if(listener.isRunning()) return true;
+			}
+		}
+
+		return false;
+	}
+
 	/**
 	 * Determines if a protocol with the given name is running on the given
 	 * port.
@@ -268,7 +278,7 @@ public class Hostage extends Service {
 		// Send Notification
 		if (sender.equals(Handler.class.getName()) && values[0].equals(getString(R.string.broadcast_started))) {
 			this.mProtocolActiveAttacks.put(values[1], true);
-			//attackNotification();
+			attackNotification();
 		}
 		// Inform UI of Preference Change
 		Intent intent = new Intent(getString(R.string.broadcast));
@@ -408,6 +418,16 @@ public class Hostage extends Service {
 		// " SERVICE STOPPED!", Toast.LENGTH_SHORT).show();
 	}
 
+	public void stopListenerAllPorts(String protocolName){
+		for(Listener listener: listeners){
+			if(listener.getProtocolName().equals(protocolName)){
+				if(listener.isRunning()){
+					listener.stop();
+					mProtocolActiveAttacks.remove(protocolName);
+				}
+			}
+		}
+	}
 	/**
 	 * Stops all running listeners.
 	 */
@@ -434,8 +454,11 @@ public class Hostage extends Service {
 		Intent launchIntent = new Intent(getApplicationContext(), MainActivity.class);
 		TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
 		stackBuilder.addParentStack(MainActivity.class);
-		stackBuilder.addNextIntent(launchIntent);
-		PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
+		Intent intent = MainActivity.getInstance().getIntent();
+		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
+		intent.setAction("SHOW_HOME");
+		stackBuilder.addNextIntent(intent);
+		PendingIntent resultPendingIntent = PendingIntent.getActivity(MainActivity.context, 0, intent, 0);
 		builder.setContentIntent(resultPendingIntent);
 		if (defaultPref.getBoolean("pref_vibration", false)) {
 			builder.setVibrate(new long[] { 100, 200, 100, 200 });
@@ -512,8 +535,11 @@ public class Hostage extends Service {
 		Intent launchIntent = new Intent(getApplicationContext(), MainActivity.class);
 		TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
 		stackBuilder.addParentStack(MainActivity.class);
-		stackBuilder.addNextIntent(launchIntent);
-		PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
+		Intent intent = MainActivity.getInstance().getIntent();
+		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
+		intent.setAction("SHOW_HOME");
+		stackBuilder.addNextIntent(intent);
+		PendingIntent resultPendingIntent = PendingIntent.getActivity(MainActivity.context, 0, intent, 0); //stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
 		builder.setContentIntent(resultPendingIntent);
 		builder.setOngoing(true);
 		NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

+ 1 - 1
src/de/tudarmstadt/informatik/hostage/model/Profile.java

@@ -102,7 +102,7 @@ public class Profile implements JSONSerializable<Profile> {
 			return this.mIcon;
 		}
 
-		if(!this.mIconName.isEmpty()){
+		if(this.mIconName != null && !this.mIconName.isEmpty()){
 			this.mIconId = MainActivity.context.getResources().getIdentifier(this.mIconName,
 					"drawable", "de.tudarmstadt.informatik.hostage");
 			this.mIcon = BitmapFactory.decodeResource(MainActivity.context.getResources(), this.mIconId);

+ 50 - 4
src/de/tudarmstadt/informatik/hostage/persistence/ProfileManager.java

@@ -5,6 +5,7 @@ import org.json.JSONException;
 import org.json.JSONObject;
 
 import android.content.Context;
+import android.content.SharedPreferences;
 
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -21,6 +22,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.Random;
 
+import de.tudarmstadt.informatik.hostage.Hostage;
+import de.tudarmstadt.informatik.hostage.Listener;
 import de.tudarmstadt.informatik.hostage.R;
 import de.tudarmstadt.informatik.hostage.model.Profile;
 import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
@@ -70,6 +73,9 @@ public class ProfileManager {
 	 */
 	public HashMap<Integer, Profile> mProfiles;
 
+	private SharedPreferences mSharedPreferences;
+	private SharedPreferences.Editor mSharedEditor;
+
 	/**
 	 * Since the profile manager should only have one instance in the whole app, we are using the singleton pattern.
 	 * This method creates a new instance of the profile manager, if no instance already exists, and returns it.
@@ -93,6 +99,10 @@ public class ProfileManager {
 	 */
 	private ProfileManager(){
 		mProfiles = new HashMap<Integer, Profile>();
+
+		String sharedPreferencePath = MainActivity.getContext().getString(R.string.shared_preference_path);
+		mSharedPreferences = MainActivity.getContext().getSharedPreferences(sharedPreferencePath, Hostage.MODE_PRIVATE);
+		mSharedEditor = mSharedPreferences.edit();
 	}
 
 	/**
@@ -145,7 +155,7 @@ public class ProfileManager {
 				}
 
 				if(p.mActivated){
-					this.mCurrentActivatedProfile = p;
+					activateProfile(p, false);
 				}
 
 				if(p.mIsRandom){
@@ -355,12 +365,21 @@ public class ProfileManager {
 		persistData();
 	}
 
+	/**
+	 * Same as {@see activateProfile(Profile profile, boolean persist)} but with persist arg being always true
+	 * @param profile the profile to active
+	 */
+	public void activateProfile(Profile profile){
+		this.activateProfile(profile, true);
+	}
+
 	/**
 	 * Makes a given profile active.
 	 *
 	 * @param profile the profile to activate
+	 * @param persist indicates if the profile should be persisted after activating
 	 */
-	public void activateProfile(Profile profile){
+	public void activateProfile(Profile profile, boolean persist){
 		if(profile.equals(this.mCurrentActivatedProfile)) return;
 
 		if(this.mCurrentActivatedProfile != null){
@@ -371,7 +390,33 @@ public class ProfileManager {
 		profile.mActivated = true;
 		this.mCurrentActivatedProfile = profile;
 		this.mProfiles.put(profile.mId, profile);
-		persistData();
+
+		mSharedEditor.putString("os", profile.mLabel);
+		mSharedEditor.commit();
+
+		if(persist) persistData();
+
+		if(this.mProfileListAdapter != null){
+			this.mProfileListAdapter.notifyDataSetChanged();
+		}
+
+		if(MainActivity.getInstance().getHostageService() != null){
+			if(MainActivity.getInstance().getHostageService().hasRunningListeners()){
+				List<String> protocolsToStart = profile.getActiveProtocols();
+
+				for(Listener listener: MainActivity.getInstance().getHostageService().getListeners()){
+					if(listener.isRunning()){
+						if(protocolsToStart.contains(listener.getProtocolName())){
+							protocolsToStart.remove(listener.getProtocolName());
+						} else {
+							MainActivity.getInstance().getHostageService().stopListenerAllPorts(listener.getProtocolName());
+						}
+					}
+				}
+
+				MainActivity.getInstance().startMonitorServices(protocolsToStart);
+			}
+		}
 	}
 
 	/**
@@ -554,7 +599,8 @@ public class ProfileManager {
 		this.addProfile(paranoidProfile, false);
 
 		mIncrementValue = 8;
-		this.mCurrentActivatedProfile = paranoidProfile;
+
+		this.activateProfile(paranoidProfile, false);
 
 		persistData();
 	}

+ 1 - 1
src/de/tudarmstadt/informatik/hostage/protocol/SMB.java

@@ -55,7 +55,7 @@ public class SMB implements Protocol {
 	}
 	
 	public void setIP(String ip) {
-		new NMB(ip, "ABCDEFG", "WORKGROUPA").start();
+		/*new NMB(ip, "ABCDEFG", "WORKGROUPA").start();*/
 	}
 
 	private String[] initServerVersion() {

+ 29 - 12
src/de/tudarmstadt/informatik/hostage/ui2/activity/MainActivity.java

@@ -2,6 +2,7 @@ package de.tudarmstadt.informatik.hostage.ui2.activity;
 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 
 import android.app.ActionBar;
 import android.app.Activity;
@@ -32,6 +33,8 @@ import android.widget.AdapterView;
 import android.widget.ListView;
 import de.tudarmstadt.informatik.hostage.Hostage;
 import de.tudarmstadt.informatik.hostage.R;
+import de.tudarmstadt.informatik.hostage.model.Profile;
+import de.tudarmstadt.informatik.hostage.persistence.ProfileManager;
 import de.tudarmstadt.informatik.hostage.ui.LogFilter;
 import de.tudarmstadt.informatik.hostage.ui2.adapter.DrawerListAdapter;
 import de.tudarmstadt.informatik.hostage.ui2.fragment.AboutFragment;
@@ -76,6 +79,8 @@ public class MainActivity extends Activity {
 
 	private boolean mServiceBound = false;
 
+	private ProfileManager mProfileManager;
+
 	/**
 	 * Connection to bind the background service
 	 * 
@@ -146,11 +151,12 @@ public class MainActivity extends Activity {
 		super.onCreate(savedInstanceState);
 
 		sInstance = this;
-
 		MainActivity.context = getApplicationContext();
 
 		setContentView(R.layout.activity_drawer_main);
 
+		mProfileManager = ProfileManager.getInstance();
+
 		ThreatIndicatorGLRenderer.assets = getAssets();
 		ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING);
 		// set background color
@@ -402,7 +408,11 @@ public class MainActivity extends Activity {
 
 			getFragmentManager().popBackStackImmediate(HomeFragment.class.getName(), 0);
 			// update selected item and title, then close the drawer if needed
-			if(!(fragment instanceof HomeFragment) || isFirst) injectFragment(fragment);// , false, menuItemPosition);
+			if(!(fragment instanceof HomeFragment) || isFirst) {
+				injectFragment(fragment);// , false, menuItemPosition);
+			} else {
+				this.mDisplayedFragment = fragment;
+			}
 
 			mDrawerList.setItemChecked(position, true);
 			mDrawerList.setSelection(position);
@@ -497,16 +507,6 @@ public class MainActivity extends Activity {
 		return super.onKeyDown(keycode, e);
 	}
 
-	public static void displayBackStack(FragmentManager fm) {
-		int count = fm.getBackStackEntryCount();
-		Log.d("Backstack log", "There are " + count + " entries");
-		for (int i = 0; i < count; i++) {
-			// Display Backstack-entry data like
-			String name = fm.getBackStackEntryAt(i).getName();
-			Log.d("Backstack log", "entry " + i + ": " + name);
-		}
-	}
-
 	public Intent getServiceIntent() {
 		return new Intent(this, Hostage.class);
 	}
@@ -533,6 +533,23 @@ public class MainActivity extends Activity {
 		return false;
 	}
 
+	public void startMonitorServices(List<String> protocols){
+		for(String protocol: protocols){
+			if(protocol.equals("GHOST")){
+				if(mProfileManager.getCurrentActivatedProfile() != null){
+					Profile profile = mProfileManager.getCurrentActivatedProfile();
+					if(profile.mGhostActive){
+						for(int port: profile.getGhostPorts()){
+							if(!getHostageService().isRunning(protocol, port)) getHostageService().startListener(protocol, port);
+						}
+					}
+				}
+			} else {
+				if(!getHostageService().isRunning(protocol)) getHostageService().startListener(protocol);
+			}
+		}
+	}
+
 	public enum MainMenuItem {
 		HOME(0, HomeFragment.class),
 		THREAT_MAP(1, ThreatMapFragment.class),

+ 9 - 21
src/de/tudarmstadt/informatik/hostage/ui2/fragment/HomeFragment.java

@@ -1,6 +1,9 @@
 package de.tudarmstadt.informatik.hostage.ui2.fragment;
 
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
 
 import android.annotation.SuppressLint;
 import android.app.Activity;
@@ -296,12 +299,7 @@ public class HomeFragment extends Fragment {
 						if (isChecked) {
 							boolean protocolActivated = false;
 							if (ProfileManager.getInstance().getCurrentActivatedProfile() == null) {
-								for (String protocol : getResources().getStringArray(R.array.protocols)) {
-									if (!MainActivity.getInstance().getHostageService().isRunning(protocol)) {
-										MainActivity.getInstance().getHostageService().startListener(protocol);
-										protocolActivated = true;
-									}
-								}
+								MainActivity.getInstance().startMonitorServices(Arrays.asList(getResources().getStringArray(R.array.protocols)));
 							} else {
 								ProfileManager profileManager = ProfileManager.getInstance();
 
@@ -309,23 +307,13 @@ public class HomeFragment extends Fragment {
 									profileManager.randomizeProtocols(profileManager.getRandomProfile());
 								}
 
-								for (String protocol : profileManager.getCurrentActivatedProfile().getActiveProtocols()) {
-									if (protocol.equals("GHOST"))
-										continue;
-
-									if (!MainActivity.getInstance().getHostageService().isRunning(protocol)) {
-										MainActivity.getInstance().getHostageService().startListener(protocol);
-										protocolActivated = true;
-									}
-								}
-
 								Profile currentProfile = profileManager.getCurrentActivatedProfile();
+								List<String> protocols = currentProfile.getActiveProtocols();
 
-								if (currentProfile.mGhostActive) {
-									for (int port : currentProfile.getGhostPorts()) {
-										MainActivity.getInstance().getHostageService().startListener("GHOST", port);
-										protocolActivated = true;
-									}
+								if(protocols.size() > 0 || currentProfile.mGhostActive){
+									protocols.add("GHOST");
+									MainActivity.getInstance().startMonitorServices(protocols);
+									protocolActivated = true;
 								}
 							}
 

+ 5 - 1
src/de/tudarmstadt/informatik/hostage/ui2/fragment/ProfileManagerFragment.java

@@ -12,6 +12,7 @@ import android.view.ViewGroup;
 
 import com.fortysevendeg.android.swipelistview.BaseSwipeListViewListener;
 
+import java.util.LinkedList;
 import java.util.List;
 
 import de.tudarmstadt.informatik.hostage.R;
@@ -45,7 +46,7 @@ public class ProfileManagerFragment extends Fragment {
 		final ProfileManager pmanager = ProfileManager.getInstance();
 		pmanager.loadData();
 
-        List<Profile> strList = pmanager.getProfilesList();
+        final List<Profile> strList = new LinkedList<Profile>(pmanager.getProfilesList());
 
 	    if(strList.size() > 0){
 		    Profile tProfile = new Profile();
@@ -65,6 +66,7 @@ public class ProfileManagerFragment extends Fragment {
 				Profile profile = mAdapter.getItem(position);
 				if(profile.mShowTooltip){
 					mAdapter.remove(profile);
+					strList.remove(profile);
 					list.dismiss(position);
 				}
 			}
@@ -72,6 +74,8 @@ public class ProfileManagerFragment extends Fragment {
 			@Override
 			public void onClickFrontView(int position) {
 				Profile profile = mAdapter.getItem(position);
+				if(profile.mShowTooltip) return;
+
 				pmanager.activateProfile(profile);
 
 				mAdapter.notifyDataSetChanged();