Peach 2.1 – Fixups, checksums, crc32's

Peach 2.1 includes a much improved mechanism for performing what I call data fix-ups were we calculate checksums required to make our data correct.  Many protocols and most file formats include some form of checksum field to verify that data was not corrupted.  When we generate/mutate data we want to make sure we re-calculate these checksums after our modifications, otherwise we will likely not get very far down the parser code paths due to failed validations.

Peach 2.1 BETA3 includes the following fixups out of the box:

  • checksums.Crc32Fixup — This fixup computes the standard CRC32 as defined by ISO 3309 and is used by PNG, zip, etc.
  • checksums.EthernetChecksumFixup — Computes the ethernet checksum.
  • checksums.IcmpChecksumFixup — Computes the ICMP packet checksum.

Using Fixups

Our example will be a PNG chunk data model which looks like this:

<DataModel name=”Chunk”>
    <Number name=”Length” size=”32″ endian=”network” signed=”false”>
        <Relation type=”size” of=”Core.Data”/>
    </Number>
    <String name=”Type” length=”4″/>
    <Blob name=”Data” value=”"/>
    <Number name=”CRC” size=”32″ signed=”false” endian=”network” isStatic=”true” value=”9999″ />
</DataModel>

From the PNG spec we know the CRC should be of Type and Data.  We will need to do two modifications to this data model.  First we will need to wrap Type and Data in a Block element.  Second we will add our fixup.

<DataModel name=”Chunk”>
    <Number name=”Length” size=”32″ endian=”network” signed=”false”>
        <Relation type=”size” of=”Core.Data”/>
    </Number>
    <Block name=”Core”>
        <String name=”Type” length=”4″/>
        <Blob name=”Data” value=”"/>
    </Block>
    <Number name=”CRC” size=”32″ signed=”false” endian=”network” isStatic=”true” value=”9999″>
        <Fixup class=”checksums.Crc32Fixup”>
            <Param name=”ref” value=”Core”/>
        </Fixup>
    </Number>
</DataModel>

The bolded portions are the changes we made.  The one of most interest is the <Fixup> element that is a child of the CRC number.  All fixups take a single parameter of ref that specifies the name of the element that we will operate on.  In this case that is Core the wrapper of Length and Data.

Creating Custom Fixups

Creating custom fixups are easy and require implementing a single class and two methods.  The following is the code for the Crc32Fixup that we used in the above example:

class Crc32Fixup(Fixup):
	'''
	Standard CRC32 as defined by ISO 3309.  Used by PNG, zip, etc.
	'''

	def __init__(self, ref):
		Fixup.__init__(self)
		self.ref = ref

	def fixup(self):
		stuff = self._findDataElementByName(self.ref).getValue()
		if stuff == None:
			raise Exception("Error: Crc32Fixup was unable to locate [%s]" % self.ref)

		return zlib.crc32(stuff)

To implement your own Fixup simply copy this code and change the class name (Crc33Fixup), the Exception string and finally implement your own fixup code instead of “return zlib.crc32″.  To include your custom module see the documentation about <PythonPath> and <Import> in the Peach 2 Tutorial.

~ by meddington on January 13, 2008.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.