From a531cefa00923cc89fe94750bb1b4af12a50df93 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 21 Jan 2017 16:23:10 -0500 Subject: [PATCH] Terminal remote control! --- ShiftOS.Objects/Exploit.cs | 38 --------- ShiftOS.Objects/ExploitContext.cs | 38 --------- ShiftOS.Objects/Hack.cs | 44 ----------- ShiftOS.Objects/ShiftOS.Objects.csproj | 3 - ShiftOS.Server/Program.cs | 95 ++++++++++++++++++++++- ShiftOS.WinForms/Applications/Terminal.cs | 46 ++++++++--- ShiftOS.WinForms/Commands.cs | 24 ++++++ ShiftOS.WinForms/Resources/Shiftorium.txt | 6 ++ ShiftOS_TheReturn/ServerManager.cs | 2 +- ShiftOS_TheReturn/TerminalBackend.cs | 55 +++++++++++++ ShiftOS_TheReturn/TerminalTextWriter.cs | 44 +++++++++-- 11 files changed, 254 insertions(+), 141 deletions(-) delete mode 100644 ShiftOS.Objects/Exploit.cs delete mode 100644 ShiftOS.Objects/ExploitContext.cs delete mode 100644 ShiftOS.Objects/Hack.cs diff --git a/ShiftOS.Objects/Exploit.cs b/ShiftOS.Objects/Exploit.cs deleted file mode 100644 index 7b220f1..0000000 --- a/ShiftOS.Objects/Exploit.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ShiftOS.Objects -{ - public abstract class Exploit - { - public void BeginExploit(string remote_user, bool isMud) - { - var ctx = new ExploitContext(); - SendToMUD(remote_user, "hack_getcontext"); - MessageReceived += (u, c, j) => - { - - }; - ThisContext = ctx; - } - - public ExploitContext ThisContext { get; internal set; } - - public virtual void SendToMUD(string target_user, string command, string json = "") - { - ThisContext.IsMUDHack = false; - if (command == "hack_getcontext") - { - MessageReceived?.Invoke(target_user, "context_info", ExploitContext.CreateRandom()); - } - } - - public event MUDMessageEventHandler MessageReceived; - - - public abstract void OnRun(ExploitContext ctx); - } -} diff --git a/ShiftOS.Objects/ExploitContext.cs b/ShiftOS.Objects/ExploitContext.cs deleted file mode 100644 index 5d8f378..0000000 --- a/ShiftOS.Objects/ExploitContext.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ShiftOS.Objects -{ - public class ExploitContext - { - public static string CreateRandom() - { - //We can't use JSON.NET. We must construct the JSON ourselves. - StringBuilder jBuilder = new StringBuilder(); - jBuilder.AppendLine("{"); - jBuilder.Append("\tIsMUDHack: \"false\","); - - jBuilder.AppendLine("}"); - return jBuilder.ToString(); - } - - /// - /// Gets or sets whether or not this exploit context belongs to a MUD hack session. - /// - public bool IsMUDHack { get; set; } - - /// - /// Gets or sets the target username for this exploit context. Used for talking with the MUD about it. - /// - public string TargetUsername { get; set; } - - /// - /// Gets or sets the target's locks. - /// - public List TargetLocks { get; set; } - - } -} diff --git a/ShiftOS.Objects/Hack.cs b/ShiftOS.Objects/Hack.cs deleted file mode 100644 index c1791ec..0000000 --- a/ShiftOS.Objects/Hack.cs +++ /dev/null @@ -1,44 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ShiftOS.Objects -{ - - - public delegate void MUDMessageEventHandler(string target_user, string command, string json); - - - - public abstract class Lock - { - public abstract bool Unlocked { get; } - public abstract void Unlock(); - } -} diff --git a/ShiftOS.Objects/ShiftOS.Objects.csproj b/ShiftOS.Objects/ShiftOS.Objects.csproj index 022a511..5a23ef0 100644 --- a/ShiftOS.Objects/ShiftOS.Objects.csproj +++ b/ShiftOS.Objects/ShiftOS.Objects.csproj @@ -55,9 +55,6 @@ - - - diff --git a/ShiftOS.Server/Program.cs b/ShiftOS.Server/Program.cs index 0219590..d8d953e 100644 --- a/ShiftOS.Server/Program.cs +++ b/ShiftOS.Server/Program.cs @@ -245,6 +245,99 @@ Contents: switch (msg.Name) { + case "usr_getcp": + + break; + case "usr_takecp": + if (args["username"] != null && args["password"] != null && args["amount"] != null && args["yourusername"] != null) + { + string userName = args["username"] as string; + string passw = args["password"] as string; + int amount = (int)args["amount"]; + + if (Directory.Exists("saves")) + { + foreach (var saveFile in Directory.GetFiles("saves")) + { + var saveFileContents = JsonConvert.DeserializeObject(File.ReadAllText(saveFile)); + if (saveFileContents.Username == userName && saveFileContents.Password == passw) + { + saveFileContents.Codepoints += amount; + File.WriteAllText(saveFile, JsonConvert.SerializeObject(saveFileContents, Formatting.Indented)); + server.DispatchAll(new NetObject("stop_being_drunk_michael", new ServerMessage + { + Name = "update_your_cp", + GUID = "server", + Contents = $@"{{ + username: ""{userName}"", + amount: -{amount} +}}" + })); + server.DispatchTo(new Guid(msg.GUID), new NetObject("argh", new ServerMessage + { + Name = "update_your_cp", + GUID = "server", + Contents = $@"{{ + username: ""{args["yourusername"]}"", + amount: {amount} +}}" + })); + return; + } + } + } + } + server.DispatchTo(new Guid(msg.GUID), new NetObject("no", new ServerMessage + { + Name = "user_cp_not_found", + GUID = "Server", + })); + break; + case "trm_handshake_accept": + if(args["guid"] != null) + { + server.DispatchTo(new Guid(msg.GUID), new NetObject("hold_it", new ServerMessage + { + Name = "trm_handshake_guid", + GUID = args["guid"] as string + })); + } + break; + case "trm_handshake_request": + if(args["username"] != null && args["password"] != null && args["sysname"] != null) + { + server.DispatchAll(new NetObject("hold_my_hand", new ServerMessage + { + Name = "handshake_from", + GUID = msg.GUID, + Contents = JsonConvert.SerializeObject(args) + })); + } + break; + case "write": + if(args["guid"] != null && args["text"] != null) + { + server.DispatchTo(new Guid(args["guid"] as string), new NetObject("pleaseWrite", new ServerMessage + { + Name = "pleasewrite", + GUID = "server", + Contents = args["text"] as string + })); + } + break; + case "trm_invcmd": + if(args["guid"] != null && args["cmdstr"] != null) + { + string cmd = args["cmdstr"] as string; + string cGuid = args["guid"] as string; + server.DispatchTo(new Guid(cGuid), new NetObject("trminvoke", new ServerMessage + { + Name = "trm_invokecommand", + GUID = "server", + Contents = cmd + })); + } + break; case "usr_givecp": if (args["username"] != null && args["amount"] != null) { @@ -400,7 +493,7 @@ Contents: })); break; - case "mud_checkuserexists": + case "mud_checkuserexists": if (args["username"] != null && args["password"] != null) { foreach (var savefile in Directory.GetFiles("saves")) diff --git a/ShiftOS.WinForms/Applications/Terminal.cs b/ShiftOS.WinForms/Applications/Terminal.cs index 01ed194..2708e85 100644 --- a/ShiftOS.WinForms/Applications/Terminal.cs +++ b/ShiftOS.WinForms/Applications/Terminal.cs @@ -42,6 +42,7 @@ using System.Text.RegularExpressions; using System.Collections; using static ShiftOS.Engine.SkinEngine; using ShiftOS.Engine; +using ShiftOS.Objects; namespace ShiftOS.WinForms.Applications { @@ -55,6 +56,10 @@ namespace ShiftOS.WinForms.Applications public static string latestCommmand = ""; + + public static bool IsInRemoteSystem = false; + public static string RemoteGuid = ""; + [Obsolete("This is used for compatibility with old parts of the backend. Please use TerminalBackend instead.")] public static bool PrefixEnabled { @@ -110,6 +115,7 @@ namespace ShiftOS.WinForms.Applications [Obsolete("This is used for compatibility with old parts of the backend. Please use TerminalBackend instead.")] public static void InvokeCommand(string text) { + TerminalBackend.InvokeCommand(text); } @@ -245,19 +251,29 @@ namespace ShiftOS.WinForms.Applications var text3 = ""; var text4 = Regex.Replace(text2, @"\t|\n|\r", ""); - if (TerminalBackend.PrefixEnabled) + if (IsInRemoteSystem == true) { - text3 = text4.Remove(0, $"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ ".Length); + ServerManager.SendMessage("trm_invcmd", $@"{{ + guid = ""{RemoteGuid}"", + command: ""{text4}"" +}}"); } - TerminalBackend.LastCommand = text3; - TextSent?.Invoke(text4); - if (TerminalBackend.InStory == false) + else { - TerminalBackend.InvokeCommand(text3); - } - if (TerminalBackend.PrefixEnabled) - { - Console.Write($"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ "); + if (TerminalBackend.PrefixEnabled) + { + text3 = text4.Remove(0, $"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ ".Length); + } + TerminalBackend.LastCommand = text3; + TextSent?.Invoke(text4); + if (TerminalBackend.InStory == false) + { + TerminalBackend.InvokeCommand(text3); + } + if (TerminalBackend.PrefixEnabled) + { + Console.Write($"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ "); + } } } catch @@ -337,12 +353,22 @@ namespace ShiftOS.WinForms.Applications private void Terminal_Load(object sender, EventArgs e) { + ServerManager.MessageReceived += (msg) => + { + if(msg.Name == "trm_handshake_guid") + { + IsInRemoteSystem = true; + RemoteGuid = msg.GUID; + } + }; } private void Terminal_FormClosing(object sender, FormClosingEventArgs e) { ti.Stop(); + IsInRemoteSystem = false; + RemoteGuid = ""; } public void OnLoad() diff --git a/ShiftOS.WinForms/Commands.cs b/ShiftOS.WinForms/Commands.cs index a0a1692..ba0ed31 100644 --- a/ShiftOS.WinForms/Commands.cs +++ b/ShiftOS.WinForms/Commands.cs @@ -32,12 +32,36 @@ using System.IO; using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; +using Newtonsoft.Json; /// /// Coherence commands. /// namespace ShiftOS.WinForms { + [Namespace("trm")] + public static class TerminalExtensions + { + [Command("setpass", true)] + [RequiresArgument("pass")] + public static bool setPass(Dictionary args) + { + SaveSystem.CurrentSave.Password = args["pass"] as string; + return true; + } + + [Command("remote", "username:,sysname:,pass:", "Allows you to control a remote system on the multi-user domain given a username, password and system name.")] + [RequiresArgument("username")] + [RequiresArgument("sysname")] + [RequiresArgument("pass")] + public static bool RemoteControl(Dictionary args) + { + ServerManager.SendMessage("trm_handshake_request", JsonConvert.SerializeObject(args)); + return true; + } + } + + [Namespace("coherence")] [RequiresUpgrade("kernel_coherence")] public static class CoherenceCommands diff --git a/ShiftOS.WinForms/Resources/Shiftorium.txt b/ShiftOS.WinForms/Resources/Shiftorium.txt index 614c668..575cfaa 100644 --- a/ShiftOS.WinForms/Resources/Shiftorium.txt +++ b/ShiftOS.WinForms/Resources/Shiftorium.txt @@ -302,6 +302,12 @@ Description: "Adds an app launcher entry for the MUD chat application.", Dependencies: "mud_fundamentals;app_launcher" }, + { + Name: "Another test upgrade", + Cost: 1, + Description: "Read the ID or Name", + Dependencies: "test_upgrade" + }, { Name: "Draggable windows", Cost: 400, diff --git a/ShiftOS_TheReturn/ServerManager.cs b/ShiftOS_TheReturn/ServerManager.cs index 13c4ffa..aad63ef 100644 --- a/ShiftOS_TheReturn/ServerManager.cs +++ b/ShiftOS_TheReturn/ServerManager.cs @@ -49,7 +49,7 @@ namespace ShiftOS.Engine {JsonConvert.SerializeObject(client, Formatting.Indented)}"); } - private static Guid thisGuid { get; set; } + public static Guid thisGuid { get; private set; } private static NetObjectClient client { get; set; } public static void Disconnect() diff --git a/ShiftOS_TheReturn/TerminalBackend.cs b/ShiftOS_TheReturn/TerminalBackend.cs index 156a916..0d2f386 100644 --- a/ShiftOS_TheReturn/TerminalBackend.cs +++ b/ShiftOS_TheReturn/TerminalBackend.cs @@ -240,6 +240,61 @@ namespace ShiftOS.Engine return false; } + static TerminalBackend() + { + ServerMessageReceived onMessageReceived = (msg) => + { + if (msg.Name == "trm_invokecommand") + { + string text3 = ""; + string text4 = msg.Contents; + if (TerminalBackend.PrefixEnabled) + { + text3 = text4.Remove(0, $"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ ".Length); + } + IsForwardingConsoleWrites = true; + if (TerminalBackend.InStory == false) + { + TerminalBackend.InvokeCommand(text3); + } + if (TerminalBackend.PrefixEnabled) + { + Console.Write($"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ "); + } + IsForwardingConsoleWrites = false; + } + else if(msg.Name == "pleasewrite") + { + Console.Write(msg.Contents); + } + else if(msg.Name == "handshake_from") + { + var a = JsonConvert.DeserializeObject>(msg.Contents); + string uName = a["username"] as string; + string pass = a["password"] as string; + string sys = a["sysname"] as string; + string guid = msg.GUID; + if(SaveSystem.CurrentSave.Username == uName && SaveSystem.CurrentSave.Password == pass && CurrentSave.SystemName == sys) + { + ForwardGUID = guid; + ServerManager.SendMessage("trm_handshake_accept", $@"{{ + guid: ""{ServerManager.thisGuid}"" +}}"); + + IsForwardingConsoleWrites = true; + InvokeCommand("sos.status"); + Console.Write($"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ "); + IsForwardingConsoleWrites = false; + } + } + }; + + ServerManager.MessageReceived += onMessageReceived; + } + + public static bool IsForwardingConsoleWrites { get; private set; } + public static string ForwardGUID { get; private set; } + } } diff --git a/ShiftOS_TheReturn/TerminalTextWriter.cs b/ShiftOS_TheReturn/TerminalTextWriter.cs index 78959d5..bc242a9 100644 --- a/ShiftOS_TheReturn/TerminalTextWriter.cs +++ b/ShiftOS_TheReturn/TerminalTextWriter.cs @@ -65,20 +65,41 @@ namespace ShiftOS.Engine public override void Write(char value) { - Desktop.InvokeOnWorkerThread(new Action(() => + if (TerminalBackend.IsForwardingConsoleWrites) { - UnderlyingControl.Write(value.ToString()); - select(); - })); + ServerManager.SendMessage("write", $@"{{ + guid: ""{TerminalBackend.ForwardGUID}"", + text: ""{value}"" +}}"); + } + else + { + Desktop.InvokeOnWorkerThread(new Action(() => + { + UnderlyingControl.Write(value.ToString()); + select(); + })); + } } public override void WriteLine(string value) { - Desktop.InvokeOnWorkerThread(new Action(() => + if (TerminalBackend.IsForwardingConsoleWrites) + { + ServerManager.SendMessage("write", $@"{{ + guid: ""{TerminalBackend.ForwardGUID}"", + text: ""{value + Environment.NewLine}"" +}}"); + } + else + { + + Desktop.InvokeOnWorkerThread(new Action(() => { UnderlyingControl.WriteLine(value); select(); })); + } } public void SetLastText() @@ -87,11 +108,22 @@ namespace ShiftOS.Engine public override void Write(string value) { - Desktop.InvokeOnWorkerThread(new Action(() => + if (TerminalBackend.IsForwardingConsoleWrites) + { + ServerManager.SendMessage("write", $@"{{ + guid: ""{TerminalBackend.ForwardGUID}"", + text: ""{value}"" +}}"); + } + else + { + + Desktop.InvokeOnWorkerThread(new Action(() => { UnderlyingControl.Write(value.ToString()); select(); })); + } }