Peach is arguably the most established, freely available fuzzer out there. It has tons of built in functionality to support a huge range of features. While you can data model even the most complex protocols, you can only go so far with a PeachPit before you realize that you just need a custom publisher. In this blog post we'll show how to write and compile a custom publisher so you can spend all your CPU cycles fuzzing the stuff that matters.
When Is It Time?Since you can do so much with a DataModel and a StateModel, identifying when it's time to transition from a PeachPit to a custom publisher can be tough. To me, it all depends on what you're looking to fuzz. The most common case is your target protocol or file format has multiple levels of encapsulation. Sure, you could easily DataModel this encapsulation, but then you're stuck manually excluding higher level encapsulated data. And in some cases, the encapsulation creates a situation that the DataModel just can't handle.
Here's a sort interesting example I've recently come across. The application implemented it own custom protocol within a TLS tunnel. The tricky part here is that it was all over UDP. So there had to be another layer of encapsulation (XYZ Proto) above TLS but below UDP to keep state of the TLS tunnel, since UDP is stateless. Here's what the encapsulation looked like from a high level:
Now if we're just looking to fuzz XYZ Proto then a DataModel here using the UDP Publisher would do just fine. However, since we're looking to fuzz Custom Protocol, we have a bit of work to do. Establishing a TLS tunnel is beyond the purpose of the DataModel - and the only way for us to get at the important part, is to buld a custom publisher.
If you're just dealing with file formats, this same idea still applies, but its more likely you can build out the DataModel for the entire file format, rather then hitting the TLS brick wall. That being said, it might not be necessary to build the DataModel for the higher level file formats if a custom publisher can be written.
Compiling PeachTechnically, you don't have to compile Peach from source. A little later on I'll walk you through compiling your custom Publisher without the entire Peach source code. But the reality is that when you're building your Publisher, you'll need to look at the source of other Publishers to get better understanding of how everything works, so you might as well learn to build everything from source anyway.
Download the source package from Peach's sourceforge page. I'd recommend downloading the latest Beta source code, rather then the stable source so that you can take advantage of bug fixes, etc..
To compile from source is as simple as it gets due to a handy install script:
Peach will install the compiled binaries into
Publisher StructurePublishers are located within the
Peach.Core/Publishersdirectory of the source package. There are a number available for you to use a reference. Basically every publisher inherits from the
Peach.Core/Publisher.cs) and should override a few key functions that are tied back to the corresponding Action Types referenced in the PeachPit. The following table provides a summary of those functions (all are of type
protected virtual voidunless otherwise noted and descriptions are from the
||Called when the publisher is started. This method will be called once per fuzzing "Session", not on every iteration.|
||Called when the publisher is stopped. This method will be called once per fuzzing "Session", not on every iteration.|
||Open or connect to a resource. Will be called automatically if not called specifically.|
||Close a resource. Will be called automatically when state model exists. Can also be called explicitly when needed.|
||Accept an incoming connection.|
||Call a method on the Publishers resource|
||Set a property on the Publishers resource.|
||Get value of a property exposed by Publishers resource|
Depending on the purpose of the Publisher, some of the above functions are more important then others. For instance, if we're only concerned with modifying the output of data right before its sent, then we'd just override
Getting StartedFrom here on out we'll demonstrate everything else you need to get started by building a simple example that adds a layer of encapsulation within the UDP protocol. This example would be trivial to add to a DataModel, but for the sake of this example, we'll implement it in a Publisher.
First up we'll start out by making a copy of the
UdpPublisherwhich extends the
We'll set the name for our Publisher that will be referenced in the PeachPit by replacing "
Udp" with "
MyCustomPublisheron line 35:
And name the class of our publisher by replacing "
UdpPublisher" with "
MyCustomPublisher" on line 43:
and line 49:
And that's it! We have our custom publisher all done! Granted, its really a waste at this point since its exactly the same thing as the UdpPublisher, but nonetheless it's still custom :)
Extending FunctionalityTo make this example a little more interesting, lets add that layer of encapsulation, something like this:
Here we care about fuzzing Custom, but not ABC Proto. So we'd create a custom Publisher to handle ABC Proto and a DataModel for fuzzing Custom. Let's say ABC Proto is structured this way:
First thing we'll need to do in our new Publisher is override the OnOutput function so that we can modify the data before its sent. So we'll add a new line after line 72 and insert:
Now comes our program body, we'll need to build a new packet with the ABC Proto's header and length fields. Header is a 2 byte static value of
1234, and length is a 2 byte value for the length of the data field in network byte order. Since the length field is only 2 bytes, we first need to put in some intelligence that ensures the length of data does not exceed the maximum value of that field.
This can be an controversial move and its an important note to make about custom publishers. Our intention is to fuzz the heck out of Custom Protocol and as part of that fuzzing, we should be trying really long strings and other values. By implementing this limitation we are effectively limiting our test cases. It might be worthwhile just to forget about an accurate value in the ABC Proto Length field as it might lead to more vulnerabilities.
That being said, let's leave that option up to the end user of the publisher. We'll do that via a parameter that we'll implement a little further down below.
Next we'll create our ABC Proto Start Header which is just a constant
Our final product will be a buffer containing the original data packet encapsulated within ABC Proto, so here we'll create that buffer:
Now we'll build our packet by first copying the ABC Proto Header into the buffer:
Next we'll handle the length field. It needs to be in network bit order, so we'll do with
Array.Reverse()after we copy it to the output buffer:
To wrap up the buffer we'll just copy over the original data:
At this point we've built-in that ABC Proto layer of encapsulation, since that's all we really needed to do, we can pass that data to the original
OnOutput()function that SocketPublisher implements to send:
Passing ParametersOk back to that ABC Proto Length issue we ran into earlier on. We could either restrict the data length and limit our fuzzing or just ignore it. The best approach might be to allow the user make that decision via a Parameter passed to the publisher. To do this we'll need to create a new parameter by inserting a new line after line 51 and providing:
Here we have
StrictLengthas a boolean option, set to
trueby default. Next we'll need to create local variable for it by inserting a new line after line 62:
And now we can wrap our length adjustment code into an
Alright! Our custom ABC Proto publisher is written! On to compiling..
We could recompile the entire Peach source code as per the instructions above, or using a standard Peach binary release, we can save time by compiling only our new custom publisher. Peach runs on Linux with the help of the mono framework which allows .Net applications to run on a number of different platforms. The
dmcsutility is a compiler within mono which we'll use on our Kali installation.
Enter the peach binary release directory with your
MyCustomPublisher.cscopied into it, and compile with:
If all went well, you should should have a
Calling from the PeachPitThe last thing we need to do is call our custom publisher from a PeachPit via the
<Publisher>tag within our Test definition:
The full PeachPit for this project can be found here.
Testing with WiresharkWe'll run a single instance of our fuzz case and use Wireshark to inspect output on the wire:
Now Wireshark doesn't have have a plug-in to parse for our ABC Proto (WTH wireshark dev team?!) - but if we look at the raw data we can see our ABC Proto header, length fields, and included within the data is the content of our DataModel.