HoneyService.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. if(!stopped && (bssid_new == null || !bssid_new.equals(bssid_old))){
  106. wifiChangeNotification();
  107. stopListeners();
  108. deleteSessionData();
  109. notifyUI("SERVICE", "STOPPED");
  110. mStopSelf();
  111. }
  112. }
  113. };
  114. private void createNotification() {
  115. DatabaseHandler dbh = new DatabaseHandler(this);
  116. boolean activeHandlers = false;
  117. boolean bssidSeen = false;
  118. for(String protocol : getResources().getStringArray(R.array.protocols)){
  119. int handlerCount = pref.getInt(protocol + MainActivity.HANDLER_COUNT, 0);
  120. if(handlerCount > 0){
  121. activeHandlers = true;
  122. }
  123. if(dbh.bssidSeen(protocol, HelperUtils.getBSSID(getApplicationContext()))){
  124. bssidSeen = true;
  125. }
  126. }
  127. builder = new NotificationCompat.Builder(this)
  128. .setContentTitle(getString(R.string.app_name))
  129. .setWhen(System.currentTimeMillis());
  130. if(activeHandlers){
  131. builder.setSmallIcon(R.drawable.ic_service_red);
  132. builder.setContentText("Network is infected!");
  133. } else if(bssidSeen){
  134. builder.setSmallIcon(R.drawable.ic_service_yellow);
  135. builder.setContentText("Network has been infected in previous session!");
  136. } else{
  137. builder.setSmallIcon(R.drawable.ic_service_green);
  138. builder.setContentText("Everything looks fine!");
  139. }
  140. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  141. stackBuilder.addParentStack(MainActivity.class);
  142. stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
  143. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
  144. PendingIntent.FLAG_UPDATE_CURRENT);
  145. builder.setContentIntent(resultPendingIntent);
  146. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  147. mNotificationManager.notify(1, builder.build());
  148. }
  149. private void updateNotification() {
  150. SharedPreferences defaultPref = PreferenceManager.getDefaultSharedPreferences(this);
  151. String strRingtonePreference = defaultPref.getString("pref_notification_sound", "content://settings/system/notification_sound");
  152. builder = new NotificationCompat.Builder(this)
  153. .setContentTitle(getString(R.string.app_name))
  154. .setTicker("Honeypot under attack!")
  155. .setContentText("Network is infected!")
  156. .setSmallIcon(R.drawable.ic_service_red)
  157. .setWhen(System.currentTimeMillis())
  158. .setSound(Uri.parse(strRingtonePreference));
  159. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  160. stackBuilder.addParentStack(MainActivity.class);
  161. stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
  162. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
  163. PendingIntent.FLAG_UPDATE_CURRENT);
  164. builder.setContentIntent(resultPendingIntent);
  165. if(defaultPref.getBoolean("pref_vibration", false)){
  166. builder.setVibrate(new long[]{100, 200, 100, 200});
  167. }
  168. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  169. mNotificationManager.notify(1, builder.build());
  170. }
  171. private void cancelNotification() {
  172. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  173. mNotificationManager.cancel(1);
  174. }
  175. private void wifiChangeNotification(){
  176. SharedPreferences defaultPref = PreferenceManager.getDefaultSharedPreferences(this);
  177. String strRingtonePreference = defaultPref.getString("pref_notification_sound", "content://settings/system/notification_sound");
  178. NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
  179. .setContentTitle(getString(R.string.app_name))
  180. .setWhen(System.currentTimeMillis())
  181. .setTicker("HOsTaGe stopped due Connection change!")
  182. .setContentText("HOsTaGe stopped due Connection change!")
  183. .setSmallIcon(R.drawable.ic_launcher)
  184. .setAutoCancel(true)
  185. .setSound(Uri.parse(strRingtonePreference));
  186. if(defaultPref.getBoolean("pref_vibration", true)){
  187. builder.setVibrate(new long[]{100, 200, 100, 200});
  188. }
  189. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  190. stackBuilder.addParentStack(MainActivity.class);
  191. stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
  192. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
  193. PendingIntent.FLAG_UPDATE_CURRENT);
  194. builder.setContentIntent(resultPendingIntent);
  195. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  196. mNotificationManager.notify(2, builder.build());
  197. }
  198. private ArrayList<Protocol> getProtocolArray() {
  199. String[] protocols = getResources().getStringArray(R.array.protocols);
  200. String packageName = Protocol.class.getPackage().getName();
  201. ArrayList<Protocol> protocolArray = new ArrayList<Protocol>();
  202. for (String protocol : protocols) {
  203. try {
  204. protocolArray.add((Protocol) Class.forName(
  205. String.format("%s.%s", packageName, protocol))
  206. .newInstance());
  207. } catch (Exception e) {
  208. e.printStackTrace();
  209. }
  210. }
  211. return protocolArray;
  212. }
  213. public boolean hasRunningListeners(){
  214. for (HoneyListener listener : listeners) {
  215. if (listener.isRunning())
  216. return true;
  217. }
  218. return false;
  219. }
  220. // IPC
  221. public void notifyUI(String protocol, String key) {
  222. // Send Notification
  223. if (key.equals(MainActivity.HANDLER_COUNT)){
  224. updateNotification();
  225. }
  226. // Inform UI of Preference Change
  227. Intent intent = new Intent(MainActivity.BROADCAST);
  228. LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
  229. }
  230. public void startListeners() {
  231. for (HoneyListener listener : listeners) {
  232. if(!listener.isRunning()){
  233. listener.start();
  234. }
  235. }
  236. Toast.makeText(getApplicationContext(), "SERVICES STARTED!", Toast.LENGTH_SHORT).show();
  237. }
  238. public void stopListeners() {
  239. for (HoneyListener listener : listeners) {
  240. if(listener.isRunning()){
  241. listener.stop();
  242. }
  243. }
  244. Toast.makeText(getApplicationContext(), "SERVICES STOPPED!", Toast.LENGTH_SHORT).show();
  245. }
  246. public void startListener(String protocolName) {
  247. for (HoneyListener listener : listeners) {
  248. if (listener.getProtocolName().equals(protocolName)) {
  249. if(!listener.isRunning()){
  250. listener.start();
  251. }
  252. }
  253. }
  254. Toast.makeText(getApplicationContext(), protocolName + " SERVICE STARTED!", Toast.LENGTH_SHORT).show();
  255. }
  256. public void stopListener(String protocolName) {
  257. for (HoneyListener listener : listeners) {
  258. if (listener.getProtocolName().equals(protocolName)) {
  259. if(listener.isRunning()){
  260. listener.stop();
  261. }
  262. }
  263. }
  264. Toast.makeText(getApplicationContext(), protocolName + " SERVICE STOPPED!", Toast.LENGTH_SHORT).show();
  265. }
  266. public void toggleListener(String protocolName) {
  267. for (HoneyListener listener : listeners) {
  268. if (listener.getProtocolName().equals(protocolName)) {
  269. if (listener.isRunning()) {
  270. stopListener(protocolName);
  271. } else {
  272. startListener(protocolName);
  273. }
  274. }
  275. }
  276. }
  277. }