HoneyService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. package de.tudarmstadt.informatik.hostage;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.app.Service;
  7. import android.content.BroadcastReceiver;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.IntentFilter;
  11. import android.content.SharedPreferences;
  12. import android.content.SharedPreferences.Editor;
  13. import android.net.ConnectivityManager;
  14. import android.net.Uri;
  15. import android.os.Binder;
  16. import android.os.IBinder;
  17. import android.preference.PreferenceManager;
  18. import android.support.v4.app.NotificationCompat;
  19. import android.support.v4.app.TaskStackBuilder;
  20. import android.support.v4.content.LocalBroadcastManager;
  21. import android.util.Log;
  22. import android.widget.Toast;
  23. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  24. import de.tudarmstadt.informatik.hostage.logging.DatabaseHandler;
  25. import de.tudarmstadt.informatik.hostage.logging.Logger;
  26. import de.tudarmstadt.informatik.hostage.logging.MyLocationManager;
  27. import de.tudarmstadt.informatik.hostage.logging.SQLLogger;
  28. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  29. import de.tudarmstadt.informatik.hostage.ui.MainActivity;
  30. /**
  31. * Background service running as long as at least one protocol is active.
  32. * Service controls start and stop of protocol listener.
  33. * Notifies GUI about events happening in the background.
  34. * Creates Notifications to inform the user what it happening.
  35. * @author Mihai Plasoianu
  36. * @author Lars Pandikow
  37. */
  38. public class HoneyService extends Service {
  39. private ArrayList<HoneyListener> listeners = new ArrayList<HoneyListener>();
  40. private NotificationCompat.Builder builder;
  41. private SharedPreferences sessionPref;
  42. private Editor editor;
  43. public List<HoneyListener> getListeners() {
  44. return listeners;
  45. }
  46. private Logger log;
  47. public Logger getLog() {
  48. return log;
  49. }
  50. private final IBinder mBinder = new LocalBinder();
  51. public class LocalBinder extends Binder {
  52. public HoneyService getService() {
  53. return HoneyService.this;
  54. }
  55. }
  56. @Override
  57. public IBinder onBind(Intent intent) {
  58. return mBinder;
  59. }
  60. @Override
  61. public void onCreate() {
  62. super.onCreate();
  63. log = new SQLLogger(getApplicationContext());
  64. sessionPref = getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
  65. editor = sessionPref.edit();
  66. createNotification();
  67. for (Protocol<?> protocol : getProtocolArray()) {
  68. listeners.add(new HoneyListener(this, protocol));
  69. }
  70. registerNetReceiver();
  71. getLocationData();
  72. }
  73. @Override
  74. public int onStartCommand(Intent intent, int flags, int startId) {
  75. // We want this service to continue running until it is explicitly
  76. // stopped, so return sticky.
  77. return START_STICKY;
  78. }
  79. @Override
  80. public void onDestroy() {
  81. cancelNotification();
  82. super.onDestroy();
  83. unregisterNetReceiver();
  84. }
  85. /** Starts an Instance of MyLocationManager to set the location within this class.
  86. */
  87. private void getLocationData(){
  88. MyLocationManager locationManager = new MyLocationManager(this);
  89. locationManager.getUpdates(60 * 1000);
  90. }
  91. /**
  92. * Deletes all session related data.
  93. */
  94. private void deleteSessionData(){
  95. editor.clear();
  96. editor.commit();
  97. }
  98. /**
  99. * Register broadcast receiver for connectivity changes
  100. */
  101. private void registerNetReceiver() {
  102. // register BroadcastReceiver on network state changes
  103. IntentFilter intent = new IntentFilter();
  104. intent.addAction(ConnectivityManager.CONNECTIVITY_ACTION); //"android.net.conn.CONNECTIVITY_CHANGE"
  105. registerReceiver(netReceiver, intent);
  106. }
  107. /**
  108. * Unregister broadcast receiver for connectivity changes
  109. */
  110. private void unregisterNetReceiver() {
  111. unregisterReceiver(netReceiver);
  112. }
  113. /**
  114. * Receiver for connectivity change broadcast.
  115. * @see MainActivity#BROADCAST
  116. */
  117. private BroadcastReceiver netReceiver = new BroadcastReceiver() {
  118. @Override
  119. public void onReceive(Context context, Intent intent) {
  120. String bssid_old = sessionPref.getString(MainActivity.BSSID, "");
  121. String bssid_new = HelperUtils.getBSSID(context);
  122. //TODO INFORM UI
  123. //TODO CHECK IF OTHER NETWORKS WORK TOO
  124. if(bssid_new == null || !bssid_new.equals(bssid_old)){
  125. getLocationData();
  126. String[] protocols = getResources().getStringArray(R.array.protocols);
  127. for (String protocol : protocols) {
  128. editor.remove(protocol + MainActivity.HANDLER_COUNT);
  129. }
  130. notifyUI("SERVICE", "CONNECTIVITY_CHANGE");
  131. }
  132. }
  133. };
  134. /**
  135. * Creates a Notification in the notification bar.
  136. */
  137. private void createNotification() {
  138. DatabaseHandler dbh = new DatabaseHandler(this);
  139. boolean activeHandlers = false;
  140. boolean bssidSeen = false;
  141. for(String protocol : getResources().getStringArray(R.array.protocols)){
  142. int handlerCount = sessionPref.getInt(protocol + MainActivity.HANDLER_COUNT, 0);
  143. if(handlerCount > 0){
  144. activeHandlers = true;
  145. }
  146. if(dbh.bssidSeen(protocol, HelperUtils.getBSSID(getApplicationContext()))){
  147. bssidSeen = true;
  148. }
  149. }
  150. builder = new NotificationCompat.Builder(this)
  151. .setContentTitle(getString(R.string.app_name))
  152. .setWhen(System.currentTimeMillis());
  153. if(activeHandlers){
  154. builder.setSmallIcon(R.drawable.ic_service_red);
  155. builder.setContentText("Network is infected!");
  156. } else if(bssidSeen){
  157. builder.setSmallIcon(R.drawable.ic_service_yellow);
  158. builder.setContentText("Network has been infected in previous session!");
  159. } else{
  160. builder.setSmallIcon(R.drawable.ic_service_green);
  161. builder.setContentText("Everything looks fine!");
  162. }
  163. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  164. stackBuilder.addParentStack(MainActivity.class);
  165. stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
  166. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
  167. PendingIntent.FLAG_UPDATE_CURRENT);
  168. builder.setContentIntent(resultPendingIntent);
  169. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  170. mNotificationManager.notify(1, builder.build());
  171. }
  172. /**
  173. * Updates the notification when a attack is registered.
  174. */
  175. private void updateNotification() {
  176. SharedPreferences defaultPref = PreferenceManager.getDefaultSharedPreferences(this);
  177. String strRingtonePreference = defaultPref.getString("pref_notification_sound", "content://settings/system/notification_sound");
  178. builder = new NotificationCompat.Builder(this)
  179. .setContentTitle(getString(R.string.app_name))
  180. .setTicker("Honeypot under attack!")
  181. .setContentText("Network is infected!")
  182. .setSmallIcon(R.drawable.ic_service_red)
  183. .setAutoCancel(true)
  184. .setWhen(System.currentTimeMillis())
  185. .setSound(Uri.parse(strRingtonePreference));
  186. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  187. stackBuilder.addParentStack(MainActivity.class);
  188. stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
  189. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
  190. PendingIntent.FLAG_UPDATE_CURRENT);
  191. builder.setContentIntent(resultPendingIntent);
  192. if(defaultPref.getBoolean("pref_vibration", false)){
  193. builder.setVibrate(new long[]{100, 200, 100, 200});
  194. }
  195. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  196. mNotificationManager.notify(1, builder.build());
  197. }
  198. /**
  199. * Cancels the Notification
  200. */
  201. private void cancelNotification() {
  202. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  203. mNotificationManager.cancel(1);
  204. }
  205. /**
  206. * Creates a instance of each protocol defined in /res/values/protocols.xml and puts it in a List
  207. * @return ArrayList of {@link de.tudarmstadt.informatik.hostage.protocol.Protocol Protocol}
  208. */
  209. private ArrayList<Protocol<?>> getProtocolArray() {
  210. String[] protocols = getResources().getStringArray(R.array.protocols);
  211. String packageName = Protocol.class.getPackage().getName();
  212. ArrayList<Protocol<?>> protocolArray = new ArrayList<Protocol<?>>();
  213. for (String protocol : protocols) {
  214. try {
  215. protocolArray.add((Protocol<?>) Class.forName(
  216. String.format("%s.%s", packageName, protocol))
  217. .newInstance());
  218. } catch (Exception e) {
  219. e.printStackTrace();
  220. }
  221. }
  222. return protocolArray;
  223. }
  224. /**
  225. * Determines if there are running listeners.
  226. * @return True if there is a running listener, else false.
  227. */
  228. public boolean hasRunningListeners(){
  229. for (HoneyListener listener : listeners) {
  230. if (listener.isRunning())
  231. return true;
  232. }
  233. return false;
  234. }
  235. /**
  236. * Notifies the GUI about a event.
  237. * @param protocol The protocol where the event happened.
  238. * @param key The key for the event.
  239. */
  240. public void notifyUI(String sender, String key) {
  241. // Send Notification
  242. if (key.equals(MainActivity.HANDLER_COUNT)){
  243. updateNotification();
  244. }else if(key.equals("CONNECTIVITY_CHANGE")){
  245. createNotification();
  246. }
  247. Log.i("HoneyService", key);
  248. // Inform UI of Preference Change
  249. Intent intent = new Intent(MainActivity.BROADCAST);
  250. intent.putExtra("SENDER", sender);
  251. intent.putExtra("SENDER", key);
  252. LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
  253. }
  254. /**
  255. * Starts all listeners which are not already running
  256. */
  257. public void startListeners() {
  258. for (HoneyListener listener : listeners) {
  259. if(!listener.isRunning()){
  260. listener.start();
  261. }
  262. }
  263. Toast.makeText(getApplicationContext(), "SERVICES STARTED!", Toast.LENGTH_SHORT).show();
  264. }
  265. /**
  266. * Stops all running listeners.
  267. */
  268. public void stopListeners() {
  269. for (HoneyListener listener : listeners) {
  270. if(listener.isRunning()){
  271. listener.stop();
  272. }
  273. }
  274. Toast.makeText(getApplicationContext(), "SERVICES STOPPED!", Toast.LENGTH_SHORT).show();
  275. }
  276. /**
  277. * Starts the listener for the specified protocol.
  278. * @param protocolName Name of the protocol that should be started.
  279. */
  280. public void startListener(String protocolName) {
  281. for (HoneyListener listener : listeners) {
  282. if (listener.getProtocolName().equals(protocolName)) {
  283. if(!listener.isRunning()){
  284. listener.start();
  285. }
  286. }
  287. }
  288. Toast.makeText(getApplicationContext(), protocolName + " SERVICE STARTED!", Toast.LENGTH_SHORT).show();
  289. }
  290. /**
  291. * Stops the listener for the specified protocol.
  292. * @param protocolName Name of the protocol that should be stopped.
  293. */
  294. public void stopListener(String protocolName) {
  295. for (HoneyListener listener : listeners) {
  296. if (listener.getProtocolName().equals(protocolName)) {
  297. if(listener.isRunning()){
  298. listener.stop();
  299. }
  300. }
  301. }
  302. Toast.makeText(getApplicationContext(), protocolName + " SERVICE STOPPED!", Toast.LENGTH_SHORT).show();
  303. }
  304. /**
  305. * Toggles a listener for specified protocol.
  306. * @param protocolName Name of the protocol that should be toggled.
  307. */
  308. public void toggleListener(String protocolName) {
  309. for (HoneyListener listener : listeners) {
  310. if (listener.getProtocolName().equals(protocolName)) {
  311. if (listener.isRunning()) {
  312. stopListener(protocolName);
  313. } else {
  314. startListener(protocolName);
  315. }
  316. }
  317. }
  318. }
  319. }