HoneyService.java 12 KB

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