-
Notifications
You must be signed in to change notification settings - Fork 4
/
patchagent.py
133 lines (110 loc) · 4.02 KB
/
patchagent.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import time
import lief
import random
import os
import sys
class Patcher:
ELF_MAGIC_MAIN = "7F 45 4C 46 "
exclusions = []
@staticmethod
def verify_patched_binary(self, path):
print ("\n[*] validating patched binary at: " + path)
if lief.is_elf(path):
bin = lief.parse(path)
elif lief.is_macho(path):
print("[!] Mach-O/iOS binary verification is not supported yet")
sys.exit(1)
else:
print("[!] binary verification is only present in ELF formats")
sys.exit(1)
bin_segments = bin.segments
bin_sections = bin.sections
header = bin.header
print ("[*] detected segments:", len(bin_segments))
print ("[*] detected sections:", len(bin_sections))
if len(bin_segments) != header.numberof_segments:
print ("[!] segment mismatch detected!!: " + path)
return -1
if len(bin_sections) != header.numberof_sections:
print ("[!] section mismatch detected!!: " + path)
return -1
print ("[*] section & segment verification completed")
print ("[*] verifying magic")
magic_header_id_bytes = header.identity
magic = ""
for x in range(4):
byte_magic = str(hex(magic_header_id_bytes[x]))
magic += byte_magic.upper()[2:] + " "
if self.ELF_MAGIC_MAIN != magic:
print ("[!] ELF magic mismatch detected, binary is corrupted!!")
return -1
print ("[*] ELF magic: ", magic)
print ("[*] binary verification successful")
return 0
@staticmethod
def generate_name(length):
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
return ''.join(random.choice(chars) for _ in range(length))
@staticmethod
def do_patch(self, binary, replacer, replacee="", startpos = 0, endpos = 0):
match = replacer.encode('utf8')
length = len(match)
if replacee == '':
val = self.generate_name(length).encode('utf8')[startpos:]
else:
val = replacee.encode('utf8')[startpos:]
if len(val) > length:
raise Exception('[!] input length is higher than required')
else:
val += int.to_bytes(0, length - len(val), 'big')
if endpos > 0:
val = val[:-endpos]
cur_index = 0
while True:
try:
index = binary.index(match, cur_index)
cur_index = index + 1
if index in self.exclusions:
continue
except:
break
binary[index + startpos : index + length - endpos] = val
print("[*] patching: " + replacer + " at: " + str(hex(index)) + " with: " + val.decode("utf8"))
@staticmethod
def check_path(binarypath):
if os.path.exists(binarypath):
return True
return False
@staticmethod
def initiate_patching_process(self, path, outpath):
with open(path, 'rb') as f:
binary = bytearray(f.read())
try:
self.exclusions.append(binary.index(b'/System/Library/Caches/') + len('/System'))
except:
pass
frida_strings_to_patch = [
"linjector",
"gmain",
"gum-js-loop",
"re.frida.server",
"frida-helper",
"gdbus",
"frida-agent",
"pipe-",
"GADGET",
"gadget.so",
"FRIDA",
"AGENT",
"frida-",
"frida-agent-32.so",
"frida-server",
"frida-agent-64.so",
]
for value in frida_strings_to_patch:
self.do_patch(self, binary, value)
time.sleep(0.2)
self.do_patch(self, binary, '\"frida\"', startpos=1, endpos=1)
with open(outpath, 'wb') as f:
f.write(binary)
print(f"[*] modified binary has been saved to: {outpath}")