Last active
September 7, 2016 22:43
-
-
Save jef79m/e3e68a3b6112dcb183a8c9e7ebf5828c to your computer and use it in GitHub Desktop.
Python tool that soes the stuff the pattern_create.rb and pattern_offset.rb do. Just for fun.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
pattern_create.py | |
generates unique strings similar to metasploits | |
pattern_create.rb for ease of use in fuzzers etc. | |
example: | |
In [1]: import pattern_create | |
In [2]: payload = pattern_create.make_pattern(length=50) | |
In [3]: print(payload) | |
Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2Ad3Ad4Ad5Ad6Ad7Ad8Ad9Ae0Ae1Ae2Ae3Ae4Ae5Ae6Ae7Ae8Ae9 | |
""" | |
import argparse | |
seta_default = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
setb_default = "abcdefghijklmnopqrstuvwxyz" | |
setc_default = "0123456789" | |
length_default = 600 | |
def make_pattern(seta=seta_default, | |
setb=setb_default, | |
setc=setc_default, | |
length=length_default): | |
pattern = ''.join([u+l+d for u in seta for l in setb for d in setc]) | |
if length is None: | |
return pattern | |
else: | |
if length > len(pattern): | |
pattern = pattern * (length / len(pattern) +1) | |
return pattern[:length] | |
else: | |
return pattern[:length] | |
def toChars(val): | |
''' | |
Convert a 4 byte number into a string of 4 chars to search pattern for. | |
''' | |
if len(val) == 4: | |
return val | |
if val[0:2] == '0x' and len(val) > 8: | |
val = val[2:] | |
return ''.join(reversed([chr(int(val[i:i+2], 16)) for i in range(0, len(val), 2)])) | |
def findPattern(needle, pattern, cum_offset=0): | |
offset = pattern.find(needle) | |
if offset >= 0: | |
print("Pattern %s found at offset: %i" % (needle, cum_offset + offset)) | |
offset = findPattern(needle, pattern[offset+len(needle):], cum_offset + offset) | |
return | |
if cum_offset == 0: | |
print("Pattern %s was not found!" % needle) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Generate and locate unique string patterns.') | |
parser.add_argument('--length' ,'-l', metavar='N', type=int, default=length_default, | |
help='Length of string to generate') | |
parser.add_argument('--seta', '-a', metavar='N', type=str, help='First sequence to cycle', | |
default=seta_default) | |
parser.add_argument('--setb', '-b', metavar='N', type=str, help='Second sequence to cycle', | |
default=setb_default) | |
parser.add_argument('--setc', '-c', metavar='N', type=str, help='Third sequence to cycle', | |
default=setc_default) | |
parser.add_argument('--find', '-f', metavar='N', type=str, help='Value to find index of') | |
args = parser.parse_args() | |
pattern = make_pattern(seta=args.seta, setb=args.setb, setc=args.setc, length=args.length) | |
if not args.find: | |
print("Inject the following string to overflow:") | |
print("") | |
print(pattern) | |
print("") | |
print("Then enter the value to decode:") | |
args.find = raw_input() | |
needle = toChars(args.find) | |
pattern = make_pattern(seta=args.seta, setb=args.setb, setc=args.setc, length=args.length) | |
findPattern(needle, pattern) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment