123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- package de.tudarmstadt.informatik.hostage;
- import java.util.ArrayList;
- import java.util.List;
- 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.Uri;
- 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.MyLocationManager;
- 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 sessionPref;
- private Editor editor;
- public List<HoneyListener> getListeners() {
- return listeners;
- }
- private Logger log;
- public Logger getLog() {
- return log;
- }
- private final IBinder mBinder = new LocalBinder();
- public class LocalBinder extends Binder {
- public HoneyService getService() {
- return HoneyService.this;
- }
- }
- @Override
- public IBinder onBind(Intent intent) {
- return mBinder;
- }
- @Override
- public void onCreate() {
- super.onCreate();
- log = new SQLLogger(getApplicationContext());
- sessionPref = getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
- editor = sessionPref.edit();
- createNotification();
- for (Protocol<?> protocol : getProtocolArray()) {
- listeners.add(new HoneyListener(this, protocol));
- }
- registerNetReceiver();
- getLocationData();
- }
-
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- // 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();
- }
-
- /** Starts an Instance of MyLocationManager to set the location within this class.
- */
- private void getLocationData(){
- MyLocationManager locationManager = new MyLocationManager(this);
- locationManager.getUpdates(60 * 1000);
- }
-
- /**
- * Deletes all session related data.
- */
- private void deleteSessionData(){
- editor.clear();
- editor.commit();
- }
-
- /**
- * Register broadcast receiver for connectivity changes
- */
- 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);
- }
- /**
- * Unregister broadcast receiver for connectivity changes
- */
- private void unregisterNetReceiver() {
- unregisterReceiver(netReceiver);
- }
- /**
- * Receiver for connectivity change broadcast.
- * @see MainActivity#BROADCAST
- */
- private BroadcastReceiver netReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String bssid_old = sessionPref.getString(MainActivity.BSSID, "");
- String bssid_new = HelperUtils.getBSSID(context);
- //TODO INFORM UI
- //TODO CHECK IF OTHER NETWORKS WORK TOO
- if(bssid_new == null || !bssid_new.equals(bssid_old)){
- getLocationData();
- String[] protocols = getResources().getStringArray(R.array.protocols);
- for (String protocol : protocols) {
- editor.remove(protocol + MainActivity.HANDLER_COUNT);
- }
- notifyUI("SERVICE", "CONNECTIVITY_CHANGE");
- }
- }
- };
- /**
- * Creates a Notification in the notification bar.
- */
- private void createNotification() {
- DatabaseHandler dbh = new DatabaseHandler(this);
- boolean activeHandlers = false;
- boolean bssidSeen = false;
-
- for(String protocol : getResources().getStringArray(R.array.protocols)){
- int handlerCount = sessionPref.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.ic_service_red);
- builder.setContentText("Network is infected!");
- } else if(bssidSeen){
- builder.setSmallIcon(R.drawable.ic_service_yellow);
- builder.setContentText("Network has been infected in previous session!");
- } else{
- builder.setSmallIcon(R.drawable.ic_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());
- }
-
- /**
- * 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");
- builder = new NotificationCompat.Builder(this)
- .setContentTitle(getString(R.string.app_name))
- .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);
- stackBuilder.addParentStack(MainActivity.class);
- stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
- PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
- PendingIntent.FLAG_UPDATE_CURRENT);
- builder.setContentIntent(resultPendingIntent);
- 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());
- }
- /**
- * Cancels the Notification
- */
- private void cancelNotification() {
- NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- mNotificationManager.cancel(1);
- }
- /**
- * 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 Protocol}
- */
- private ArrayList<Protocol<?>> getProtocolArray() {
- String[] protocols = getResources().getStringArray(R.array.protocols);
- String packageName = Protocol.class.getPackage().getName();
- ArrayList<Protocol<?>> protocolArray = new ArrayList<Protocol<?>>();
- for (String protocol : protocols) {
- try {
- protocolArray.add((Protocol<?>) Class.forName(
- String.format("%s.%s", packageName, protocol))
- .newInstance());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- 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())
- return true;
- }
- return false;
- }
- /**
- * Notifies the GUI about a event.
- * @param protocol The protocol where the event happened.
- * @param key The key for the event.
- */
- public void notifyUI(String sender, String key) {
- // Send Notification
- if (key.equals(MainActivity.HANDLER_COUNT)){
- updateNotification();
- }else if(key.equals("CONNECTIVITY_CHANGE")){
- createNotification();
- }
- Log.i("HoneyService", key);
- // Inform UI of Preference Change
- Intent intent = new Intent(MainActivity.BROADCAST);
- intent.putExtra("SENDER", sender);
- intent.putExtra("SENDER", key);
- LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
- }
-
- /**
- * Starts all listeners which are not already running
- */
- public void startListeners() {
- for (HoneyListener listener : listeners) {
- if(!listener.isRunning()){
- listener.start();
- }
- }
- Toast.makeText(getApplicationContext(), "SERVICES STARTED!", Toast.LENGTH_SHORT).show();
- }
- /**
- * Stops all running listeners.
- */
- public void stopListeners() {
- for (HoneyListener listener : listeners) {
- if(listener.isRunning()){
- listener.stop();
- }
- }
- 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)) {
- if(!listener.isRunning()){
- listener.start();
- }
- }
- }
- 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)) {
- if(listener.isRunning()){
- listener.stop();
- }
- }
- }
- 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)) {
- if (listener.isRunning()) {
- stopListener(protocolName);
- } else {
- startListener(protocolName);
- }
- }
- }
- }
- }
|