Tutorial

How To Install and Use Radamsa to Fuzz Test Programs and Network Services on Ubuntu 18.04

Published on January 31, 2020
English
How To Install and Use Radamsa to Fuzz Test Programs and Network Services on Ubuntu 18.04

The author selected the Electronic Frontier Foundation Inc to receive a donation as part of the Write for DOnations program.

Introduction

Security threats are continually becoming more sophisticated, so developers and systems administrators need to take a proactive approach in defending and testing the security of their applications.

A common method for testing the security of client applications or network services is fuzzing, which involves repeatedly sending invalid or malformed data to the application and analyzing its response. This is useful to help test how resilient and robust the application is to unexpected input, which may include corrupted data or actual attacks.

Radamsa is an open-source fuzzing tool that can generate test cases based on user-specified input data. Radamsa is fully scriptable, and so far has been successful in finding vulnerabilities in real-world applications, such as Gzip.

In this tutorial, you will install and use Radamsa to fuzz test command-line and network-based applications using your own test cases.

Warning: Radamsa is a penetration testing tool which may allow you to identify vulnerabilities or weaknesses in certain systems or applications. You must not use vulnerabilities found with Radamsa for any form of reckless behavior, harm, or malicious exploitation. Vulnerabilities should be ethically reported to the maintainer of the affected application, and not disclosed publicly without explicit permission.

Prerequisites

Before you begin this guide you’ll need the following:

  • One Ubuntu 18.04 server set up by following the Initial Server Setup with Ubuntu 18.04, including a sudo non-root user and enabled firewall to block non-essential ports.
  • A command-line or network-based application that you wish to test, for example Gzip, Tcpdump, Bind, Apache, jq, or any other application of your choice. As an example for the purposes of this tutorial, we’ll use jq.

Warning: Radamsa can cause applications or systems to run unstably or crash, so only run Radamsa in an environment where you are prepared for this, such as a dedicated server. Please also ensure that you have explicit written permission from the owner of a system before conducting fuzz testing against it.

Once you have these ready, log in to your server as your non-root user to begin.

Step 1 — Installing Radamsa

Firstly, you will download and compile Radamsa in order to begin using it on your system. The Radamsa source code is available in the official repository on GitLab.

Begin by updating the local package index to reflect any new upstream changes:

  1. sudo apt update

Then, install the gcc, git, make, and wget packages needed to compile the source code into an executable binary:

  1. sudo apt install gcc git make wget

After confirming the installation, apt will download and install the specified packages and all of their required dependencies.

Next, you’ll download a copy of the source code for Radamsa by cloning it from the repository hosted on GitLab:

  1. git clone https://gitlab.com/akihe/radamsa.git

This will create a directory called radamsa, containing the source code for the application. Move into the directory to begin compiling the code:

  1. cd radamsa

Next, you can start the compilation process using make:

  1. make

Finally, you can install the compiled Radamsa binary to your $PATH:

  1. sudo make install

Once this is complete, you can check the installed version to make sure that everything is working:

  1. radamsa --version

Your output will look similar to the following:

Output
Radamsa 0.6

If you see a radamsa: command not found error, double-check that all required dependencies were installed and that there were no errors during compilation.

Now that you’ve installed Radamsa, you can begin to generate some sample test cases to understand how Radamsa works and what it can be used for.

Step 2 — Generating Fuzzing Test Cases

Now that Radamsa has been installed, you can use it to generate some fuzzing test cases.

A test case is a piece of data that will be used as input to the program that you are testing. For example, if you are fuzz testing an archiving program such as Gzip, a test case may be a file archive that you are attempting to decompress.

Note: Radamsa will manipulate input data in a wide variety of unexpected ways, including extreme repetition, bit flips, control character injection, and so on. This may cause your terminal session to break or become unstable, so be aware of this before proceeding.

Firstly, pass a simple piece of text to Radamsa to see what happens:

  1. echo "Hello, world!" | radamsa

This will manipulate (or fuzz) the inputted data and output a test case, for example:

Output
Hello,, world!

In this case, Radamsa added an extra comma between Hello and world. This may not seem like a significant change, but in some applications this may cause the data to be interpreted incorrectly.

Let’s try again by running the same command. You’ll see different output:

Output
Hello, '''''''wor'd!

This time, multiple single quotes (') were inserted into the string, including one that overwrote the l in world. This particular test case is more likely to result in problems for an application, as single/double quotes are often used to separate different pieces of data in a list.

Let’s try one more time:

Output
Hello, $+$PATH\u0000`xcalc`world!

In this case, Radamsa inserted a shell injection string, which will be useful to test for command injection vulnerabilities in the application that you are testing.

You’ve used Radamsa to fuzz an input string and produce a series of test cases. Next, you will use Radamsa to fuzz a command-line application.

Step 3 — Fuzzing a Command-line Application

In this step, you’ll use Radamsa to fuzz a command-line application and report on any crashes that occur.

The exact technique for fuzzing each program varies massively, and different methods will be most effective for different programs. However, in this tutorial we will use the example of jq, which is a command-line program for processing JSON data.

You may use any other similar program as long as it follows the general principle of taking some form of structured or unstructured data, doing something with it, and then outputting a result. For instance this example would also work with Gzip, Grep, bc, tr, and so on.

If you don’t already have jq installed, you can install it using apt:

  1. sudo apt install jq

jq will now be installed.

To begin fuzzing, create a sample JSON file that you’ll use as the input to Radamsa:

  1. nano test.json

Then, add the following sample JSON data to the file:

test.json
{
  "test": "test",
  "array": [
    "item1: foo",
    "item2: bar"
  ]
}

You can parse this file using jq if you wish to check that the JSON syntax is valid:

  1. jq . test.json

If the JSON is valid, jq will output the file. Otherwise, it will display an error, which you can use to correct the syntax where required.

Next, fuzz the test JSON file using Radamsa and then pass it to jq. This will cause jq to read the fuzzed/manipulated test case, rather than the original valid JSON data:

  1. radamsa test.json | jq

If Radamsa fuzzes the JSON data in a way that it is still syntactically valid, jq will output the data, but with whatever changes Radamsa made to it.

Alternatively, if Radamsa causes the JSON data to become invalid, jq will display a relevant error. For example:

Output
parse error: Expected separator between values at line 5, column 16

The alternate outcome would be that jq is unable to correctly handle the fuzzed data, causing it to crash or misbehave. This is what you’re really looking for with fuzzing, as this may be indicative of a security vulnerability such as a buffer overflow or command injection.

In order to more efficiently test for vulnerabilities like this, a Bash script can be used to automate the fuzzing process, including generating test cases, passing them to the target program and capturing any relevant output.

Create a file named jq-fuzz.sh:

  1. nano jq-fuzz.sh

The exact script content will vary depending on the type of program that you’re fuzzing and the input data, but in the case of jq and other similar programs, the following script suffices.

Copy the script into your jq-fuzz.sh file:

jq-fuzz.sh
#!/bin/bash
while true; do
  radamsa test.json > input.txt
  jq . input.txt > /dev/null 2>&1
  if [ $? -gt 127 ]; then
    cp input.txt crash-`date +s%.%N`.txt
    echo "Crash found!"
  fi
done

This script contains a while to make the contents loop repeatedly. Each time the script loops, Radamsa will generate a test case based on test.json and save it to input.txt.

The input.txt test case will then be run through jq, with all standard and error output redirected to /dev/null to avoid filling up the terminal screen.

Finally, the exit value of jq is checked. If the exit value is greater than 127, this is indicative of a fatal termination (a crash), then the input data is saved for review at a later date in a file named crash- followed by the current date in Unix seconds and nanoseconds.

Mark the script as executable and set it running in order to begin automatically fuzz testing jq:

  1. chmod +x jq-fuzz.sh
  2. ./jq-fuzz.sh

You can issue CTRL+C at any time to terminate the script. You can then check whether any crashes have been found by using ls to display a directory listing containing any crash files that have been created.

You may wish to improve your JSON input data since using a more complex input file is likely to improve the quality of your fuzzing results. Avoid using a large file or one that contains a lot of repeated data—an ideal input file is one that is small in size, yet still contains as many ‘complex’ elements as possible. For example, a good input file will contain samples of data stored in all formats, including strings, integers, booleans, lists, and objects, as well as nested data where possible.

You’ve used Radamsa to fuzz a command-line application. Next, you’ll use Radamsa to fuzz requests to network services.

Step 4 — Fuzzing Requests to Network Services

Radamsa can also be used to fuzz network services, either acting as a network client or server. In this step, you’ll use Radamsa to fuzz a network service, with Radamsa acting as the client.

The purpose of fuzzing network services is to test how resilient a particular network service is to clients sending it malformed and/or malicious data. Many network services such as web servers or DNS servers are usually exposed to the internet, meaning that they are a common target for attackers. A network service that is not sufficiently resistant to receiving malformed data may crash, or even worse fail in an open state, allowing attackers to read sensitive data such as encryption keys or user data.

The specific technique for fuzzing network services varies enormously depending on the network service in question, however in this example we will use Radamsa to fuzz a basic web server serving static HTML content.

Firstly, you need to set up the web server to use for testing. You can do this using the built-in development server that comes with the php-cli package. You’ll also need curl in order to test your web server.

If you don’t have php-cli and/or curl installed, you can install them using apt:

  1. sudo apt install php-cli curl

Next, create a directory to store your web server files in and move into it:

  1. mkdir ~/www
  2. cd ~/www

Then, create a HTML file containing some sample text:

  1. nano index.html

Add the following to the file:

index.html
<h1>Hello, world!</h1>

You can now run your PHP web server. You’ll need to be able to view the web server log while still using another terminal session, so open another terminal session and SSH to your server for this:

  1. cd ~/www
  2. php -S localhost:8080

This will output something similar to the following:

Output
PHP 7.2.24-0ubuntu0.18.04.1 Development Server started at Wed Jan 1 16:06:41 2020 Listening on http://localhost:8080 Document root is /home/user/www Press Ctrl-C to quit.

You can now switch back to your original terminal session and test that the web server is working using curl:

  1. curl localhost:8080

This will output the sample index.html file that you created earlier:

Output
<h1>Hello, world!</h1>

Your web server only needs to be accessible locally, so you should not open any ports on your firewall for it.

Now that you’ve set up your test web server, you can begin to fuzz test it using Radamsa.

First, you’ll need to create a sample HTTP request to use as the input data for Radamsa. Create a new file to store this in:

  1. nano http-request.txt

Then, copy the following sample HTTP request into the file:

http-request.txt
GET / HTTP/1.1
Host: localhost:8080
User-Agent: test
Accept: */*

Next, you can use Radamsa to submit this HTTP request to your local web server. In order to do this, you’ll need to use Radamsa as a TCP client, which can be done by specifying an IP address and port to connect to:

  1. radamsa -o 127.0.0.1:8080 http-request.txt

Note: Be aware that using Radamsa as a TCP client will potentially cause malformed/malicious data to be transmitted over the network. This may break things, so be very careful to only access networks that you are authorized to test, or preferably, stick to using the localhost (127.0.0.1) address.

Finally, if you view the outputted logs for your local web server, you’ll see that it has received the requests, but most likely not processed them as they were invalid/malformed.

The outputted logs will be visible in your second terminal window:

Output
[Wed Jan 1 16:26:49 2020] 127.0.0.1:49334 Invalid request (Unexpected EOF) [Wed Jan 1 16:28:04 2020] 127.0.0.1:49336 Invalid request (Malformed HTTP request) [Wed Jan 1 16:28:05 2020] 127.0.0.1:49338 Invalid request (Malformed HTTP request) [Wed Jan 1 16:28:07 2020] 127.0.0.1:49340 Invalid request (Unexpected EOF) [Wed Jan 1 16:28:08 2020] 127.0.0.1:49342 Invalid request (Malformed HTTP request)

For optimal results and to ensure that crashes are recorded, you may wish to write an automation script similar to the one used in Step 3. You should also consider using a more complex input file, which may contain additions such as extra HTTP headers.

You’ve fuzzed a network service using Radamsa acting as a TCP client. Next, you will fuzz a network client with Radamsa acting as a server.

Step 5 — Fuzzing Network Client Applications

In this step, you will use Radamsa to fuzz test a network client application. This is achieved by intercepting responses from a network service and fuzzing them before they are received by the client.

The purpose of this kind of fuzzing is to test how resilient network client applications are to receiving malformed or malicious data from network services. For example, testing a web browser (client) receiving malformed HTML from a web server (network service), or testing a DNS client receiving malformed DNS responses from a DNS server.

As was the case with fuzzing command-line applications or network services, the exact technique for fuzzing each network client application varies considerably, however in this example you will use whois, which is a simple TCP-based send/receive application.

The whois application is used to make requests to WHOIS servers and receive WHOIS records as responses. WHOIS operates over TCP port 43 in clear text, making it a good candidate for network-based fuzz testing.

If you don’t already have whois available, you can install it using apt:

  1. sudo apt install whois

First, you’ll need to acquire a sample whois response to use as your input data. You can do this by making a whois request and saving the output to a file. You can use any domain you wish here as you’re testing the whois program locally using sample data:

  1. whois example.com > whois.txt

Next, you’ll need to set up Radamsa as a server that serves fuzzed versions of this whois response. You’ll need to be able to continue using your terminal once Radamsa is running in server mode, so it is recommended to open another terminal session and SSH connection to your server for this:

  1. radamsa -o :4343 whois.txt -n inf

Radamsa will now be running in TCP server mode, and will serve a fuzzed version of whois.txt each time a connection is made to the server, no matter what request data is received.

You can now proceed to testing the whois client application. You’ll need to make a normal whois request for any domain of your choice (it doesn’t have to be the same one that the sample data is for), but with whois pointed to your local Radamsa server:

  1. whois -h localhost:4343 example.com

The response will be your sample data, but fuzzed by Radamsa. You can continue to make requests to the local server as long as Radamsa is running, and it will serve a different fuzzed response each time.

As with fuzzing network services, to improve the efficiency of this network client fuzz testing and ensure that any crashes are captured, you may wish to write an automation script similar to the one used in Step 3.

In this final step, you used Radamsa to conduct fuzz testing of a network client application.

Conclusion

In this article you set up Radamsa and used it to fuzz a command-line application, a network service, and a network client. You now have the foundational knowledge required to fuzz test your own applications, hopefully with the result of improving their robustness and resistance to attack.

If you wish to explore Radamsa further, you may wish to review the Radamsa README file in detail, as it contains further technical information and examples of how the tool can be used:

You may also wish to check out some other fuzzing tools such as American Fuzzy Lop (AFL), which is an advanced fuzzing tool designed for testing binary applications at extremely high speed and accuracy:

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar

Security Engineer

IT Security Engineer, technical writer and occasional blogger from the United Kingdom, with an interest in security defence and blue team activities.



Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.