static CollectionAgentMessage ReadMessage(SslStream sslStream) { // Read the message sent by the server. // The end of the message is signaled using the // "<EOF>" marker. byte[] buffer = new byte[2048]; StringBuilder messageData = new StringBuilder(); int bytes = -1; do { bytes = sslStream.Read(buffer, 0, buffer.Length); // Use Decoder class to convert from bytes to UTF8 // in case a character spans two buffers. Decoder decoder = Encoding.UTF8.GetDecoder(); char[] chars = new char[decoder.GetCharCount(buffer, 0, bytes)]; decoder.GetChars(buffer, 0, bytes, chars, 0); messageData.Append(chars); // Check for EOF. if (messageData.ToString().IndexOf("<EOF>") != -1) { break; } } while (bytes != 0); Console.WriteLine(messageData.ToString()); String strJSON = messageData.ToString(); // If there is a trailing <EOF> character, strip it so that JSON // deserialization will work correctly int index = (strJSON.IndexOf("<EOF>")); if (index != -1) { strJSON = strJSON.Substring(0, index); } ICommandMessageFactory factory = new CommandMessageFactory(); GetRegistryKeyResponseMessage deserializedMsg = (GetRegistryKeyResponseMessage)factory.constructMessageFromJSON("GetRegistryKeyResponseMessage", strJSON); // Return the new object return(deserializedMsg); }
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); }