pwnlib.util.cyclic — Generation of unique sequences

pwnlib.util.cyclic.cyclic(length=None, alphabet=string.ascii_lowercase, n=4) → list/str[source]

A simple wrapper over de_bruijn(). This function returns a at most length elements.

If the given alphabet is a string, a string is returned from this function. Otherwise a list is returned.

Parameters:
  • length – The desired length of the list or None if the entire sequence is desired.
  • alphabet – List or string to generate the sequence over.
  • n (int) – The length of subsequences that should be unique.

Example

>>> cyclic(alphabet="ABC", n=3)
'AAABAACABBABCACBACCBBBCBCCC'
>>> cyclic(20)
'aaaabaaacaaadaaaeaaa'
>>> alphabet, n = range(30), 3
>>> len(alphabet)**n, len(cyclic(alphabet=alphabet, n=n))
(27000, 27000)
pwnlib.util.cyclic.cyclic_find(subseq, alphabet=string.ascii_lowercase, n=None) → int[source]

Calculates the position of a substring into a De Bruijn sequence.

Parameters:
  • subseq (int, bytes, str) – The subsequence to look for. This can either be a bytes, a string, a list or an integer. If an integer is provided it will be packed as a little endian integer.
  • alphabet (bytes, str) – List or string to generate the sequence over.
  • n (int) – The length of subsequences that should be unique.

Examples

>>> cyclic_find(cyclic(1000)[514:518])
514
>>> cyclic_find(0x61616162)
4
pwnlib.util.cyclic.de_bruijn(alphabet=string.ascii_lowercase, n=4) → generator[source]

Generator for a sequence of unique substrings of length n. This is implemented using a De Bruijn Sequence over the given alphabet.

The returned generator will yield up to len(alphabet)**n elements.

Parameters:
  • alphabet – List or string to generate the sequence over.
  • n (int) – The length of subsequences that should be unique.