예제 #1
0
        public void EncryptedPacketToByteBufferCheck()
        {
            bool isValid;

            DaedalusGlobal.ReturnCodes returnCode;
            int packetStart;
            int packetLength;

            //                   <STX><packetLength><encryptedPayload ->>                                                                                                                                                                                <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 = { 0x01, 0x61, 0x00, 0x97, 0xBD, 0x95, 0xCF, 0xEB, 0x0A, 0x27, 0x0D, 0xE8, 0x09, 0x4F, 0xA4, 0x7F,
                                  0x67, 0x0F, 0xC4, 0x08, 0xDA, 0x1A, 0x28, 0x6D, 0x0B, 0xDB, 0x32, 0x91, 0xA1, 0x9B, 0x37, 0xD3,
                                  0xD7, 0xCA, 0x31, 0x00, 0xCA, 0xA0, 0x59, 0xC9, 0xC5, 0xB8, 0xDD, 0x59, 0xFC, 0xEB, 0x90, 0xCD,
                                  0xA4, 0x4A, 0x71, 0x4B, 0xB9, 0xB0, 0x82, 0x64, 0x60, 0x0D, 0x5D, 0x28, 0x8F, 0x9F, 0xB1, 0x9F,
                                  0x65, 0xEE, 0x96, 0xA6, 0xAE, 0xC3, 0xB5, 0x2B, 0x7F, 0x66, 0x40, 0x78, 0x4D, 0xAA, 0x25, 0xBA,
                                  //                                                                                                               <ETX>    <CRC>    <EOT>
                                  0x78, 0xD5, 0x64, 0x05, 0xCB, 0x61, 0x51, 0x04, 0x02, 0xFC, 0x74, 0x96, 0xE8, 0x39, 0x3F, 0x53, 0xBA, 0x7F, 0x9E, 0x03, 0xd2, 0x22, 0x04 };
            isValid = EncryptedDaedalusPacket.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);
            EncryptedDaedalusPacket encryptedPacket2 = new EncryptedDaedalusPacket(testPacket, packetStart, packetLength, out returnCode, TestHelpers.goodAESKey);

            Assert.IsNotNull(encryptedPacket2);
            byte[] encryptedByteBuffer = encryptedPacket2.toByteBuffer();
            Assert.IsTrue(testPacket.SequenceEqual(encryptedByteBuffer));
        }
예제 #2
0
        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.");
            }
        }
예제 #3
0
        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));
        }