forked from Kiv/poclbm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
70 lines (62 loc) · 2.22 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from log import say_exception
from struct import pack, unpack, error
import sys
class Object(object):
pass
def uint32(x):
return x & 0xffffffffL
def bytereverse(x):
return uint32(( ((x) << 24) | (((x) << 8) & 0x00ff0000) | (((x) >> 8) & 0x0000ff00) | ((x) >> 24) ))
def belowOrEquals(hash_, target):
for i in range(len(hash_) - 1, -1, -1):
reversed_ = bytereverse(hash_[i])
if reversed_ < target[i]:
return True
elif reversed_ > target[i]:
return False
return True
def if_else(condition, trueVal, falseVal):
if condition:
return trueVal
else:
return falseVal
def chunks(l, n):
for i in xrange(0, len(l), n):
yield l[i:i+n]
def tokenize(option, name, default=[0], cast=int):
if option:
try:
return [cast(x) for x in option.split(',')]
except ValueError:
say_exception('Invalid %s(s) specified: %s\n\n' % (name, option))
sys.exit()
return default
def patch(data):
pos = data.find('\x7fELF', 1)
if pos != -1 and data.find('\x7fELF', pos+1) == -1:
data2 = data[pos:]
try:
(id, a, b, c, d, e, f, offset, g, h, i, j, entrySize, count, index) = unpack('QQHHIIIIIHHHHHH', data2[:52])
if id == 0x64010101464c457f and offset != 0:
(a, b, c, d, nameTableOffset, size, e, f, g, h) = unpack('IIIIIIIIII', data2[offset+index * entrySize : offset+(index+1) * entrySize])
header = data2[offset : offset+count * entrySize]
firstText = True
for i in xrange(count):
entry = header[i * entrySize : (i+1) * entrySize]
(nameIndex, a, b, c, offset, size, d, e, f, g) = unpack('IIIIIIIIII', entry)
nameOffset = nameTableOffset + nameIndex
name = data2[nameOffset : data2.find('\x00', nameOffset)]
if name == '.text':
if firstText: firstText = False
else:
data2 = data2[offset : offset + size]
patched = ''
for i in xrange(len(data2) / 8):
instruction, = unpack('Q', data2[i * 8 : i * 8 + 8])
if (instruction&0x9003f00002001000) == 0x0001a00000000000:
instruction ^= (0x0001a00000000000 ^ 0x0000c00000000000)
patched += pack('Q', instruction)
return ''.join([data[:pos+offset], patched, data[pos + offset + size:]])
except error:
pass
return data