-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathusePatch.py
221 lines (133 loc) · 6.41 KB
/
usePatch.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from sys import exit as sysExit
from time import sleep
from binascii import unhexlify, hexlify
from shutil import copyfile
from pathlib import Path as pathLibPath
from os import path as osPath, makedirs as osMakedirs, environ as env, listdir, system as cmd
def make_dirs(dest):
if not osPath.exists(dest):
osMakedirs(dest)
def isExists(path):
result = osPath.exists(path)
if result == False:
try:
f = open(path)
f.close()
return True
except IOError:
return False
return False
return True
class Patch():
def getSystem32(self):
system32 = osPath.join(env["windir"], "System32")
if not isExists(system32):
return osPath.join(env["windir"], "system32")
return system32
def __init__(self):
self.thisPath = pathLibPath(__file__).parent.absolute()
self.mainFileName = "InputSwitch.dll"
self.hasErrors = False
dirs = [
self.getSystem32()
]
for dir in listdir(osPath.join(env["windir"], "WinSxS")):
if osPath.isdir(osPath.join(env["windir"], "WinSxS", dir)):
if "inputswitch" in dir:
dirs.append(osPath.join(env["windir"], "WinSxS", dir))
print("Процесс патчинга начинается. Отключение explorer.exe...")
cmd("taskkill /F /IM explorer.exe")
sleep(2)
for dir in dirs:
self.do(dir)
cmd("start %windir%\explorer.exe")
if self.hasErrors is True:
print("Патч завершился с ошибками. Это есть не добрый знак. Попытайтесь откатить изменения с помощью \"python offPatch.py\"")
sysExit(1)
else:
print("Done!")
sysExit(0)
def warn(self, warntext):
print("WARN! FilePath: " + self.filePath + " : " + warntext)
def error(self, error):
self.hasErrors = True
print("ERROR! FilePath: " + self.filePath + " : " + error)
def do(self, dir):
basename = osPath.basename(dir)
self.filePath = osPath.join(dir, self.mainFileName)
if not isExists(self.filePath):
self.warn("not exists")
return
# set group of admins as the owners of file
cmd("takeown /F \"" + self.filePath + "\" /A")
# give the administrator group full access to this file
cmd("icacls \"" + self.filePath + "\" /grant:r \"*S-1-5-32-544\":f")
# backup
if not isExists("./backup/" + basename):
backupPath = osPath.join(self.thisPath, "backup", basename, self.mainFileName)
make_dirs("./backup/" + basename)
copyfile(self.filePath, backupPath)
f = open(osPath.join(self.thisPath, "backup", basename, "info.txt"), "a")
f.write(self.filePath)
f.close()
status = self.processPatch()
# return the rights to trusted installer
cmd("icacls \"" + self.filePath + "\" /setowner \"NT SERVICE\TrustedInstaller\" /C /L /Q")
# give administrators and trusted installer read and execute permissions
cmd("icacls \"" + self.filePath + "\" /grant:r \"NT SERVICE\TrustedInstaller\":rx")
cmd("icacls \"" + self.filePath + "\" /grant:r \"*S-1-5-32-544\":rx")
def processPatch(self):
with open(self.filePath, 'rb') as f:
hexdata = hexlify(f.read()).decode("utf-8")
i = 0
pointer = 0
hexAsList = []
for h in hexdata:
if i % 2 == 0 and i != 0: pointer += 1
if not pointer < len(hexAsList): hexAsList.append(h)
else: hexAsList[pointer] += h
i += 1
i = 0
inARow = 0
maxArea = 40
res = 0
for h in hexAsList:
if inARow >= 5:
if maxArea > 0:
maxArea -= 1
hexAsList[i] = "90"
if h == "33" and inARow == 5:
inARow += 1
elif h == "c0" and inARow == 6:
inARow += 1
elif (h == "48" or h == "8b") and inARow == 7:
# final
hexAsList[i] = h
hexAsList[i - 1] = "c0"
hexAsList[i - 2] = "33"
res = 1
break
else:
break
elif h == "ff" and inARow == 0:
inARow += 1
elif h == "ff" and inARow == 1:
inARow += 1
elif h == "83" and inARow == 2:
inARow += 1
elif h == "f8" and inARow == 3:
inARow += 1
elif h == "ff" and inARow == 4:
inARow += 1
else:
inARow = 0
i += 1
if res == 0:
self.error("cant patch this dll!")
return False
with open(self.filePath, 'wb') as fout:
for h in hexAsList:
fout.write(unhexlify(h))
print(self.filePath + ": SUCCESSFUL PATCHING")
return True
Patch();