forked from newbit1/rootAVD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rootAVD.sh
executable file
·2819 lines (2423 loc) · 88.3 KB
/
rootAVD.sh
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
##########################################################################################
#
# Magisk Boot Image Patcher - original created by topjohnwu and modded by shakalaca's
# modded by NewBit XDA for Android Studio AVD
##########################################################################################
###################
# Helper Functions
###################
# Copied 1 to 1 from topjohnwu
# This code block seems to be a generic utility function for extracting the parent directory of a given path. It does not contain any inherently malicious functionality.
getdir() {
case "$1" in
*/*) dir=${1%/*}; [ -z $dir ] && echo "/" || echo $dir ;;
*) echo "." ;;
esac
}
# This code block appears to be responsible for retrieving and setting various flags and variables related to the system configuration and state. It doesn't contain any obvious malicious code, but its purpose and potential implications depend on the context in which it is used within the overall script or application.
get_flags() {
echo "[-] Get Flags"
if [ -f /system/init -o -L /system/init ]; then
SYSTEM_ROOT=true
else
SYSTEM_ROOT=false
grep ' / ' /proc/mounts | grep -qv 'rootfs' || grep -q ' /system_root ' /proc/mounts && SYSTEM_ROOT=true
fi
if [ -z $KEEPVERITY ]; then
if $SYSTEM_ROOT; then
KEEPVERITY=true
echo "[*] System-as-root, keep dm/avb-verity"
else
KEEPVERITY=false
fi
fi
ISENCRYPTED=false
grep ' /data ' /proc/mounts | grep -q 'dm-' && ISENCRYPTED=true
[ "$(getprop ro.crypto.state)" = "encrypted" ] && ISENCRYPTED=true
if [ -z $KEEPFORCEENCRYPT ]; then
# No data access means unable to decrypt in recovery
if $ISENCRYPTED || ! $DATA; then
KEEPFORCEENCRYPT=true
echo "[-] Encrypted data, keep forceencrypt"
else
KEEPFORCEENCRYPT=false
fi
fi
RECOVERYMODE=false
if [[ $API -eq 28 ]]; then
RECOVERYMODE=true
fi
export RECOVERYMODE
export KEEPVERITY
export KEEPFORCEENCRYPT
echo "[*] RECOVERYMODE=$RECOVERYMODE"
echo "[-] KEEPVERITY=$KEEPVERITY"
echo "[*] KEEPFORCEENCRYPT=$KEEPFORCEENCRYPT"
}
# This code block seems to be responsible for copying files from specific directories to the base directory ($BASEDIR), renaming some of the files, and setting the permissions. It doesn't contain any obvious malicious code, but its purpose and potential implications depend on the context in which it is used within the overall script or application.
copyARCHfiles() {
BINDIR=$BASEDIR/lib/$ABI
ASSETSDIR=$BASEDIR/assets
STUBAPK=false
if [ -e $BINDIR/libstub.so ]; then
ABI=$ARCH32
BINDIR=$BASEDIR/lib/$ABI
echo "[*] No 64-Bit Binarys found, please consider Magisk Alpha"
elif $IS64BIT && ! $IS64BITONLY; then
echo "[*] copy $ARCH32 files to $BINDIR"
cp $BASEDIR/lib/$ARCH32/lib*32.so $BINDIR 2>/dev/null
fi
cd $BINDIR
for file in lib*.so; do mv "$file" "${file:3:${#file}-6}"; done
cd $BASEDIR
echo "[-] copy all $ABI files from $BINDIR to $BASEDIR"
cp $BINDIR/* $BASEDIR 2>/dev/null
if [ -e $ASSETSDIR/stub.apk ]; then
echo "[-] copy 'stub.apk' from $ASSETSDIR to $BASEDIR"
cp $ASSETSDIR/stub.apk $BASEDIR 2>/dev/null
STUBAPK=true
fi
chmod -R 755 $BASEDIR
export STUBAPK
}
# This code block seems to be responsible for detecting the device's architecture, API level, and Android version, and setting the corresponding variables. It doesn't contain any obvious malicious code, but its purpose and potential implications depend on the context in which it is used within the overall script or application.
api_level_arch_detect() {
echo "[-] Api Level Arch Detect"
# Detect version and architecture
# To select the right files for the patching
ABI=$(getprop ro.product.cpu.abi)
ABILIST32=$(getprop ro.product.cpu.abilist32)
ABILIST64=$(getprop ro.product.cpu.abilist64)
API=$(getprop ro.build.version.sdk)
FIRSTAPI=$(getprop ro.product.first_api_level)
AVERSION=$(getprop ro.build.version.release)
IS64BIT=false
IS64BITONLY=false
IS32BITONLY=false
if [ "$ABI" = "x86" ]; then
ARCH=x86
ARCH32=x86
elif [ "$ABI" = "arm64-v8a" ]; then
ARCH=arm64
ARCH32=armeabi-v7a
IS64BIT=true
elif [ "$ABI" = "x86_64" ]; then
ARCH=x64
ARCH32=x86
IS64BIT=true
else
ARCH=arm
ABI=armeabi-v7a
ABI32=armeabi-v7a
IS64BIT=false
fi
if [ -z "$ABILIST32" ]; then
IS64BITONLY=true
fi
if [ -z "$ABILIST64" ]; then
IS32BITONLY=true
fi
if $IS64BITONLY || $IS32BITONLY ; then
echo "[-] Device Platform is $ARCH only"
else
echo "[-] Device Platform: $ARCH"
echo "[-] ARCH32 $ARCH32"
fi
echo "[-] Device SDK API: $API"
echo "[-] First API Level: $FIRSTAPI"
echo "[-] The AVD runs on Android $AVERSION"
[ -d /system/lib64 ] && IS64BIT=true || IS64BIT=false
export ARCH
export ARCH32
export IS64BIT
export IS64BITONLY
export IS32BITONLY
export ABI
export API
export FIRSTAPI
export AVERSION
}
# This code block provides a mechanism to abort the script when necessary. It doesn't contain any malicious code and simply exits the script when invoked.
abort_script() {
echo "[!] aborting the script"
exit
}
# This code block is used to determine the compression method of a file based on its first 8 bytes. It checks if the first 8 bytes match specific patterns for the LZ4 or Gzip compression methods and sets the ENDG variable accordingly. The value of ENDG indicates the file extension associated with the compression method (".lz4" for LZ4 or ".gz" for Gzip). This function doesn't contain any obvious malicious code, but its purpose and potential implications depend on the context in which it is used within the overall script or application.
compression_method() {
local FILE="$1"
local FIRSTFILEBYTES
local METHOD_LZ4="02214c18"
local METHOD_GZ="1f8b0800"
local ENDG=""
FIRSTFILEBYTES=$(xxd -p -c8 -l8 "$FILE")
FIRSTFILEBYTES="${FIRSTFILEBYTES:0:8}"
if [ "$FIRSTFILEBYTES" == "$METHOD_LZ4" ]; then
ENDG=".lz4"
elif [ "$FIRSTFILEBYTES" == "$METHOD_GZ" ]; then
ENDG=".gz"
fi
echo "$ENDG"
}
# This code block is used to detect the compression method used for a ramdisk image file (ramdisk.img). It checks the first 8 bytes of the file and determines if it matches the patterns for the LZ4 or Gzip compression methods. It then sets the appropriate variables (ENDG, METHOD, RAMDISK_LZ4, RAMDISK_GZ, and COMPRESS_SIGN) based on the detected compression method. If the compression method is unknown, the script is aborted. This function doesn't contain any obvious malicious code, but its purpose and potential implications depend on the context in which it is used within the overall script or application.
detect_ramdisk_compression_method() {
echo "[*] Detecting ramdisk.img compression"
RDF=$BASEDIR/ramdisk.img
CPIO=$BASEDIR/ramdisk.cpio
CPIOORIG=$BASEDIR/ramdisk.cpio.orig
local FIRSTFILEBYTES
local METHOD_LZ4="02214c18"
local METHOD_GZ="1f8b0800"
COMPRESS_SIGN=""
FIRSTFILEBYTES=$(xxd -p -c8 -l8 "$RDF")
FIRSTFILEBYTES="${FIRSTFILEBYTES:0:8}"
RAMDISK_LZ4=false
RAMDISK_GZ=false
ENDG=""
METHOD=""
if [ "$FIRSTFILEBYTES" == "$METHOD_LZ4" ]; then
ENDG=".lz4"
METHOD="lz4_legacy"
RAMDISK_LZ4=true
mv $RDF $RDF$ENDG
RDF=$RDF$ENDG
COMPRESS_SIGN="$METHOD_LZ4"
elif [ "$FIRSTFILEBYTES" == "$METHOD_GZ" ]; then
ENDG=".gz"
METHOD="gzip"
RAMDISK_GZ=true
mv $RDF $RDF$ENDG
#cp $RDF $RDF$ENDG
COMPRESS_SIGN="$METHOD_GZ"
fi
if [ "$ENDG" == "" ]; then
echo "[!] Ramdisk.img uses UNKNOWN compression $FIRSTFILEBYTES"
abort_script
fi
echo "[!] Ramdisk.img uses $METHOD compression"
}
# This code block is used to run Magisk for installing or patching a fake boot image. It stops the target application, launches Magisk, prompts the user to install or patch a specific file ($FBI), and waits for the user to press Enter. The user's input is read with a timeout of 60 seconds. The code block itself doesn't contain any obvious malicious code, but its purpose and potential implications depend on the context in which it is used within the overall script or application.
runMagisk_to_Patch_fake_boot_img() {
am force-stop $PKG_NAME
echo "[-] Starting Magisk"
monkey -p $PKG_NAME -c android.intent.category.LAUNCHER 1 > /dev/null 2>&1
echo "[*] Install/Patch $FBI and hit Enter when done(max. 60s)"
read -t 60 proceed
case $proceed in
*)
;;
esac
}
# This code block is used to detect the current user and switch to user 0 if the current user ID is different from 0. The specific purpose and implications of this user detection and switching depend on the context in which it is used within the overall script or application.
detecting_users() {
local userID=""
local userZero=0
echo "[*] Detecting current user"
userID=$(am get-current-user)
echo "[-] Current user $userID"
if [ "$userID" != "$userZero" ]; then
echo "[-] Switching to user $userZero"
am switch-user $userZero
userID=$(am get-current-user)
echo "[-] Current user $userID"
fi
}
# This code block generates a build.prop file by extracting system properties using getprop and modifies it using sed. It also generates a recovery.fstab file by copying from an existing file. Finally, it adds the build.prop and recovery.fstab files to a stock ramdisk using magiskboot. The purpose and implications of these operations depend on the specific context in which this function is used within the overall script or application.
generate_build_prop() {
echo "[*] generating Build.prop"
local BPR=$BASEDIR/build.prop
local recfstab=$BASEDIR/recovery.fstab
getprop > $BPR
sed -i -e 's/: /=/g' -e 's/\[//g' -e 's/\]//g' $BPR
echo "[*] generating recovery.fstab from fstab.ranchu"
cp /system/vendor/etc/fstab.ranchu $recfstab
echo "[-] adding Build.prop and recovery.fstab to Stock Ramdisk"
$BASEDIR/magiskboot cpio $CPIO \
"add 0644 system/build.prop build.prop" \
"add 0644 system/etc/recovery.fstab recovery.fstab"
#exit
#BASEDIR=$(pwd)
}
# This code block is responsible for creating a fake boot image by generating a minimal header and repacking the ramdisk image. It also involves invoking various functions related to installing Magisk, detecting users, and running Magisk to patch the fake boot image. The purpose and implications of these operations depend on the specific context in which this function is used within the overall script or application.
create_fake_boot_img() {
echo "[*] Creating a fake Boot.img"
FBHI=$BASEDIR/fakebootheader.img
FBI=$SDCARD/fakeboot.img
echo "[-] removing old $FBI"
rm -f $FBI $RDF
printf "\x41\x4E\x44\x52\x4F\x49\x44\x21" > $FBHI # ANDROID!
printf "\x00\x00\x00\x00\x00\x00\x00\x00" >> $FBHI
printf "\x00\x00\x00\x00\x00\x00\x00\x00" >> $FBHI
printf "\x00\x00\x00\x00\x00\x00\x00\x00" >> $FBHI
printf "\x00\x00\x00\x00\x00\x08\x00\x00" >> $FBHI # 00080000 (Pagesize 2048)
echo "[!] Only a minimal header is required for Magisk to repack the ramdisk"
#mv $RDF $CPIO
if $DEBUG; then
generate_build_prop
fi
echo "[*] repacking ramdisk.img into $FBI"
$BASEDIR/magiskboot repack $FBHI $FBI > /dev/null 2>&1
InstallMagiskTemporarily
detecting_users
runMagisk_to_Patch_fake_boot_img
RemoveTemporarilyMagisk
}
# This function is responsible for unpacking the patched ramdisk from a previously created fake boot image. It searches for the latest magisk_patched file in the specified directory, unpacks the ramdisk using magiskboot, and then deletes all the magisk_patched files.
unpack_patched_ramdisk_from_fake_boot_img() {
if [ "$MagiskPatchedFiles" != "" ]; then
echo "[!] magisk_patched file(s) could be found!"
for file in `ls -tu $SDCARD/*magisk_patched*`; do
MagiskPatched=$file
break
done
echo "[*] unpacking latest $MagiskPatched"
$BASEDIR/magiskboot unpack $MagiskPatched > /dev/null 2>&1
echo "[-] deleting all magisk_patched files"
for file in `ls -tu $SDCARD/*magisk_patched*`; do
rm -f $file
done
else
echo "[!] No magisk_patched file could be found!"
abort_script
fi
}
# This function is responsible for processing the fake boot image. It checks if there are external magisk_patched files available and unpacks the ramdisk from the latest file. If no external files are found, it creates a fake boot image, checks for magisk_patched files again, and unpacks the ramdisk if found.
process_fake_boot_img() {
SDCARD=/sdcard/Download
echo "[*] Processing fake Boot.img"
MagiskPatchedFiles=$(ls "$SDCARD"/*magisk_patched*) > /dev/null 2>&1
if [ "$MagiskPatchedFiles" != "" ]; then
echo "[!] external magisk_patched file(s) could be found!"
unpack_patched_ramdisk_from_fake_boot_img
else
create_fake_boot_img
MagiskPatchedFiles=$(ls "$SDCARD"/*magisk_patched*) > /dev/null 2>&1
unpack_patched_ramdisk_from_fake_boot_img
fi
}
# This function is responsible for constructing the environment required for the script. If the script is running as root, it sets up the necessary directories and files, changes permissions, and reboots the system. If the script is not running as root, it displays an error message and suggests verifying root access.
# requires additional setup
# EnvFixTask
construct_environment() {
ROOT=`su -c "id -u"` 2>/dev/null
if [[ "$ROOT" == "" ]]; then
ROOT=$(id -u)
fi
echo "[-] Constructing environment - PAY ATTENTION to the AVDs Screen"
if [[ $ROOT -eq 0 ]]; then
echo "[!] we are root"
local BBBIN=$BB
local COMMONDIR=$BASEDIR/assets
local NVBASE=/data/adb
local MAGISKBIN=$NVBASE/magisk
`su -c "rm -rf $MAGISKBIN/* 2>/dev/null && \
mkdir -p $MAGISKBIN 2>/dev/null && \
cp -af $BINDIR/. $COMMONDIR/. $BBBIN $MAGISKBIN && \
chown root.root -R $MAGISKBIN && \
chmod -R 755 $MAGISKBIN && \
rm -rf $BASEDIR 2>/dev/null && \
reboot \
"`
fi
echo "[!] not root yet"
echo "[!] Couldn't construct environment"
echo "[!] Double Check Root Access"
echo "[!] Re-Run Script with clean ramdisk.img and try again"
abort_script
}
# The function can be used to validate a file's existence, readability, size, writability, and regularity.
checkfile() {
#echo "checkfile $1"
if [ -r "$1" ]; then
#echo "File exists and is readable"
if [ -s "$1" ]; then
#echo "and has a size greater than zero"
if [ -w "$1" ]; then
#echo "and is writable"
if [ -f "$1" ]; then
#echo "and is a regular file."
return 1
fi
fi
fi
fi
return 0
}
# The function allows for the installation of multiple APK files in the Apps folder, and it handles the scenario where an incompatible version of an app is already installed.
# If all is done well so far, you can install some APK's to the AVD
# every APK file in the Apps DIR will be (re)installed
# Like magisk.apk etc.
install_apps() {
local ADBECHO=""
APPS="Apps/*"
echo "[-] Install all APKs placed in the Apps folder"
FILES=$APPS
for f in $FILES; do
echo "[*] Trying to install $f"
ADBECHO=""
while [[ "$ADBECHO" != *"Success"* ]]; do
ADBECHO=$(adb install -r -d "$f" 2>&1)
if [[ "$ADBECHO" == *"INSTALL_FAILED_UPDATE_INCOMPATIBLE"* ]]; then
echo "$ADBECHO" | while read I; do echo "[*] $I"; done
Package=
for I in $ADBECHO; do
if [[ "$Package" == *"Package"* ]]; then
echo "[*] Need to uninstall $I first"
ADBECHO=$(adb uninstall $I 2>&1)
echo "$ADBECHO" | while read I; do echo "[*] $I"; done
ADBECHO=$(adb install -r -d "$f" 2>&1)
break
fi
Package=$I
done
fi
done
echo "$ADBECHO" | while read I; do echo "[*] $I"; done
done
}
# The function ensures that a backup file is only created if it doesn't already exist. This helps prevent overwriting existing backup files and allows for preserving the original file state before making any modifications.
create_backup() {
local FILE=""
local FILEPATH=""
local FILENAME=""
local BACKUPFILE=""
FILE="$1"
FILEPATH=${FILE%/*}
FILENAME=${FILE##*/}
BACKUPFILE="$FILENAME.backup"
# If no backup file exist, create one
if ( checkfile $FILEPATH/$BACKUPFILE -eq 0 ); then
echo "[*] create Backup File of $FILENAME"
cp $FILEPATH/$FILENAME $FILEPATH/$BACKUPFILE
#ls -l $($FILEPATH/$FILENAME)
else
echo "[-] $FILENAME Backup exists already"
fi
}
# The function is designed to simplify the process of pushing files from the local machine to the AVD using the adb tool.
pushtoAVD() {
local SRC=""
local DST="$2"
local ADBPUSHECHO=""
SRC=${1##*/}
if [[ "$DST" == "" ]]; then
echo "[*] Push $SRC into $ADBBASEDIR"
ADBPUSHECHO=$(adb push $1 $ADBBASEDIR 2>/dev/null)
else
echo "[*] Push $SRC into $ADBBASEDIR/$DST"
ADBPUSHECHO=$(adb push $1 $ADBBASEDIR/$DST 2>/dev/null)
fi
echo "[-] $ADBPUSHECHO"
}
# The function simplifies the process of pulling files from the AVD to the local machine using the adb tool.
pullfromAVD() {
local SRC=""
local DST=""
local ADBPULLECHO=""
SRC=${1##*/}
DST=${2##*/}
ADBPULLECHO=$(adb pull $ADBBASEDIR/$SRC $2 2>/dev/null)
if [[ ! "$ADBPULLECHO" == *"error"* ]]; then
echo "[*] Pull $SRC into $DST"
echo "[-] $ADBPULLECHO"
fi
}
# The function simplifies the process of restoring backed-up files by automatically iterating through the backup files in a directory and replacing the corresponding original files with their backups.
restore_backups() {
local BACKUPFILE=""
local RESTOREFILE=""
for f in $1/*.backup; do
BACKUPFILE="$f"
RESTOREFILE="${BACKUPFILE%.backup}"
echo "[!] Restoring ${BACKUPFILE##*/} to ${RESTOREFILE##*/}"
cp $BACKUPFILE $RESTOREFILE
done
echo "[*] Backups still remain in place"
exit 0
}
# The function provides a convenient way to switch between the original and patched versions of the ramdisk file by renaming the files accordingly.
toggle_Ramdisk() {
#AVDPATHWITHRDFFILE="$1"
#AVDPATH=${AVDPATHWITHRDFFILE%/*}
#RDFFILE=${AVDPATHWITHRDFFILE##*/}
#RESTOREPATH=$AVDPATH
local RamdiskFile="$AVDPATHWITHRDFFILE"
local PatchedFile="$AVDPATHWITHRDFFILE.patched"
local BackupFile="$AVDPATHWITHRDFFILE.backup"
local hasBackup=false
local hasPatched=false
if ( checkfile $BackupFile -eq 0 ); then
echo "[!] we need a valid backup file to proceed"
exit 0
fi
echo "[-] Toggle Ramdisk"
if ( checkfile $PatchedFile -eq 0 ); then
echo "[*] Pushing patched Ramdisk into Stack"
mv $RamdiskFile $PatchedFile
echo "[*] Popping original Ramdisk from Backup"
cp $BackupFile $RamdiskFile
else
echo "[*] Popping patched Ramdisk back from Stack"
mv -f $PatchedFile $RamdiskFile
fi
exit 0
}
# The function helps ensure that ADB is properly set up and functional before proceeding with further operations.
TestADB() {
local HOME=~/
local ADB_DIR_M=Library/Android/sdk/platform-tools
local ADB_DIR_L=Android/Sdk/platform-tools
local ADB_DIR=""
local ADB_EX=""
echo "[-] Test if ADB SHELL is working"
ADBWORKS=$(which adb)
if [ "$ADBWORKS" == *"not found"* ] || [ "$ADBWORKS" == "" ]; then
if [ -d "$HOME$ADB_DIR_M" ]; then
ADB_DIR=$ADB_DIR_M
elif [ -d "$HOME$ADB_DIR_L" ]; then
ADB_DIR=$ADB_DIR_L
else
echo "[!] ADB not found, please install and add it to your \$PATH"
exit
fi
cd $HOME > /dev/null
for adb in $(find $ADB_DIR -type f -name adb); do
ADB_EX="~/$adb"
done
cd - > /dev/null
if [[ $ADB_EX == "" ]]; then
echo "[!] ADB binary not found in ~/$ADB_DIR"
exit
fi
echo "[!] ADB is not in your Path, try to"
echo "export PATH=~/$ADB_DIR:\$PATH"
echo ""
exit
fi
ADBWORKS=$(adb shell 'echo true' 2>/dev/null)
if [ -z "$ADBWORKS" ]; then
echo "no ADB connection possible"
exit
fi
}
# The purpose of this function is to make BlueStacks writable by changing the type of the Root.vdi file from "Readonly" to "Normal" in the AVBox configuration.
MakeBlueStacksRW() {
if ( checkfile $BLUESTACKSPATH/$AVBOXFILE -eq 0 ); then
echo "[!] $AVBOXFILE not found"
echo "[!] check your BlueStacks installation"
exit 0
fi
echo "[!] $AVBOXFILE found"
create_backup $AVBOX
echo "[*] Changing $ROOTVDIFILE type to \"Normal\""
sed 's,location="Root.vdi" format="VDI" type="Readonly",location="Root.vdi" format="VDI" type="Normal",' $AVBOX > $AVBOX".edit"
mv $AVBOX".edit" $AVBOX
}
# The purpose of this function is to initiate the shut down and reboot process for the AVD or BlueStacks, and it provides instructions and information related to rooting and using Magisk with the respective environments.
ShutDownAVD() {
if ( "$BLUESTACKS" ); then
echo "[-] Shut-Down & Reboot BlueStacks and see if it worked"
echo "[-] Root and Su with Magisk for BlueStacks"
APPNAME=BlueStacks.app
if [ $(ps aux | grep -v grep | grep -c $APPNAME) -gt 0 ]; then
echo "[-] Trying to shut down BlueStacks"
pkill -x BlueStacks
if [ "$?" == "0" ]; then
echo "[*] Shut down Signal were send"
fi
echo "[!] If BlueStacks doesn't shut down, try it manually!"
fi
echo "[*] If BlueStacks Home Screen is closing, run Magisk from the Terminal and hide it"
echo "adb shell monkey -p com.topjohnwu.magisk -c android.intent.category.LAUNCHER 1"
else
echo "[-] Shut-Down & Reboot (Cold Boot Now) the AVD and see if it worked"
echo "[-] Root and Su with Magisk for Android Studio AVDs"
ADBPULLECHO=$(adb shell setprop sys.powerctl shutdown 2>/dev/null)
if [[ ! "$ADBPULLECHO" == *"error"* ]]; then
echo "[-] Trying to shut down the AVD"
fi
echo "[!] If the AVD doesn't shut down, try it manually!"
fi
echo "[-] Modded by NewBit XDA - Jan. 2021"
echo "[!] Huge Credits and big Thanks to topjohnwu, shakalaca, vvb2060 and HuskyDG"
}
# This function primarily focuses on copying Magisk-related files to the AVD or BlueStacks environment, executing the rootAVD.sh script, and performing additional operations based on the specified flags and conditions.
CopyMagiskToAVD() {
# Set Folders and FileNames
echo "[*] Set Directorys"
if ( "$BLUESTACKS" ); then
# BlueStacks has its ramdisk.img within, no AVD Path needed
# but the VBOX container Root.vdi should be backuped
BLUESTACKSROOTVDIFILE=~/Library/BlueStacks/Android/Root.vdi
AVBOX=~/Library/BlueStacks/Android/Android.vbox
AVBOXFILE=${AVBOX##*/}
BLUESTACKSPATH=${BLUESTACKSROOTVDIFILE%/*}
ROOTVDIFILE=${BLUESTACKSROOTVDIFILE##*/}
RESTOREPATH=$BLUESTACKSPATH
else
AVDPATHWITHRDFFILE="$1"
AVDPATH=${AVDPATHWITHRDFFILE%/*}
RDFFILE=${AVDPATHWITHRDFFILE##*/}
RESTOREPATH=$AVDPATH
fi
if ( "$restore" ); then
restore_backups $RESTOREPATH
fi
if ( "$toggleRamdisk" ); then
toggle_Ramdisk $RESTOREPATH
fi
if ( "$BLUESTACKS" ); then
if ( checkfile $BLUESTACKSPATH/$ROOTVDIFILE -eq 0 ); then
echo "[!] $ROOTVDIFILE not found"
echo "[!] check your BlueStacks installation"
exit
fi
echo "[!] $ROOTVDIFILE found"
create_backup $BLUESTACKSROOTVDIFILE
MakeBlueStacksRW
fi
TestADB
# The Folder where the script was called from
ROOTAVD="`getdir "${BASH_SOURCE:-$0}"`"
MAGISKZIP=$ROOTAVD/Magisk.zip
# change to ROOTAVD directory
cd $ROOTAVD
# Kernel Names
BZFILE=$ROOTAVD/bzImage
KRFILE=kernel-ranchu
if ( "$InstallApps" ); then
install_apps
exit
fi
ADBWORKDIR=/data/data/com.android.shell
adb shell "cd $ADBWORKDIR" 2>/dev/null
if [ "$?" != "0" ]; then
echo "[!] $ADBWORKDIR doesn't exist, switching to tmp'"
ADBWORKDIR=/data/local/tmp
fi
ADBBASEDIR=$ADBWORKDIR/Magisk
echo "[-] In any AVD via ADB, you can execute code without root in $ADBWORKDIR"
echo "[*] Cleaning up the ADB working space"
adb shell rm -rf $ADBBASEDIR
echo "[*] Creating the ADB working space"
adb shell mkdir $ADBBASEDIR
# If Magisk.zip file doesn't exist, just ignore it
if ( ! checkfile $MAGISKZIP -eq 0 ); then
echo "[-] Magisk installer Zip exists already"
pushtoAVD $MAGISKZIP
fi
# Proceed with ramdisk
if "$RAMDISKIMG"; then
# Is it a ramdisk named img file?
if [[ "$RDFFILE" != ramdisk*.img ]]; then
echo "[!] please give a path to a ramdisk file"
exit
fi
create_backup $AVDPATHWITHRDFFILE
pushtoAVD $AVDPATHWITHRDFFILE "ramdisk.img"
if ( "$InstallKernelModules" ); then
INITRAMFS=$ROOTAVD/initramfs.img
if ( ! checkfile $INITRAMFS -eq 0 ); then
pushtoAVD $INITRAMFS
fi
fi
if ( "$AddRCscripts" ); then
for f in $ROOTAVD/*.rc; do
pushtoAVD $f
done
pushtoAVD $ROOTAVD/sbin
fi
fi
pushtoAVD "rootAVD.sh"
if ( "$UpdateBusyBoxScript" ); then
pushtoAVD "libbusybox*.so"
fi
echo "[-] run the actually Boot/Ramdisk/Kernel Image Patch Script"
echo "[*] from Magisk by topjohnwu and modded by NewBit XDA"
adb shell sh $ADBBASEDIR/rootAVD.sh $@
if [ "$?" == "0" ]; then
if ( "$UpdateBusyBoxScript" ); then
pullfromAVD "bbscript.sh" "rootAVD.sh"
chmod +x rootAVD.sh
exit
fi
if ( ! "$DEBUG" && "$BLUESTACKS" ); then
pullfromAVD "Magisk.apk" "Apps/"
pullfromAVD "Magisk.zip" $ROOTAVD
echo "[-] Clean up the ADB working space"
adb shell rm -rf $ADBBASEDIR
install_apps
ShutDownAVD
adb kill-server
fi
# In Debug-Mode we can skip parts of the script
if ( ! "$DEBUG" && "$RAMDISKIMG" ); then
pullfromAVD "ramdiskpatched4AVD.img" $AVDPATHWITHRDFFILE
pullfromAVD "Magisk.apk" "Apps/"
pullfromAVD "Magisk.zip" $ROOTAVD
if ( "$InstallPrebuiltKernelModules" ); then
pullfromAVD $BZFILE $ROOTAVD
InstallKernelModules=true
fi
if ( "$InstallKernelModules" ); then
if ( ! checkfile $BZFILE -eq 0 ); then
create_backup $AVDPATH/$KRFILE
echo "[*] Copy $BZFILE (Kernel) into kernel-ranchu"
cp $BZFILE $AVDPATH/$KRFILE
if [ "$?" == "0" ]; then
rm -f $BZFILE $INITRAMFS
fi
fi
fi
echo "[-] Clean up the ADB working space"
adb shell rm -rf $ADBBASEDIR
install_apps
ShutDownAVD
fi
fi
}
###################################################
# Method to extract specified field data from json
# Globals: None
# Arguments: 2
# ${1} - value of field to fetch from json
# ${2} - Optional, nth number of value from extracted values, by default shows all.
# Input: file | here string | pipe
# _json_value "Arguments" < file
# _json_value "Arguments <<< "${varibale}"
# echo something | _json_value "Arguments"
# Result: print extracted value
###################################################
# This function is used to extract a specified field value from a JSON string or file.
# Note: The function assumes that the variable $BB contains the path to the grep, sed, and awk binaries, which are typically used in BusyBox.
json_value() {
$BB grep -o "\"""${1}""\"\:.*" | $BB sed -e "s/.*\"""${1}""\": //" -e 's/[",]*$//' -e 's/["]*$//' -e 's/[,]*$//' -e "s/\"//" -n -e "${2}"p
}
# This function is used to check the internet connection status of an AVD (Android Virtual Device).
# Note: The function assumes that the variable $BB contains the path to the timeout and wget binaries, which are typically used in BusyBox.
CheckAVDIsOnline() {
if [ -z $AVDIsOnline ]; then
echo "[-] Checking AVDs Internet connection..."
AVDIsOnline=false
$BB timeout 3 $BB wget -q --spider --no-check-certificate http://github.com > /dev/null 2>&1
if [ $? -eq 0 ]; then
AVDIsOnline=true
fi
$AVDIsOnline && echo "[!] AVD is online" || echo "[!] AVD is offline"
fi
export AVDIsOnline
}
# This function is used to format a version string in a pretty way.
# Note: The function assumes that the variable $BB contains the path to the grep binary, which is typically used in BusyBox.
GetPrettyVer() {
if echo $1 | $BB grep -q '\.'; then
PRETTY_VER=$1
else
PRETTY_VER="$1($2)"
fi
echo "$PRETTY_VER"
}
# This function is used to download a file from a specified URL.
# Note: The function assumes that the variable $BASEDIR contains the base directory path, and the variable $BB contains the path to the wget and dd binaries, which are typically used in BusyBox.
DownLoadFile() {
CheckAVDIsOnline
if ("$AVDIsOnline"); then
local URL="$1"
local SRC="$2"
local DST="$3"
OF=$BASEDIR/download.tmp
rm -f $OF
BS=1024
CUTOFF=100
if [ "$DST" == "" ]; then
DST=$BASEDIR/$SRC
else
DST=$BASEDIR/$DST
fi
#echo "[*] Downloading File $SRC"
$BB wget -q -O $DST --no-check-certificate $URL$SRC
RESULT="$?"
while [ $RESULT != "0" ]
do
echo "[!] Error while downloading File $SRC"
echo "[-] patching it together"
FSIZE=$(./busybox stat $DST -c %s)
if [ $FSIZE -gt $BS ]; then
COUNT=$(( FSIZE/BS ))
if [ $COUNT -gt $CUTOFF ]; then
COUNT=$(( COUNT - $CUTOFF ))
fi
fi
$BB dd if=$DST count=$COUNT bs=$BS of=$OF > /dev/null 2>&1
mv -f $OF $DST
$BB wget -q -O $DST --no-check-certificate $URL$SRC -c
RESULT="$?"
done
echo "[!] Downloading File $SRC complete!"
fi
}
# This function is used to download the USB Host Permissions Module zip file if it is not already present.
# Note: The function assumes that the variable $BB contains the path to the wget binary, which is typically used in BusyBox.
GetUSBHPmod() {
USBHPZSDDL="/sdcard/Download/usbhostpermissons.zip"
USBHPZ="https://github.com/newbit1/usbhostpermissons/releases/download/v1.0/usbhostpermissons.zip"
if [ ! -e $USBHPZSDDL ]; then
echo "[*] Downloading USB HOST Permissions Module Zip"
$BB wget -q -O $USBHPZSDDL --no-check-certificate $USBHPZ
else
echo "[*] USB HOST Permissions Module Zip is already present"
fi
}
# This function is used to fetch and process data from a JSON file related to Magisk downloads.
# Note: The function assumes that the $BB variable contains the path to the wget binary, which is typically used in BusyBox.
FetchMagiskDLData() {
local SRCURL="$1"
local CHANNEL="$2"
local JSON="$CHANNEL.json"
local VER=""
local VER_CODE=""
local DLL=""
local i=1
rm -rf *.json > /dev/null 2>&1
$BB wget -q --no-check-certificate $SRCURL$JSON
VER=$(json_value "version" < $JSON)
VER_CODE=$(json_value "versionCode" 1 < $JSON)
DLL=$(json_value "link" 1 < $JSON)
VER=$(GetPrettyVer $VER $VER_CODE)
if ! echo $DLL | $BB grep -q 'https'; then
DLL=$SRCURL$DLL
fi
if [ -e $MAGISK_DL_LINKS ]; then
echo $DLL >> $MAGISK_DL_LINKS
echo $VER >> $MAGISK_VERSIONS
echo $CHANNEL >> $MAGISK_CHANNEL
i=$($BB sed -n '$=' $MAGISK_DL_LINKS)
echo "[$i] $CHANNEL $VER" >> $MAGISK_MENU
else
if [[ "$MAGISK_LOCL_VER" != "" ]]; then
echo "local" > $MAGISK_DL_LINKS
echo $MAGISK_LOCL_VER > $MAGISK_VERSIONS
echo "local "$CHANNEL > $MAGISK_CHANNEL
echo "[$i] local $CHANNEL $MAGISK_LOCL_VER (ENTER)" > $MAGISK_MENU
i=$((i+1))
fi
echo $DLL >> $MAGISK_DL_LINKS
echo $VER >> $MAGISK_VERSIONS
echo $CHANNEL >> $MAGISK_CHANNEL
if [[ "$i" == "1" ]]; then
echo "[$i] $CHANNEL $VER (ENTER)" >> $MAGISK_MENU
else
#echo $CHANNEL > $MAGISK_CHANNEL
echo "[$i] $CHANNEL $VER" >> $MAGISK_MENU
fi
fi
rm -rf *.json > /dev/null 2>&1
}
# This function is used to fetch and process commit data related to Magisk releases.
# Note: The function assumes that the $BB variable contains the path to the wget binary, which is typically used in BusyBox.
FetchMagiskRLCommits() {
#$GITHUB $TJWCOMMITSURL $TJWBLOBURL $CHANNEL $TJWREPOURL
local DOMAIN="$1"
local COMMITSURL="$2"
local BLOBURL="$3"
local CHANNEL="$4"
local JSON="$CHANNEL.json"
local REPOURL="$5"
local COMMITS=""
rm -rf $JSON
$BB wget -q --no-check-certificate $DOMAIN$COMMITSURL$JSON
COMMITS=$($BB grep $BLOBURL $JSON | $BB sed -e 's,.*'"$BLOBURL"',,' -e 's,'"$JSON"'.*,,')
for commit in $COMMITS;do
FetchMagiskDLData $RAWGITHUB$REPOURL$commit $CHANNEL
done
}
# This function is used to check and choose available Magisk versions for installation.
# Note: The function assumes that the $BB variable contains the path to the wget binary, which is typically used in BusyBox.
CheckAvailableMagisks() {
MAGISK_VERSIONS=$BASEDIR/magisk_versions.txt
MAGISK_DL_LINKS=$BASEDIR/magisk_dl_links.txt
MAGISK_MENU=$BASEDIR/magisk_menu.txt
MAGISK_CHANNEL=$BASEDIR/magisk_channel.txt
local GITHUB="https://github.com/"
RAWGITHUB="https://raw.githubusercontent.com/"
local TJWREPOURL="topjohnwu/magisk-files/"
local TJWCOMMITSURL="topjohnwu/magisk-files/commits/master/"
local TJWBLOBURL="topjohnwu/magisk-files/blob/"
local VVB2060REPOURL="vvb2060/magisk_files/"
local VVB2060COMMITSURL="vvb2060/magisk_files/commits/alpha/"
local VVB2060BLOBURL="vvb2060/magisk_files/blob/"
local DLL_cnt=0
if [ -z $MAGISKVERCHOOSEN ]; then
UFSH=$BASEDIR/assets/util_functions.sh
OF=$BASEDIR/download.tmp
BS=1024
CUTOFF=100
if [ -e $UFSH ]; then
MAGISK_LOCL_VER=$($BB grep $UFSH -e "MAGISK_VER" -w | sed 's/^.*=//')
MAGISK_LOCL_VER_CODE=$($BB grep $UFSH -e "MAGISK_VER_CODE" -w | sed 's/^.*=//')
MAGISK_LOCL_VER=$(GetPrettyVer $MAGISK_LOCL_VER $MAGISK_LOCL_VER_CODE)
else
MAGISK_LOCL_VER=""
MAGISK_LOCL_VER_CODE=""
fi