Skip to content

Instantly share code, notes, and snippets.

@perliedman
Created April 16, 2013 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perliedman/5395526 to your computer and use it in GitHub Desktop.
Save perliedman/5395526 to your computer and use it in GitHub Desktop.
Simple utility to grep/find a byte pattern in a file. I can only assume you can use grep or some other utility to do this as well.
#!/usr/bin/python
from sys import argv
path = argv[1]
hex_pattern = argv[2]
str_pattern = "".join([chr(int(hex_pattern[x:x+2], 16)) for x in range(0,len(hex_pattern), 2)])
with open(path, 'r') as f:
data = f.read()
pos = data.find(str_pattern)
if pos >= 0:
while pos >= 0:
start = max(0, pos - 16)
end = min(len(data), pos + len(str_pattern) + 16)
print "Found at position %d. Showing characters %d-%d" % (pos, start, end)
print data[start:end]
print "".join([" " for x in xrange(start, pos)]) + '^'
pos = data.find(str_pattern, pos + 1)
else:
print "Not found."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment