public void DecryptedPacketToByteBufferCheck() { bool isValid; DaedalusGlobal.ReturnCodes returnCode; int packetStart; int packetLength; DecryptedDaedalusPacket packet; // <STX><packetLength><packetIndex><cmd><cmdVer> <cmdLen> <cmdPay><hash> <act> <ETX> <CRC> <EOT> // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 byte[] testPacket = { 0x02, 0x1C, 0x00, 0x01, 0x00, 0x20, 0x01, 0x15, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x3A, 0x1f, 0x04 }; isValid = DecryptedDaedalusPacket.IsValidPacket(testPacket, out returnCode, out packetStart, out packetLength); Assert.AreEqual(isValid, true); Assert.AreEqual(returnCode, DaedalusGlobal.ReturnCodes.Valid); packet = new DecryptedDaedalusPacket(testPacket, 0, testPacket.Length, out returnCode); Assert.AreEqual(returnCode, DaedalusGlobal.ReturnCodes.Valid); Assert.IsNotNull(packet); Assert.IsTrue(packet.getTotalPacketLength() == testPacket.Length); Assert.IsTrue(packet.payload.Count == 2); Assert.IsTrue(packet.payload.ContainsKey("hash")); Assert.IsTrue(packet.payload.ContainsKey("actionTaken")); byte[] bufferBackOut = packet.toByteBuffer(); Assert.IsTrue(bufferBackOut.SequenceEqual(testPacket)); }
private void packetProcessingSequence(NetPacket packet) { DaedalusGlobal.ReturnCodes rc; int packetStart, packetLength; // We already did this in the comms stream parser, but what the hey if (EncryptedDaedalusPacket.IsValidPacket(packet.payload, out rc, out packetStart, out packetLength)) { // Build the encrypted packet EncryptedDaedalusPacket encPacket = new EncryptedDaedalusPacket(packet.payload, packetStart, packetLength, out rc, AESKey); // If the decrypted payload is a valid packet... if (DecryptedDaedalusPacket.IsValidPacket(encPacket.decryptedPayload, out rc, out packetStart, out packetLength)) { // Build the decrypted packet DecryptedDaedalusPacket decPacket = new DecryptedDaedalusPacket(encPacket.decryptedPayload, packetStart, packetLength, out rc); // Resolve the command type of this packet IDaedalusCommandType commandType = decPacket.commandType; //IDaedalusCommandType commandType = DecryptedDaedalusPacket.commandTypes.Where(p => p.getCommandType() == decPacket.command).First(); // I don't really like passing the whole form as a parameter, but it was either that, a huge list of parameters, or a big switch statement here commandType.processAction(decPacket, this, packet.source); } } }
public DaedalusGlobal.ReturnCodes processAction(DecryptedDaedalusPacket packet, frmMain mainForm, IPEndPoint source) { // So this isn't exactly ideal, but I didn't want to write custom mutators for each control. // So sue me. mainForm.BeginInvoke((Action)(() => mainForm.lstTraffic.Items.Add( packet.packetIndex + ":" + source.ToString() + ": " + BitConverter.ToString(packet.toByteBuffer()) ))); return(DaedalusGlobal.ReturnCodes.Valid); }
public void CRCSuccess() { bool isValid; DaedalusGlobal.ReturnCodes returnCode; int packetStart; int packetLength; byte[] testPacket = { 0x05, 0x04, 0x02, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x03, 0x80, 0x48, 0x04 }; isValid = DecryptedDaedalusPacket.IsValidPacket(testPacket, out returnCode, out packetStart, out packetLength); Assert.AreEqual(isValid, true); Assert.AreEqual(returnCode, DaedalusGlobal.ReturnCodes.Valid); }
public void CRCFailure() { bool isValid; DaedalusGlobal.ReturnCodes returnCode; int packetStart; int packetLength; byte[] testPacket = { 0x05, 0x04, 0x02, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x03, 0xFF, 0xFF, 0x04 }; isValid = DecryptedDaedalusPacket.IsValidPacket(testPacket, out returnCode, out packetStart, out packetLength); Assert.AreEqual(isValid, false); Assert.AreEqual(returnCode, DaedalusGlobal.ReturnCodes.IncorrectCRC); }
public void SOHFailure() { bool isValid; DaedalusGlobal.ReturnCodes returnCode; int packetStart; int packetLength; byte[] testPacket = { 0x20, 0x21, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 }; isValid = DecryptedDaedalusPacket.IsValidPacket(testPacket, out returnCode, out packetStart, out packetLength); Assert.AreEqual(isValid, false); Assert.AreEqual(returnCode, DaedalusGlobal.ReturnCodes.InvalidPacketStructure); }
private void cmdSendCommand_Click(object sender, EventArgs e) { // Decrypted packet that we're going to send out DecryptedDaedalusPacket outPacket = new DecryptedDaedalusPacket(); // Should probably invert this and only allow the fields to be set via a constructor (readonly) outPacket.commandType = getDaedalusCommandTypeFromComboBox(cboProtocolCommand); unchecked { outPacket.packetIndex = packetIndex++; } outPacket.command = outPacket.commandType.getCommand(); outPacket.commandVersion = Byte.Parse((string)Invoke((Func <object>)(() => txtCommandVersion.Text))); string payloadString = (string)Invoke((Func <object>)(() => txtPacketPayload.Text)); byte[] payloadBuffer = GlobalHelpers.GlobalMethods.XXHexStringToByteArray(payloadString.Replace(" ", "")); Dictionary <string, DaedalusGlobal.PayloadElement> payload; DaedalusGlobal.ReturnCodes rc = outPacket.commandType.parsePayload(payloadBuffer, 0, out payload); outPacket.payload = payload; if (rc == DaedalusGlobal.ReturnCodes.Valid) { outPacket.setPacketLengthFieldValue(); // At this point the decrypted packet should be ready to go // Build encrypted packet here EncryptedDaedalusPacket encPacket = new EncryptedDaedalusPacket(outPacket, AESKey); string IPString = (string)Invoke((Func <object>)(() => txtDestinationIP.Text)); IPEndPoint destinationEndPoint = new IPEndPoint(IPAddress.Parse(IPString), DaedalusGlobal.DaedalusPort); //lock (commLocker) //{ lock (selectedNetworkInterfaceLocker) { comm.outPackets.Add(new NetPacket { destination = destinationEndPoint, source = new IPEndPoint(getDefaultIPv4AddressFromInterface(selectedNetworkInterface), DaedalusGlobal.DaedalusPort), transportType = TransportType.Tcp, payload = encPacket.toByteBuffer() }); } //} } else { throw new ArgumentException("Packet payload incorrectly formatted for this command type."); } }
internal DecryptedDaedalusPacket toDecryptedDaedalusPacket(out DaedalusGlobal.ReturnCodes returnCode) { DaedalusGlobal.ReturnCodes rc; int start; int len; // Build a clear text packet from the decrypted data if (DecryptedDaedalusPacket.IsValidPacket(decryptedPayload, out rc, out start, out len)) { returnCode = DaedalusGlobal.ReturnCodes.Valid; return(new DecryptedDaedalusPacket(decryptedPayload, start, len, out rc)); } else { returnCode = rc; return(null); } }
internal EncryptedDaedalusPacket(DecryptedDaedalusPacket inPacket, string AESKey) { encryptionKey = AESKey; // Not at all like that //// The decrypted packet's entire size is the payload of the encrypted packet, so the <encryptedPacketLength> is that + size of ETX //encryptedPacketLength = inPacket.getTotalPacketLength() + elementETX.ElementSize; // Copy the decrypted packet into the local byte buffer decryptedPayload = new byte[inPacket.getTotalPacketLength()]; Array.Copy(inPacket.toByteBuffer(), decryptedPayload, inPacket.getTotalPacketLength()); // Force accessor to perform encryption operation and synch payloads //byte[] temp = encryptedPayload; // Packet length field is payload length plus length of ETX encryptedPacketLengthFieldValue = encryptedPayload.Length + elementETX.ElementSize; }
public void PayloadParseCommandNotImplemented() { bool isValid; DaedalusGlobal.ReturnCodes returnCode; int packetStart; int packetLength; DecryptedDaedalusPacket packet; // <STX><packetLength><packetIndex><cmd><cmdVer> <cmdLen> <cmdPay><hash> <act> <ETX> <CRC> <EOT> // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 byte[] testPacket = { 0x02, 0x1C, 0x00, 0x01, 0x00, 0x19, 0x01, 0x15, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xb7, 0xdf, 0x04 }; isValid = DecryptedDaedalusPacket.IsValidPacket(testPacket, out returnCode, out packetStart, out packetLength); Assert.AreEqual(isValid, true); Assert.AreEqual(returnCode, DaedalusGlobal.ReturnCodes.Valid); packet = new DecryptedDaedalusPacket(testPacket, 0, testPacket.Length, out returnCode); Assert.AreEqual(returnCode, DaedalusGlobal.ReturnCodes.CommandNotImplemented); }
public void BuildEncryptedPacketFromDecryptedPacketSuccess() { bool isValid; DaedalusGlobal.ReturnCodes returnCode; int packetStart; int packetLength; DecryptedDaedalusPacket packet; // <STX><packetLength><packetIndex><cmd><cmdVer> <cmdLen> <cmdPay><hash> <act> <ETX> <CRC> <EOT> // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 byte[] testPacket = { 0x02, 0x1C, 0x00, 0x01, 0x00, 0x20, 0x01, 0x15, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x3A, 0x1f, 0x04 }; isValid = DecryptedDaedalusPacket.IsValidPacket(testPacket, out returnCode, out packetStart, out packetLength); Assert.AreEqual(isValid, true); Assert.AreEqual(returnCode, DaedalusGlobal.ReturnCodes.Valid); Assert.IsTrue(packetStart == 0); Assert.IsTrue(packetLength == testPacket.Length); packet = new DecryptedDaedalusPacket(testPacket, 0, testPacket.Length, out returnCode); Assert.AreEqual(returnCode, DaedalusGlobal.ReturnCodes.Valid); Assert.IsNotNull(packet); Assert.IsTrue(packet.payload.Count == 2); Assert.IsTrue(packet.payload.ContainsKey("hash")); Assert.IsTrue(packet.payload.ContainsKey("actionTaken")); // At this point decrypted packet is built and verified // Build encrypted packet from the decrypted packet EncryptedDaedalusPacket encryptedPacket1 = new EncryptedDaedalusPacket(packet, TestHelpers.goodAESKey); byte[] encryptedBuffer1 = encryptedPacket1.toByteBuffer(); Assert.IsTrue(encryptedBuffer1.Length > testPacket.Length); Assert.IsNotNull(encryptedPacket1); byte[] encryptedPayload = encryptedPacket1.encryptedPayload; Assert.IsNotNull(encryptedPayload); byte[] decryptedPayload1 = encryptedPacket1.decryptedPayload; Assert.IsNotNull(decryptedPayload1); Assert.IsTrue(testPacket.SequenceEqual(decryptedPayload1)); // Build encrypted packet from decrypted packet's payload List <byte> encryptedPacketRaw = new List <byte>(); encryptedPacketRaw.AddRange(new byte[] { GlobalConstants.SOH, 0x61, 0x00 }); encryptedPacketRaw.AddRange(encryptedPayload); encryptedPacketRaw.AddRange(new byte[] { GlobalConstants.ETX, 0x03, 0x46, GlobalConstants.EOT }); byte[] encryptedPacketBuffer = encryptedPacketRaw.ToArray(); // Actually we can't validate this packet because the CRC will change with each new encryption //isValid = EncryptedDaedalusPacket.IsValidPacket(encryptedPacketBuffer, out returnCode, out packetStart, out packetLength); //Assert.AreEqual(isValid, true); //Assert.AreEqual(returnCode, DaedalusGlobal.ReturnCodes.Valid); //Assert.IsTrue(packetStart == 0); //Assert.IsTrue(packetLength == encryptedPacketBuffer.Length); EncryptedDaedalusPacket encryptedPacket2 = new EncryptedDaedalusPacket(encryptedPacketBuffer, 0, encryptedPacketBuffer.Length, out returnCode, TestHelpers.goodAESKey); Assert.IsNotNull(encryptedPacket2); byte[] decryptedPayload2 = encryptedPacket2.decryptedPayload; Assert.IsNotNull(decryptedPayload2); Assert.IsTrue(testPacket.SequenceEqual(decryptedPayload2)); }