123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- package de.tudarmstadt.informatik.hostage.dao;
- import android.content.Context;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.StreamCorruptedException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import java.util.Random;
- import de.tudarmstadt.informatik.hostage.R;
- import de.tudarmstadt.informatik.hostage.model.Profile;
- import de.tudarmstadt.informatik.hostage.model.ProfilesHolder;
- import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
- import de.tudarmstadt.informatik.hostage.ui2.adapter.ProfileManagerListAdapter;
- /**
- * @author Alexander Brakowski
- * @created 10.02.14 20:24
- */
- public class ProfileManager {
- private static ProfileManager INSTANCE = null;
- private ProfileManagerListAdapter mProfileListAdapter = null;
- private Profile mCurrentActivatedProfile = null;
- private Profile mRandomProfile = null;
- private static final String PERSIST_FILENAME = "hostage_profiles.dat";
- private ProfilesHolder holder;
- public static ProfileManager getInstance(){
- if(INSTANCE == null){
- INSTANCE = new ProfileManager();
- }
- if(INSTANCE.getNumberOfProfiles() == 0){
- INSTANCE.loadData();
- }
- return INSTANCE;
- }
- private ProfileManager(){
- holder = new ProfilesHolder();
- holder.mProfiles = new HashMap<Integer, Profile>();
- }
- public void loadData(){
- try {
- FileInputStream fin = MainActivity.getContext().openFileInput(PERSIST_FILENAME);
- ObjectInputStream ois = new ObjectInputStream(fin);
- Object obj = ois.readObject();
- ois.close();
- fin.close();
- if(obj != null && obj instanceof ProfilesHolder){
- this.holder = (ProfilesHolder) obj;
- for(Map.Entry<Integer, Profile> entry: holder.mProfiles.entrySet()){
- if(entry.getValue().mActivated){
- this.mCurrentActivatedProfile = entry.getValue();
- }
- if(entry.getValue().mIsRandom){
- this.mRandomProfile = entry.getValue();
- }
- }
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (StreamCorruptedException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } finally {
- if(holder.mProfiles.size() == 0){
- this.fillWithDefaultData();
- loadData();
- }
- if(this.mRandomProfile != null){
- randomizeProtocols(mRandomProfile);
- }
- }
- }
- public void persistData(){
- try {
- FileOutputStream fout = MainActivity.getContext().openFileOutput(PERSIST_FILENAME, Context.MODE_PRIVATE);
- ObjectOutputStream oos = new ObjectOutputStream(fout);
- oos.writeObject(holder);
- oos.close();
- fout.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public List<Profile> getProfilesList(){
- return new ArrayList<Profile>(getProfilesCollection());
- }
- public Collection<Profile> getProfilesCollection(){
- if(holder.mProfiles.size() == 0 || holder.mProfiles == null){
- this.loadData();
- }
- return holder.mProfiles.values();
- }
- public Map<Integer, Profile> getMapProfiles(){
- return holder.mProfiles;
- }
- public void randomizeProtocols(Profile profile){
- LinkedList<String> protocols = new LinkedList<String>(Arrays.asList(MainActivity.getContext().getResources().getStringArray(R.array.protocols)));
- protocols.remove("GHOST");
- profile.mActiveProtocols.clear();
- Random rand = new Random();
- int numberOfProtocolsToActivate = rand.nextInt(protocols.size()) + 1;
- while(numberOfProtocolsToActivate-- > 0){
- int randomIndex = rand.nextInt(protocols.size());
- String protocol = protocols.get(randomIndex);
- profile.mActiveProtocols.put(protocol, true);
- protocols.remove(protocol);
- }
- persistData();
- }
- public long persistProfile(Profile profile){
- if(profile.mId == -1){
- profile.mId = ++holder.mIncrementValue;
- }
- holder.mProfiles.put(profile.mId, profile);
- this.persistData();
- if(this.mProfileListAdapter != null){
- this.mProfileListAdapter.notifyDataSetChanged();
- }
- return profile.mId;
- }
- public Profile getProfile(int id){
- if(holder.mProfiles.size() == 0){
- loadData();
- }
- if(this.holder.mProfiles.containsKey(id)){
- return this.holder.mProfiles.get(id);
- }
- return null;
- }
- public void addProfile(Profile profile){
- this.addProfile(profile, true);
- }
- public void addProfile(Profile profile, boolean persist){
- if(profile.mId == -1){
- profile.mId = ++holder.mIncrementValue;
- }
- holder.mProfiles.put(profile.mId, profile);
- if(persist){
- persistData();
- }
- if(this.mProfileListAdapter != null){
- this.mProfileListAdapter.add(profile);
- this.mProfileListAdapter.notifyDataSetChanged();
- }
- }
- public void deleteProfile(Profile profile){
- if(this.holder.mProfiles.containsKey(profile.mId)){
- Profile p = getProfile(profile.mId);
- this.holder.mProfiles.remove(profile.mId);
- if(p.mActivated || this.mCurrentActivatedProfile.mId == p.mId){
- mCurrentActivatedProfile = mRandomProfile;
- mRandomProfile.mActivated = true;
- }
- this.persistData();
- //this.dbh.deleteProfile(profile.mId);
- if(this.mProfileListAdapter != null){
- this.mProfileListAdapter.remove(profile);
- this.mProfileListAdapter.notifyDataSetChanged();
- }
- }
- }
- public void clearProfiles(){
- holder.mProfiles.clear();
- persistData();
- }
- public void activateProfile(Profile profile){
- if(profile.equals(this.mCurrentActivatedProfile)) return;
- if(this.mCurrentActivatedProfile != null){
- this.mCurrentActivatedProfile.mActivated = false;
- this.persistProfile(this.mCurrentActivatedProfile);
- }
- profile.mActivated = true;
- this.mCurrentActivatedProfile = profile;
- this.holder.mProfiles.put(profile.mId, profile);
- persistData();
- }
- public boolean isRandomActive(){
- return this.mCurrentActivatedProfile.equals(this.mRandomProfile);
- }
- public Profile getRandomProfile(){
- return this.mRandomProfile;
- }
- public Profile getCurrentActivatedProfile(){
- return mCurrentActivatedProfile;
- }
- public void setProfileListAdapter(ProfileManagerListAdapter profileListAdapter){
- this.mProfileListAdapter = profileListAdapter;
- }
- public ProfileManagerListAdapter getProfileListAdapter(){
- return this.mProfileListAdapter;
- }
- public int getNumberOfProfiles(){
- return holder.mProfiles.size();
- }
- public void fillWithDefaultData(){
- Profile windowsVista = new Profile(
- 0,
- "Windows Vista",
- "This profile will imitate a Windows Vista machine",
- R.drawable.ic_profile_vista,
- false
- );
- windowsVista.mActiveProtocols.put("ECHO", true);
- windowsVista.mActiveProtocols.put("TELNET", true);
- this.addProfile(windowsVista, false);
- Profile windowsXP = new Profile(
- 1,
- "Windows XP",
- "This profile will activate Windows XP typical services",
- R.drawable.ic_profile_xp,
- false
- );
- windowsXP.mActiveProtocols.put("ECHO", true);
- windowsXP.mActiveProtocols.put("TELNET", true);
- windowsXP.mActiveProtocols.put("MySQL", true);
- this.addProfile(windowsXP, false);
- Profile serverHTTP = new Profile(
- 2,
- "Webserver HTTP",
- "This profile will imitate a simple webserver, which just supports the HTTP protocol",
- R.drawable.ic_profile_apache,
- false
- );
- serverHTTP.mActiveProtocols.put("HTTP", true);
- this.addProfile(serverHTTP, false);
- Profile serverWeb = new Profile(
- 3,
- "Webserver",
- "This profile will imitate a simple webserver, which supports both the HTTP and HTTPS protocol",
- R.drawable.ic_profile_apache,
- false
- );
- serverWeb.mActiveProtocols.put("HTTP", true);
- serverWeb.mActiveProtocols.put("HTTPS", true);
- this.addProfile(serverWeb, false);
- Profile unixMachine = new Profile(
- 4,
- "Unix",
- "This profile monitors unix typical services",
- R.drawable.ic_profile_unix,
- false
- );
- unixMachine.mActiveProtocols.put("SSH", true);
- unixMachine.mActiveProtocols.put("ECHO", true);
- this.addProfile(unixMachine, false);
- Profile linuxMachine = new Profile(
- 5,
- "Linux",
- "This profile will imitate a linux machine by monitoring linux typical services",
- R.drawable.ic_profile_linux,
- false
- );
- linuxMachine.mActiveProtocols.put("SSH", true);
- linuxMachine.mActiveProtocols.put("TELNET", true);
- linuxMachine.mActiveProtocols.put("ECHO", true);
- linuxMachine.mActiveProtocols.put("SMB", true);
- this.addProfile(linuxMachine, false);
- Profile voipServer = new Profile(
- 6,
- "VOIP Server",
- "This profile imitates a VOIP Server by monitoring the SIP service",
- R.drawable.ic_profile_asterisks,
- false
- );
- voipServer.mActiveProtocols.put("SIP", true);
- this.addProfile(voipServer, false);
- Profile randomProfile = new Profile(
- 7,
- "Random",
- "This profile monitors services randomly",
- R.drawable.ic_launcher,
- false
- );
- randomProfile.mIsRandom = true;
- this.addProfile(randomProfile, false);
- Profile paranoidProfile = new Profile(
- 8,
- "Paranoid",
- "This profile monitors all available services",
- R.drawable.ic_launcher,
- false
- );
- for(String protocol: MainActivity.context.getResources().getStringArray(R.array.protocols)){
- if(protocol.equals("GHOST")) continue;
- paranoidProfile.mActiveProtocols.put(protocol, true);
- }
- paranoidProfile.mActivated = true;
- this.addProfile(paranoidProfile, false);
- holder.mIncrementValue = 8;
- this.mCurrentActivatedProfile = paranoidProfile;
- persistData();
- }
- }
|