diff --git a/src/main/protocol/memory/Rc4Obtainer.java b/src/main/protocol/memory/Rc4Obtainer.java index cd173fb..62d5f7e 100644 --- a/src/main/protocol/memory/Rc4Obtainer.java +++ b/src/main/protocol/memory/Rc4Obtainer.java @@ -72,6 +72,7 @@ public class Rc4Obtainer { if (payloadBuffer.peak().length == 0) { outgoingHandler.setRc4(rc4Tryout); + incomingHandler.setRc4(rc4Tryout); break outerloop; } diff --git a/src/main/protocol/packethandler/Handler.java b/src/main/protocol/packethandler/Handler.java index ab0d206..fd12dff 100644 --- a/src/main/protocol/packethandler/Handler.java +++ b/src/main/protocol/packethandler/Handler.java @@ -2,6 +2,7 @@ package main.protocol.packethandler; import main.protocol.HMessage; import main.protocol.TrafficListener; +import main.protocol.crypto.RC4; import java.io.IOException; import java.io.OutputStream; @@ -10,6 +11,8 @@ import java.util.List; public abstract class Handler { + protected static final boolean DEBUG = false; + volatile PayloadBuffer payloadBuffer = new PayloadBuffer(); volatile OutputStream out; volatile Object[] listeners = null; //get notified on packet send @@ -17,6 +20,9 @@ public abstract class Handler { volatile boolean isDataStream = false; volatile int currentIndex = 0; + protected RC4 clientcipher = null; + protected RC4 servercipher = null; + public Handler(OutputStream outputStream, Object[] listeners) { this.listeners = listeners; @@ -28,18 +34,11 @@ public abstract class Handler { isDataStream = true; } - public void act(byte[] buffer) throws IOException { - if (isDataStream) { - payloadBuffer.push(buffer); - notifyBufferListeners(buffer.length); + public abstract void act(byte[] buffer) throws IOException; - if (!isTempBlocked) { - flush(); - } - } - else { - out.write(buffer); - } + public void setRc4(RC4 rc4) { + this.clientcipher = rc4.deepCopy(); + this.servercipher = rc4.deepCopy(); } public void block() { @@ -73,7 +72,7 @@ public abstract class Handler { public abstract void flush() throws IOException; - + protected abstract void printForDebugging(byte[] bytes); private List bufferListeners = new ArrayList<>(); public void addBufferListener(BufferListener listener) { diff --git a/src/main/protocol/packethandler/IncomingHandler.java b/src/main/protocol/packethandler/IncomingHandler.java index a085e6a..168a70c 100644 --- a/src/main/protocol/packethandler/IncomingHandler.java +++ b/src/main/protocol/packethandler/IncomingHandler.java @@ -13,13 +13,48 @@ public class IncomingHandler extends Handler { super(outputStream, listeners); } + private final Object lock = new Object(); + private Boolean isEncryptedStream = null; + + + @Override + public void act(byte[] buffer) throws IOException { + if (isDataStream) { + if (DEBUG) { + printForDebugging(buffer); + } + + + if (isEncryptedStream == null || !isEncryptedStream) { + payloadBuffer.push(buffer); + } + else { + payloadBuffer.push(servercipher.rc4(buffer)); + } + + + notifyBufferListeners(buffer.length); + + if (!isTempBlocked) { + flush(); + } + } + else { + out.write(buffer); + } + } + @Override public void sendToStream(byte[] buffer) { synchronized (lock) { try { - out.write(buffer); + out.write( + (isEncryptedStream == null || !isEncryptedStream) + ? buffer + : clientcipher.rc4(buffer) + ); } catch (IOException e) { e.printStackTrace(); } @@ -33,14 +68,29 @@ public class IncomingHandler extends Handler { for (HPacket hpacket : hpackets){ HMessage hMessage = new HMessage(hpacket, HMessage.Side.TOCLIENT, currentIndex); - if (isDataStream) notifyListeners(hMessage); + if (isDataStream) { + notifyListeners(hMessage); + } if (!hMessage.isBlocked()) { - out.write(hMessage.getPacket().toBytes()); + out.write( + (isEncryptedStream == null || !isEncryptedStream) + ? hMessage.getPacket().toBytes() + : clientcipher.rc4(hMessage.getPacket().toBytes()) + ); + } + + if (isDataStream && isEncryptedStream == null && hpacket.length() == 261) { + isEncryptedStream = hpacket.readBoolean(264); } currentIndex++; } } } + + @Override + protected void printForDebugging(byte[] bytes) { + System.out.println("-- DEBUG INCOMING -- " + new HPacket(bytes).toString() + " -- DEBUG --"); + } } diff --git a/src/main/protocol/packethandler/OutgoingHandler.java b/src/main/protocol/packethandler/OutgoingHandler.java index ece5193..545dbe9 100644 --- a/src/main/protocol/packethandler/OutgoingHandler.java +++ b/src/main/protocol/packethandler/OutgoingHandler.java @@ -14,10 +14,7 @@ public class OutgoingHandler extends Handler { private final Object lock = new Object(); - private final static int encryptOffset = 3; //all packets with index < 3 aren't encrypted - private RC4 clientcipher = null; - private RC4 servercipher = null; private List tempEncryptedBuffer = new ArrayList<>(); public OutgoingHandler(OutputStream outputStream, Object[] listeners) { @@ -35,7 +32,6 @@ public class OutgoingHandler extends Handler { public void act(byte[] buffer) throws IOException { dataStreamCheck(buffer); if (isDataStream) { - if (currentIndex < encryptOffset) { payloadBuffer.push(buffer); } @@ -45,7 +41,11 @@ public class OutgoingHandler extends Handler { } } else { - payloadBuffer.push(clientcipher.rc4(buffer)); + byte[] tm = clientcipher.rc4(buffer); + if (DEBUG) { + printForDebugging(tm); + } + payloadBuffer.push(tm); } notifyBufferListeners(buffer.length); @@ -60,21 +60,9 @@ public class OutgoingHandler extends Handler { } @Override - public void sendToStream(byte[] buffer) { - synchronized (lock) { - try { - out.write(servercipher.rc4(buffer)); - } catch (IOException e) { - e.printStackTrace(); - } - } - - } - public void setRc4(RC4 rc4) { - this.clientcipher = rc4; - this.servercipher = rc4.deepCopy(); - + super.setRc4(rc4); + byte[] encrbuffer = new byte[tempEncryptedBuffer.size()]; for (int i = 0; i < tempEncryptedBuffer.size(); i++) { encrbuffer[i] = tempEncryptedBuffer.get(i); @@ -87,6 +75,19 @@ public class OutgoingHandler extends Handler { } tempEncryptedBuffer = null; } + + @Override + public void sendToStream(byte[] buffer) { + synchronized (lock) { + try { + out.write(servercipher.rc4(buffer)); + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + public List getEncryptedBuffer() { return tempEncryptedBuffer; } @@ -109,4 +110,10 @@ public class OutgoingHandler extends Handler { } } + + + @Override + protected void printForDebugging(byte[] bytes) { + System.out.println("-- DEBUG OUTGOING -- " + new HPacket(bytes).toString() + " -- DEBUG --"); + } } diff --git a/src/main/ui/logger/loggerdisplays/PacketLoggerFactory.java b/src/main/ui/logger/loggerdisplays/PacketLoggerFactory.java index bb74f55..267957a 100644 --- a/src/main/ui/logger/loggerdisplays/PacketLoggerFactory.java +++ b/src/main/ui/logger/loggerdisplays/PacketLoggerFactory.java @@ -1,14 +1,19 @@ package main.ui.logger.loggerdisplays; +import main.misc.OSValidator; + /** * Created by Jonas on 04/04/18. */ public class PacketLoggerFactory { public static PacketLogger get() { - if (System.getenv("XDG_CURRENT_DESKTOP") != null && System.getenv("XDG_CURRENT_DESKTOP").toLowerCase().contains("gnome")) { + if (OSValidator.isUnix()) { return new GnomeTerminalLogger(); } +// if (System.getenv("XDG_CURRENT_DESKTOP") != null && System.getenv("XDG_CURRENT_DESKTOP").toLowerCase().contains("gnome")) { +// return new GnomeTerminalLogger(); +// } return new SimpleTerminalLogger(); }