package de.tudarmstadt.informatik.hostage.handler; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.preference.PreferenceManager; import de.tudarmstadt.informatik.hostage.HoneyListener; import de.tudarmstadt.informatik.hostage.HoneyService; import de.tudarmstadt.informatik.hostage.commons.HelperUtils; import de.tudarmstadt.informatik.hostage.logging.Logger; import de.tudarmstadt.informatik.hostage.logging.Record; import de.tudarmstadt.informatik.hostage.logging.Record.TYPE; import de.tudarmstadt.informatik.hostage.protocol.Protocol; public abstract class AbstractHandler implements Runnable { protected static final int TIMEOUT = 30 * 1000; protected Protocol protocol; private Socket client; protected Thread thread; private int attack_id; private String BSSID; private String SSID; private HoneyListener listener; protected Logger log; public AbstractHandler(HoneyService service, HoneyListener listener, Protocol protocol, Socket client) { this.listener = listener; this.log = service.getLog(); this.protocol = protocol; this.client = client; this.thread = new Thread(this); //TODO attack_id für jeden Logger SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(service); Editor editor = pref.edit(); attack_id = pref.getInt("ATTACK_ID_COUNTER", 0); editor.putInt("ATTACK_ID_COUNTER", attack_id + 1); editor.commit(); BSSID = HelperUtils.getBSSID(service.getApplicationContext()); SSID = HelperUtils.getSSID(service.getApplicationContext()); setSoTimeout(client); thread.start(); } private void setSoTimeout(Socket client) { try { client.setSoTimeout(TIMEOUT); } catch (Exception e) { e.printStackTrace(); } } @Override public void run() { InputStream in; OutputStream out; try { in = client.getInputStream(); out = client.getOutputStream(); talkToClient(in, out); } catch (Exception e) { e.printStackTrace(); } kill(); } public void kill() { thread.interrupt(); try { client.close(); } catch (Exception e) { e.printStackTrace(); } listener.refreshHandlers(); } public boolean isTerminated() { return thread.isInterrupted(); } abstract protected void talkToClient(InputStream in, OutputStream out) throws IOException; protected Record createRecord(TYPE type, String packet) { Record record = new Record(); record.setAttack_id(attack_id); record.setProtocol(protocol.toString()); record.setType(type); record.setTimestamp(System.currentTimeMillis()); record.setLocalIP(client.getLocalAddress()); record.setLocalPort(protocol.getPort()); record.setRemoteIP(client.getInetAddress()); record.setRemotePort(client.getPort()); record.setBSSID(BSSID); record.setSSID(SSID); record.setPacket(packet); return record; } }