public IConnectionRequest Deserialize(Guid connectionId, Stream source, out Type type)
        {
            UserConnection user;
            var            typeName = string.Empty;

            ConnectionManager._connections.TryGetValue(connectionId, out user);
            if (user != null)
            {
                var data = ReadFully(source);
                if (data != null && data.Length > 0)
                {
                    if (user.AesShook)
                    {
                        try
                        {
                            var keybytes   = Encoding.UTF8.GetBytes(Rsa.SecureStringToString(user.AesKey));
                            var iv         = Encoding.UTF8.GetBytes(Rsa.SecureStringToString(user.AesIv));
                            var packetJson = JObject.Parse(UlteriusAes.Decrypt(data, keybytes, iv));
                            typeName = packetJson.Property("type").Value.ToString();
                            return(Build(typeName, packetJson, out type, user));
                        }
                        catch (Exception exception)
                        {
                            Console.WriteLine(exception.Message);
                            return(Build("error", null, out type));
                        }
                    }
                    var json = JObject.Parse(Encoding.UTF8.GetString(data));
                    typeName = json.Property("type").Value.ToString();
                    return(Build(typeName, json, out type, user));
                }
            }
            return(Build("error", null, out type));
        }
示例#2
0
 /// <summary>
 /// Decrypt a packet
 /// </summary>
 /// <param name="authClient"></param>
 /// <param name="data"></param>
 public PacketManager(AuthClient authClient, byte[] data)
 {
     _authClient = authClient;
     try
     {
         var keyBytes = Encoding.UTF8.GetBytes(Rsa.SecureStringToString(authClient.AesKey));
         var ivBytes  = Encoding.UTF8.GetBytes(Rsa.SecureStringToString(authClient.AesIv));
         _plainText = UlteriusAes.Decrypt(data, keyBytes, ivBytes);
     }
     catch (Exception e)
     {
         Console.WriteLine($"Packet failed to decrypt: {e.Message}");
         _packetType = PacketTypes.InvalidOrEmptyPacket;
     }
 }
示例#3
0
        public IConnectionRequest Deserialize(Guid connectionId, Stream source, out Type type)
        {
            UserConnection user;
            var            typeName = string.Empty;

            ConnectionManager._connections.TryGetValue(connectionId, out user);
            if (user != null)
            {
                using (var reader = new StreamReader(source, Encoding.UTF8))
                {
                    var data = reader.ReadToEnd();
                    if (data.IsBase64String())
                    {
                        try
                        {
                            var keybytes   = Encoding.UTF8.GetBytes(Rsa.SecureStringToString(user.AesKey));
                            var iv         = Encoding.UTF8.GetBytes(Rsa.SecureStringToString(user.AesIv));
                            var packet     = Convert.FromBase64String(data);
                            var packetJson = JObject.Parse(UlteriusAes.Decrypt(packet, keybytes, iv));
                            typeName = packetJson.Property("type").Value.ToString();
                            return(Build(typeName, packetJson, out type, user));
                        }
                        catch (Exception exception)
                        {
                            Console.WriteLine("Error deserial");
                            Console.WriteLine(exception.Message);
                            return(Build("error", null, out type));
                        }
                    }
                    if (user.AesShook)
                    {
                        return(Build("alreadyshook", null, out type));
                    }
                    var json = JObject.Parse(data);
                    typeName = json.Property("type").Value.ToString();
                    return(Build(typeName, json, out type, user));
                }
            }
            return(Build("error", null, out type));
        }
示例#4
0
        public Packets(AuthClient client, string packetData)
        {
            //An entire base64 string is an aes encrypted packet
            if ((bool)Settings.Get("TaskServer").Encryption)
            {
                if (packetData.IsBase64String())
                {
                    try
                    {
                        var keybytes = Encoding.UTF8.GetBytes(Rsa.SecureStringToString(client.AesKey));
                        var iv       = Encoding.UTF8.GetBytes(Rsa.SecureStringToString(client.AesIv));
                        var packet   = Convert.FromBase64String(packetData);
                        packetData = UlteriusAes.Decrypt(packet, keybytes, iv);
                    }
                    catch (Exception exception)
                    {
                        Console.WriteLine(exception.Message);

                        PacketType = PacketType.InvalidOrEmptyPacket;
                        return;
                    }
                }
                else
                {
                    //the only non encrypted packet allowed is the first handshake
                    try
                    {
                        var validHandshake = JObject.Parse(packetData);
                        var endpoint       = validHandshake["endpoint"].ToString().Trim().ToLower();
                        if (!endpoint.Equals("aeshandshake"))
                        {
                            PacketType = PacketType.InvalidOrEmptyPacket;
                            return;
                        }
                        //prevent sending a new aes key pair after a handshake has already taken place
                        if (client.AesShook)
                        {
                            PacketType = PacketType.InvalidOrEmptyPacket;
                            return;
                        }
                    }
                    catch (Exception)
                    {
                        PacketType = PacketType.InvalidOrEmptyPacket;
                        return;
                    }
                }
            }


            JObject deserializedPacket = null;

            try
            {
                deserializedPacket = JObject.Parse(packetData);
            }
            catch (Exception)
            {
                PacketType = PacketType.InvalidOrEmptyPacket;
                return;
            }
            if (deserializedPacket != null)
            {
                try
                {
                    Endpoint = deserializedPacket["endpoint"].ToString().Trim().ToLower();
                }
                catch (Exception)
                {
                    PacketType = PacketType.InvalidOrEmptyPacket;
                    return;
                }
                try
                {
                    SyncKey = deserializedPacket["synckey"].ToString().Trim();
                }
                catch (Exception)
                {
                    SyncKey = null;
                }
                try
                {
                    Args.AddRange(JArray.Parse(deserializedPacket["args"].ToString()));
                }
                catch (Exception)
                {
                    // no args
                }
                switch (Endpoint)
                {
                case "authenticate":
                    PacketType = PacketType.Authenticate;
                    break;

                case "requestgpuinformation":
                    PacketType = PacketType.RequestGpuInformation;
                    break;

                case "createfiletree":
                    PacketType = PacketType.CreateFileTree;
                    break;

                case "requestprocessinformation":
                    PacketType = PacketType.RequestProcess;
                    break;

                case "requestcpuinformation":
                    PacketType = PacketType.RequestCpuInformation;
                    break;

                case "requestosinformation":
                    PacketType = PacketType.RequestOsInformation;
                    break;

                case "requestnetworkinformation":
                    PacketType = PacketType.RequestNetworkInformation;
                    break;

                case "requestsysteminformation":
                    PacketType = PacketType.RequestSystemInformation;
                    break;

                case "startprocess":
                    PacketType = PacketType.StartProcess;
                    break;

                case "killprocess":
                    PacketType = PacketType.KillProcess;
                    break;

                case "generatenewkey":
                    PacketType = PacketType.GenerateNewKey;
                    break;

                case "togglewebserver":
                    PacketType = PacketType.UseWebServer;
                    break;

                case "changewebserverport":
                    PacketType = PacketType.ChangeWebServerPort;
                    break;

                case "changewebfilepath":
                    PacketType = PacketType.ChangeWebFilePath;
                    break;

                case "startvncserver":
                    PacketType = PacketType.StartScreenShare;
                    break;

                case "stopvncserver":
                    PacketType = PacketType.StopScreenShare;
                    break;

                case "changevncpass":
                    PacketType = PacketType.ChangeVncPass;
                    break;

                case "changetaskserverport":
                    PacketType = PacketType.ChangeTaskServerPort;
                    break;

                case "changevncport":
                    PacketType = PacketType.ChangeVncPort;
                    break;

                case "changevncproxyport":
                    PacketType = PacketType.ChangeVncProxyPort;
                    break;

                case "changenetworkresolve":
                    PacketType = PacketType.ChangeNetworkResolve;
                    break;

                case "changeloadplugins":
                    PacketType = PacketType.ChangeLoadPlugins;
                    break;

                case "changeuseterminal":
                    PacketType = PacketType.ChangeUseTerminal;
                    break;

                case "getcurrentsettings":
                    PacketType = PacketType.GetCurrentSettings;
                    break;

                case "geteventlogs":
                    PacketType = PacketType.GetEventLogs;
                    break;

                case "checkforupdate":
                    PacketType = PacketType.CheckUpdate;
                    break;

                case "restartserver":
                    PacketType = PacketType.RestartServer;
                    break;

                case "getwindowsdata":
                    PacketType = PacketType.RequestWindowsInformation;
                    break;

                case "getactivewindowssnapshots":
                    PacketType = PacketType.GetActiveWindowsSnapshots;
                    break;

                case "plugin":
                    PacketType = PacketType.Plugin;
                    break;

                case "getplugins":
                    PacketType = PacketType.GetPlugins;
                    break;

                case "getbadplugins":
                    PacketType = PacketType.GetBadPlugins;
                    break;

                case "startcamera":
                    PacketType = PacketType.StartCamera;
                    break;

                case "stopcamera":
                    PacketType = PacketType.StopCamera;
                    break;

                case "pausecamera":
                    PacketType = PacketType.PauseCamera;
                    break;

                case "getcameras":
                    PacketType = PacketType.GetCameras;
                    break;

                case "getcameraframe":
                    PacketType = PacketType.GetCameraFrame;
                    break;

                case "startcamerastream":
                    PacketType = PacketType.StartCameraStream;
                    break;

                case "stopcamerastream":
                    PacketType = PacketType.StopCameraStream;
                    break;

                case "refreshcameras":
                    PacketType = PacketType.RefreshCameras;
                    break;

                case "approvefile":
                    PacketType = PacketType.ApproveFile;
                    break;

                case "requestfile":
                    PacketType = PacketType.RequestFile;
                    break;

                case "aeshandshake":
                    PacketType = PacketType.AesHandshake;
                    break;

                case "approveplugin":
                    PacketType = PacketType.ApprovePlugin;
                    break;

                case "getpendingplugins":
                    PacketType = PacketType.GetPendingPlugins;
                    break;

                case "removefile":
                    PacketType = PacketType.RemoveFile;
                    break;

                case "searchfiles":
                    PacketType = PacketType.SearchFiles;
                    break;

                default:
                    PacketType = PacketType.InvalidOrEmptyPacket;
                    break;
                }
            }
        }