MainActivity.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. package de.tudarmstadt.informatik.hostage.ui;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import android.app.Activity;
  5. import android.app.ActivityManager;
  6. import android.app.ActivityManager.RunningServiceInfo;
  7. import android.content.BroadcastReceiver;
  8. import android.content.ComponentName;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.content.IntentFilter;
  12. import android.content.ServiceConnection;
  13. import android.content.SharedPreferences;
  14. import android.content.SharedPreferences.Editor;
  15. import android.net.ConnectivityManager;
  16. import android.os.Bundle;
  17. import android.os.IBinder;
  18. import android.support.v4.content.LocalBroadcastManager;
  19. import android.view.GestureDetector;
  20. import android.view.GestureDetector.SimpleOnGestureListener;
  21. import android.view.Menu;
  22. import android.view.MenuItem;
  23. import android.view.MotionEvent;
  24. import android.view.View;
  25. import android.view.View.OnTouchListener;
  26. import android.view.animation.Animation;
  27. import android.view.animation.AnimationUtils;
  28. import android.widget.AdapterView;
  29. import android.widget.AdapterView.OnItemClickListener;
  30. import android.widget.CheckBox;
  31. import android.widget.ImageView;
  32. import android.widget.ListView;
  33. import android.widget.TextView;
  34. import android.widget.Toast;
  35. import android.widget.ToggleButton;
  36. import android.widget.ViewAnimator;
  37. import de.tudarmstadt.informatik.hostage.HoneyService;
  38. import de.tudarmstadt.informatik.hostage.HoneyService.LocalBinder;
  39. import de.tudarmstadt.informatik.hostage.R;
  40. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  41. import de.tudarmstadt.informatik.hostage.logging.DatabaseHandler;
  42. import de.tudarmstadt.informatik.hostage.logging.Logger;
  43. import de.tudarmstadt.informatik.hostage.logging.SQLLogger;
  44. /**
  45. * MainActivity is the central activity for the GUI of the application.
  46. * MainActivity is launched when the application is first started.
  47. * It shows the user: <br>
  48. * - information about the network<br>
  49. * - light indicators for recorded attacks on each protocol<br>
  50. * - amount of attacks on each protocols<br>
  51. * The user can start and stop services.
  52. * @author Mihai Plasoianu
  53. * @author Lars Pandikow
  54. *
  55. */
  56. public class MainActivity extends Activity {
  57. // String constants for whole application
  58. public static final String BROADCAST = "de.tudarmstadt.informatik.hostage.BROADCAST";
  59. public static final String SESSION_DATA = "de.tudarmstadt.informatik.hostage.SESSION_DATA";
  60. public static final String PERSISTENT_DATA = "de.tudarmstadt.informatik.hostage.SESSION_DATA";
  61. public static final String LISTENER = "_LISTENER";
  62. public static final String HANDLER_COUNT = "_HANDLER_COUNT";
  63. public static final String SSID = "SSID";
  64. public static final String BSSID = "BSSID";
  65. public static final String INTERNAL_IP = "INTERNAL_IP";
  66. public static final String EXTERNAL_IP = "EXTERNAL_IP";
  67. public static final int LIGHT_GREY = 0x01;
  68. public static final int LIGHT_GREEN = 0x02;
  69. public static final int LIGHT_RED = 0x03;
  70. public static final int LIGHT_YELLOW = 0x04;
  71. private HoneyService mService;
  72. private boolean serviceBound;
  73. private SharedPreferences pref;
  74. private Editor editor;
  75. private Logger logger;
  76. // variables for the swipe animation
  77. private ViewAnimator viewAnimator;
  78. private GestureDetector gestureDetector;
  79. private Animation animFlipInLR;
  80. private Animation animFlipOutLR;
  81. private Animation animFlipInRL;
  82. private Animation animFlipOutRL;
  83. private ListView listView;
  84. private ListViewAdapter adapter;
  85. private String protocolClicked;
  86. @Override
  87. protected void onCreate(Bundle savedInstanceState) {
  88. super.onCreate(savedInstanceState);
  89. setContentView(R.layout.activity_main);
  90. // Create dynamic view elements
  91. initViewAnimator();
  92. initListView();
  93. // Initialize Class variables
  94. pref = getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
  95. logger = new SQLLogger(this);
  96. editor = pref.edit();
  97. }
  98. @Override
  99. public boolean onCreateOptionsMenu(Menu menu) {
  100. getMenuInflater().inflate(R.menu.main, menu);
  101. return true;
  102. }
  103. @Override
  104. public boolean onOptionsItemSelected(MenuItem item) {
  105. // Handle item selection
  106. switch (item.getItemId()) {
  107. case R.id.action_settings:
  108. startActivity(new Intent(this, SettingsActivity.class));
  109. break;
  110. case R.id.action_about:
  111. startActivity(new Intent(this, AboutActivity.class));
  112. break;
  113. default:
  114. }
  115. return super.onOptionsItemSelected(item);
  116. }
  117. @Override
  118. protected void onStart() {
  119. super.onStart();
  120. //Register Broadcast Receiver
  121. registerReceiver();
  122. registerNetReceiver();
  123. // Bind service if running, else check for connection change and delete sessionData
  124. if (isServiceRunning()) {
  125. bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
  126. } else {
  127. String bssid_old = pref.getString(MainActivity.BSSID, "");
  128. String bssid_new = HelperUtils.getBSSID(this);
  129. if(bssid_new == null || !bssid_new.equals(bssid_old)){
  130. deleteSessionData();
  131. }
  132. }
  133. // Update UI
  134. updateUI();
  135. updateConnectionInfo();
  136. }
  137. @Override
  138. protected void onStop() {
  139. //Unbind running service
  140. if (isServiceRunning()) {
  141. unbindService(mConnection);
  142. }
  143. // Unregister Broadcast Receiver
  144. unregisterNetReceiver();
  145. unregisterReceiver();
  146. super.onStop();
  147. }
  148. @Override
  149. protected void onDestroy(){
  150. super.onDestroy();
  151. // If service not running delete session data
  152. if(!isServiceRunning()){
  153. deleteSessionData();
  154. }
  155. }
  156. /**
  157. * Called when User presses on/off button
  158. * @param view
  159. */
  160. public void buttonOnOffClick(View view) {
  161. if (((ToggleButton) view).isChecked()) {
  162. if (isParanoid()) {
  163. protocolClicked = "PANIC";
  164. } else {
  165. protocolClicked = "SMB";
  166. }
  167. startAndBind();
  168. } else {
  169. mService.stopListeners();
  170. stopAndUnbind();
  171. }
  172. }
  173. /**
  174. * Starts the ViewLog activity, when the Button is pressed
  175. * @see ViewLog
  176. * @param view View elements which triggers the method call.
  177. */
  178. public void showLog(View view){
  179. startActivity(new Intent(this, ViewLog.class));
  180. }
  181. /**
  182. * If mobile phone is connected to a wireless network starts the background service ands binds itself to it.
  183. * Else notifies the user that service could not be started.
  184. */
  185. private void startAndBind() {
  186. if(HelperUtils.getBSSID(this) != null){
  187. startService(getServiceIntent());
  188. bindService();
  189. } else{
  190. ToggleButton button = (ToggleButton) findViewById(R.id.toggleButtonOnOff);
  191. button.setChecked(false);
  192. Toast.makeText(getApplicationContext(), "To start a service, first connect to a wireless network.", Toast.LENGTH_SHORT).show();
  193. }
  194. }
  195. /**
  196. * Binds service to Activity
  197. * @see HoneyService
  198. */
  199. private void bindService(){
  200. bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
  201. serviceBound = true;
  202. }
  203. /**
  204. * Stops service and unbinds it.
  205. * @see HoneyService
  206. */
  207. private void stopAndUnbind() {
  208. unbindService();
  209. stopService(getServiceIntent());
  210. }
  211. /**
  212. * Unbinds service.
  213. * @see HoneyService
  214. */
  215. private void unbindService(){
  216. unbindService(mConnection);
  217. serviceBound = false;
  218. }
  219. /**
  220. * Connection to bind the background service
  221. * @see HoneyService
  222. */
  223. private ServiceConnection mConnection = new ServiceConnection() {
  224. /**
  225. * After the service is bound, check which has been clicked and start it.
  226. * @see android.content.ServiceConnection#onServiceConnected(android.content.ComponentName)
  227. */
  228. @Override
  229. public void onServiceConnected(ComponentName name, IBinder service) {
  230. mService = ((LocalBinder) service).getService();
  231. if(protocolClicked != null && protocolClicked.equals("PANIC")){
  232. mService.startListeners();
  233. }else if (protocolClicked != null){
  234. mService.toggleListener(protocolClicked);
  235. }
  236. protocolClicked = null;
  237. }
  238. /**
  239. * After the service is unbound, delete reference.
  240. * @see android.content.ServiceConnection#onServiceDisconnected(android.content.ComponentName)
  241. */
  242. @Override
  243. public void onServiceDisconnected(ComponentName name) {
  244. mService = null;
  245. }
  246. };
  247. /**
  248. * Returns an intent to start HoneyService.
  249. * @return An Intent to start HoneyService
  250. */
  251. private Intent getServiceIntent() {
  252. return new Intent(this, HoneyService.class);
  253. }
  254. /**
  255. * Checks if user selected paranoid mode.
  256. * @return True when paranoid mode is selected, else returns false.
  257. */
  258. private boolean isParanoid() {
  259. return ((CheckBox) findViewById(R.id.checkBoxParanoid)).isChecked();
  260. }
  261. /**
  262. * Initializes the ListView. Creating its contents dynamic from protocol protocols.xml
  263. * @see /res/values/protocols.xml
  264. */
  265. private void initListView() {
  266. ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
  267. for (String protocol : getResources().getStringArray(R.array.protocols)) {
  268. HashMap<String, String> d = new HashMap<String, String>();
  269. d.put("light", String.valueOf(R.drawable.light_grey));
  270. d.put("protocol", protocol);
  271. d.put("connections", "-");
  272. data.add(d);
  273. }
  274. listView = (ListView) findViewById(R.id.listViewProtocols);
  275. adapter = new ListViewAdapter(getLayoutInflater(), data);
  276. listView.setAdapter(adapter);
  277. listView.setOnTouchListener(new OnTouchListener() {
  278. @Override
  279. public boolean onTouch(View v, MotionEvent event) {
  280. return gestureDetector.onTouchEvent(event);
  281. }
  282. });
  283. listView.setOnItemClickListener(new OnItemClickListener() {
  284. @Override
  285. public void onItemClick(AdapterView<?> parent, View view,
  286. int position, long id) {
  287. String protocolName = (String) ((HashMap<?, ?>) adapter
  288. .getItem(position)).get("protocol");
  289. if (isServiceRunning()) {
  290. mService.toggleListener(protocolName);
  291. if(!mService.hasRunningListeners()){
  292. stopAndUnbind();
  293. }
  294. }else{
  295. protocolClicked = protocolName;
  296. startAndBind();
  297. }
  298. }
  299. });
  300. }
  301. /**
  302. * Checks if a HoneService instance is running.
  303. * @return True if HonerService is running, else false.
  304. * @see HonerService
  305. */
  306. private boolean isServiceRunning() {
  307. ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  308. for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
  309. if (service.service.getClassName().equals(HoneyService.class.getName())) {
  310. return true;
  311. }
  312. }
  313. return false;
  314. }
  315. /**
  316. * Deletes all session related Data.
  317. */
  318. private void deleteSessionData(){
  319. editor.clear();
  320. editor.commit();
  321. }
  322. /**
  323. * Register broadcast receiver for costume broadcast.
  324. * @see BROADCAST
  325. */
  326. private void registerReceiver() {
  327. LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver,
  328. new IntentFilter(BROADCAST));
  329. }
  330. /**
  331. * Unregisters broadcast receiver for custom broadcast.
  332. * @see MainActivity#BROADCAST
  333. */
  334. private void unregisterReceiver() {
  335. LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
  336. }
  337. /**
  338. * Receiver for custom broadcast.
  339. */
  340. private BroadcastReceiver mReceiver = new BroadcastReceiver() {
  341. @Override
  342. public void onReceive(Context context, Intent intent) {
  343. // Update user interface.
  344. updateUI();
  345. }
  346. };
  347. /**
  348. * Register broadcast receiver for network state changes.
  349. * @see ConnectivityManager#CONNECTIVITY_ACTION
  350. */
  351. private void registerNetReceiver() {
  352. IntentFilter intent = new IntentFilter();
  353. intent.addAction(ConnectivityManager.CONNECTIVITY_ACTION); //"android.net.conn.CONNECTIVITY_CHANGE"
  354. registerReceiver(netReceiver, intent);
  355. }
  356. /**
  357. * Unregister broadcast receiver for network state changes.
  358. */
  359. private void unregisterNetReceiver() {
  360. unregisterReceiver(netReceiver);
  361. }
  362. /**
  363. * Receiver for network state change events.
  364. */
  365. private BroadcastReceiver netReceiver = new BroadcastReceiver() {
  366. @Override
  367. public void onReceive(Context context, Intent intent) {
  368. String bssid_old = pref.getString(BSSID, "");
  369. String bssid_new = HelperUtils.getBSSID(context);
  370. if ((bssid_new == null || !bssid_new.equals(bssid_old)) && serviceBound) {
  371. Toast.makeText(getApplicationContext(),"Connection changed! Services stopped!", Toast.LENGTH_LONG).show();
  372. unbindService();
  373. }
  374. updateConnectionInfo();
  375. }
  376. };
  377. /**
  378. * Updates Information shown by the GUI.
  379. */
  380. private void updateUI() {
  381. boolean activeListeners = false;
  382. boolean activeHandlers = false;
  383. boolean yellowLight = false;
  384. //Check for all protocols if listeners are active and attacks have been recorded
  385. //Update protocol lights and connection information.
  386. for(String protocol : getResources().getStringArray(R.array.protocols)){
  387. //Check if protocol is active
  388. if(pref.getBoolean(protocol + LISTENER, false)){
  389. activeListeners = true;
  390. int handlerCount = pref.getInt(protocol + HANDLER_COUNT, 0);
  391. //Check if attacks have been recorded in this session.
  392. if(handlerCount > 0){
  393. activeHandlers = true;
  394. updateProtocolLight(LIGHT_RED, protocol);
  395. updateProtocolConnections(handlerCount, protocol);
  396. } else{
  397. //Check if the bssid of the wireless network has already been recorded as infected.
  398. if(logger.bssidSeen(protocol, HelperUtils.getBSSID(getApplicationContext()))){
  399. updateProtocolLight(LIGHT_YELLOW, protocol);
  400. yellowLight = true;
  401. } else{
  402. updateProtocolLight(LIGHT_GREEN, protocol);
  403. }
  404. updateProtocolConnections(0, protocol);
  405. }
  406. }else{
  407. updateProtocolLight(LIGHT_GREY, protocol);
  408. }
  409. }
  410. //Update the big attack indicator.
  411. if (activeListeners) {
  412. if (activeHandlers) {
  413. updateStatusLight(LIGHT_RED);
  414. } else {
  415. if(yellowLight){
  416. updateStatusLight(LIGHT_YELLOW);
  417. } else {
  418. updateStatusLight(LIGHT_GREEN);
  419. }
  420. }
  421. ((ToggleButton) findViewById(R.id.toggleButtonOnOff))
  422. .setChecked(true);
  423. findViewById(R.id.checkBoxParanoid).setEnabled(false);
  424. } else {
  425. updateStatusLight(LIGHT_GREY);
  426. ((ToggleButton) findViewById(R.id.toggleButtonOnOff))
  427. .setChecked(false);
  428. findViewById(R.id.checkBoxParanoid).setEnabled(true);
  429. }
  430. }
  431. /**
  432. * Sets the big light indicator.
  433. * @param light Integer code to set the light color.
  434. * @see MainActivity#LIGHT_GREY
  435. * @see MainActivity#LIGHT_GREEN
  436. * @see MainActivity#LIGHT_RED
  437. * @see MainActivity#LIGHT_YELLOW
  438. */
  439. private void updateStatusLight(int light) {
  440. switch (light) {
  441. case LIGHT_GREY:
  442. ((ImageView) findViewById(R.id.imageViewLight))
  443. .setImageResource(R.drawable.light_grey_large);
  444. break;
  445. case LIGHT_GREEN:
  446. ((ImageView) findViewById(R.id.imageViewLight))
  447. .setImageResource(R.drawable.light_green_large);
  448. break;
  449. case LIGHT_RED:
  450. ((ImageView) findViewById(R.id.imageViewLight))
  451. .setImageResource(R.drawable.light_red_large);
  452. break;
  453. case LIGHT_YELLOW:
  454. ((ImageView) findViewById(R.id.imageViewLight))
  455. .setImageResource(R.drawable.light_yellow_large);
  456. break;
  457. }
  458. }
  459. /**
  460. * Sets the light indicator for a given protocol.
  461. * @param light Integer code to set the light color.
  462. * @param protocolName Name of the protocol which should be updated.
  463. */
  464. private void updateProtocolLight(int light, String protocolName) {
  465. for (int i = 0; i < adapter.getCount(); ++i) {
  466. HashMap<String, String> d = (HashMap<String, String>) adapter
  467. .getItem(i);
  468. if (d.get("protocol").equals(protocolName)) {
  469. switch (light) {
  470. case LIGHT_GREY:
  471. d.put("light", String.valueOf(R.drawable.light_grey));
  472. d.put("connections", "-");
  473. break;
  474. case LIGHT_GREEN:
  475. d.put("light", String.valueOf(R.drawable.light_green));
  476. break;
  477. case LIGHT_RED:
  478. d.put("light", String.valueOf(R.drawable.light_red));
  479. break;
  480. case LIGHT_YELLOW:
  481. d.put("light", String.valueOf(R.drawable.light_yellow));
  482. break;
  483. }
  484. }
  485. }
  486. adapter.notifyDataSetChanged();
  487. }
  488. /**
  489. * Sets the connections count for a given protocol
  490. * @param connections New value for recorded connections.
  491. * @param protocolName Name of the protocol which should be updated.
  492. */
  493. private void updateProtocolConnections(int connections, String protocolName) {
  494. for (int i = 0; i < adapter.getCount(); ++i) {
  495. HashMap<String, String> d = ((HashMap<String, String>) adapter
  496. .getItem(i));
  497. if (d.get("protocol").equals(protocolName)) {
  498. d.put("connections", String.valueOf(connections));
  499. }
  500. }
  501. adapter.notifyDataSetChanged();
  502. }
  503. /**
  504. * Gets Information about connection state and updates the GUI.
  505. */
  506. private void updateConnectionInfo() {
  507. //Get text fields
  508. TextView ssidView = (TextView) findViewById(R.id.textViewSSIDValue);
  509. TextView bssidView = (TextView) findViewById(R.id.textViewBSSIDValue);
  510. TextView internalIPView = (TextView) findViewById(R.id.textViewInternalIPValue);
  511. TextView externalIPView = (TextView) findViewById(R.id.textViewExternalIPValue);
  512. //Update the connection information
  513. HelperUtils.updateConnectionInfo(this);
  514. //Get connection information
  515. String ssid = pref.getString(SSID, "-");
  516. String bssid = pref.getString(BSSID, "-");
  517. String internalIP = pref.getString(INTERNAL_IP, "-");
  518. String externalIP = pref.getString(EXTERNAL_IP, "-");
  519. //Set text fields
  520. if (ssid != null)
  521. ssidView.setText(ssid);
  522. else
  523. ssidView.setText("-");
  524. if (bssid != null)
  525. bssidView.setText(bssid);
  526. else
  527. bssidView.setText("-");
  528. if (internalIP != null)
  529. internalIPView.setText(internalIP);
  530. else
  531. internalIPView.setText("-");
  532. if (externalIP != null)
  533. externalIPView.setText(externalIP);
  534. else
  535. externalIPView.setText("-");
  536. }
  537. /*############# Help functions for animation ##################*/
  538. @Override
  539. public boolean onTouchEvent(MotionEvent event) {
  540. return gestureDetector.onTouchEvent(event);
  541. }
  542. /**
  543. * Initializes variables for screen animation
  544. */
  545. private void initViewAnimator() {
  546. viewAnimator = (ViewAnimator) findViewById(R.id.viewAnimator);
  547. gestureDetector = new GestureDetector(this, simpleOnGestureListener);
  548. animFlipInLR = AnimationUtils.loadAnimation(this,
  549. R.anim.in_left_to_right);
  550. animFlipOutLR = AnimationUtils.loadAnimation(this,
  551. R.anim.out_left_to_right);
  552. animFlipInRL = AnimationUtils.loadAnimation(this,
  553. R.anim.in_right_to_left);
  554. animFlipOutRL = AnimationUtils.loadAnimation(this,
  555. R.anim.out_right_to_left);
  556. }
  557. /**
  558. * Called when a swipe to the Left is registered.
  559. */
  560. private void swipeRightToLeft() {
  561. if (viewAnimator.getDisplayedChild() == 0) {
  562. viewAnimator.setInAnimation(animFlipInRL);
  563. viewAnimator.setOutAnimation(animFlipOutRL);
  564. viewAnimator.setDisplayedChild(1);
  565. }
  566. }
  567. /**
  568. * Called when a swipe to the Right is registered.
  569. */
  570. private void swipeLeftToRight() {
  571. if (viewAnimator.getDisplayedChild() == 1) {
  572. viewAnimator.setInAnimation(animFlipInLR);
  573. viewAnimator.setOutAnimation(animFlipOutLR);
  574. viewAnimator.setDisplayedChild(0);
  575. }
  576. }
  577. SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {
  578. @Override
  579. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  580. float velocityY) {
  581. float sensitvity = 50;
  582. if ((e1.getX() - e2.getX()) > sensitvity) {
  583. swipeRightToLeft();
  584. } else if ((e2.getX() - e1.getX()) > sensitvity) {
  585. swipeLeftToRight();
  586. }
  587. return true;
  588. }
  589. };
  590. }