HoneyService.java 11 KB

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