HoneyService.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. public class HoneyService extends Service {
  29. private ArrayList<HoneyListener> listeners = new ArrayList<HoneyListener>();
  30. private NotificationCompat.Builder builder;
  31. private SharedPreferences pref;
  32. Editor editor;
  33. private boolean stopped = false;
  34. public List<HoneyListener> getListeners() {
  35. return listeners;
  36. }
  37. private Logger log;
  38. public Logger getLog() {
  39. return log;
  40. }
  41. private final IBinder mBinder = new LocalBinder();
  42. public class LocalBinder extends Binder {
  43. public HoneyService getService() {
  44. return HoneyService.this;
  45. }
  46. }
  47. @Override
  48. public IBinder onBind(Intent intent) {
  49. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  50. mNotificationManager.cancel(2);
  51. return mBinder;
  52. }
  53. @Override
  54. public void onCreate() {
  55. super.onCreate();
  56. log = new SQLLogger(getApplicationContext());
  57. pref = getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
  58. editor = pref.edit();
  59. createNotification();
  60. for (Protocol protocol : getProtocolArray()) {
  61. listeners.add(new HoneyListener(this, protocol));
  62. }
  63. registerNetReceiver();
  64. }
  65. @Override
  66. public int onStartCommand(Intent intent, int flags, int startId) {
  67. // We want this service to continue running until it is explicitly
  68. // stopped, so return sticky.
  69. return START_STICKY;
  70. }
  71. @Override
  72. public void onDestroy() {
  73. cancelNotification();
  74. super.onDestroy();
  75. unregisterNetReceiver();
  76. }
  77. // Delete session data
  78. private void deleteSessionData(){
  79. editor.clear();
  80. editor.commit();
  81. }
  82. private void registerNetReceiver() {
  83. // register BroadcastReceiver on network state changes
  84. IntentFilter intent = new IntentFilter();
  85. intent.addAction(ConnectivityManager.CONNECTIVITY_ACTION); //"android.net.conn.CONNECTIVITY_CHANGE"
  86. registerReceiver(netReceiver, intent);
  87. }
  88. private void unregisterNetReceiver() {
  89. unregisterReceiver(netReceiver);
  90. }
  91. private void mStopSelf(){
  92. stopped = true;
  93. stopSelf();
  94. }
  95. private BroadcastReceiver netReceiver = new BroadcastReceiver() {
  96. @Override
  97. public void onReceive(Context context, Intent intent) {
  98. String bssid_old = pref.getString(MainActivity.BSSID, "");
  99. String bssid_new = HelperUtils.getBSSID(context);
  100. if(!stopped && (bssid_new == null || !bssid_new.equals(bssid_old))){
  101. wifiChangeNotification();
  102. stopListeners();
  103. deleteSessionData();
  104. notifyUI("SERVICE", "STOPPED");
  105. mStopSelf();
  106. }
  107. }
  108. };
  109. private void createNotification() {
  110. DatabaseHandler dbh = new DatabaseHandler(this);
  111. boolean activeHandlers = false;
  112. boolean bssidSeen = false;
  113. for(String protocol : getResources().getStringArray(R.array.protocols)){
  114. int handlerCount = pref.getInt(protocol + MainActivity.HANDLER_COUNT, 0);
  115. if(handlerCount > 0){
  116. activeHandlers = true;
  117. }
  118. if(dbh.bssidSeen(protocol, HelperUtils.getBSSID(getApplicationContext()))){
  119. bssidSeen = true;
  120. }
  121. }
  122. builder = new NotificationCompat.Builder(this)
  123. .setContentTitle(getString(R.string.app_name))
  124. .setWhen(System.currentTimeMillis());
  125. if(activeHandlers){
  126. builder.setSmallIcon(R.drawable.ic_service_red);
  127. builder.setContentText("Network is infected!");
  128. } else if(bssidSeen){
  129. builder.setSmallIcon(R.drawable.ic_service_yellow);
  130. builder.setContentText("Network has been infected in previous session!");
  131. } else{
  132. builder.setSmallIcon(R.drawable.ic_service_green);
  133. builder.setContentText("Everything looks fine!");
  134. }
  135. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  136. stackBuilder.addParentStack(MainActivity.class);
  137. stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
  138. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
  139. PendingIntent.FLAG_UPDATE_CURRENT);
  140. builder.setContentIntent(resultPendingIntent);
  141. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  142. mNotificationManager.notify(1, builder.build());
  143. }
  144. private void updateNotification() {
  145. SharedPreferences defaultPref = PreferenceManager.getDefaultSharedPreferences(this);
  146. String strRingtonePreference = defaultPref.getString("pref_notification_sound", "content://settings/system/notification_sound");
  147. builder = new NotificationCompat.Builder(this)
  148. .setContentTitle(getString(R.string.app_name))
  149. .setTicker("Honeypot under attack!")
  150. .setContentText("Network is infected!")
  151. .setSmallIcon(R.drawable.ic_service_red)
  152. .setWhen(System.currentTimeMillis())
  153. .setSound(Uri.parse(strRingtonePreference));
  154. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  155. stackBuilder.addParentStack(MainActivity.class);
  156. stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
  157. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
  158. PendingIntent.FLAG_UPDATE_CURRENT);
  159. builder.setContentIntent(resultPendingIntent);
  160. if(defaultPref.getBoolean("pref_vibration", false)){
  161. builder.setVibrate(new long[]{100, 200, 100, 200});
  162. }
  163. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  164. mNotificationManager.notify(1, builder.build());
  165. }
  166. private void cancelNotification() {
  167. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  168. mNotificationManager.cancel(1);
  169. }
  170. private void wifiChangeNotification(){
  171. SharedPreferences defaultPref = PreferenceManager.getDefaultSharedPreferences(this);
  172. String strRingtonePreference = defaultPref.getString("pref_notification_sound", "content://settings/system/notification_sound");
  173. NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
  174. .setContentTitle(getString(R.string.app_name))
  175. .setWhen(System.currentTimeMillis())
  176. .setTicker("HOsTaGe stopped due Connection change!")
  177. .setContentText("HOsTaGe stopped due Connection change!")
  178. .setSmallIcon(R.drawable.ic_launcher)
  179. .setAutoCancel(true)
  180. .setSound(Uri.parse(strRingtonePreference));
  181. if(defaultPref.getBoolean("pref_vibration", true)){
  182. builder.setVibrate(new long[]{100, 200, 100, 200});
  183. }
  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. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  191. mNotificationManager.notify(2, builder.build());
  192. }
  193. private ArrayList<Protocol> getProtocolArray() {
  194. String[] protocols = getResources().getStringArray(R.array.protocols);
  195. String packageName = Protocol.class.getPackage().getName();
  196. ArrayList<Protocol> protocolArray = new ArrayList<Protocol>();
  197. for (String protocol : protocols) {
  198. try {
  199. protocolArray.add((Protocol) Class.forName(
  200. String.format("%s.%s", packageName, protocol))
  201. .newInstance());
  202. } catch (Exception e) {
  203. e.printStackTrace();
  204. }
  205. }
  206. return protocolArray;
  207. }
  208. public boolean hasRunningListeners(){
  209. for (HoneyListener listener : listeners) {
  210. if (listener.isRunning())
  211. return true;
  212. }
  213. return false;
  214. }
  215. // IPC
  216. public void notifyUI(String protocol, String key) {
  217. // Send Notification
  218. if (key.equals(MainActivity.HANDLER_COUNT)){
  219. updateNotification();
  220. }
  221. // Inform UI of Preference Change
  222. Intent intent = new Intent(MainActivity.BROADCAST);
  223. LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
  224. }
  225. public void startListeners() {
  226. for (HoneyListener listener : listeners) {
  227. if(!listener.isRunning()){
  228. listener.start();
  229. }
  230. }
  231. Toast.makeText(getApplicationContext(), "SERVICES STARTED!", Toast.LENGTH_SHORT).show();
  232. }
  233. public void stopListeners() {
  234. for (HoneyListener listener : listeners) {
  235. if(listener.isRunning()){
  236. listener.stop();
  237. }
  238. }
  239. Toast.makeText(getApplicationContext(), "SERVICES STOPPED!", Toast.LENGTH_SHORT).show();
  240. }
  241. public void startListener(String protocolName) {
  242. for (HoneyListener listener : listeners) {
  243. if (listener.getProtocolName().equals(protocolName)) {
  244. if(!listener.isRunning()){
  245. listener.start();
  246. }
  247. }
  248. }
  249. Toast.makeText(getApplicationContext(), protocolName + " SERVICE STARTED!", Toast.LENGTH_SHORT).show();
  250. }
  251. public void stopListener(String protocolName) {
  252. for (HoneyListener listener : listeners) {
  253. if (listener.getProtocolName().equals(protocolName)) {
  254. if(listener.isRunning()){
  255. listener.stop();
  256. }
  257. }
  258. }
  259. Toast.makeText(getApplicationContext(), protocolName + " SERVICE STOPPED!", Toast.LENGTH_SHORT).show();
  260. }
  261. public void toggleListener(String protocolName) {
  262. for (HoneyListener listener : listeners) {
  263. if (listener.getProtocolName().equals(protocolName)) {
  264. if (listener.isRunning()) {
  265. stopListener(protocolName);
  266. } else {
  267. startListener(protocolName);
  268. }
  269. }
  270. }
  271. }
  272. }