Beispiel #1
0
		public void CrackOnlyFlag()
		{
			string xml = @"
<Peach>
	<DataModel name=""TheDataModel"">
		<Flags size=""8"" endian=""big"">
			<Flag position=""0"" size=""3"" token=""true"" value=""7""/>
		</Flags>
	</DataModel>
</Peach>
";

			PitParser parser = new PitParser();
			Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

			var data = Bits.Fmt("{0}", new byte[] { 0xe6 });

			DataCracker cracker = new DataCracker();
			cracker.CrackData(dom.dataModels[0], data);

			Assert.AreEqual(1, dom.dataModels[0].Count);
			var flags = dom.dataModels[0][0] as Flags;
			Assert.AreEqual(1, flags.Count);
			var flag = flags[0] as Flag;
			Assert.AreEqual(7, (int)flag.DefaultValue);

			var bad = Bits.Fmt("{0}", new byte[] { 0x16 });

			Assert.Throws<CrackingFailure>(delegate()
			{
				cracker.CrackData(dom.dataModels[0], bad);
			});
		}
Beispiel #2
0
        public void CrackingTest2()
        {
            string xml = @"
						<Peach>
							<DataModel name='DM'>
								<Block name='blk' length='16'>
									<String name='IP'>
										<Transformer class='Ipv6StringToOctet'/>
									</String>
								</Block>
								<String name='Payload'/>
							</DataModel>
						</Peach>"                        ;

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            var data = Bits.Fmt("{0}{1}",
                                new byte[] { 0x3F, 0xFE, 0x19, 0x00, 0x45, 0x45, 0x00, 0x03, 0x02, 0x00, 0xF8, 0xFF, 0xFE, 0x21, 0x67, 0xCF },
                                "Hello");

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual("3ffe:1900:4545:3:200:f8ff:fe21:67cf", (string)dom.dataModels[0].find("blk.IP").DefaultValue);
            Assert.AreEqual("Hello", (string)dom.dataModels[0][1].DefaultValue);
        }
Beispiel #3
0
        public void CrackArrayEmptyElementMin()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<Block minOccurs=\"0\"/>"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            BitStream data = new BitStream();

            data.WriteByte(0);
            data.SeekBits(0, SeekOrigin.Begin);

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual(1, dom.dataModels[0].Count);
            Dom.Array array = (Dom.Array)dom.dataModels[0][0];
            Assert.AreEqual(0, array.Count);
        }
Beispiel #4
0
        public void CrackArrayWithTokenSibling()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<Block minOccurs=\"0\">"+
                         "			<Number name=\"num1\" size=\"8\" constraint=\"str(element.DefaultValue) != '0'\" />"+
                         "		</Block>"+
                         "		<Number name=\"zero\" size=\"8\" valueType=\"hex\" value=\"0\" token=\"true\" />"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            BitStream data = new BitStream();

            data.LittleEndian();
            data.WriteBytes(new byte[] { 0x01, 0x02, 0x03, 0x00 });
            data.SeekBits(0, SeekOrigin.Begin);

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual(2, dom.dataModels[0].Count);
            Dom.Array blockArray = dom.dataModels[0][0] as Dom.Array;
            Assert.NotNull(blockArray);
            Assert.AreEqual(3, blockArray.Count);
        }
Beispiel #5
0
        public void CrackArrayRelation()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<Number size=\"8\">"+
                         "			<Relation type=\"count\" of=\"TheArray\"/>"+
                         "		</Number>"+
                         "		<Blob name=\"TheArray\" length=\"1\" minOccurs=\"0\" maxOccurs=\"100\" />"+
                         "		<Blob name=\"Rest\"/>"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            BitStream data = new BitStream();

            data.LittleEndian();
            data.WriteBytes(new byte[] { 3, 1, 2, 3, 4, 5, 6 });
            data.SeekBits(0, SeekOrigin.Begin);

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Dom.Array array = (Dom.Array)dom.dataModels[0][1];
            Blob      blob  = (Blob)dom.dataModels[0][2];

            Assert.AreEqual(3, array.Count);
            Assert.AreEqual(new byte[] { 1 }, (byte[])array[0].InternalValue);
            Assert.AreEqual(new byte[] { 2 }, (byte[])array[1].InternalValue);
            Assert.AreEqual(new byte[] { 3 }, (byte[])array[2].InternalValue);
            Assert.AreEqual(new byte[] { 4, 5, 6 }, (byte[])blob.InternalValue);
        }
Beispiel #6
0
        public void CrackArrayVerifyMin()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<Blob length=\"1\" minOccurs=\"4\" maxOccurs=\"6\" />"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            BitStream data = new BitStream();

            data.LittleEndian();
            data.WriteBytes(new byte[] { 1, 2, 3 });
            data.SeekBits(0, SeekOrigin.Begin);

            try
            {
                DataCracker cracker = new DataCracker();
                cracker.CrackData(dom.dataModels[0], data);
                Assert.True(false);
            }
            catch (CrackingFailure)
            {
                Assert.True(true);
            }
        }
Beispiel #7
0
        public void CrackCountOf4()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<Number size=\"8\">"+
                         "			<Relation type=\"count\" of=\"numbers\" expressionGet=\"count/2\"/>"+
                         "		</Number>"+
                         "		<Number size=\"8\" name=\"numbers\" minOccurs=\"0\" maxOccurs=\"-1\"/>"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            var data = Bits.Fmt("{0}", new byte[] { 0x0A, 0x01, 0x02, 0x03, 0x04, 0x05 });

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual(10, (int)dom.dataModels[0][0].DefaultValue);
            Assert.AreEqual(5, (int)dom.dataModels[0][0].InternalValue);
            Assert.IsInstanceOf <Dom.Array>(dom.dataModels[0][1]);
            Assert.AreEqual(5, ((Dom.Array)dom.dataModels[0][1]).Count);
            Assert.AreEqual(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 }, dom.dataModels[0][1].InternalValue.BitsToArray());
        }
Beispiel #8
0
        public void CrackCountOf5()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<String length=\"8\">"+
                         "			<Relation type=\"count\" of=\"Data\" />"+
                         "			<Hint name='NumericalString' value='true' />"+
                         "		</String>"+
                         "		<String name=\"Data\" length=\"1\" minOccurs=\"0\"/>"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            var data = Bits.Fmt("{0}", "000000088ByteLen");

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual(8, (int)dom.dataModels[0][0].DefaultValue);
            var array = dom.dataModels[0][1] as Dom.Array;

            Assert.NotNull(array);
            Assert.AreEqual(8, array.Count);
        }
Beispiel #9
0
        public void TooSmallOffset()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<Number size=\"8\">"+
                         "			<Relation type=\"offset\" of=\"Data\" />"+
                         "		</Number>"+
                         "		<String name=\"Data\" />"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            BitStream data = new BitStream();

            data.WriteInt8((sbyte)0);
            data.WriteBytes(ASCIIEncoding.ASCII.GetBytes("Hello World"));
            data.SeekBits(0, SeekOrigin.Begin);

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);
        }
Beispiel #10
0
        public void RelativeOffsetSpace()
        {
            string xml = @"<?xml version='1.0' encoding='utf-8'?>
<Peach>
	<DataModel name='TheDataModel'>
		<Number size='8'>
			<Relation type='offset' of='Data' relative='true' />
		</Number>
		<String length='5'/>
		<String name='Data'/>
	</DataModel>
</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            byte[] offsetdata = ASCIIEncoding.ASCII.GetBytes("AAAAAAAAAA");

            BitStream data = new BitStream();

            data.WriteByte((byte)(offsetdata.Length));
            data.WriteBytes(offsetdata);
            data.WriteBytes(ASCIIEncoding.ASCII.GetBytes("Hello World"));
            data.SeekBits(0, SeekOrigin.Begin);

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual("Hello World", (string)dom.dataModels[0]["Data"].DefaultValue);
        }
Beispiel #11
0
        public void RelativeOffset()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<Blob length=\"5\"/>"+
                         "		<Number size=\"8\">"+
                         "			<Relation type=\"offset\" of=\"Data\" relative=\"true\" />"+
                         "		</Number>"+
                         "		<Blob name=\"Data\" />"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            byte[] otherdata  = ASCIIEncoding.ASCII.GetBytes("12345");
            byte[] offsetdata = ASCIIEncoding.ASCII.GetBytes("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");

            BitStream data = new BitStream();

            data.WriteBytes(otherdata);
            data.WriteInt8((sbyte)offsetdata.Length);
            data.WriteBytes(offsetdata);
            data.WriteBytes(ASCIIEncoding.ASCII.GetBytes("Hello World"));
            data.SeekBits(0, SeekOrigin.Begin);

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual(offsetdata.Length, (int)dom.dataModels[0][1].DefaultValue);
            Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("Hello World"), (byte[])dom.dataModels[0][2].DefaultValue);
        }
Beispiel #12
0
        public void BadOffset()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<Number size=\"8\">"+
                         "			<Relation type=\"offset\" of=\"Data\" />"+
                         "		</Number>"+
                         "		<String name=\"Middle\"/>"+
                         "		<String name=\"Sized\" length=\"5\"/>"+
                         "		<String name=\"Data\" />"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            string offsetdata = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
            string sizeddata  = "12345";
            string target     = "Hello World";

            BitStream data = new BitStream();

            data.WriteInt8((sbyte)3);
            data.WriteBytes(Encoding.ASCII.GetBytes(offsetdata));
            data.WriteBytes(Encoding.ASCII.GetBytes(sizeddata));
            data.WriteBytes(Encoding.ASCII.GetBytes(target));
            data.SeekBits(0, SeekOrigin.Begin);

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);
        }
Beispiel #13
0
        public void Basic3Offset()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<Number size=\"8\">"+
                         "			<Relation type=\"offset\" of=\"Data\" />"+
                         "		</Number>"+
                         "		<String name=\"Middle\"/>"+
                         "		<String name=\"Data\" />"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            string offsetdata = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
            string target     = "Hello World";

            BitStream data = new BitStream();

            data.WriteInt8((sbyte)(offsetdata.Length + 1));
            data.WriteBytes(Encoding.ASCII.GetBytes(offsetdata));
            data.WriteBytes(Encoding.ASCII.GetBytes(target));
            data.SeekBits(0, SeekOrigin.Begin);

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual(offsetdata.Length + 1, (int)dom.dataModels[0][0].DefaultValue);
            Assert.AreEqual(offsetdata, (string)dom.dataModels[0][1].DefaultValue);
            Assert.AreEqual(target, (string)dom.dataModels[0][2].DefaultValue);
        }
Beispiel #14
0
        public void CrackString7()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<String />"+
                         "		<Block>"+
                         "			<Number size=\"16\" />"+
                         "		</Block>"+
                         "		<Block>"+
                         "			<Number size=\"16\" />"+
                         "		</Block>"+
                         "		<Block>"+
                         "			<Number size=\"16\" />"+
                         "		</Block>"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            var data = Bits.Fmt("{0}{1:L16}{2:L16}{3:L16}", "Hello World", 3111, 3112, 3113);

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual("Hello World", (string)dom.dataModels[0][0].DefaultValue);
            Assert.AreEqual(3111, (int)((DataElementContainer)dom.dataModels[0][1])[0].DefaultValue);
            Assert.AreEqual(3112, (int)((DataElementContainer)dom.dataModels[0][2])[0].DefaultValue);
            Assert.AreEqual(3113, (int)((DataElementContainer)dom.dataModels[0][3])[0].DefaultValue);
        }
Beispiel #15
0
        public void CrackFlagsSecond()
        {
            string xml = @"
<Peach>
	<DataModel name=""TheDataModel"">
		<Number size=""8""/>
		<Flags size=""8"" endian=""big"">
			<Flag position=""0"" size=""4""/>
			<Flag position=""4"" size=""4""/>
		</Flags>
	</DataModel>
</Peach>
";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            var data = Bits.Fmt("{0}", new byte[] { 0x00, 0xff });

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual(2, dom.dataModels[0].Count);
            var flags = dom.dataModels[0][1] as Flags;

            Assert.AreEqual(2, flags.Count);
            var flag1 = flags[0] as Flag;

            Assert.AreEqual(0xf, (int)flag1.DefaultValue);
            var flag2 = flags[1] as Flag;

            Assert.AreEqual(0xf, (int)flag2.DefaultValue);
        }
Beispiel #16
0
        public void BasicOffset()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<Number size=\"8\">"+
                         "			<Relation type=\"offset\" of=\"Data\" />"+
                         "		</Number>"+
                         "		<Blob name=\"Data\" />"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            string offsetdata = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
            var    data       = Bits.Fmt("{0:L8}{1}{2}", offsetdata.Length + 1, offsetdata, "Hello World");

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual(offsetdata.Length + 1, (int)dom.dataModels[0][0].DefaultValue);
            Assert.AreEqual("Hello World", dom.dataModels[0][1].DefaultValue.BitsToString());
        }
Beispiel #17
0
        public void CrackBlob3()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<Blob />"+
                         "		<Blob length=\"5\" />"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            BitStream data = new BitStream();

            data.LittleEndian();
            data.WriteBytes(new byte[] { 1, 2, 3, 4, 5, 6, 0xff, 0xfe, 0xff });
            data.SeekBits(0, SeekOrigin.Begin);

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual(new byte[] { 1, 2, 3, 4 }, (byte[])dom.dataModels[0][0].DefaultValue);
            Assert.AreEqual(new byte[] { 5, 6, 0xff, 0xfe, 0xff }, (byte[])dom.dataModels[0][1].DefaultValue);
        }
Beispiel #18
0
        public void BadOffset()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<Number size=\"8\">"+
                         "			<Relation type=\"offset\" of=\"Data\" />"+
                         "		</Number>"+
                         "		<String name=\"Middle\"/>"+
                         "		<String name=\"Sized\" length=\"5\"/>"+
                         "		<String name=\"Data\" />"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            string offsetdata = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
            string sizeddata  = "12345";
            string target     = "Hello World";
            var    data       = Bits.Fmt("{0:L8}{1}{2}{3}", 3, offsetdata, sizeddata, target);

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);
        }
Beispiel #19
0
        public void CrackLeftovers()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<Number name=\"count\" size=\"8\">"+
                         "			<Relation type=\"count\" of=\"num\" />"+
                         "		</Number>"+
                         "		<Number name=\"num\" size=\"8\" minOccurs=\"2\"/>"+
                         "		<String name=\"str\" length=\"9\"/>"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            var data = Bits.Fmt("{0}", "\x06QWERTYleftoversextrajunk");

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual(3, dom.dataModels[0].Count);
            Dom.Array array = (Dom.Array)dom.dataModels[0][1];
            Assert.AreEqual(6, array.Count);
            Assert.AreEqual("leftovers", (string)dom.dataModels[0][2].DefaultValue);
        }
Beispiel #20
0
        public void BadOffsetInSizedBlock()
        {
            string xml = @"<?xml version='1.0' encoding='utf-8'?>
<Peach>
	<DataModel name='TheDataModel'>

		<Number name='offset' size='8'>
			<Relation type='offset' of='Data'/>
		</Number>

		<Block name='block'>
			<Number name='size' size='8'>
				<Relation type='size' of='block'/>
			</Number>

			<String name='Data'/>
		</Block>
	</DataModel>
</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            string offsetdata = "AAAAAAAAAA";
            string payload    = "Hello World";
            var    data       = Bits.Fmt("{0:L8}{1:L8}{2}{3}", 30, 1 + offsetdata.Length + payload.Length, offsetdata, payload);

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual("Hello World", (string)dom.dataModels[0].find("block.Data").DefaultValue);
        }
Beispiel #21
0
        public void CrackArrayBlob1()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<Blob length=\"1\" minOccurs=\"1\" maxOccurs=\"100\" />"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            BitStream data = new BitStream();

            data.LittleEndian();
            data.WriteBytes(new byte[] { 1, 2, 3, 4, 5, 6 });
            data.SeekBits(0, SeekOrigin.Begin);

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Dom.Array array = (Dom.Array)dom.dataModels[0][0];

            Assert.AreEqual(6, array.Count);
            Assert.AreEqual(new byte[] { 1 }, (byte[])array[0].InternalValue);
            Assert.AreEqual(new byte[] { 6 }, (byte[])array[5].InternalValue);
        }
Beispiel #22
0
        public void RelativeToOffset()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<String length=\"5\"/>"+
                         "		<Number name=\"Size\" size=\"8\">"+
                         "			<Relation type=\"offset\" of=\"Data\" relative=\"true\" relativeTo=\"RelData\" />"+
                         "		</Number>"+
                         "		<String length=\"5\"/>"+
                         "		<String name=\"RelData\" length=\"5\"/>"+
                         "		<String name=\"Data\" />"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            string otherdata  = "12345";
            string offsetdata = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
            var    data       = Bits.Fmt("{0}{1:L8}{2}{3}{4}{5}", otherdata, offsetdata.Length + otherdata.Length, otherdata, otherdata, offsetdata, "Hello World");

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual(offsetdata.Length + otherdata.Length, (int)dom.dataModels[0]["Size"].DefaultValue);
            Assert.AreEqual("Hello World", (string)dom.dataModels[0]["Data"].DefaultValue);
        }
Beispiel #23
0
        public void CrackZeroArray()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<String value=\"Item\" minOccurs=\"0\" token=\"true\" />"+
                         "		<Blob name=\"Rest\"/>"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            BitStream data = new BitStream();

            data.LittleEndian();
            data.WriteBytes(new byte[] { 1, 2, 3, 4, 5, 6 });
            data.SeekBits(0, SeekOrigin.Begin);

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Dom.Array array = (Dom.Array)dom.dataModels[0][0];

            Assert.AreEqual(0, array.Count);

            Blob rest = (Blob)dom.dataModels[0].find("Rest");

            Assert.AreEqual(new byte[] { 1, 2, 3, 4, 5, 6 }, (byte[])rest.InternalValue);
        }
Beispiel #24
0
        public void RelativeOffsetSpace()
        {
            string xml = @"<?xml version='1.0' encoding='utf-8'?>
<Peach>
	<DataModel name='TheDataModel'>
		<Number size='8'>
			<Relation type='offset' of='Data' relative='true' />
		</Number>
		<String length='5'/>
		<String name='Data'/>
	</DataModel>
</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            string offsetdata = "AAAAAAAAAA";
            var    data       = Bits.Fmt("{0:L8}{1}{2}", offsetdata.Length, offsetdata, "Hello World");

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual("Hello World", (string)dom.dataModels[0]["Data"].DefaultValue);
        }
Beispiel #25
0
        public void CrackArrayOfZeroOrOne()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<String name=\"str1\" nullTerminated=\"true\" minOccurs=\"0\" maxOccurs=\"1\"/>"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            BitStream data = new BitStream();

            data.LittleEndian();
            data.WriteBytes(Encoding.ASCII.GetBytes("Hello\x00"));
            data.SeekBits(0, SeekOrigin.Begin);

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual(1, dom.dataModels[0].Count);
            Dom.Array array = (Dom.Array)dom.dataModels[0][0];
            Assert.AreEqual(1, array.Count);
            string str = (string)array[0].InternalValue;

            Assert.AreEqual("Hello", str);
        }
Beispiel #26
0
        public void NonChildRelativeToOffset()
        {
            string xml = @"<?xml version='1.0' encoding='utf-8'?>
<Peach>
	<DataModel name='TheDataModel'>
		<Block name='blk'>
			<Number size='8'>
				<Relation type='size' of='blk' />
			</Number>
			<Block name='inner'>
				<String/>
				<String name='off' length='1'/>
				<Number size='8'>
					<Relation type='offset' of='Data' relativeTo='off'/>
				</Number>
			</Block>
		</Block>
		<String name='Data'/>
	</DataModel>
</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            string otherdata  = "abcde";
            string offsetdata = "AAAAAAAAA";
            var    data       = Bits.Fmt("{0:L8}{1}{2:L8}{3}{4}", 1 + otherdata.Length + 1, otherdata, 1 + 1 + offsetdata.Length, offsetdata, "Hello World");

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual("Hello World", (string)dom.dataModels[0]["Data"].DefaultValue);
        }
Beispiel #27
0
        public void CrackArrayParentName()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<String name=\"str1\" nullTerminated=\"true\" minOccurs=\"1\"/>"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            BitStream data = new BitStream();

            data.LittleEndian();
            data.WriteBytes(Encoding.ASCII.GetBytes("Hello\x00World\x00"));
            data.SeekBits(0, SeekOrigin.Begin);

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual(1, dom.dataModels[0].Count);
            Dom.Array array = (Dom.Array)dom.dataModels[0][0];
            Assert.AreEqual(2, array.Count);
            Assert.AreEqual("TheDataModel.str1", array.fullName);
            Assert.AreEqual("TheDataModel.str1.str1", array.origionalElement.fullName);
            Assert.AreEqual("TheDataModel.str1.str1", array[0].fullName);
            Assert.AreEqual("TheDataModel.str1.str1_1", array[1].fullName);
        }
Beispiel #28
0
        public void CrackStringOffset()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<String length=\"8\">"+
                         "			<Relation type=\"offset\" of=\"Data\" />"+
                         "			<Hint name='NumericalString' value='true' />"+
                         "		</String>"+
                         "		<String name=\"Data\"/>"+
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            var data = Bits.Fmt("{0}", "00000010  Payload");

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual(10, (int)dom.dataModels[0][0].DefaultValue);
            Assert.AreEqual("Payload", (string)dom.dataModels[0][1].DefaultValue);
        }
Beispiel #29
0
        public void BasicTest()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "       <Blob name=\"TheBlob\">" +
                         "           <Analyzer class=\"Binary\"> " +
                         "               <Param name=\"AnalyzeStrings\" value=\"false\"/> " +
                         "           </Analyzer> " +
                         "       </Blob>" +
                         "	</DataModel>"+
                         "</Peach>";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            Random rnd = new Random(123);

            BitStream data = new BitStream();

            data.LittleEndian();

            for (int cnt = 0; cnt < 100; cnt++)
            {
                data.WriteInt32(rnd.NextInt32());
            }

            data.WriteBytes(ASCIIEncoding.ASCII.GetBytes("Hello World"));

            for (int cnt = 0; cnt < 100; cnt++)
            {
                data.WriteInt32(rnd.NextInt32());
            }

            data.WriteBytes(ASCIIEncoding.ASCII.GetBytes("Peach Fuzzer"));

            for (int cnt = 0; cnt < 100; cnt++)
            {
                data.WriteInt32(rnd.NextInt32());
            }

            data.SeekBits(0, SeekOrigin.Begin);

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);
            data.SeekBytes(0, SeekOrigin.Begin);

            Assert.IsTrue(dom.dataModels["TheDataModel"][0] is Block);
            Assert.AreEqual("TheBlob", dom.dataModels["TheDataModel"][0].name);
            Assert.AreEqual(data.Value, dom.dataModels["TheDataModel"].Value.Value);

            var block = dom.dataModels["TheDataModel"][0] as Block;

            Assert.IsTrue(block[5] is Dom.String);
            Assert.AreEqual("Hello WorldYY&", (string)block[5].InternalValue);

            Assert.IsTrue(block[11] is Dom.String);
            Assert.AreEqual("Peach Fuzzer|", (string)block[11].InternalValue);
        }
Beispiel #30
0
        public void RelativeToDataModel()
        {
            string xml = @"
<Peach>
	<DataModel name='TheDataModel'>
		<String length='4'/>
		<Choice>
		<Block>
			<String length='4'>
				<Relation type='offset' of='Data' relative='true' relativeTo='TheDataModel'/>
			</String>
			<String name='Data' length='4' />
		</Block>
		</Choice>
		<String/>
	</DataModel>

	<DataModel name='DM2' ref='TheDataModel'/>

</Peach>";

            var data = Bits.Fmt("{0}", "abcd0010  dataend");

            var parser = new PitParser();
            var dom    = parser.asParser(null, new MemoryStream(Encoding.ASCII.GetBytes(xml)));

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[1], data);

            Assert.AreEqual("abcd", (string)dom.dataModels[1][0].DefaultValue);
            Assert.AreEqual("end", (string)dom.dataModels[1][2].DefaultValue);
        }