ProfileManager.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. package de.tudarmstadt.informatik.hostage.dao;
  2. import android.content.Context;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.io.StreamCorruptedException;
  10. import java.util.ArrayList;
  11. import java.util.Arrays;
  12. import java.util.Collection;
  13. import java.util.HashMap;
  14. import java.util.LinkedList;
  15. import java.util.List;
  16. import java.util.Map;
  17. import java.util.Random;
  18. import de.tudarmstadt.informatik.hostage.R;
  19. import de.tudarmstadt.informatik.hostage.model.Profile;
  20. import de.tudarmstadt.informatik.hostage.model.ProfilesHolder;
  21. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  22. import de.tudarmstadt.informatik.hostage.ui2.adapter.ProfileManagerListAdapter;
  23. /**
  24. * @author Alexander Brakowski
  25. * @created 10.02.14 20:24
  26. */
  27. public class ProfileManager {
  28. private static ProfileManager INSTANCE = null;
  29. private ProfileManagerListAdapter mProfileListAdapter = null;
  30. private Profile mCurrentActivatedProfile = null;
  31. private Profile mRandomProfile = null;
  32. private static final String PERSIST_FILENAME = "hostage_profiles.dat";
  33. private ProfilesHolder holder;
  34. public static ProfileManager getInstance(){
  35. if(INSTANCE == null){
  36. INSTANCE = new ProfileManager();
  37. }
  38. if(INSTANCE.getNumberOfProfiles() == 0){
  39. INSTANCE.loadData();
  40. }
  41. return INSTANCE;
  42. }
  43. private ProfileManager(){
  44. holder = new ProfilesHolder();
  45. holder.mProfiles = new HashMap<Integer, Profile>();
  46. }
  47. public void loadData(){
  48. try {
  49. FileInputStream fin = MainActivity.getContext().openFileInput(PERSIST_FILENAME);
  50. ObjectInputStream ois = new ObjectInputStream(fin);
  51. Object obj = ois.readObject();
  52. ois.close();
  53. fin.close();
  54. if(obj != null && obj instanceof ProfilesHolder){
  55. this.holder = (ProfilesHolder) obj;
  56. for(Map.Entry<Integer, Profile> entry: holder.mProfiles.entrySet()){
  57. if(entry.getValue().mActivated){
  58. this.mCurrentActivatedProfile = entry.getValue();
  59. }
  60. if(entry.getValue().mIsRandom){
  61. this.mRandomProfile = entry.getValue();
  62. }
  63. }
  64. }
  65. } catch (FileNotFoundException e) {
  66. e.printStackTrace();
  67. } catch (StreamCorruptedException e) {
  68. e.printStackTrace();
  69. } catch (IOException e) {
  70. e.printStackTrace();
  71. } catch (ClassNotFoundException e) {
  72. e.printStackTrace();
  73. } finally {
  74. if(holder.mProfiles.size() == 0){
  75. this.fillWithDefaultData();
  76. loadData();
  77. }
  78. if(this.mRandomProfile != null){
  79. randomizeProtocols(mRandomProfile);
  80. }
  81. }
  82. }
  83. public void persistData(){
  84. try {
  85. FileOutputStream fout = MainActivity.getContext().openFileOutput(PERSIST_FILENAME, Context.MODE_PRIVATE);
  86. ObjectOutputStream oos = new ObjectOutputStream(fout);
  87. oos.writeObject(holder);
  88. oos.close();
  89. fout.close();
  90. } catch (FileNotFoundException e) {
  91. e.printStackTrace();
  92. } catch (IOException e) {
  93. e.printStackTrace();
  94. }
  95. }
  96. public List<Profile> getProfilesList(){
  97. return new ArrayList<Profile>(getProfilesCollection());
  98. }
  99. public Collection<Profile> getProfilesCollection(){
  100. if(holder.mProfiles.size() == 0 || holder.mProfiles == null){
  101. this.loadData();
  102. }
  103. return holder.mProfiles.values();
  104. }
  105. public Map<Integer, Profile> getMapProfiles(){
  106. return holder.mProfiles;
  107. }
  108. public void randomizeProtocols(Profile profile){
  109. LinkedList<String> protocols = new LinkedList<String>(Arrays.asList(MainActivity.getContext().getResources().getStringArray(R.array.protocols)));
  110. protocols.remove("GHOST");
  111. profile.mActiveProtocols.clear();
  112. Random rand = new Random();
  113. int numberOfProtocolsToActivate = rand.nextInt(protocols.size()) + 1;
  114. while(numberOfProtocolsToActivate-- > 0){
  115. int randomIndex = rand.nextInt(protocols.size());
  116. String protocol = protocols.get(randomIndex);
  117. profile.mActiveProtocols.put(protocol, true);
  118. protocols.remove(protocol);
  119. }
  120. persistData();
  121. }
  122. public long persistProfile(Profile profile){
  123. if(profile.mId == -1){
  124. profile.mId = ++holder.mIncrementValue;
  125. }
  126. holder.mProfiles.put(profile.mId, profile);
  127. this.persistData();
  128. if(this.mProfileListAdapter != null){
  129. this.mProfileListAdapter.notifyDataSetChanged();
  130. }
  131. return profile.mId;
  132. }
  133. public Profile getProfile(int id){
  134. if(holder.mProfiles.size() == 0){
  135. loadData();
  136. }
  137. if(this.holder.mProfiles.containsKey(id)){
  138. return this.holder.mProfiles.get(id);
  139. }
  140. return null;
  141. }
  142. public void addProfile(Profile profile){
  143. this.addProfile(profile, true);
  144. }
  145. public void addProfile(Profile profile, boolean persist){
  146. if(profile.mId == -1){
  147. profile.mId = ++holder.mIncrementValue;
  148. }
  149. holder.mProfiles.put(profile.mId, profile);
  150. if(persist){
  151. persistData();
  152. }
  153. if(this.mProfileListAdapter != null){
  154. this.mProfileListAdapter.add(profile);
  155. this.mProfileListAdapter.notifyDataSetChanged();
  156. }
  157. }
  158. public void deleteProfile(Profile profile){
  159. if(this.holder.mProfiles.containsKey(profile.mId)){
  160. Profile p = getProfile(profile.mId);
  161. this.holder.mProfiles.remove(profile.mId);
  162. if(p.mActivated || this.mCurrentActivatedProfile.mId == p.mId){
  163. mCurrentActivatedProfile = mRandomProfile;
  164. mRandomProfile.mActivated = true;
  165. }
  166. this.persistData();
  167. //this.dbh.deleteProfile(profile.mId);
  168. if(this.mProfileListAdapter != null){
  169. this.mProfileListAdapter.remove(profile);
  170. this.mProfileListAdapter.notifyDataSetChanged();
  171. }
  172. }
  173. }
  174. public void clearProfiles(){
  175. holder.mProfiles.clear();
  176. persistData();
  177. }
  178. public void activateProfile(Profile profile){
  179. if(profile.equals(this.mCurrentActivatedProfile)) return;
  180. if(this.mCurrentActivatedProfile != null){
  181. this.mCurrentActivatedProfile.mActivated = false;
  182. this.persistProfile(this.mCurrentActivatedProfile);
  183. }
  184. profile.mActivated = true;
  185. this.mCurrentActivatedProfile = profile;
  186. this.holder.mProfiles.put(profile.mId, profile);
  187. persistData();
  188. }
  189. public boolean isRandomActive(){
  190. return this.mCurrentActivatedProfile.equals(this.mRandomProfile);
  191. }
  192. public Profile getRandomProfile(){
  193. return this.mRandomProfile;
  194. }
  195. public Profile getCurrentActivatedProfile(){
  196. return mCurrentActivatedProfile;
  197. }
  198. public void setProfileListAdapter(ProfileManagerListAdapter profileListAdapter){
  199. this.mProfileListAdapter = profileListAdapter;
  200. }
  201. public ProfileManagerListAdapter getProfileListAdapter(){
  202. return this.mProfileListAdapter;
  203. }
  204. public int getNumberOfProfiles(){
  205. return holder.mProfiles.size();
  206. }
  207. public void fillWithDefaultData(){
  208. Profile windowsVista = new Profile(
  209. 0,
  210. "Windows Vista",
  211. "This profile will imitate a Windows Vista machine",
  212. R.drawable.ic_profile_vista,
  213. false
  214. );
  215. windowsVista.mActiveProtocols.put("ECHO", true);
  216. windowsVista.mActiveProtocols.put("TELNET", true);
  217. this.addProfile(windowsVista, false);
  218. Profile windowsXP = new Profile(
  219. 1,
  220. "Windows XP",
  221. "This profile will activate Windows XP typical services",
  222. R.drawable.ic_profile_xp,
  223. false
  224. );
  225. windowsXP.mActiveProtocols.put("ECHO", true);
  226. windowsXP.mActiveProtocols.put("TELNET", true);
  227. windowsXP.mActiveProtocols.put("MySQL", true);
  228. this.addProfile(windowsXP, false);
  229. Profile serverHTTP = new Profile(
  230. 2,
  231. "Webserver HTTP",
  232. "This profile will imitate a simple webserver, which just supports the HTTP protocol",
  233. R.drawable.ic_profile_apache,
  234. false
  235. );
  236. serverHTTP.mActiveProtocols.put("HTTP", true);
  237. this.addProfile(serverHTTP, false);
  238. Profile serverWeb = new Profile(
  239. 3,
  240. "Webserver",
  241. "This profile will imitate a simple webserver, which supports both the HTTP and HTTPS protocol",
  242. R.drawable.ic_profile_apache,
  243. false
  244. );
  245. serverWeb.mActiveProtocols.put("HTTP", true);
  246. serverWeb.mActiveProtocols.put("HTTPS", true);
  247. this.addProfile(serverWeb, false);
  248. Profile unixMachine = new Profile(
  249. 4,
  250. "Unix",
  251. "This profile monitors unix typical services",
  252. R.drawable.ic_profile_unix,
  253. false
  254. );
  255. unixMachine.mActiveProtocols.put("SSH", true);
  256. unixMachine.mActiveProtocols.put("ECHO", true);
  257. this.addProfile(unixMachine, false);
  258. Profile linuxMachine = new Profile(
  259. 5,
  260. "Linux",
  261. "This profile will imitate a linux machine by monitoring linux typical services",
  262. R.drawable.ic_profile_linux,
  263. false
  264. );
  265. linuxMachine.mActiveProtocols.put("SSH", true);
  266. linuxMachine.mActiveProtocols.put("TELNET", true);
  267. linuxMachine.mActiveProtocols.put("ECHO", true);
  268. linuxMachine.mActiveProtocols.put("SMB", true);
  269. this.addProfile(linuxMachine, false);
  270. Profile voipServer = new Profile(
  271. 6,
  272. "VOIP Server",
  273. "This profile imitates a VOIP Server by monitoring the SIP service",
  274. R.drawable.ic_profile_asterisks,
  275. false
  276. );
  277. voipServer.mActiveProtocols.put("SIP", true);
  278. this.addProfile(voipServer, false);
  279. Profile randomProfile = new Profile(
  280. 7,
  281. "Random",
  282. "This profile monitors services randomly",
  283. R.drawable.ic_launcher,
  284. false
  285. );
  286. randomProfile.mIsRandom = true;
  287. this.addProfile(randomProfile, false);
  288. Profile paranoidProfile = new Profile(
  289. 8,
  290. "Paranoid",
  291. "This profile monitors all available services",
  292. R.drawable.ic_profile_paranoid,
  293. false
  294. );
  295. for(String protocol: MainActivity.context.getResources().getStringArray(R.array.protocols)){
  296. if(protocol.equals("GHOST")) continue;
  297. paranoidProfile.mActiveProtocols.put(protocol, true);
  298. }
  299. paranoidProfile.mActivated = true;
  300. this.addProfile(paranoidProfile, false);
  301. holder.mIncrementValue = 8;
  302. this.mCurrentActivatedProfile = paranoidProfile;
  303. persistData();
  304. }
  305. }