forked from QiuChenly/InjectLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
440 lines (372 loc) · 20 KB
/
main.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import json
import os
import plistlib
import subprocess
import shutil
from pathlib import Path
import time
def read_input(prompt):
return input(prompt).strip().lower()
def search_apps(app_list, install_apps, keyword):
keyword = keyword.lower()
installed_apps = {app['CFBundleIdentifier']: app['CFBundleName'].lower() for app in install_apps}
def is_match(pn):
return keyword in (pn.lower() if isinstance(pn, str) else any(keyword in p.lower() for p in pn))
matched_apps = []
for app in app_list:
pn_list = app.get("packageName")
if isinstance(pn_list, list):
matched = [pn for pn in pn_list if is_match(pn) and any(pid == pn and keyword in name for pid, name in installed_apps.items())]
matched_apps.extend({**app, "packageName": pn} for pn in matched)
elif pn_list and is_match(pn_list):
matched_apps.append({**app, "packageName": pn_list})
return matched_apps
def parse_app_info(app_base_locate, app_info_file):
with open(app_info_file, "rb") as f:
app_info = plistlib.load(f)
app_info = {
"appBaseLocate": app_base_locate,
"CFBundleIdentifier": app_info.get("CFBundleIdentifier"),
"CFBundleVersion": app_info.get("CFBundleVersion", ""),
"CFBundleShortVersionString": app_info.get("CFBundleShortVersionString", ""),
"CFBundleName": app_info.get("CFBundleExecutable", ""),
"CFBundleExecutable": app_info.get("CFBundleExecutable", ""),
}
return app_info
def scan_apps():
appList = []
base_dirs = ["/Applications", "/Applications/Setapp"]
for base_dir in base_dirs:
if not os.path.exists(base_dir):
continue
lst = os.listdir(base_dir)
for app in lst:
app_info_file = os.path.join(base_dir, app, "Contents", "Info.plist")
if not os.path.exists(app_info_file):
continue
try:
appList.append(parse_app_info(base_dir + "/" + app, app_info_file))
# print("检查本地App:", app_info_file)
except Exception:
continue
return appList
def check_compatible(compatible_version_code, compatible_version_subcode, app_version_code, app_subversion_code):
if compatible_version_code is None and compatible_version_subcode is None:
return True
if compatible_version_code:
for code in compatible_version_code:
if app_version_code == code:
return True
if compatible_version_subcode:
for code in compatible_version_subcode:
if app_subversion_code == code:
return True
return False
def handle_keygen(bundleIdentifier):
# 取出用户名
username = os.path.expanduser("~").split("/")[-1]
subprocess.run("chmod +x ./tool/KeygenStarter", shell=True)
subprocess.run(f"./tool/KeygenStarter '{bundleIdentifier}' '{username}'", shell=True)
def handle_helper(app_base, target_helper, component_apps, SMExtra, bridge_path):
"""增强Helper
Args:
app_base (dict): app信息
target_helper (string): helper文件路径
"""
subprocess.run("chmod +x ./tool/GenShineImpactStarter", shell=True)
subprocess.run(f"./tool/GenShineImpactStarter '{target_helper}' {'' if SMExtra is None else SMExtra}", shell=True)
subprocess.run(f"./tool/insert_dylib '{bridge_path}91QiuChenly.dylib' '{target_helper}' '{target_helper}'", shell=True)
helper_name = target_helper.split("/")[-1]
# 检查是否存在
target = f"/Library/LaunchDaemons/{helper_name}.plist"
if os.path.exists(target):
subprocess.run(f"sudo /bin/launchctl unload {target}", shell=True)
subprocess.run(f"sudo /usr/bin/killall -u root -9 {helper_name}", shell=True)
subprocess.run(f"sudo /bin/rm {target}", shell=True)
subprocess.run(f"sudo /bin/rm /Library/PrivilegedHelperTools/{helper_name}", shell=True)
subprocess.run(f"sudo xattr -c '{app_base}'", shell=True)
src_info = [f"{app_base}/Contents/Info.plist"]
if isinstance(component_apps, list):
src_info.extend([f"{app_base}{i}/Contents/Info.plist" for i in component_apps])
for i in src_info:
command = ["/usr/libexec/PlistBuddy", "-c", f"Set :SMPrivilegedExecutables:{helper_name} 'identifier \\\"{helper_name}\\\"'", i]
subprocess.run(command, text=True)
subprocess.run(f'/usr/bin/codesign -f -s - --all-architectures --deep "{target_helper}"', shell=True)
subprocess.run(f'/usr/bin/codesign -f -s - --all-architectures --deep "{app_base}"', shell=True)
def getAppMainExecutable(app_base):
# 读取Contents/Info.plist中的CFBundleExecutable
with open(f"{app_base}/Contents/Info.plist", "rb") as f:
app_info = plistlib.load(f)
return app_info["CFBundleExecutable"]
# 获取BundleID
def getBundleID(app_base):
with open(f"{app_base}/Contents/Info.plist", "rb") as f:
app_info = plistlib.load(f)
return app_info["CFBundleIdentifier"]
def main():
try:
with open("config.json", "r") as f:
config = json.load(f)
base_public_config = config["basePublicConfig"]
app_list = config["AppList"]
proc_version = config["Version"]
print()
print(" ██████╗ ██╗██╗ ██╗ ██████╗██╗ ██╗███████╗███╗ ██╗██╗ ██╗ ██╗ ")
print(" ██╔═══██╗██║██║ ██║██╔════╝██║ ██║██╔════╝████╗ ██║██║ ╚██╗ ██╔╝ ")
print(" ██║ ██║██║██║ ██║██║ ███████║█████╗ ██╔██╗ ██║██║ ╚████╔╝ ")
print(" ██║▄▄ ██║██║██║ ██║██║ ██╔══██║██╔══╝ ██║╚██╗██║██║ ╚██╔╝ ")
print(" ╚██████╔╝██║╚██████╔╝╚██████╗██║ ██║███████╗██║ ╚████║███████╗██║ ")
print(" ╚══▀▀═╝ ╚═╝ ╚═════╝ ╚═════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═══╝╚══════╝╚═╝ ")
print()
print("Original Design By QiuChenly(github.com/qiuchenly), Py ver. by X1a0He")
print(f"自动注入版本号: {proc_version}")
print("注入时请根据提示输入'y' 或者按下回车键跳过这一项。")
# QiuChenlyTeam 特殊变量
isDevHome = os.getenv("InjectLibDev")
# start_time = time.time()
install_apps = scan_apps()
# end_time = time.time()
# elapsed_time = end_time - start_time
# print("扫描本地App耗时: {:.2f}s".format(elapsed_time))
app_Lst = []
keyword = input("请输入应用名称或包名的关键字进行搜索,或直接按回车键遍历所有支持的应用: ").strip()
if keyword:
matched_apps = search_apps(app_list, install_apps, keyword)
if not matched_apps:
print("未找到匹配的应用程序。")
else:
app_Lst = []
selected = set()
while len(selected) < len(matched_apps):
print("找到以下匹配的应用程序:")
for i, app in enumerate(matched_apps, 1):
status = " ✅[已选中]" if i-1 in selected else ""
print(f"{i}. {app.get('packageName')}{status}")
if len(selected) == len(matched_apps):
print("所有应用已选中,即将开始处理...")
break
choice = input("请输入要注入的应用程序编号,输入0退出,或按回车继续: ").strip()
if choice == '0':
print("已退出程序。")
exit(0)
elif choice.isdigit() and 0 < int(choice) <= len(matched_apps):
index = int(choice) - 1
if index not in selected:
app_Lst.append(matched_apps[index])
selected.add(index)
if len(selected) == len(matched_apps): print("所有应用已选中,即将开始处理...")
else: print(f"应用 {matched_apps[index].get('packageName')} 已经被选择,请选择其他应用。")
elif choice == '':
if not app_Lst: print("未选择任何应用,请至少选择一个应用。")
else: break
else: print("无效的输入,请重新选择。")
else:
app_Lst = [app.copy() | {"packageName": name} for app in app_list
if not (app.get("forQiuChenly") and not os.path.exists("/Users/qiuchenly"))
for name in (app["packageName"] if isinstance(app["packageName"], list) else [app["packageName"]])]
for app in app_Lst:
package_name = app.get("packageName")
app_base_locate = app.get("appBaseLocate")
bridge_file = app.get("bridgeFile")
inject_file = app.get("injectFile")
support_version = app.get("supportVersion")
support_subversion = app.get("supportSubVersion")
extra_shell = app.get("extraShell")
need_copy_to_app_dir = app.get("needCopyToAppDir")
deep_sign_app = app.get("deepSignApp")
disable_library_validate = app.get("disableLibraryValidate")
entitlements = app.get("entitlements")
no_sign_target = app.get("noSignTarget")
no_deep = app.get("noDeep")
tccutil = app.get("tccutil")
auto_handle_setapp = app.get("autoHandleSetapp")
auto_handle_helper = app.get("autoHandleHelper")
helper_file = app.get("helperFile")
componentApp = app.get("componentApp")
onlysh = app.get("onlysh")
SMExtra = app.get("SMExtra")
keygen = app.get("keygen")
local_app = [
local_app
for local_app in install_apps
if local_app["CFBundleIdentifier"] == package_name
]
if not local_app and (
app_base_locate is None or not os.path.isdir(app_base_locate)
):
continue
if not local_app:
# print("[🔔] 此App包不是常见类型结构,请注意当前App注入的路径是 {appBaseLocate}".format(appBaseLocate=app_base_locate))
# print("读取的是 {appBaseLocate}/Contents/Info.plist".format(appBaseLocate=app_base_locate))
local_app.append(
parse_app_info(
app_base_locate,
os.path.join(app_base_locate, "Contents", "Info.plist"),
)
)
local_app = local_app[0]
if app_base_locate is None:
app_base_locate = local_app["appBaseLocate"]
if bridge_file is None:
bridge_file = base_public_config.get("bridgeFile", bridge_file)
if auto_handle_setapp is not None:
bridge_file = "/Contents/MacOS/"
executableAppName = local_app["CFBundleExecutable"]
inject_file = os.path.basename(app_base_locate + bridge_file + executableAppName)
print(f"======== Setapp下一个App的处理结果如下 [{app_base_locate}] [{bridge_file}] [{inject_file}]")
if not check_compatible(support_version, support_subversion, local_app["CFBundleShortVersionString"], local_app["CFBundleVersion"],):
print(f"[😅] [{local_app['CFBundleName']}] - [{local_app['CFBundleShortVersionString']}] - [{local_app['CFBundleIdentifier']}]不是受支持的版本,跳过注入😋。")
continue
print(f"[🤔] [{local_app['CFBundleName']}] - [{local_app['CFBundleShortVersionString']}] 是受支持的版本,是否需要注入?y/n(默认n)")
action = read_input("").strip().lower()
if action != "y":
continue
if onlysh:
subprocess.run(f"sudo sh tool/{extra_shell}", shell=True)
continue
# 检查是否为com.adobe开头
# if local_app["CFBundleIdentifier"].startswith("com.adobe"):
# subprocess.run(
# "sudo chmod -R 777 /Applications/Utilities/Adobe Creative Cloud/Components/Apps/*",
# shell=True,
# )
# # 检查是否存在/Applications/Utilities/Adobe Creative Cloud/Components/Apps/Apps1_0.js
# if not os.path.exists(
# "/Applications/Utilities/Adobe Creative Cloud/Components/Apps/Apps1_0.js"
# ):
# # 替换文件中的key:"getEntitlementStatus",value:function(e){为key:"getEntitlementStatus",value:function(e){return "Entitled Installed"
# with open(
# "/Applications/Utilities/Adobe Creative Cloud/Components/Apps/Apps1_0.js",
# "r",
# encoding="utf-8",
# ) as f:
# content = f.read()
# # 判断是否写过了
# if (
# 'key:"getEntitlementStatus",value:function(e){return "Entitled Installed"'
# not in content
# ):
# # sed -i "s#key:\"getEntitlementStatus\",value:function(e){#key:\"getEntitlementStatus\",value:function(e){return \"Entitled Installed\"#g" /Applications/Utilities/Adobe\ Creative\ Cloud/Components/Apps/Apps1_0.js
# content = content.replace(
# 'key:"getEntitlementStatus",value:function(e){',
# 'key:"getEntitlementStatus",value:function(e){return "Entitled Installed";',
# )
# with open(
# "/Applications/Utilities/Adobe Creative Cloud/Components/Apps/Apps1_0.js",
# "w",
# encoding="utf-8",
# ) as f:
# f.write(content)
print(f"开始注入App: {package_name}")
subprocess.run(["sudo", "chmod", "-R", "777", app_base_locate])
subprocess.run(["sudo", "xattr", "-cr", app_base_locate])
subprocess.run(
["sudo", "pkill", "-f", getAppMainExecutable(app_base_locate)]
)
if keygen is not None:
print("正在注册App...")
handle_keygen(local_app["CFBundleIdentifier"])
continue
# dest = os.path.join(app_base_locate, bridge_file, inject_file)
dest = rf"{app_base_locate}{bridge_file}{inject_file}"
backup = rf"{dest}_backup"
if os.path.exists(backup):
print("备份的原始文件已经存在,需要直接用这个文件注入吗?y/n(默认y)")
action = read_input("").strip().lower()
if action == "n":
os.remove(backup)
subprocess.run(f"sudo cp '{dest}' '{backup}'", shell=True)
else:
subprocess.run(f"sudo cp '{dest}' '{backup}'", shell=True)
current = Path(__file__).resolve()
sh = f"chmod +x {current.parent}/tool/insert_dylib"
subprocess.run(sh, shell=True)
sh = f"sudo {current.parent}/tool/insert_dylib '{current.parent}/tool/91QiuChenly.dylib' '{backup}' '{dest}'"
if need_copy_to_app_dir:
source_dylib = f"{current.parent}/tool/91QiuChenly.dylib"
if isDevHome:
# 开发者自己的prebuild库路径 直接在.zshrc设置环境变量这里就可以读取到。
# export InjectLibDev="自己的路径/91QiuChenly.dylib"
# 要设置全路径哦 并且不要用sudo python3 main.py 启动 否则读不到你的环境变量
source_dylib = isDevHome
destination_dylib = f"'{app_base_locate}{bridge_file}91QiuChenly.dylib'"
command = "ln -f -s" if isDevHome else "cp"
subprocess.run(
f"{command} {source_dylib} {destination_dylib}",
shell=True,
)
sh = []
desireApp = [dest]
if componentApp:
desireApp.extend(
[
f"{app_base_locate}{i}/Contents/MacOS/{getAppMainExecutable(app_base_locate+i)}"
for i in componentApp
]
)
for it in desireApp:
bsh = rf"sudo {current.parent}/tool/insert_dylib {destination_dylib} '{backup}' '{it}'"
sh.append(bsh)
if isinstance(sh, list):
[subprocess.run(command, shell=True) for command in sh]
else:
subprocess.run(sh, shell=True)
sign_prefix = (
"/usr/bin/codesign -f -s - --timestamp=none --all-architectures"
)
if no_deep is None:
print("Need Deep Sign.")
sign_prefix += " --deep"
if entitlements is not None:
sign_prefix += f" --entitlements {current.parent}/tool/{entitlements}"
if no_sign_target is None:
print("开始签名...")
subprocess.run(f"{sign_prefix} '{dest}'", shell=True)
if disable_library_validate is not None:
subprocess.run(
"sudo defaults write /Library/Preferences/com.apple.security.libraryvalidation.plist DisableLibraryValidation -bool true",
shell=True,
)
if extra_shell is not None:
subprocess.run(
f"sudo sh {current.parent}/tool/{extra_shell}", shell=True
)
if deep_sign_app:
subprocess.run(f"{sign_prefix} '{app_base_locate}'", shell=True)
subprocess.run(f"sudo xattr -cr '{dest}'", shell=True)
if auto_handle_helper and helper_file:
helpers = []
if isinstance(helper_file, list):
helpers = helper_file
else:
helpers.append(helper_file)
for helper in helpers:
handle_helper(
app_base_locate,
f"{app_base_locate}{helper}",
componentApp,
SMExtra,
f"{app_base_locate}{bridge_file}",
)
if tccutil is not None:
if tccutil := tccutil:
# 如果componentApp不为空,则创建一个数组
ids = [local_app["CFBundleIdentifier"]]
if isinstance(componentApp, list):
ids.extend(
[getBundleID(app_base_locate + i) for i in componentApp]
)
for id in ids:
if isinstance(tccutil, str):
subprocess.run(f"tccutil reset {tccutil} {id}", shell=True)
else:
if isinstance(tccutil, list):
for i in tccutil:
subprocess.run(
f"tccutil reset {i} {id}", shell=True
)
print("App处理完成。")
except KeyboardInterrupt:
print("\n用户手动退出程序,祝你使用愉快,再见.")
if __name__ == "__main__":
main()