Make binbase_find.py Python 3 code.
This commit is contained in:
100
binbase_find.py
100
binbase_find.py
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
# https://github.com/mncoppola/ws30/blob/master/basefind.py
|
# https://github.com/mncoppola/ws30/blob/master/basefind.py
|
||||||
|
|
||||||
@ -10,59 +10,59 @@ import struct
|
|||||||
import sys
|
import sys
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
|
|
||||||
chars = r"A-Za-z0-9/\-:.,_$%'\"()[\]<> "
|
chars = "A-Za-z0-9/\\-:.,_$%'\"()[\]<> "
|
||||||
min_length = 10
|
min_length = 10
|
||||||
scores = []
|
scores = []
|
||||||
top_score = 0
|
top_score = 0
|
||||||
|
|
||||||
regexp = "[%s]{%d,}" % (chars, min_length)
|
regexp = bytes("[{}]{{{:d},}}".format(chars, min_length), "us-ascii")
|
||||||
pattern = re.compile(regexp)
|
pattern = re.compile(regexp)
|
||||||
regexpc = "[%s]{1,}" % chars
|
regexpc = bytes("[{}]{{1,}}".format(chars), "us-ascii")
|
||||||
patternc = re.compile(regexpc)
|
patternc = re.compile(regexpc)
|
||||||
|
|
||||||
def high_scores(signal, frame):
|
def high_scores(signal, frame):
|
||||||
print "\nTop 20 base address candidates:"
|
print("\nTop 20 base address candidates:")
|
||||||
for score in sorted(scores, key=itemgetter(1), reverse=True)[:20]:
|
for score in sorted(scores, key=itemgetter(1), reverse=True)[:20]:
|
||||||
print "0x%x\t%d" % score
|
print("0x{:x}\t{:d}".format(*score))
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
def get_pointers(f):
|
def get_pointers(filename):
|
||||||
table = {}
|
table = {}
|
||||||
f.seek(0)
|
with open(filename, "rb") as f:
|
||||||
while True:
|
while True:
|
||||||
try:
|
|
||||||
value = struct.unpack("<L", f.read(4))[0]
|
|
||||||
try:
|
try:
|
||||||
table[value] += 1
|
value = struct.unpack("<L", f.read(4))[0]
|
||||||
except KeyError:
|
try:
|
||||||
table[value] = 1
|
table[value] += 1
|
||||||
except:
|
except KeyError:
|
||||||
break
|
table[value] = 1
|
||||||
|
except:
|
||||||
|
break
|
||||||
return table
|
return table
|
||||||
|
|
||||||
def get_strings(f, size):
|
def get_strings(filename, size):
|
||||||
table = set([])
|
table = set()
|
||||||
tbladd = table.add
|
|
||||||
offset = 0
|
offset = 0
|
||||||
while True:
|
with open(filename, "rb") as f:
|
||||||
if offset >= size:
|
while True:
|
||||||
break
|
if offset >= size:
|
||||||
f.seek(offset)
|
break
|
||||||
try:
|
f.seek(offset)
|
||||||
data = f.read(10)
|
|
||||||
except:
|
|
||||||
break
|
|
||||||
match = pattern.match(data)
|
|
||||||
if match:
|
|
||||||
f.seek(offset - 1)
|
|
||||||
try:
|
try:
|
||||||
char = f.read(1)
|
data = f.read(10)
|
||||||
except:
|
except:
|
||||||
continue
|
break
|
||||||
if not patternc.match(char):
|
match = pattern.match(data)
|
||||||
tbladd(offset)
|
if match:
|
||||||
offset += len(match.group(0))
|
f.seek(offset - 1)
|
||||||
offset += 1
|
try:
|
||||||
|
char = f.read(1)
|
||||||
|
except:
|
||||||
|
continue
|
||||||
|
if not patternc.match(char):
|
||||||
|
table.add(offset)
|
||||||
|
offset += len(match.group(0))
|
||||||
|
offset += 1
|
||||||
return table
|
return table
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@ -77,28 +77,27 @@ if __name__ == "__main__":
|
|||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
size = os.path.getsize(args.infile)
|
size = os.path.getsize(args.infile)
|
||||||
f = open(args.infile, "rb")
|
|
||||||
scores = []
|
scores = []
|
||||||
|
|
||||||
print "Scanning binary for strings..."
|
print("Scanning binary for strings...")
|
||||||
str_table = get_strings(f, size)
|
str_table = get_strings(args.infile, size)
|
||||||
print "Total strings found: %d" % len(str_table)
|
print("Total strings found: {:d}".format(len(str_table)))
|
||||||
|
|
||||||
print "Scanning binary for pointers..."
|
print("Scanning binary for pointers...")
|
||||||
ptr_table = get_pointers(f)
|
ptr_table = get_pointers(args.infile)
|
||||||
print "Total pointers found: %d" % len(ptr_table)
|
print("Total pointers found: {:d}".format(len(ptr_table)))
|
||||||
|
|
||||||
f.close()
|
|
||||||
gc.disable()
|
gc.disable()
|
||||||
signal.signal(signal.SIGINT, high_scores)
|
signal.signal(signal.SIGINT, high_scores)
|
||||||
|
|
||||||
for base in xrange(args.min_addr, args.max_addr, args.page_size):
|
for base in range(args.min_addr, args.max_addr, args.page_size):
|
||||||
if base % args.page_size == 0:
|
if base % ( args.page_size * 1000 ) == 0:
|
||||||
print u"Trying base address 0x%x\u001b[F\u001b[K" % base
|
print("Trying base address 0x{:x}".format(base))
|
||||||
score = 0
|
score = 0
|
||||||
for ptr in ptr_table.keys():
|
ptrs = list(ptr_table.keys())
|
||||||
|
for ptr in ptrs:
|
||||||
if ptr < base:
|
if ptr < base:
|
||||||
#print "Removing pointer 0x%x from table" % ptr
|
#print("Removing pointer 0x{:x} from table".format(ptr))
|
||||||
del ptr_table[ptr]
|
del ptr_table[ptr]
|
||||||
continue
|
continue
|
||||||
if ptr >= (base + size):
|
if ptr >= (base + size):
|
||||||
@ -110,7 +109,6 @@ if __name__ == "__main__":
|
|||||||
scores.append((base, score))
|
scores.append((base, score))
|
||||||
if score > top_score:
|
if score > top_score:
|
||||||
top_score = score
|
top_score = score
|
||||||
print "New highest score, 0x%x: %d" % (base, score)
|
print("New highest score, 0x{:x}: {:d}".format(base, score))
|
||||||
print ""
|
|
||||||
|
|
||||||
high_scores(0, 0)
|
high_scores(0, 0)
|
||||||
|
Reference in New Issue
Block a user