2

This is what crunch can generate:

[user@localhost ~] crunch 3 3 ab
Crunch will now generate the following amount of data: 32 bytes
0 MB
0 GB
0 TB
0 PB
Crunch will now generate the following number of lines: 8 
aaa
aab
aba
abb
baa
bab
bba
bbb
[user@localhost ~] 

But are there any solutions that is the same as crunch, the only difference is that is randomly outputs the lines?

[user@localhost ~] SOMEMAGIC 3 3 ab
bba
bab
abb
aaa
bbb
aab
baa
aba
[user@localhost ~] 

using a "sort -R" like method isn't good! Because the solution needs to be "on the fly"

3
  • 4
    What do you mean with "on the fly"?
    – holgero
    Commented Oct 17, 2014 at 17:35
  • On the fly outputing all the possible random lines. Not just outputing like crunch, then "sort -R" Commented Oct 17, 2014 at 18:26
  • Can you explain what you want here further? I too am confused by your reference to "on the fly".
    – slm
    Commented Oct 17, 2014 at 18:36

4 Answers 4

3

Mimicking crunch

Something like this will generate all the permutations of the set {a,b} @ 3 strings long, and it will shuffle up the output using shuf.

$ printf "%s\n" {a..b}{a..b}{a..b} | shuf
bbb
aab
abb
bba
baa
aba
bab
aaa

This is really no different though than using sort -R.

Hiding the randomizing

If you're intending to hide the interface so that you give scriptX a list of arguments and it returns back a randomized list similar to crunch, then wrapping this in an alias or a shell script would do the job. You could put the following into a shell script called mycrunch.bash:

#!/bin/bash

crunch "$1" "$2" "$3" | sort -R

Mark it as executable:

$ chmod +x mycrunch.bash

And run it like so:

$ ./mycrunch.bash 3 3 ab

Filtering bogus crunch output

You can use a grep command within the script above to filter those lines out like so:

crunch "$1" "$2" "$3" | grep -vE "Crunch|0" | sort -R

That will take care of omitting any lines that contain the strings "Crunch" or "0".

8
  • this doesn't guarantee that all the possible lines will be shown! Commented Oct 17, 2014 at 18:27
  • @somelooser28533 - I have no idea what you're talking about. Please update your Q to reflect additional requirements. What you've shown us isn't enough info to provide proper solutions then! Perhaps explaining what crunch does since I've never heard of it.
    – slm
    Commented Oct 17, 2014 at 18:29
  • @somelooser28533 - is that what you want?
    – slm
    Commented Oct 17, 2014 at 18:33
  • If you want to pass arguments to another command it is a bite more robust to use "$@" instead of "$1" "$2" "$3".
    – holgero
    Commented Oct 17, 2014 at 18:47
  • 1
    @holgero, "more robust" only if you're dealing with arbitrary parameters. Here, we know exactly what we need to pass, no more, no less Commented Oct 17, 2014 at 18:52
2

Analysis

There's a fundamental difficulty in what you want to do.

If crunch was to output all possible lines in random order (without generating them all beforehand and acting as sort -R or shuf) then it would have to remember which lines have already been printed. Storing this information would ultimately require space comparable to storing all the lines. Randomly picking not-yet-printed lines from the whole set would require computational work comparable in total to what sort -R or shuf would do when acting on the whole set.

crunch can generate lines without this burden only because it generates them in some strict order. To tell the next line, it's enough to know the command line parameters and the current line (and the algorithm crunch uses). My point is crunch does not need to remember previous lines, it does not need to compare or search; it moves to the next line quickly because it already knows what the line should be and that it hasn't been printed yet.

Try it by yourself. Speak aloud all numbers from 0 to 26 in a random sequence (or at least in a sequence that seems random). No repetitions, no omissions, each number just once. Hurry up! Now speak aloud all numbers from 0 to 26 in their natural order.

In theory there are at least two methods to get a sequence that looks random without the mentioned burden:

  1. Knowing only the current line and the command line arguments, the algorithm inside crunch could somehow generate the next line that (in general) doesn't resemble several (in case of large sets: several million) previous lines, so the final result looks randomly ordered.

  2. Knowing the command line arguments crunch uses, an external filter could take the current line from crunch (working in its ordered fashion) and transform it somehow, so when crunch progresses, the final result looks randomly ordered.

Note the algorithm inside crunch or inside the filter would have to generate all possible lines without repetitions and without omissions. And you would probably want to be able to give it some seed, so the result looks randomly shuffled in the next full run. I think creating such algorithm is a non-trivial task (if possible at all without hiding the burden inside the algorithm itself).


But what for?

Usually crunch is used to generate a list of words to guess a password by brute force. Let's assume you want a list generated by:

crunch 9 9

Can randomized sequence be better than the ordered one? If the actual password contains another character or if it's of the different length then it's not on the list and both sequences are equally bad: you need to fully deplete one list or the other to learn the password is something else.

If the actual password is on the list then you may or may not be lucky to have it early on the list. This means if the owner (creator) of the password predicts you will use crunch with the default a-z ordered character set, he or she may deliberately choose a "clever" password starting with z, only to delay your success. If you shuffle your list then you will nullify this strategy: the actual password may be near the front as well as near the end of the list, so on average you will need to deplete half of the list. On average this will take you about half of the time you would need to guess the "clever" password starting with z.

So yes, in this scenario shuffling the list helps.

But then why not to use z-a to outsmart the owner of the "clever" password?

crunch 9 9 zyxwvutsrqponmlkjihgfedcba

This will allow you to guess the "clever" password starting with z relatively quickly. But if the owner predicts this specific attack then he or she will rather create a password starting with a!

Here's what to do: flip a coin and use a-z (heads) or z-a (tails).

For any password (created with whatever strategy or without any strategy) a-z is as fortunate for you as z-a is unfortunate; and a-z is as unfortunate as z-a is fortunate. Therefore if only the password is on the list, your chance of using the more fortunate sequence is 50%, so on average you will need to deplete half of the list, just like with a shuffled list. One flip of a coin will nullify any strategy.

0

Well, you could just pipe everything after the output line that starts with 'Crunch will now' through sort -R. Like this:

crunch 3 3 ab| sed -e '0,/^Crunch will now/d' | sort -R

Still not sure if this counts as 'on the fly', though.

-1

But are there any solutions that is the same as crunch, the only difference is that is randomly outputs the lines?

No.

This problem can be solved only in two steps:

  1. Create all possible combinations
  2. Generate random permutation of result

You cannot do both at the same time, at least not with single algorithm for creating combinations. You would need to have different algorithms and at the very beginning of the program choose one randomly, taking care that final results of those different algorithms are uniformly distributed.

Anyway, I doubt you will find anything simple, much different then crunch+sort -R.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .