|
@@ -27,13 +27,20 @@ import de.tudarmstadt.informatik.hostage.logging.Logger;
|
|
|
import de.tudarmstadt.informatik.hostage.logging.SQLLogger;
|
|
|
import de.tudarmstadt.informatik.hostage.protocol.Protocol;
|
|
|
import de.tudarmstadt.informatik.hostage.ui.MainActivity;
|
|
|
-
|
|
|
+/**
|
|
|
+ * Background service running as long as at least one protocol is active.
|
|
|
+ * Service controls start and stop of protocol listener.
|
|
|
+ * Notifies GUI about events happening in the background.
|
|
|
+ * Creates Notifications to inform the user what it happening.
|
|
|
+ * @author Mihai Plasoianu
|
|
|
+ * @author Lars Pandikow
|
|
|
+ */
|
|
|
public class HoneyService extends Service {
|
|
|
|
|
|
private ArrayList<HoneyListener> listeners = new ArrayList<HoneyListener>();
|
|
|
private NotificationCompat.Builder builder;
|
|
|
private SharedPreferences pref;
|
|
|
- Editor editor;
|
|
|
+ private Editor editor;
|
|
|
private boolean stopped = false;
|
|
|
|
|
|
public List<HoneyListener> getListeners() {
|
|
@@ -56,8 +63,6 @@ public class HoneyService extends Service {
|
|
|
|
|
|
@Override
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
- NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
- mNotificationManager.cancel(2);
|
|
|
return mBinder;
|
|
|
}
|
|
|
|
|
@@ -88,12 +93,18 @@ public class HoneyService extends Service {
|
|
|
unregisterNetReceiver();
|
|
|
}
|
|
|
|
|
|
- // Delete session data
|
|
|
+ /**
|
|
|
+ * Deletes all session related data.
|
|
|
+ */
|
|
|
private void deleteSessionData(){
|
|
|
editor.clear();
|
|
|
editor.commit();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Register broadcast receiver for custom broadcast.
|
|
|
+ * @see MainActivity#BROADCAST
|
|
|
+ */
|
|
|
private void registerNetReceiver() {
|
|
|
// register BroadcastReceiver on network state changes
|
|
|
IntentFilter intent = new IntentFilter();
|
|
@@ -101,15 +112,18 @@ public class HoneyService extends Service {
|
|
|
registerReceiver(netReceiver, intent);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Unregister broadcast receiver for custom broadcast.
|
|
|
+ * @see MainActivity#BROADCAST
|
|
|
+ */
|
|
|
private void unregisterNetReceiver() {
|
|
|
unregisterReceiver(netReceiver);
|
|
|
}
|
|
|
-
|
|
|
- private void mStopSelf(){
|
|
|
- stopped = true;
|
|
|
- stopSelf();
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
+ * Receiver for custom broadcast.
|
|
|
+ * @see MainActivity#BROADCAST
|
|
|
+ */
|
|
|
private BroadcastReceiver netReceiver = new BroadcastReceiver() {
|
|
|
@Override
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
@@ -124,7 +138,18 @@ public class HoneyService extends Service {
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Service stops himself.
|
|
|
+ */
|
|
|
+ private void mStopSelf(){
|
|
|
+ stopped = true;
|
|
|
+ stopSelf();
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * Creates a Notification in the notification bar.
|
|
|
+ */
|
|
|
private void createNotification() {
|
|
|
DatabaseHandler dbh = new DatabaseHandler(this);
|
|
|
boolean activeHandlers = false;
|
|
@@ -162,6 +187,9 @@ public class HoneyService extends Service {
|
|
|
mNotificationManager.notify(1, builder.build());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Updates the notification when a attack is registered.
|
|
|
+ */
|
|
|
private void updateNotification() {
|
|
|
SharedPreferences defaultPref = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
String strRingtonePreference = defaultPref.getString("pref_notification_sound", "content://settings/system/notification_sound");
|
|
@@ -170,6 +198,7 @@ public class HoneyService extends Service {
|
|
|
.setTicker("Honeypot under attack!")
|
|
|
.setContentText("Network is infected!")
|
|
|
.setSmallIcon(R.drawable.ic_service_red)
|
|
|
+ .setAutoCancel(true)
|
|
|
.setWhen(System.currentTimeMillis())
|
|
|
.setSound(Uri.parse(strRingtonePreference));
|
|
|
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
|
|
@@ -186,6 +215,9 @@ public class HoneyService extends Service {
|
|
|
mNotificationManager.notify(1, builder.build());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Cancels the Notification
|
|
|
+ */
|
|
|
private void cancelNotification() {
|
|
|
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
mNotificationManager.cancel(1);
|
|
@@ -216,6 +248,10 @@ public class HoneyService extends Service {
|
|
|
mNotificationManager.notify(2, builder.build());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Creates a instance of each protocol defined in /res/values/protocols.xml and puts it in a List
|
|
|
+ * @return ArrayList of {@link de.tudarmstadt.informatik.hostage.protocol.Protocol}
|
|
|
+ */
|
|
|
private ArrayList<Protocol> getProtocolArray() {
|
|
|
String[] protocols = getResources().getStringArray(R.array.protocols);
|
|
|
String packageName = Protocol.class.getPackage().getName();
|
|
@@ -233,6 +269,10 @@ public class HoneyService extends Service {
|
|
|
return protocolArray;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Determines if there are running listeners.
|
|
|
+ * @return True if there is a running listener, else false.
|
|
|
+ */
|
|
|
public boolean hasRunningListeners(){
|
|
|
for (HoneyListener listener : listeners) {
|
|
|
if (listener.isRunning())
|
|
@@ -241,8 +281,11 @@ public class HoneyService extends Service {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // IPC
|
|
|
-
|
|
|
+ /**
|
|
|
+ * Notifies the GUI about a event.
|
|
|
+ * @param protocol The protocol where the event happend.
|
|
|
+ * @param key The key for the event.
|
|
|
+ */
|
|
|
public void notifyUI(String protocol, String key) {
|
|
|
// Send Notification
|
|
|
if (key.equals(MainActivity.HANDLER_COUNT)){
|
|
@@ -253,7 +296,9 @@ public class HoneyService extends Service {
|
|
|
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ /**
|
|
|
+ * Starts all listeners which are not already running
|
|
|
+ */
|
|
|
public void startListeners() {
|
|
|
for (HoneyListener listener : listeners) {
|
|
|
if(!listener.isRunning()){
|
|
@@ -263,6 +308,9 @@ public class HoneyService extends Service {
|
|
|
Toast.makeText(getApplicationContext(), "SERVICES STARTED!", Toast.LENGTH_SHORT).show();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Stops all running listeners.
|
|
|
+ */
|
|
|
public void stopListeners() {
|
|
|
for (HoneyListener listener : listeners) {
|
|
|
if(listener.isRunning()){
|
|
@@ -272,6 +320,10 @@ public class HoneyService extends Service {
|
|
|
Toast.makeText(getApplicationContext(), "SERVICES STOPPED!", Toast.LENGTH_SHORT).show();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Starts the listener for the specified protocol.
|
|
|
+ * @param protocolName Name of the protocol that should be started.
|
|
|
+ */
|
|
|
public void startListener(String protocolName) {
|
|
|
for (HoneyListener listener : listeners) {
|
|
|
if (listener.getProtocolName().equals(protocolName)) {
|
|
@@ -283,6 +335,10 @@ public class HoneyService extends Service {
|
|
|
Toast.makeText(getApplicationContext(), protocolName + " SERVICE STARTED!", Toast.LENGTH_SHORT).show();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Stops the listener for the specified protocol.
|
|
|
+ * @param protocolName Name of the protocol that should be stopped.
|
|
|
+ */
|
|
|
public void stopListener(String protocolName) {
|
|
|
for (HoneyListener listener : listeners) {
|
|
|
if (listener.getProtocolName().equals(protocolName)) {
|
|
@@ -294,6 +350,10 @@ public class HoneyService extends Service {
|
|
|
Toast.makeText(getApplicationContext(), protocolName + " SERVICE STOPPED!", Toast.LENGTH_SHORT).show();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Toggles a listener for specified protocol.
|
|
|
+ * @param protocolName Name of the protocol that should be toggled.
|
|
|
+ */
|
|
|
public void toggleListener(String protocolName) {
|
|
|
for (HoneyListener listener : listeners) {
|
|
|
if (listener.getProtocolName().equals(protocolName)) {
|