Keyspace Iterator in AWK

by Justin Parrott

I wrote a little keyspace iterator in AWK.  The first two uses for something like this that I've thought of are brute forcing password cracking and groping a web server for hidden files (by combining this script with something that can fetch from a URL).

AWK probably isn't going to be the fastest language, but performance may not be such a concern as the real bottleneck is in the application of the string to the situation at hand(the encryption of URL fetch, for example).

The first one I wrote was in C, but I'm submitting the AWK version because I think it's a little more interesting (even though it's basically the same implementation of a recursive function).

#!/usr/bin/awk -f
#
# A keyspace iterator for brute force, etc. This can be used, for example,
# in conjuction with a tool like curl to fetch https://site.com/*.html
# - Justin Parrott
#
# Usage: ./ks [minlen [maxlen [string]]]
#
# Tested with nawk and gawk

function f(keyspace, kslen, prefix, count, i) {
	if (count > 1) {
		for (i = 1; i <= kslen; i++) {
			newpfx = sprintf("%s%c", prefix, keyspace[i])
			f(keyspace, kslen, newpfx, count - 1)
		}
	} else {
	for (i = 1; i <= kslen; i++) 
		printf "%s%c\n", prefix, keyspace[i]
	}
}

BEGIN {
	minlen = 3
	maxlen = 8
	keyspacestr = "abcdefghijklmnopqrstuvwxyz"

	if (ARGC >= 2)
		minlen = ARGV[1]
	if (ARGC >= 3)
		maxlen = ARGV[2]
	if (ARGC >= 4)
		keyspacestr = ARGV[3]

	if (minlen > maxlen) {
		print "Minimum length is greater than maximum length"
		exit
	}

	n = split(keyspacestr, ksary, "")

	for (len = minlen; len <= maxlen; len++)
		f(ksary, n, "", len)

	exit;
}

Code: ks.awk  (Example Output for 'test')

Return to $2600 Index