Skip to content

Instantly share code, notes, and snippets.

@deqline
Created March 23, 2021 22:11
Show Gist options
  • Save deqline/9a16a3fc76d617efa4009c7f1de5d613 to your computer and use it in GitHub Desktop.
Save deqline/9a16a3fc76d617efa4009c7f1de5d613 to your computer and use it in GitHub Desktop.
Find all the occurences of pattern in a byte buffer using python.
#usage python FindPattern.py sequence pattern
import sys
def str_to_bytes(_bytes):
current = ""
sequence = bytearray()
for ltr in _bytes:
for i in range(2):
current += ltr
sequence.append(int(current, 16))
current = ""
return sequence
_bytes = str_to_bytes(sys.argv[1])
toFind = str_to_bytes(sys.argv[2])
o = 0
x = 0
m = 0
while(x < len(_bytes)):
o = x
for y in range(len(toFind)):
if(o >= len(_bytes)):
print("\nNumber of sequences found %u" % m)
exit(0)
if(_bytes[o] == toFind[y]):
o += 1
if(o-x == len(toFind)-1):
m += 1
print("Sequence found at offset: 0x%X" % int(x/2))
x = o
x += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment