fuzzer

package module
v0.0.0-alpha.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2024 License: Unlicense Imports: 17 Imported by: 1

README

Fuzzer

fuzzer is a versatile string fuzzing library for Golang that allows the generation of random data based on tokens embedded in strings. It supports a range of token types for generating random strings, numbers, UUIDs, and custom user-defined functions. Additionally, it can load data from a directory into memory for use in fuzzing.

Features

  • Randomized Data Generation: Generate strings, integers, floats, and UUIDs with simple tokens.
  • Custom Functions: Define and use custom functions for generating specialized data.
  • In-Memory Data Cache: Load data from files into memory and access them during fuzzing.
  • Data Encoding: Encode data as Binary, Base32, Base64, Base85, Hex, or URL-encoded strings.

Installation

go get github.com/toxyl/fuzzer

Usage

Initialization

Before using the fuzzer, initialize it with an optional data directory and a map of custom functions:

import (
    "github.com/toxyl/fuzzer"
)

func main() {
    userFns := map[string]func(args ...string) string{
        "customFn": func(args ...string) string {
            return "custom output"
        },
    }
    fuzzer.Init("data_dir", userFns)
}
Fuzzing a String

After initialization, use the Fuzz function to generate a randomized output based on the provided string:

output := fuzzer.Fuzz("[sl:10] [su:5] [i:4] [f:6.2] [$customFn:arg1;arg2]")

Available Tokens

The following tokens can be used within strings passed to Fuzz():

  • UUIDs and Hashes

    • [UUID] or [#UUID] - Generates a random UUID (e.g., xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx).
    • [#56] - Generates a random 56-character hash.
  • Random Numbers

    • [i:6] - Random integer with a 6-character length (zero-padded).
    • [f:6.2] - Random float with a 6-character integer part and a 2-character fractional part.
    • [10:500] - Random integer between 10 and 500 (inclusive, can include negative values).
    • [0.5:5.5] - Random float between 0.5 and 5.5 (inclusive, can include negative values).
  • Random Strings

    • [sl:6] - Random lowercase string with 6 characters (a-z).
    • [su:6] - Random uppercase string with 6 characters (A-Z).
    • [s:6] - Random mixed-case string with 6 characters (a-z, A-Z).
    • [al:6] - Random lowercase alphanumeric string with 6 characters (a-z, 0-9).
    • [au:6] - Random uppercase alphanumeric string with 6 characters (A-Z, 0-9).
    • [a:6] - Random mixed-case alphanumeric string with 6 characters (a-z, A-Z, 0-9).
  • Lists and Ranges

    • [10..500] - Generates a comma-separated list of all integers from 10 to 500 (inclusive).
    • [a,b,c] - Randomly selects a value from the given list.
  • In-Memory Cache

    • [:path] - Reads a random line from the specified path in the in-memory cache. If the path is a directory, a random file from that directory will be used.
  • User Functions

    • [$fn:arg1;arg2;...;argN] - Executes a user-defined function fn with the specified arguments.
  • Data Encoding

    • [bin:data] - Encodes data as binary.
    • [b32:data] - Encodes data as Base32.
    • [b64:data] - Encodes data as Base64.
    • [b85:data] - Encodes data as Base85.
    • [hex:data] - Encodes data as hexadecimal.
    • [url:data] - Encodes data as URL (percent-encoding).
Custom Functions

Use custom functions for more complex data generation:

userFns := map[string]func(args ...string) string{
    "greet": func(args ...string) string {
        return "Hello, " + strings.Join(args, " ")
    },
}

fuzzer.Init("", userFns)

output := fuzzer.Fuzz("[$greet:John;Doe]")
fmt.Println(output) // Output: Hello, John Doe

Examples

Run the test suite for examples that illustrate the use of different tokens and custom functions:

go test -v ./...

License

This project is released into the public domain under the UNLICENSE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fuzz

func Fuzz(str string) string

Fuzz takes the input string and replaces all tokens with randomly generated data. Tokens can be nested.

Available tokens:

[#UUID]    = random UUID (xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx)
[#56]      = random 56-characters hash
[f:6.2]    = random float with a 6-character integer part and a 2-character fractional part (zero-padded)
[i:6]      = random 6-characters integer (zero-padded)
[sl:6]     = random 6-characters lowercase string (a-z)
[su:6]     = random 6-characters uppercase string (A-Z)
[s:6]      = random 6-characters mixed-case string (a-z, A-Z)
[al:6]     = random 6-characters lowercase alphanumeric string (a-z, 0-9)
[au:6]     = random 6-characters uppercase alphanumeric string (A-Z, 0-9)
[a:6]      = random 6-characters mixed-case alphanumeric string (a-z, A-Z, 0-9)
[0.5:5.5]  = random float64 value between 0.5 and 5.5 (inclusive, values can be negative)
[10:500]   = random value between 10 and 500 (inclusive, values can be negative)
[10..500]  = comma separated list with all ints from 10 to 500 (inclusive, values can be negative)
[a,b,c]    = random value from the list

In-memory cache:

[:path]

Reads a random line from the given "path" in the memory cache. If path is a directory a random file from that directory will be used.

User functions:

[$fn:arg1;arg2;...;argN]

Executes the user-supplied function "fn" with the arguments "arg1" through "argN" (ie. fn(arg1,arg2,...,argN)) which must return a new string.

Data encoding:

[bin:data] = encodes 'data' as Binary
[b32:data] = encodes 'data' as Base32
[b64:data] = encodes 'data' as Base64
[b85:data] = encodes 'data' as Base85
[hex:data] = encodes 'data' as Hex
[url:data] = encodes 'data' as URL (percent-encoding)

func Init

func Init(dataDir string, userFns map[string]func(args ...string) string)

Init initializes the fuzzer. If the given data dir not an empty string it will recursively load the files from the given directory into memory. You can supply additional functions to the fuzzer using the userFns param. These can then be used with the `[$fn:arg1;arg2;...;argN]` syntax.

Calling Init() multiple times will add files and user functions to the existing instance.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL