Exemplo n.º 1
0
        public static void RunClient(string machineName, string serverName)
        {
            // Create a TCP/IP client socket.
            // machineName is the host running the server application.
            TcpClient client = new TcpClient(machineName, 12345);

            Console.WriteLine("Client connected.");

            // Create an SSL stream that will close the client's stream.
            SslStream sslStream = new SslStream(client.GetStream(),
                                                false,
                                                new RemoteCertificateValidationCallback(ValidateServerCertificate),
                                                null
                                                );

            // The server name must match the name on the server certificate.
            try
            {
                sslStream.AuthenticateAsClient(serverName);
            }
            catch (AuthenticationException e)
            {
                Console.WriteLine("Exception: {0}", e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
                }
                Console.WriteLine("Authentication failed - closing the connection.");
                client.Close();
                return;
            }

            GetRegistryKeyRequestMessage msg = new GetRegistryKeyRequestMessage(1, RootKey.HKEY_CURRENT_USER, "Software\\CollectionAgentTest");

            //CollectionAgentMessage msg = new CollectionAgentMessage(1);

            // Send the message to the CollectionAgent
            SendMessage(sslStream, msg);

            // Read message from the server.
            CollectionAgentMessage caMsg = ReadMessage(sslStream);

            Console.WriteLine("Server says: {0}", caMsg.ToJSON());

            // Close the client connection.
            client.Close();
            Console.WriteLine("Client closed.");
        }
Exemplo n.º 2
0
        public CollectionAgentMessage processCommand(CollectionAgentMessage msg)
        {
            CollectionAgentMessage       responseMsg    = null;
            GetRegistryKeyRequestMessage requestMessage = null;

            if (typeof(GetRegistryKeyRequestMessage) != msg.GetType())
            {
                responseMsg = new CollectionAgentErrorMessage(msg.requestID, "Invalid request type.");
            }
            else
            {
                RegistryKey regKey = null;

                requestMessage = (GetRegistryKeyRequestMessage)msg;

                // Open the key from the appropriate root key
                switch (requestMessage.root)
                {
                case RootKey.HKEY_CLASSES_ROOT:
                    regKey = Registry.ClassesRoot.OpenSubKey(requestMessage.keyPath, false);
                    break;

                case RootKey.HKEY_CURRENT_CONFIG:
                    regKey = Registry.CurrentConfig.OpenSubKey(requestMessage.keyPath, false);
                    break;

                case RootKey.HKEY_CURRENT_USER:
                    regKey = Registry.CurrentUser.OpenSubKey(requestMessage.keyPath, false);
                    break;

                case RootKey.HKEY_LOCAL_MACHINE:
                    regKey = Registry.LocalMachine.OpenSubKey(requestMessage.keyPath, false);
                    break;

                case RootKey.HKEY_USERS:
                    regKey = Registry.Users.OpenSubKey(requestMessage.keyPath, false);
                    break;
                }

                // If we found the key, then read the values and
                if (null != regKey)
                {
                    GetRegistryKeyResponseMessage regResponse = new GetRegistryKeyResponseMessage(requestMessage.requestID);

                    // Set the path on the Registry key
                    regResponse.regKey.path = requestMessage.keyPath;

                    // Populate the Registry key with values and subkeys
                    populateRegistrykey(regResponse.regKey, regKey);

                    // Set the return value
                    responseMsg = regResponse;
                }
                else // send an error message instead
                {
                    responseMsg = new CollectionAgentErrorMessage(requestMessage.requestID, "Registry key not found");
                }
            }

            return(responseMsg);
        }