def cyclic_metasploit(length=None, sets=None): """cyclic_metasploit(length = None, sets = [ string.ascii_uppercase, string.ascii_lowercase, string.digits ]) -> str A simple wrapper over :func:`metasploit_pattern`. This function returns a string of length `length`. Arguments: length: The desired length of the string or None if the entire sequence is desired. sets: List of strings to generate the sequence over. Example: >>> cyclic_metasploit(32) b'Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab' >>> cyclic_metasploit(sets = [b"AB",b"ab",b"12"]) b'Aa1Aa2Ab1Ab2Ba1Ba2Bb1Bb2' >>> cyclic_metasploit()[1337:1341] b'5Bs6' >>> len(cyclic_metasploit()) 20280 """ sets = sets or [ string.ascii_uppercase.encode(), string.ascii_lowercase.encode(), string.digits.encode() ] generator = metasploit_pattern(sets) out = iters.take(length, generator) if length != None and len(out) < length: log.error("Can't create a pattern of length %i with sets of lengths %s. Maximum pattern length is %i." \ % (length, list(map(len, sets)), len(out))) return _join_sequence(out, sets[0])
def cyclic(length=None, alphabet=None, n=None): """cyclic(length = None, alphabet = None, n = None) -> list/str A simple wrapper over :func:`de_bruijn`. This function returns at most `length` elements. If the given alphabet is a string, a string is returned from this function. Otherwise a list is returned. Arguments: 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. Notes: The maximum length is `len(alphabet)**n`. The default values for `alphabet` and `n` restrict the total space to ~446KB. If you need to generate a longer cyclic pattern, provide a longer `alphabet`, or if possible a larger `n`. Example: Cyclic patterns are usually generated by providing a specific `length`. >>> cyclic(20) b'aaaabaaacaaadaaaeaaa' >>> cyclic(32) b'aaaabaaacaaadaaaeaaafaaagaaahaaa' The `alphabet` and `n` arguments will control the actual output of the pattern >>> cyclic(20, alphabet=string.ascii_uppercase) 'AAAABAAACAAADAAAEAAA' >>> cyclic(20, n=8) b'aaaaaaaabaaaaaaacaaa' >>> cyclic(20, n=2) b'aabacadaeafagahaiaja' The size of `n` and `alphabet` limit the maximum length that can be generated. Without providing `length`, the entire possible cyclic space is generated. >>> cyclic(alphabet = "ABC", n = 3) 'AAABAACABBABCACBACCBBBCBCCC' >>> cyclic(length=512, alphabet = "ABC", n = 3) Traceback (most recent call last): ... PwnlibException: Can't create a pattern length=512 with len(alphabet)==3 and n==3 The `alphabet` can be set in `context`, which is useful for circumstances when certain characters are not allowed. See :obj:`.context.cyclic_alphabet`. >>> context.cyclic_alphabet = "ABC" >>> cyclic(10) b'AAAABAAACA' The original values can always be restored with: >>> context.clear() The following just a test to make sure the length is correct. >>> alphabet, n = range(30), 3 >>> len(alphabet)**n, len(cyclic(alphabet = alphabet, n = n)) (27000, 27000) """ if n is None: n = context.cyclic_size if alphabet is None: alphabet = context.cyclic_alphabet if length != None and len(alphabet)**n < length: log.error("Can't create a pattern length=%i with len(alphabet)==%i and n==%i" \ % (length, len(alphabet), n)) generator = de_bruijn(alphabet, n) out = iters.take(length, generator) return _join_sequence(out, alphabet)