Leveraging Ruby to Build a Framework
is a powerful scripting language that can be used to build a nice fuzzing framework. This article covers only the initial phase of the framework; a more advanced library can be built around it. The following are some of the advantages of using Ruby as the language for the framework:
- It can be used across platforms once support for the language is in place.
- It is object-oriented, permitting you to leverage object-oriented programming features to make the framework more flexible.
- It supports HTTP and HTTPS with sockets and libraries.
- Its interactive shell enables effective interactive fuzzing.
The Ruby code is contained in the file Webfuzz.rb (see Listing 1), which contains two classes for implementing web fuzzing:
- TargetNeeded for specifying the IP address, port, and request you want to fuzz
- FuzzContains methods to fuzz each outgoing request with various payloads; provides an interface to the result array
You can build fuzzing logic using these two classes to provide a customized fuzzing load for a specific request. Let's explore each of these classes in detail.
The Target class (see Figure 1 for a high-level Ruby documentation view) offers the following methods to set critical parameters:
ip= set the target IP address
port= set the target port
request= set the HTTP request for fuzzing
As you can see, these methods all end with the "=" operator and "new" indicates a constructor. The following is simple webfuzz.rb code for ip=, which takes a parameter and sets it to a @ip instance variable:
# File webfuzz.rb, line 11
@ip = newip
The other two methods, port= and request=, work the same way.
The following webfuzz.rb code initializes an instance of the class in the form of an object and creates four variables:
# File webfuzz.rb, line 4
The other two methods, request and response, are the HTTP fuzz request and the placeholder for its response, respectively.
Finally, the send method contains code to "throw" the request on the wire and fetch a response from the target:
# File webfuzz.rb, line 42
s = TCPsocket.open(@ip,@port)
@response = s.read
This very simple code snippet will open a TCP connection with the socket library, send a request to the server, and then wait for a response and assign it to the @response variable.
Hence, each instance of Target has its own IP, port, and request. By using the send method, it can access, target, and fill the response variable. It has another method called show that also enables you to see all variables of an instance.
Let's quickly look at how you can use the Target class. Ruby has its own interactive prompt called irb. Here is how you can use it for interactive assessment:
- You Open an irb session and load webfuzz.rb using the require directive, as follows:
D:\webfuzz> irb --simple-prompt
>> require 'webfuzz'
Loading the library ...
Make sure you have webfuzz.rb in the same directory where you are executing irb. Also note that you don't need to write webfuzz.rb; webfuzz is sufficient for the operation.
- Once the library is loaded, you can start using the classes to you assign a target and send a very basic request to it on the wire. For example, you take following URL:
Say you want to fuzz the variable "id", which takes an integer value in the webshop application. You would create your target setting as follows:
- Create an object for the target:
>> t = Target.new
=> #<Target:0x2be93d8 @port="", @ip="", @response="", @request="">
- Assign "webshop.example.com" as ip:
>> t.ip = "webshop.example.com"
- Assign port 80:
Here's a quick peek at the object "t":
>> t.port = 80
=> #<Target:0x2be93d8 @port=80, @ip="webshop.example.com", @response="", @request="">
- Set the GET request for the Target class. You want to fuzz the "id" variable, so you insert "$fuzz$" as the value:
>> t.request = "GET /dvds4less/details.asp?id=$fuzz$ HTTP/1.0\r\n\r\n"
=> "/dvds4less/details.asp?id=$fuzz$ HTTP/1.0\r\n\r\n "
At this point, you can use the send method by issuing the t.send command on irb to send the request on the wire. At the same time, you can use t.show to view the entire class instance. You are all set with your fuzzing target at this point.
Now let's look at the Fuzz class, which uses Target when doing web fuzzing (in this case, the target you just defined ). The next section shows how the Fuzz class reads and processes the "$fuzz$" value.