|
@@ -7,18 +7,27 @@ import android.app.Notification;
|
|
|
import android.app.NotificationManager;
|
|
|
import android.app.PendingIntent;
|
|
|
import android.app.Service;
|
|
|
+import android.content.BroadcastReceiver;
|
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
+import android.content.IntentFilter;
|
|
|
import android.content.SharedPreferences;
|
|
|
import android.content.SharedPreferences.Editor;
|
|
|
+import android.net.ConnectivityManager;
|
|
|
+import android.net.NetworkInfo;
|
|
|
import android.net.Uri;
|
|
|
+import android.net.wifi.WifiInfo;
|
|
|
+import android.net.wifi.WifiManager;
|
|
|
import android.os.Binder;
|
|
|
import android.os.IBinder;
|
|
|
import android.preference.PreferenceManager;
|
|
|
import android.support.v4.app.NotificationCompat;
|
|
|
import android.support.v4.app.TaskStackBuilder;
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
|
|
+import android.util.Log;
|
|
|
import android.widget.Toast;
|
|
|
+import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
|
|
|
+import de.tudarmstadt.informatik.hostage.logging.DatabaseHandler;
|
|
|
import de.tudarmstadt.informatik.hostage.logging.Logger;
|
|
|
import de.tudarmstadt.informatik.hostage.logging.SQLLogger;
|
|
|
import de.tudarmstadt.informatik.hostage.protocol.Protocol;
|
|
@@ -27,6 +36,10 @@ import de.tudarmstadt.informatik.hostage.ui.MainActivity;
|
|
|
public class HoneyService extends Service {
|
|
|
|
|
|
private ArrayList<HoneyListener> listeners = new ArrayList<HoneyListener>();
|
|
|
+ private NotificationCompat.Builder builder;
|
|
|
+ private SharedPreferences pref;
|
|
|
+ Editor editor;
|
|
|
+ private boolean stopped = false;
|
|
|
|
|
|
public List<HoneyListener> getListeners() {
|
|
|
return listeners;
|
|
@@ -48,6 +61,8 @@ public class HoneyService extends Service {
|
|
|
|
|
|
@Override
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
+ NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
+ mNotificationManager.cancel(2);
|
|
|
return mBinder;
|
|
|
}
|
|
|
|
|
@@ -55,10 +70,13 @@ public class HoneyService extends Service {
|
|
|
public void onCreate() {
|
|
|
super.onCreate();
|
|
|
log = new SQLLogger(getApplicationContext());
|
|
|
+ pref = getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
|
|
|
+ editor = pref.edit();
|
|
|
createNotification();
|
|
|
for (Protocol protocol : getProtocolArray()) {
|
|
|
listeners.add(new HoneyListener(this, protocol));
|
|
|
}
|
|
|
+ registerNetReceiver();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -66,26 +84,114 @@ public class HoneyService extends Service {
|
|
|
// We want this service to continue running until it is explicitly
|
|
|
// stopped, so return sticky.
|
|
|
return START_STICKY;
|
|
|
- }
|
|
|
-
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public void onDestroy() {
|
|
|
cancelNotification();
|
|
|
super.onDestroy();
|
|
|
+ unregisterNetReceiver();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Delete session data
|
|
|
+ private void deleteSessionData(){
|
|
|
+ editor.clear();
|
|
|
+ editor.commit();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void registerNetReceiver() {
|
|
|
+ // register BroadcastReceiver on network state changes
|
|
|
+ IntentFilter intent = new IntentFilter();
|
|
|
+ intent.addAction(ConnectivityManager.CONNECTIVITY_ACTION); //"android.net.conn.CONNECTIVITY_CHANGE"
|
|
|
+ registerReceiver(netReceiver, intent);
|
|
|
}
|
|
|
|
|
|
+ private void unregisterNetReceiver() {
|
|
|
+ unregisterReceiver(netReceiver);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void mStopSelf(){
|
|
|
+ stopped = true;
|
|
|
+ stopSelf();
|
|
|
+ }
|
|
|
+
|
|
|
+ private BroadcastReceiver netReceiver = new BroadcastReceiver() {
|
|
|
+ @Override
|
|
|
+ public void onReceive(Context context, Intent intent) {
|
|
|
+ String bssid_old = pref.getString(MainActivity.BSSID, "");
|
|
|
+ String bssid_new = HelperUtils.getBSSID(context);
|
|
|
+ Log.i("HoneyService", "Connection changed");
|
|
|
+
|
|
|
+ if(!stopped && (bssid_new == null || !bssid_new.equals(bssid_old))){
|
|
|
+ Log.i("HoneyService", "Connection lost");
|
|
|
+ wifiChangeNotification();
|
|
|
+ stopListeners();
|
|
|
+ deleteSessionData();
|
|
|
+ notifyUI("SERVICE", "STOPPED");
|
|
|
+ mStopSelf();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
private void createNotification() {
|
|
|
- NotificationCompat.Builder builder = new NotificationCompat.Builder(
|
|
|
- this).setSmallIcon(R.drawable.ic_launcher)
|
|
|
+ DatabaseHandler dbh = new DatabaseHandler(this);
|
|
|
+ boolean activeHandlers = false;
|
|
|
+ boolean bssidSeen = false;
|
|
|
+
|
|
|
+ for(String protocol : getResources().getStringArray(R.array.protocols)){
|
|
|
+ int handlerCount = pref.getInt(protocol + MainActivity.HANDLER_COUNT, 0);
|
|
|
+ if(handlerCount > 0){
|
|
|
+ activeHandlers = true;
|
|
|
+ }
|
|
|
+ if(dbh.bssidSeen(protocol, HelperUtils.getBSSID(getApplicationContext()))){
|
|
|
+ bssidSeen = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ builder = new NotificationCompat.Builder(this)
|
|
|
+ .setContentTitle(getString(R.string.app_name))
|
|
|
+ .setWhen(System.currentTimeMillis());
|
|
|
+ if(activeHandlers){
|
|
|
+ builder.setSmallIcon(R.drawable.icon_service_red);
|
|
|
+ builder.setContentText("Network is infected!");
|
|
|
+ } else if(bssidSeen){
|
|
|
+ builder.setSmallIcon(R.drawable.icon_service_yellow);
|
|
|
+ builder.setContentText("Network has been infected in previous session!");
|
|
|
+ } else{
|
|
|
+ builder.setSmallIcon(R.drawable.icon_service_green);
|
|
|
+ builder.setContentText("Everything looks fine!");
|
|
|
+ }
|
|
|
+ TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
|
|
|
+ stackBuilder.addParentStack(MainActivity.class);
|
|
|
+ stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
|
|
|
+ PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
|
|
|
+ PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
+ builder.setContentIntent(resultPendingIntent);
|
|
|
+ NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
+ mNotificationManager.notify(1, builder.build());
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateNotification() {
|
|
|
+ SharedPreferences defaultPref = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
+ String strRingtonePreference = defaultPref.getString("pref_notification_sound", "content://settings/system/notification_sound");
|
|
|
+ Log.i("HoneyService", "Ringtone: " + strRingtonePreference);
|
|
|
+ builder = new NotificationCompat.Builder(this)
|
|
|
.setContentTitle(getString(R.string.app_name))
|
|
|
- .setContentText("Honeypot up and running!");
|
|
|
+ .setTicker("Honeypot under attack!")
|
|
|
+ .setContentText("Network is infected!")
|
|
|
+ .setSmallIcon(R.drawable.icon_service_red)
|
|
|
+ .setWhen(System.currentTimeMillis())
|
|
|
+ .setSound(Uri.parse(strRingtonePreference));
|
|
|
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
|
|
|
stackBuilder.addParentStack(MainActivity.class);
|
|
|
stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
|
|
|
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
builder.setContentIntent(resultPendingIntent);
|
|
|
+ Log.i("HoneyService", "Vibrating: " + defaultPref.getBoolean("pref_vibration", false));
|
|
|
+ if(defaultPref.getBoolean("pref_vibration", false)){
|
|
|
+ builder.setVibrate(new long[]{100, 200, 100, 200});
|
|
|
+ }
|
|
|
+
|
|
|
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
mNotificationManager.notify(1, builder.build());
|
|
|
}
|
|
@@ -94,6 +200,31 @@ public class HoneyService extends Service {
|
|
|
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
mNotificationManager.cancel(1);
|
|
|
}
|
|
|
+
|
|
|
+ private void wifiChangeNotification(){
|
|
|
+ SharedPreferences defaultPref = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
+ String strRingtonePreference = defaultPref.getString("pref_notification_sound", "content://settings/system/notification_sound");
|
|
|
+
|
|
|
+ NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
|
|
|
+ .setContentTitle(getString(R.string.app_name))
|
|
|
+ .setWhen(System.currentTimeMillis())
|
|
|
+ .setTicker("HOsTaGe stopped due Connection change!")
|
|
|
+ .setContentText("HOsTaGe stopped due Connection change!")
|
|
|
+ .setSmallIcon(R.drawable.ic_launcher)
|
|
|
+ .setAutoCancel(true)
|
|
|
+ .setSound(Uri.parse(strRingtonePreference));
|
|
|
+ if(defaultPref.getBoolean("pref_vibration", true)){
|
|
|
+ builder.setVibrate(new long[]{100, 200, 100, 200});
|
|
|
+ }
|
|
|
+ TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
|
|
|
+ stackBuilder.addParentStack(MainActivity.class);
|
|
|
+ stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
|
|
|
+ PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
|
|
|
+ PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
+ builder.setContentIntent(resultPendingIntent);
|
|
|
+ NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
+ mNotificationManager.notify(2, builder.build());
|
|
|
+ }
|
|
|
|
|
|
private ArrayList<Protocol> getProtocolArray() {
|
|
|
String[] protocols = getResources().getStringArray(R.array.protocols);
|
|
@@ -109,7 +240,6 @@ public class HoneyService extends Service {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
return protocolArray;
|
|
|
}
|
|
|
|
|
@@ -123,18 +253,10 @@ public class HoneyService extends Service {
|
|
|
|
|
|
// IPC
|
|
|
|
|
|
- public void notifyUI(String key) {
|
|
|
+ public void notifyUI(String protocol, String key) {
|
|
|
// Send Notification
|
|
|
- SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
if (key.equals(MainActivity.HANDLER_COUNT)){
|
|
|
- NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
|
|
- Notification notification = new Notification();
|
|
|
- String strRingtonePreference = pref.getString("pref_notification_sound", "DEFAULT_SOUND");
|
|
|
- notification.sound = Uri.parse(strRingtonePreference);
|
|
|
- if(pref.getBoolean("pref_vibration", true)){
|
|
|
- notification.vibrate = new long[]{100, 200, 100, 200};
|
|
|
- }
|
|
|
- nManager.notify(0, notification);
|
|
|
+ updateNotification();
|
|
|
}
|
|
|
// Inform UI of Preference Change
|
|
|
Intent intent = new Intent(MainActivity.BROADCAST);
|
|
@@ -144,14 +266,18 @@ public class HoneyService extends Service {
|
|
|
|
|
|
public void startListeners() {
|
|
|
for (HoneyListener listener : listeners) {
|
|
|
- listener.start();
|
|
|
+ if(!listener.isRunning()){
|
|
|
+ listener.start();
|
|
|
+ }
|
|
|
}
|
|
|
Toast.makeText(getApplicationContext(), "SERVICES STARTED!", Toast.LENGTH_SHORT).show();
|
|
|
}
|
|
|
|
|
|
public void stopListeners() {
|
|
|
for (HoneyListener listener : listeners) {
|
|
|
- listener.stop();
|
|
|
+ if(listener.isRunning()){
|
|
|
+ listener.stop();
|
|
|
+ }
|
|
|
}
|
|
|
Toast.makeText(getApplicationContext(), "SERVICES STOPPED!", Toast.LENGTH_SHORT).show();
|
|
|
}
|
|
@@ -159,7 +285,9 @@ public class HoneyService extends Service {
|
|
|
public void startListener(String protocolName) {
|
|
|
for (HoneyListener listener : listeners) {
|
|
|
if (listener.getProtocolName().equals(protocolName)) {
|
|
|
- listener.start();
|
|
|
+ if(!listener.isRunning()){
|
|
|
+ listener.start();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
Toast.makeText(getApplicationContext(), protocolName + " SERVICE STARTED!", Toast.LENGTH_SHORT).show();
|
|
@@ -168,7 +296,9 @@ public class HoneyService extends Service {
|
|
|
public void stopListener(String protocolName) {
|
|
|
for (HoneyListener listener : listeners) {
|
|
|
if (listener.getProtocolName().equals(protocolName)) {
|
|
|
- listener.stop();
|
|
|
+ if(listener.isRunning()){
|
|
|
+ listener.stop();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
Toast.makeText(getApplicationContext(), protocolName + " SERVICE STOPPED!", Toast.LENGTH_SHORT).show();
|