static void Main(string[] args) { PreCon.FreeTcpPort(); //finds a free port PreCon.PreSock(); //tries to connect to each address Server.SetCommands(); //sets the server's commands WmiFuncs.AddPaths(Environment.MachineName); //saves the server's local shared folders byte[] bytes = new byte[4096]; IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); for (int i = 0; i < ipHostInfo.AddressList.Length; i++) { if (ipHostInfo.AddressList[i].ToString().StartsWith("192")) { ipAddress = ipHostInfo.AddressList[i]; } } // builds the socket and starts listening for clients IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port); Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listener.Bind(localEndPoint); string pass = SetPassword(); Console.WriteLine("listening on ip: " + ipAddress + " port: " + port); listener.Listen(3); while (true) { Socket handler = listener.Accept(); Console.WriteLine("A client with the ip of " + handler.RemoteEndPoint + " has connected"); //once a new client is connected, starts the socket maintenance function for him on a separate thread Thread t = new Thread(() => KeepInTact(handler, pass)); t.Start(); } }
public static void SetCommands() { Func <string[], string>[] methods = //a dictionary of function names and their delegates { targets => WmiFuncs.GetIp(), targets => WmiFuncs.FreeSpace(), targets => WmiFuncs.ShowProcess(), targets => Disconnect(), targets => WmiFuncs.KillProcess(targets[1]), targets => Directory.GetCurrentDirectory(), targets => WmiFuncs.RemoteProcess(targets[1]), targets => WmiFuncs.ShareFolder(targets[2], targets[1]), targets => WmiFuncs.ListFiles(targets[1]), targets => WmiFuncs.ShowFolders(), targets => File.ReadAllText(Environment.CurrentDirectory + "\\info.txt"), targets => File.ReadAllText(Environment.CurrentDirectory + "\\about.txt"), targets => WmiFuncs.CopyFile(pass.Dequeue(), targets[2], targets[1]), targets => WmiFuncs.CreateFile(targets[2], targets[1]), targets => WmiFuncs.DeleteFile(targets[2], targets[1]), targets => WmiFuncs.ClientList(), targets => WmiFuncs.CPUName(), targets => WmiFuncs.TotalRAM(), targets => WmiFuncs.GetWinVer() }; for (int i = 0; i < funcs.Length; i++) { dict.Add(funcs[i], methods[i]); } }
public static string CommandOutput(string command, Socket sc) { //Console.WriteLine("message: " + command); string output = ""; bool flag = false; string[] pararms = Interpreter(command); //interprets the command and returns a list of parameters if (pararms.Length >= 2) { foreach (KeyValuePair <string, Func <string[], string> > pair in dict) { if (pair.Key == pararms[0]) //finds the right dunction { flag = true; try { if (sc != null) { pass.Enqueue(sc); //enqueues the socket to check from which client was the message sent } WmiFuncs.TryCon(pararms[pararms.Length - 1]); //tries to build the connection scope output = pair.Value(pararms); //executes and gets the value of the function } catch { output = "an error occurred, please check your parameters\n";// + e.ToString(); } } } } if (flag) //if the command succceeded, the output is returned { return(output + "stoprightnow"); } else if (pararms[0] != "command failed") { //if the command name is wrong, the most similar command is suggested to the client return("no such command as --> " + pararms[0] + MostSimilar(pararms[0]) + "stoprightnow"); } else { //for any other reason, the output is as follows return("couldn't execute your command, please check your parametersstoprightnow"); } }
private static void KeepInTact(Socket s, string pass) { byte[] bytes = new byte[4096]; bool flag = false; int bytesRec; try { while (!flag) //checks if the password matches with the saved one { bytesRec = s.Receive(bytes); data = Encoding.Unicode.GetString(bytes, 0, bytesRec); if (data == pass) { SendPackets("good to go", s); flag = true; } else { SendPackets("wrong password", s); } } bytesRec = s.Receive(bytes); //recieves data from the client to save data = Encoding.Unicode.GetString(bytes, 0, bytesRec); char[] sep = { '+' }; string[] datas = data.Split(sep); Client.CheckAndAdd(s, datas[0], datas[1]); WmiFuncs.AddPaths(datas[0]); while (true) //and starts listenung to incoming requests { bytesRec = s.Receive(bytes); data = Encoding.Unicode.GetString(bytes, 0, bytesRec); data = Server.CommandOutput(data, s); SendPackets(data, s); if (data.Contains("disconnect")) { break; // ends communication } } } catch (SocketException) { Console.WriteLine("Client has disconnected"); } }