Skip to content

Instantly share code, notes, and snippets.

@adi928
Last active January 19, 2020 21:05
Show Gist options
  • Save adi928/64cc783821b64f5fba8d8c81583818c0 to your computer and use it in GitHub Desktop.
Save adi928/64cc783821b64f5fba8d8c81583818c0 to your computer and use it in GitHub Desktop.
#! /usr/local/bin/python
import sys
import string
import re
import argparse
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# USAGE:
# $ ./pattern.py 233
# Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2Ad3Ad4Ad5Ad6Ad7Ad8Ad9Ae0Ae1Ae2Ae3Ae4Ae5Ae6Ae7Ae8Ae9Af0Af1Af2Af3Af4Af5Af6Af7Af8Af9Ag0Ag1Ag2Ag3Ag4Ag5Ag6Ag7Ag8Ag9Ah0Ah1Ah2Ah3Ah4Ah5Ah6Ah
# $ ./pattern.py --offset 0x63413563 233
# 76
parser = argparse.ArgumentParser(description='Find offset from string')
# It is imperative to provide original pattern's length along with the offset.
parser.add_argument('--offset', action='store', dest='offsetStr', help='Find the offset for given length of pattern')
parser.add_argument('lengthOfString', help='Generate pattern of this length')
alphabets = string.ascii_lowercase
alpha_Caps = string.ascii_uppercase
def makePat(lenReq):
pattern = ''
lenReq = int(lenReq)
for capA in alpha_Caps:
for alpha in alphabets:
for nums in range(0,10):
pattern += capA + alpha + str(nums)
if len(pattern) >= lenReq:
return pattern[:lenReq]
return pattern
def findOffset(hexStr, lenReq):
pattern = makePat(lenReq)
hexNum = hexStr[2:]
subPattern = bytearray.fromhex(hexNum).decode()
subPattern = subPattern[::-1]
print pattern.find(subPattern)
if __name__ == "__main__":
args = parser.parse_args()
if args.offsetStr:
findOffset(args.offsetStr, args.lengthOfString)
else:
print makePat(args.lengthOfString)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment