Repository navigation
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·454 lines (402 loc) · 13.4 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·454 lines (402 loc) · 13.4 KB
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#!/usr/bin/env bash
# Theta build helper
# ./build.sh [sideload] [debug] → inject into input/Payload → output/Instagram_patched.ipa
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
make_cmd=make
if command -v gmake &>/dev/null; then
make_cmd=gmake
fi
export COPYFILE_DISABLE=1
# scripts/setup-toolchain.sh installs Theos to ~/theos by default.
export THEOS="${THEOS:-$HOME/theos}"
if [[ ! -f "$THEOS/makefiles/common.mk" ]]; then
echo "[Build] ERROR: Theos not found at $THEOS — run ./scripts/setup-toolchain.sh"
exit 1
fi
# Linux: the toolchain dir setup-toolchain.sh builds holds ldid, otool, install_name_tool, strip.
# On macOS it links the Xcode toolchain, which has no ldid and lets the system tools win.
if [[ "$(uname -s)" == "Linux" ]]; then
export PATH="$THEOS/toolchain/Xcode14.xctoolchain/usr/bin:$PATH"
fi
usage() {
cat <<'EOF'
Usage: ./build.sh [sideload] [debug]
(no args) / sideload Build the dylibs (Theta + ThetaNSE) and inject Theta
into the app and ThetaNSE into the notification
service extension (required for decrypted banners)
debug Also compile in diagnostic logging (THETA_DEBUG=1)
Expects a decrypted Instagram IPA unpacked as:
input/Payload/Instagram.app/...
Output:
output/Instagram_patched.ipa
EOF
}
# "sideload" is the only target and may be omitted; "debug" compiles in diagnostic logging.
THETA_DEBUG=0
for arg in "$@"; do
case "$(echo "$arg" | tr '[:upper:]' '[:lower:]')" in
sideload) ;;
debug) THETA_DEBUG=1 ;;
-h|--help|help) usage; exit 0 ;;
*) echo "[Build] Invalid argument: $arg"; usage; exit 1 ;;
esac
done
strip_entitlements() {
local target="$1"
[[ -f "$target" ]] || return 0
if command -v codesign &>/dev/null; then
codesign --remove-signature "$target" 2>/dev/null || true
codesign -f -s - "$target" 2>/dev/null || true
else
# Linux: ldid -r drops the signature, ldid -S signs ad hoc with no entitlements.
ldid -r "$target" 2>/dev/null || true
ldid -S "$target" 2>/dev/null || true
fi
}
# Prints a value from a (binary or XML) plist; key is a path like ":NSExtension:NSExtensionPointIdentifier".
plist_print() {
local plist="$1" key="$2"
python3 -I -c '
import plistlib, sys
try:
with open(sys.argv[1], "rb") as f:
v = plistlib.load(f)
for k in sys.argv[2].strip(":").split(":"):
v = v[k]
print(v)
except Exception:
pass
' "$plist" "$key" 2>/dev/null || true
}
binary_already_loads() {
local bin="$1" needle="$2"
if command -v otool &>/dev/null; then
otool -L "$bin" 2>/dev/null | grep -q "$needle" && return 0
fi
return 1
}
macho_filetype() {
local path="$1"
[[ -f "$path" ]] || return 1
# Skip otool's column-header line (it also contains the word "filetype").
otool -h "$path" 2>/dev/null | awk '$1 ~ /^0x/{print $5; exit}'
}
is_macho_dylib() {
local path="$1" ft
[[ -f "$path" ]] || return 1
[[ "$path" == *.dSYM/* ]] && return 1
ft="$(macho_filetype "$path")"
[[ "$ft" == "6" ]]
}
install_and_verify_dylib() {
local src="$1" dest="$2" id_path="$3"
if ! is_macho_dylib "$src"; then
echo "[Build] ERROR: refusing to copy non-dylib: $src"
file "$src" || true
otool -h "$src" 2>/dev/null | head -8 || true
exit 1
fi
echo "[Build] Copy $(basename "$dest") from $src ($(file -b "$src"))"
cp -f "$src" "$dest"
if command -v install_name_tool &>/dev/null && [[ -n "$id_path" ]]; then
install_name_tool -id "$id_path" "$dest" 2>/dev/null || true
fi
shift 3
local old new
while [[ $# -ge 2 ]]; do
old="$1"; new="$2"; shift 2
install_name_tool -change "$old" "$new" "$dest" 2>/dev/null || true
done
strip_entitlements "$dest"
if ! is_macho_dylib "$dest"; then
echo "[Build] ERROR: $dest is not MH_DYLIB after install"
file "$dest" || true
exit 1
fi
}
find_built_dylib() {
local name="$1"
local cand
for cand in \
"$SCRIPT_DIR/.theos/obj/${name}" \
"$SCRIPT_DIR/.theos/obj/debug/${name}" \
"$SCRIPT_DIR/.theos/obj/arm64/${name}"
do
if is_macho_dylib "$cand"; then
printf '%s\n' "$cand"
return 0
fi
done
if [[ -d "$SCRIPT_DIR/.theos" ]]; then
while IFS= read -r cand; do
[[ -n "$cand" ]] || continue
if is_macho_dylib "$cand"; then
printf '%s\n' "$cand"
return 0
fi
done < <(find "$SCRIPT_DIR/.theos" -name "$name" -type f ! -path '*.dSYM/*' 2>/dev/null)
fi
return 1
}
compile_theta_nse_clang() {
local src="$SCRIPT_DIR/Source/SideloadNSE/ThetaNSE.m"
local out="$SCRIPT_DIR/.theos/obj/ThetaNSE.dylib"
local sdk
[[ -f "$src" ]] || return 1
mkdir -p "$(dirname "$out")"
sdk="$(xcrun --sdk iphoneos --show-sdk-path 2>/dev/null || true)"
[[ -n "$sdk" ]] || return 1
echo "[Build] Compiling ThetaNSE.dylib with clang..."
xcrun -sdk iphoneos clang -arch arm64 -isysroot "$sdk" \
-miphoneos-version-min=14.0 -DTHETA_DEBUG="$THETA_DEBUG" \
-fobjc-arc -fPIC -shared -fvisibility=hidden \
-I"$SCRIPT_DIR" \
-framework Foundation -framework Security \
-o "$out" "$src" "$SCRIPT_DIR/Source/SideloadNSE/ThetaHPKEKeyFile.m" "$SCRIPT_DIR/fishhook.c"
}
# Instagram encrypts APNs; InstagramNotificationExtension decrypts them.
# Inject the tiny ThetaNSE remap dylib (full Theta.dylib is too big for NSE jetsam).
inject_theta_nse() {
local app_dir="$1"
local nse_src="$2"
local plugins="$app_dir/PlugIns"
local injected=0
if [[ ! -f "$nse_src" ]]; then
echo "[Build] ERROR: ThetaNSE.dylib not found at $nse_src"
exit 1
fi
if [[ ! -d "$plugins" ]]; then
echo "[Build] WARNING: no PlugIns — rich notifications will stay generic"
return 0
fi
# Resigners sign *.framework under Frameworks/; they often skip loose
# .dylib files and then the NSE dies on a required LC_LOAD (no logs).
# The NSE already has rpath @executable_path/Frameworks and ../../Frameworks.
local fw_id="@rpath/ThetaNSE.framework/ThetaNSE"
local appex plist point exe bin patched
while IFS= read -r -d '' appex; do
plist="$appex/Info.plist"
[[ -f "$plist" ]] || continue
point="$(plist_print "$plist" ":NSExtension:NSExtensionPointIdentifier")"
[[ "$point" == "com.apple.usernotifications.service" ]] || continue
exe="$(plist_print "$plist" ":CFBundleExecutable")"
if [[ -z "$exe" ]]; then
exe="$(basename "$appex" .appex)"
fi
bin="$appex/$exe"
if [[ ! -f "$bin" ]]; then
echo "[Build] WARNING: missing NSE binary $bin"
continue
fi
mkdir -p "$appex/Frameworks"
stage_theta_nse_framework "$nse_src" "$appex/Frameworks/ThetaNSE.framework" "$fw_id"
local old
for old in \
"@executable_path/../../ThetaNSE.dylib" \
"@executable_path/ThetaNSE.dylib"
do
if binary_already_loads "$bin" "$old"; then
echo "[Build] Rewriting NSE load path $old → $fw_id"
install_name_tool -change "$old" "$fw_id" "$bin" 2>/dev/null || true
fi
done
if ! binary_already_loads "$bin" "ThetaNSE.framework"; then
patched="$appex/${exe}_patched"
cp -f "$bin" "$patched"
echo "[Build] Injecting $fw_id (weak) into $(basename "$appex")..."
if ! "$SCRIPT_DIR/tools/insert_dylib" --weak "$fw_id" "$patched" --all-yes --inplace; then
rm -f "$patched"
echo "[Build] ERROR: insert_dylib failed for $(basename "$appex")"
exit 1
fi
rm -f "$bin"
cp -f "$patched" "$bin"
rm -f "$patched"
chmod +x "$bin"
fi
strip_entitlements "$bin"
if ! binary_already_loads "$bin" "ThetaNSE.framework"; then
echo "[Build] ERROR: $(basename "$appex") does not list ThetaNSE.framework after inject"
exit 1
fi
injected=1
done < <(find "$plugins" -maxdepth 1 -type d -name "*.appex" -print0)
if [[ "$injected" -eq 0 ]]; then
echo "[Build] ERROR: no com.apple.usernotifications.service extension found"
echo "[Build] Keep PlugIns/InstagramNotificationExtension.appex in the IPA"
exit 1
fi
}
stage_theta_nse_framework() {
local src="$1" dest="$2" id_path="$3"
mkdir -p "$dest"
install_and_verify_dylib "$src" "$dest/ThetaNSE" "$id_path"
cat > "$dest/Info.plist" <<'PLIST'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>ThetaNSE</string>
<key>CFBundleIdentifier</key>
<string>com.theta.nse</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>ThetaNSE</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>MinimumOSVersion</key>
<string>14.0</string>
</dict>
</plist>
PLIST
}
ensure_insert_dylib() {
local src="$SCRIPT_DIR/tools/insert_dylib.c"
local bin="$SCRIPT_DIR/tools/insert_dylib"
if [[ ! -f "$src" ]]; then
echo "[Build] ERROR: missing $src"
exit 1
fi
if [[ ! -x "$bin" || "$src" -nt "$bin" ]]; then
echo "[Build] Compiling insert_dylib..."
cc -O2 -o "$bin" "$src"
fi
}
resolve_app_binary() {
# Prints: <app_dir>|<binary_basename>
local payload_dir="$1"
local app_dir binary_name input_bin plist
app_dir="$(find "$payload_dir" -maxdepth 1 -type d -name "*.app" | head -1 || true)"
if [[ -z "${app_dir}" ]]; then
echo "[Build] ERROR: no .app found under $payload_dir" >&2
return 1
fi
binary_name="$(basename "$app_dir")"
binary_name="${binary_name%.app}"
plist="$app_dir/Info.plist"
if [[ -f "$plist" ]]; then
local exe
exe="$(plist_print "$plist" ":CFBundleExecutable")"
if [[ -n "${exe}" ]]; then
binary_name="$exe"
fi
fi
input_bin="$app_dir/$binary_name"
if [[ ! -f "$input_bin" ]]; then
input_bin="$(find "$app_dir" -maxdepth 1 -type f -perm -111 ! -name '.*' | head -1 || true)"
fi
if [[ -z "${input_bin}" || ! -f "$input_bin" ]]; then
echo "[Build] ERROR: could not find main binary inside $(basename "$app_dir")" >&2
return 1
fi
binary_name="$(basename "$input_bin")"
printf '%s|%s\n' "$app_dir" "$binary_name"
}
build_sideload() {
local input_payload="$SCRIPT_DIR/input/Payload"
local output_dir="$SCRIPT_DIR/output"
local output_payload="$output_dir/Payload"
local ipa_out="$output_dir/Instagram_patched.ipa"
local app_dir="" app_name="" binary_name="" out_app="" out_bin="" patched="" dylib_src="" nse_src=""
if [[ ! -d "$input_payload" ]]; then
echo "[Build] ERROR: missing input/Payload"
echo "[Build] Unpack a decrypted Instagram IPA so you have:"
echo "[Build] input/Payload/Instagram.app/Instagram"
exit 1
fi
local resolved
resolved="$(resolve_app_binary "$input_payload")" || exit 1
app_dir="${resolved%%|*}"
binary_name="${resolved##*|}"
app_name="$(basename "$app_dir")"
echo "[Build] App: $app_name binary: $binary_name"
echo "[Build] Building dylibs..."
$make_cmd clean
$make_cmd THETA_DEBUG="$THETA_DEBUG"
local dylib_src nse_src
dylib_src="$(find_built_dylib Theta.dylib)" || {
echo "[Build] ERROR: Theta.dylib not found after build"
exit 1
}
nse_src="$(find_built_dylib ThetaNSE.dylib || true)"
if [[ -z "$nse_src" ]]; then
compile_theta_nse_clang || {
echo "[Build] ERROR: ThetaNSE.dylib not found after build"
exit 1
}
nse_src="$(find_built_dylib ThetaNSE.dylib)" || {
echo "[Build] ERROR: ThetaNSE.dylib not found after clang fallback"
exit 1
}
fi
ensure_insert_dylib
echo "[Build] Preparing output/Payload from input/Payload..."
rm -rf "$output_dir"
mkdir -p "$output_dir"
cp -f -R "$input_payload" "$output_payload"
out_app="$output_payload/$app_name"
out_bin="$out_app/$binary_name"
patched="$output_dir/${binary_name}_patched"
if [[ ! -f "$out_bin" ]]; then
echo "[Build] ERROR: missing $out_bin"
exit 1
fi
cp -f "$out_bin" "$patched"
echo "[Build] Injecting @executable_path/Theta.dylib into ${binary_name}..."
"$SCRIPT_DIR/tools/insert_dylib" "@executable_path/Theta.dylib" "$patched" --all-yes --inplace
rm -f "$out_bin"
cp -f "$patched" "$out_bin"
chmod +x "$out_bin"
strip_entitlements "$out_bin"
echo "[Build] Installing Theta.dylib into app root..."
install_and_verify_dylib "$dylib_src" "$out_app/Theta.dylib" \
"@executable_path/Theta.dylib"
inject_theta_nse "$out_app" "$nse_src"
# Optional resources / ffmpeg (best-effort)
if [[ -d "$SCRIPT_DIR/ThetaResources.bundle" ]]; then
rm -rf "$out_app/ThetaResources.bundle"
cp -f -R "$SCRIPT_DIR/ThetaResources.bundle" "$out_app/ThetaResources.bundle"
fi
local ffmpeg_src="$SCRIPT_DIR/vendor/ffmpeg.framework"
if [[ -d "$ffmpeg_src" ]]; then
echo "[Build] Embedding ffmpeg.framework..."
rm -rf "$out_app/ffmpeg.framework"
# Follow symlink if present
cp -f -R "$ffmpeg_src" "$out_app/ffmpeg.framework"
# Instagram ships its own stripped libavcodec/libavutil under Frameworks/, with the same
# @rpath install names ours use. Without this, dyld binds our libavformat to Instagram's
# copies and the AV1 transcode path never loads. Do NOT "fix" that by deleting the copies
# under Frameworks/ — they belong to Instagram.
python3 "$SCRIPT_DIR/scripts/isolate-ffmpeg-dylibs.py" "$out_app/ffmpeg.framework" \
| sed 's/^/[Build] /'
# The vendored dylibs ship unsigned. Ad-hoc sign them so a re-signer that only re-signs
# already-signed Mach-Os still picks them up.
local ffmpeg_dylib
for ffmpeg_dylib in "$out_app/ffmpeg.framework/dylibs/"*.dylib; do
strip_entitlements "$ffmpeg_dylib"
done
fi
# Housekeeping
find "$output_payload" -name ".DS_Store" -delete 2>/dev/null || true
xattr -rc "$output_payload" 2>/dev/null || true
echo "[Build] Zipping IPA..."
rm -f "$ipa_out"
(
cd "$output_dir"
zip -9 -r "Instagram_patched.ipa" Payload
)
echo "[Build] Sideload IPA ready:"
echo " $ipa_out"
ls -lh "$ipa_out"
}
build_sideload