123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- package de.tudarmstadt.informatik.hostage;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.Socket;
- import java.util.List;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.content.SharedPreferences.Editor;
- import android.preference.PreferenceManager;
- import android.util.Log;
- import de.tudarmstadt.informatik.hostage.io.ByteArrayReaderWriter;
- import de.tudarmstadt.informatik.hostage.io.ReaderWriter;
- import de.tudarmstadt.informatik.hostage.io.StringReaderWriter;
- import de.tudarmstadt.informatik.hostage.logging.Logger;
- import de.tudarmstadt.informatik.hostage.logging.MyLocationManager;
- import de.tudarmstadt.informatik.hostage.logging.Record;
- import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
- import de.tudarmstadt.informatik.hostage.protocol.Protocol;
- import de.tudarmstadt.informatik.hostage.protocol.Protocol.TALK_FIRST;
- import de.tudarmstadt.informatik.hostage.ui.MainActivity;
- import de.tudarmstadt.informatik.hostage.wrapper.Packet;
- /**
- * Abstract class for a connection handler using a given protocol.
- *
- * @author Mihai Plasoianu
- * @author Wulf Pfeiffer
- */
- public class HoneyHandler implements Runnable {
- /** Time until the socket throws a time out. The time is in milliseconds. */
- private int TIMEOUT;
- /**
- * Time that a inputstream waits if no content is available to read again
- * from stream.
- */
- private int SLEEPTIME;
- protected Protocol protocol;
- private Socket client;
- protected Thread thread;
- private int attack_id;
- private int message_id = 0;
- private String externalIP;
- private String BSSID;
- private String SSID;
- private HoneyListener listener;
- /**
- * Constructor of the class. Initializes class variables for communication
- * and logging. Then starts itself in a new Thread.
- *
- * @param service
- * The background service.
- * @param listener
- * The Listener that called the service.
- * @param protocol
- * The protocol on which the handler is running.
- * @param client
- * A Socket for the communication with a remote client.
- */
- public HoneyHandler(HoneyService service, HoneyListener listener,
- Protocol protocol, Socket client) {
- this.listener = listener;
- this.protocol = protocol;
- this.client = client;
- this.thread = new Thread(this);
- SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(service);
- SLEEPTIME = pref.getInt("sleeptime", 1); // 1 ms already removes ressource leak
- TIMEOUT = pref.getInt("timeout", 30) * 1000;
- // TODO ThreadSicher?
- getAndIncrementAttackID(pref);
- SharedPreferences sessionPref = service.getSharedPreferences(MainActivity.CONNECTION_INFO, Context.MODE_PRIVATE);
- BSSID = sessionPref.getString(MainActivity.BSSID, null);
- SSID = sessionPref.getString(MainActivity.SSID, null);
- externalIP = sessionPref.getString(MainActivity.EXTERNAL_IP, null);
- setSoTimeout(client);
- thread.start();
- }
- /**
- * Set the timeout of the socket to the hard coded time out variable.
- *
- * @param client
- * The socket
- * @see #TIMEOUT
- */
- private void setSoTimeout(Socket client) {
- try {
- client.setSoTimeout(TIMEOUT);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Creates InputStream and OutputStream for the socket. Starts communication
- * with client. When the client closes the connection or a time out occurs
- * the handler is finished.
- */
- @Override
- public void run() {
- InputStream in;
- OutputStream out;
- try {
- in = client.getInputStream();
- out = client.getOutputStream();
- talkToClient(in, out);
- } catch (Exception e) {
- e.printStackTrace();
- }
- Log.i("HoneyHandler", "Connection closed, close Socket and kill Thread");
- kill();
- }
- /**
- * Sets the interrupt flag of the thread and tries to close the socket.
- */
- public void kill() {
- thread.interrupt();
- try {
- client.close();
- Log.i("HoneyHandler", "Socket closed: " + client.isClosed());
- } catch (Exception e) {
- e.printStackTrace();
- }
- listener.refreshHandlers();
- }
- /**
- * Determines if the interrupt flag of the thread is set.
- *
- * @return True when the flag is set, else false.
- */
- public boolean isTerminated() {
- return thread.isInterrupted();
- }
- /**
- * Communicates with a client using the corresponding protocol
- * implementation.
- *
- * @param in
- * InputStream of the socket.
- * @param out
- * OutputStream of the socket.
- * @throws IOException
- */
- protected void talkToClient(InputStream in, OutputStream out)
- throws IOException {
- ReaderWriter stream;
- if (protocol.getType().equals(byte[].class)) {
- stream = new ByteArrayReaderWriter(in, out, SLEEPTIME);
- } else {
- stream = new StringReaderWriter(in, out);
- }
- Packet inputLine;
- List<Packet> outputLine;
- if (protocol.whoTalksFirst() == TALK_FIRST.SERVER) {
- outputLine = protocol.processMessage(null);
- stream.write(outputLine);
- for (Packet o : outputLine) {
- Logger.log(HoneyService.getContext(),
- createRecord(TYPE.SEND, o.toString()));
- }
- }
- while (!thread.isInterrupted() && (inputLine = stream.read()) != null) {
- outputLine = protocol.processMessage(inputLine);
- Logger.log(HoneyService.getContext(),
- createRecord(TYPE.RECEIVE, inputLine.toString()));
- if (outputLine != null) {
- stream.write(outputLine);
- for (Packet o : outputLine) {
- Logger.log(HoneyService.getContext(),
- createRecord(TYPE.SEND, o.toString()));
- }
- }
- if (protocol.isClosed()) {
- break;
- }
- }
- }
- /**
- * Creates a Record for a message exchanged with a client.
- *
- * @param type
- * The type of the message.
- * @param packet
- * The content of the message.
- * @return The Record representing the communication message.
- */
- protected Record createRecord(TYPE type, String packet) {
- Record record = new Record();
- record.setId(message_id++);
- record.setAttack_id(attack_id);
- record.setProtocol(protocol.toString());
- record.setType(type);
- record.setTimestamp(System.currentTimeMillis());
- record.setExternalIP(externalIP);
- record.setLocalIP(client.getLocalAddress().getHostAddress());
- record.setLocalHost(client.getLocalAddress().getHostName());
- record.setLocalPort(protocol.getPort());
- record.setRemoteIP(client.getInetAddress().getHostAddress());
- record.setRemoteHost(client.getInetAddress().getHostName());
- record.setRemotePort(client.getPort());
- record.setBssid(BSSID);
- record.setSsid(SSID);
- record.setPacket(packet);
- if (MyLocationManager.getNewestLocation() != null) {
- record.setLatitude(MyLocationManager.getNewestLocation()
- .getLatitude());
- record.setLongitude(MyLocationManager.getNewestLocation()
- .getLongitude());
- record.setAccuracy(MyLocationManager.getNewestLocation()
- .getAccuracy());
- record.setTimestampLocation(MyLocationManager.getNewestLocation()
- .getTime());
- } else {
- record.setLatitude(0.0);
- record.setLongitude(0.0);
- record.setAccuracy(Float.MAX_VALUE);
- record.setTimestampLocation(0);
- }
- return record;
- }
- /**
- * Gets attack ID for the attack. Also increases the attack ID counter by
- * one. Method is synchronized for thread safety.
- *
- * @param pref
- * The default SharedPreference of the application
- * @return Unique integer attack ID
- */
- private synchronized void getAndIncrementAttackID(SharedPreferences pref) {
- Editor editor = pref.edit();
- attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
- editor.putInt("ATTACK_ID_COUNTER", attack_id + 1);
- editor.commit();
- }
- }
|