-
Notifications
You must be signed in to change notification settings - Fork 8
/
tgpatcher.py
1238 lines (1038 loc) · 42.2 KB
/
tgpatcher.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
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
#!/data/data/com.termux/files/usr/bin/python
# Telegram Patcher script in python
# @author: Abhi (@AbhiTheModder)
# Patch credits: @Zylern_OP, @rezaAa1177, @AbhiTheModder and Nekogram
import argparse
import os
import re
import sys
RED = "\033[0;31m"
GREEN = "\033[0;32m"
YELLOW = "\033[1;33m"
BLUE = "\033[0;34m"
NC = "\033[0m" # No Color
HOOK_SMALI = """
.class public Lorg/telegram/abhi/Hook;
.super Ljava/lang/Object;
.source "SourceFile"
# static fields
.field public static candelMessages:Z
# direct methods
.method public constructor <init>()V
.registers 1
invoke-direct {p0}, Ljava/lang/Object;-><init>()V
return-void
.end method
.method public static hook()V
.registers 1
const/4 v0, 0x1
.line 17
invoke-static {v0}, Lorg/telegram/abhi/Hook;->setCanDelMessages(Z)V
return-void
.end method
.method public static setCanDelMessages(Z)V
.registers 4
sput-boolean p0, Lorg/telegram/abhi/Hook;->candelMessages:Z
sget-object v0, Lorg/telegram/messenger/ApplicationLoader;->applicationContext:Landroid/content/Context;
const-string v1, "mainconfig"
const/4 v2, 0x0
invoke-virtual {v0, v1, v2}, Landroid/content/Context;->getSharedPreferences(Ljava/lang/String;I)Landroid/content/SharedPreferences;
move-result-object v0
invoke-interface {v0}, Landroid/content/SharedPreferences;->edit()Landroid/content/SharedPreferences$Editor;
move-result-object v0
const-string v1, "candelMessages"
invoke-interface {v0, v1, p0}, Landroid/content/SharedPreferences$Editor;->putBoolean(Ljava/lang/String;Z)Landroid/content/SharedPreferences$Editor;
move-result-object v0
invoke-interface {v0}, Landroid/content/SharedPreferences$Editor;->apply()V
return-void
.end method
.method public static unhook()V
.registers 1
.line 23
sget-boolean v0, Lorg/telegram/abhi/Hook;->candelMessages:Z
if-eqz v0, :cond_8
const/4 v0, 0x0
.line 24
invoke-static {v0}, Lorg/telegram/abhi/Hook;->setCanDelMessages(Z)V
:cond_8
return-void
.end method
"""
class NoMethodFoundError(Exception):
"""Exception raised when the method is not found in the file."""
pass
def find_smali_file(root_directory, target_file):
"""Recursively search for the target file within the root directory."""
for dirpath, _, filenames in os.walk(root_directory):
if target_file in filenames:
return os.path.join(dirpath, target_file)
return None
def find_smali_file_by_method(root_directory, method_name):
"""Recursively search for the method in any smali file within the root directory."""
for dirpath, _, filenames in os.walk(root_directory):
for filename in filenames:
if filename.endswith(".smali"):
file_path = os.path.join(dirpath, filename)
with open(file_path, "r") as file:
file_content = file.read()
if method_name in file_content:
return file_path
return None
def modify_method(file_path, method_name, new_method_code):
"""Modify the method in the smali file."""
with open(file_path, "r") as file:
lines = file.readlines()
in_method = False
new_lines = []
method_found = False
for line in lines:
if f".method {method_name}" in line:
in_method = True
method_found = True
new_lines.extend(new_method_code)
continue
if in_method:
if ".end method" in line:
in_method = False
continue
new_lines.append(line)
if method_found:
with open(file_path, "w") as file:
file.writelines(new_lines)
print(f"{GREEN}INFO: {NC}Method {method_name} modified successfully.")
else:
raise NoMethodFoundError(
f"{YELLOW}WARN: {NC}Method {method_name} not found in the file."
)
def modify_del_method(file_path, method_name, new_method_code):
"""Modify the method in the smali file."""
with open(file_path, "r") as file:
lines = file.readlines()
in_method = False
in_annotation = False
method_found = False
annotation_end_index = -1
new_lines = []
for i, line in enumerate(lines):
if f".method {method_name}" in line:
in_method = True
method_found = True
new_lines.append(line)
continue
if in_method:
if ".annotation" in line:
in_annotation = True
elif in_annotation and ".end annotation" in line:
in_annotation = False
annotation_end_index = i
if (
not in_annotation
and annotation_end_index != -1
and i > annotation_end_index
):
new_lines.extend(new_method_code)
annotation_end_index = -1
new_lines.append(line)
if ".end method" in line:
in_method = False
else:
new_lines.append(line)
if method_found:
with open(file_path, "w") as file:
file.writelines(new_lines)
print(f"{GREEN}INFO: {NC}Method {method_name} modified successfully.")
else:
raise NoMethodFoundError(
f"{YELLOW}WARN: {NC}Method {method_name} not found in the file."
)
def copy_method(file_path, original_method_name, new_method_name):
"""Copy the method in the smali file."""
with open(file_path, "r") as file:
lines = file.readlines()
in_method = False
method_found = False
method_lines = []
for line in lines:
if f".method {original_method_name}" in line:
in_method = True
method_found = True
method_lines.append(line.replace(original_method_name, new_method_name))
continue
if in_method:
method_lines.append(line)
if ".end method" in line:
in_method = False
if method_found:
with open(file_path, "a") as file:
file.writelines(method_lines)
print(
f"{GREEN}INFO: {NC}Method {original_method_name} copied to {new_method_name} successfully."
)
else:
print(f"{YELLOW}WARN: {NC}Method {original_method_name} not found in the file.")
def apply_regex(root_directory, search_pattern, replace_pattern, file_path=None):
"""Apply a regex search and replace patch across all smali files in the root directory or a specific file."""
pattern = re.compile(search_pattern)
if file_path:
with open(file_path, "r") as file:
file_content = file.read()
new_content = pattern.sub(replace_pattern, file_content)
if new_content != file_content:
with open(file_path, "w") as file:
file.write(new_content)
print(f"{GREEN}INFO: {NC}Applied regex patch to {file_path}")
else:
for dirpath, _, filenames in os.walk(root_directory):
for filename in filenames:
if filename.endswith(".smali"):
file_path = os.path.join(dirpath, filename)
with open(file_path, "r") as file:
file_content = file.read()
new_content = pattern.sub(replace_pattern, file_content)
if new_content != file_content:
with open(file_path, "w") as file:
file.write(new_content)
print(f"{GREEN}INFO: {NC}Applied regex patch to {file_path}")
def apply_isRestrictedMessage(root_directory):
"""Access Banned Channels [Related]"""
search_pattern = r"(iget-boolean\s([v|p]\d+)\, ([v|p]\d+)\, Lorg/telegram/.*isRestrictedMessage:Z)"
replace_pattern = r"\1\nconst \2, 0x0"
apply_regex(root_directory, search_pattern, replace_pattern)
def apply_enableSavingMedia(root_directory):
"""Enabling Saving Media Everywhere"""
search_pattern = (
r"(iget-boolean\s([v|p]\d+)\, ([v|p]\d+)\, Lorg/telegram/.*noforwards:Z)"
)
replace_pattern = r"\1\nconst \2, 0x0"
apply_regex(root_directory, search_pattern, replace_pattern)
def apply_premiumLocked(root_directory):
"""make premiumLocked bool false."""
search_pattern = (
r"(iget-boolean\s([v|p]\d+)\, ([v|p]\d+)\, Lorg/telegram/.*premiumLocked:Z)"
)
replace_pattern = r"\1\nconst \2, 0x0"
apply_regex(root_directory, search_pattern, replace_pattern)
def apply_EnableScreenshots(root_directory):
"""Enables Screenshots"""
initial_search_pattern = r"Landroid/view/Window;->.*Flags"
secondary_search_pattern = r"const/16 (.*), 0x2000"
replace_pattern = r"const/16 \1, 0x0"
for dirpath, _, filenames in os.walk(root_directory):
for filename in filenames:
if filename.endswith(".smali"):
file_path = os.path.join(dirpath, filename)
with open(file_path, "r") as file:
file_content = file.read()
initial_matches = re.findall(initial_search_pattern, file_content)
if initial_matches:
new_content = file_content
for match in initial_matches:
new_content = re.sub(
secondary_search_pattern, replace_pattern, new_content
)
if new_content != file_content:
with open(file_path, "w") as file:
file.write(new_content)
print(
f"{GREEN}INFO: {NC}Applied windowFlags patch to {file_path}"
)
def apply_EnableScreenshots2(root_directory):
"""Enables Screenshots"""
search_pattern1 = r"(sget-boolean\s([v|p]\d+).*SharedConfig;->allowScreenCapture:Z)"
replace_pattern1 = r"\1\nconst \2, 0x1"
search_pattern2 = r"(iget-boolean\s([v|p]\d+)\, ([v|p]\d+)\, Lorg/telegram/ui/.*allowScreenshots:Z)"
replace_pattern2 = r"\1\nconst \2, 0x1"
for dirpath, _, filenames in os.walk(root_directory):
for filename in filenames:
if filename.endswith(".smali"):
file_path = os.path.join(dirpath, filename)
with open(file_path, "r") as file:
file_content = file.read()
new_content = re.sub(search_pattern1, replace_pattern1, file_content)
new_content = re.sub(search_pattern2, replace_pattern2, new_content)
if new_content != file_content:
with open(file_path, "w") as file:
file.write(new_content)
print(
f"{GREEN}INFO: {NC}Applied allowScreenCapture patch to {file_path}"
)
def apply_EnableScreenshots3(root_directory):
"""Enables Screenshots"""
search_pattern = r"or-int/lit16\s+([vp]\d+),\s+([vp]\d+),\s+0x2000"
replace_pattern = r"or-int/lit16 \1, \1, 0x0"
secret_media_viewer_path = find_smali_file(
root_directory, "SecretMediaViewer.smali"
)
if secret_media_viewer_path:
apply_regex(
root_directory, search_pattern, replace_pattern, secret_media_viewer_path
)
photo_viewer_path = find_smali_file(root_directory, "PhotoViewer.smali")
if photo_viewer_path:
apply_regex(root_directory, search_pattern, replace_pattern, photo_viewer_path)
def modify_isPremium(file_path):
"""Modify isPremium method to return true."""
new_method_code = [
".method public isPremium()Z\n",
" .locals 1\n",
" const/4 v0, 0x1\n",
" return v0\n",
".end method\n",
]
try:
modify_method(file_path, "public isPremium()Z", new_method_code)
except NoMethodFoundError as e:
print(e)
def modify_markMessagesAsDeleted(file_path):
"""Modify markMessagesAsDeleted methods"""
smali_dir = os.path.dirname(file_path).split("/")[0:2]
root_dir = os.path.dirname(file_path).split("/")[0]
smali_dir = "/".join(smali_dir)
new_dir = os.path.join(smali_dir, "org", "telegram", "abhi")
if not os.path.exists(new_dir):
os.makedirs(new_dir, exist_ok=True)
hook_file = os.path.join(new_dir, "Hook.smali")
with open(hook_file, "w") as file:
file.write(HOOK_SMALI)
search_pattern = r"sget\s([v|p]\d),\sLorg/telegram/messenger/R\$string;->ShowAds:I\n+\s+(invoke-static\s{\1},\sLorg/telegram/messenger/LocaleController;->getString\(I\)Ljava/lang/String;\n+\s+move-result-object\s\1|goto\s:goto_\d+)((\n.*)*?)invoke-virtual\s({.*}),\sLorg/telegram/ui/Cells/TextCell;->setTextAndCheck\(Ljava/lang/CharSequence;ZZ\)V"
search_pattern2 = r"sget\s([v|p]\d),\sLorg/telegram/messenger/R\$string;->ShowAdsInfo:I\n+\s+(invoke-static\s{\1},\sLorg/telegram/messenger/LocaleController;->getString\(I\)Ljava/lang/String;\n+\s+move-result-object\s\1|goto\s:goto_\d+)"
search_pattern3 = r"sget\s([v|p]\d),\sLorg/telegram/messenger/R\$string;->ShowAdsTitle:I\n+\s+(invoke-static\s{\1},\sLorg/telegram/messenger/LocaleController;->getString\(I\)Ljava/lang/String;\n+\s+move-result-object\s\1|goto\s:goto_\d+)"
replace_pattern = r'const-string \1, "Do Not Delete Messages"\n\3\4\n invoke-virtual \5, Lorg/telegram/ui/Cells/TextCell;->setTextAndCheck2(Ljava/lang/CharSequence;ZZ)V'
replace_pattern2 = r'const-string \1, "After enabling or disabling the feature, ensure you revisit this page for the changes to take effect.\\nMod by Abhi"'
replace_pattern3 = r'const-string \1, "Anti-Delete Messages"\n invoke-virtual {v1, \1}, Lorg/telegram/ui/Cells/HeaderCell;->setText(Ljava/lang/CharSequence;)V\n return-void'
search_patterns = [search_pattern, search_pattern2, search_pattern3]
replace_patterns = [replace_pattern, replace_pattern2, replace_pattern3]
for i, s_p in enumerate(search_patterns):
apply_regex(root_dir, s_p, replace_patterns[i])
automate_modification(root_dir, "TextCell.smali", create_delcopy_method)
automate_modification(root_dir, "LaunchActivity.smali", modify_del_oncreate_method)
new_code_to_append = [
" sget-boolean v0, Lorg/telegram/abhi/Hook;->candelMessages:Z\n",
" if-eqz v0, :cond_7\n",
" const/4 p1, 0x0\n",
" return-object p1\n",
" :cond_7\n",
]
new_code_to_append2 = [
" sget-boolean v0, Lorg/telegram/abhi/Hook;->candelMessages:Z\n",
" if-eqz v0, :cond_7\n",
" const/4 v1, 0x0\n",
" return-object v1\n",
" :cond_7\n",
]
try:
modify_del_method(
file_path,
"public markMessagesAsDeleted(JIZZ)Ljava/util/ArrayList;",
new_code_to_append,
)
except NoMethodFoundError as e:
print(e)
try:
modify_del_method(
file_path,
"public markMessagesAsDeleted(JLjava/util/ArrayList;ZZII)Ljava/util/ArrayList;",
new_code_to_append2,
)
except NoMethodFoundError as e:
print(e)
def modify_del_oncreate_method(file_path):
method_name = "protected onCreate(Landroid/os/Bundle;)V"
method_name2 = "public onCreate(Landroid/os/Bundle;)V" # For plus
with open(file_path, "r") as file:
lines = file.readlines()
new_lines = []
in_method = False
method_found = False
cond_label_pattern = re.compile(r":cond_\d")
new_codes = [
" sget-object v0, Lorg/telegram/messenger/ApplicationLoader;->applicationContext:Landroid/content/Context;\n",
' const-string v1, "mainconfig"\n',
" const/4 v2, 0x0\n",
" invoke-virtual {v0, v1, v2}, Landroid/content/Context;->getSharedPreferences(Ljava/lang/String;I)Landroid/content/SharedPreferences;\n",
" move-result-object v0\n",
' const-string v1, "candelMessages"\n',
" const/4 v2, 0x0\n",
" invoke-interface {v0, v1, v2}, Landroid/content/SharedPreferences;->getBoolean(Ljava/lang/String;Z)Z\n",
" move-result v0\n",
" sput-boolean v0, Lorg/telegram/abhi/Hook;->candelMessages:Z\n",
]
for line in lines:
if f".method {method_name}" or f".method {method_name2}" in line:
in_method = True
method_found = True
new_lines.append(line)
continue
if in_method:
if cond_label_pattern.search(line):
new_lines.append(line)
continue
if ".locals" in line:
new_lines.append(line)
new_lines.extend(new_codes)
else:
new_lines.append(line)
if ".end method" in line:
in_method = False
continue
new_lines.append(line)
if method_found:
with open(file_path, "w") as file:
file.writelines(new_lines)
print(f"{GREEN}INFO: {NC}Method {method_name} modified successfully.")
else:
print(f"{YELLOW}WARN: {NC}Method {method_name} not found in the file.")
sys.exit(1)
def create_delcopy_method(file_path):
method_name = "public setTextAndCheck2(Ljava/lang/CharSequence;ZZ)V"
copy_method(
file_path,
"public setTextAndCheck(Ljava/lang/CharSequence;ZZ)V",
method_name,
)
with open(file_path, "r") as file:
lines = file.readlines()
new_lines = []
in_method = False
method_found = False
cond_label_pattern = re.compile(r":cond_\d")
new_codes = [
" invoke-virtual {p0}, Landroid/view/View;->getContext()Landroid/content/Context;\n",
" move-result-object v1\n",
' const-string v0, "Turned off"\n',
" if-eqz p2, :cond_48\n",
' const-string v0, "Turned on"\n',
" :cond_48\n",
" invoke-static {v1, v0, v2}, Landroid/widget/Toast;->makeText(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;\n",
" move-result-object v0\n",
" invoke-virtual {v0}, Landroid/widget/Toast;->show()V\n",
" if-eqz p2, :cond_55\n",
" invoke-static {}, Lorg/telegram/abhi/Hook;->hook()V\n",
" goto :goto_58\n",
" :cond_55\n",
" invoke-static {}, Lorg/telegram/abhi/Hook;->unhook()V\n",
" :goto_58\n",
" return-void\n",
]
for line in lines:
if f".method {method_name}" in line:
in_method = True
method_found = True
new_lines.append(line)
continue
if in_method:
if cond_label_pattern.search(line):
new_lines.append(line)
continue
if "return-void" in line:
new_lines.extend(new_codes)
else:
new_lines.append(line)
if ".end method" in line:
in_method = False
continue
new_lines.append(line)
if method_found:
with open(file_path, "w") as file:
file.writelines(new_lines)
print(f"{GREEN}INFO: {NC}Method {method_name} modified successfully.")
else:
print(f"{YELLOW}WARN: {NC}Method {method_name} not found in the file.")
sys.exit(1)
def modify_isPremium_stories(file_path):
"""Modify isPremium method in StoriesController.
- This is different from simple isPremium method.
It's for Stories related checks
"""
new_method_code = [
".method private isPremium(J)Z\n",
" .locals 1\n",
" const/4 p1, 0x1\n",
" return p1\n",
".end method\n",
]
try:
modify_method(file_path, "private isPremium(J)Z", new_method_code)
except NoMethodFoundError:
# Maybe plus messenger ? let's look for 2nd method
new_method_code = [
".method public final isPremium(J)Z\n",
" .locals 1\n",
" const/4 p1, 0x1\n",
" return p1\n",
".end method\n",
]
try:
modify_method(file_path, "public final isPremium(J)Z", new_method_code)
except NoMethodFoundError:
print(f"{YELLOW}No isPremium(J)Z method found in StoriesController!{NC}")
def modify_getCertificateSHA256Fingerprint(file_path):
"""Modify getCertificateSHA256Fingerprint method.
- In Telegram [Official] this change is very important
because it allows you to use the app without any restrictions.
else you'll not even be able to login into the app
if somehow you does then you account may get at risk
"""
new_method_code = [
".method public static getCertificateSHA256Fingerprint()Ljava/lang/String;\n",
" .locals 1\n",
' const-string v0, "49C1522548EBACD46CE322B6FD47F6092BB745D0F88082145CAF35E14DCC38E1"\n',
" return-object v0\n",
".end method\n",
]
try:
modify_method(
file_path,
"public static getCertificateSHA256Fingerprint()Ljava/lang/String;",
new_method_code,
)
except NoMethodFoundError as e:
print(e)
def modify_forcePremium(file_path):
"""Modify forcePremium method to true.
- Though this change feels useless to me most of the time
"""
new_method_code = [
".method static synthetic access$3000(Lorg/telegram/ui/PremiumPreviewFragment;)Z\n",
" .locals 0\n",
" const/4 p0, 0x1\n",
" return p0\n",
".end method\n",
]
try:
modify_method(
file_path,
"static synthetic access$3000(Lorg/telegram/ui/PremiumPreviewFragment;)Z",
new_method_code,
)
except NoMethodFoundError as e:
print(e)
def modify_markStories_method(file_path):
"""Modify markStoryAsRead methods
- Allows users to 'hide their views' to watched stories
"""
new_method_code1 = [
".method public markStoryAsRead(Lorg/telegram/tgnet/tl/TL_stories$PeerStories;Lorg/telegram/tgnet/tl/TL_stories$StoryItem;Z)Z\n",
" .locals 1\n",
" const/4 v0, 0x0\n",
" return v0\n",
".end method\n",
]
new_method_code2 = [
".method public markStoryAsRead(JLorg/telegram/tgnet/tl/TL_stories$StoryItem;)Z\n",
" .locals 2\n",
" const/4 p1, 0x0\n",
" return p1\n",
".end method\n",
]
try:
modify_method(
file_path,
"public markStoryAsRead(Lorg/telegram/tgnet/tl/TL_stories$PeerStories;Lorg/telegram/tgnet/tl/TL_stories$StoryItem;Z)Z",
new_method_code1,
)
except NoMethodFoundError as e:
print(e)
try:
modify_method(
file_path,
"public markStoryAsRead(JLorg/telegram/tgnet/tl/TL_stories$StoryItem;)Z",
new_method_code2,
)
except NoMethodFoundError as e:
print(e)
def modify_isPremiumFeatureAvailable_method(file_path, method_name):
"""Modify isPremiumFeatureAvailable method to change 'const/4 v1, 0x0' to 'const/4 v1, 0x1'."""
with open(file_path, "r") as file:
lines = file.readlines()
new_lines = []
in_method = False
method_found = False
cond_label_pattern = re.compile(r":cond_\d")
for line in lines:
if f".method {method_name}" in line:
in_method = True
method_found = True
new_lines.append(line)
continue
if in_method:
if cond_label_pattern.search(line):
new_lines.append(line)
continue
if "const/4 v1, 0x0" in line:
new_lines.append(" const/4 v1, 0x1\n")
else:
new_lines.append(line)
if ".end method" in line:
in_method = False
continue
new_lines.append(line)
if method_found:
with open(file_path, "w") as file:
file.writelines(new_lines)
print(f"{GREEN}INFO: {NC}Method {method_name} modified successfully.")
else:
print(f"{YELLOW}WARN: {NC}Method {method_name} not found in the file.")
def modify_secret_media_methods(file_path):
"""Modify given methods in MessageObject.smali for Secret Media Enabler.
- This allows users to view secret media without worrying about their destruction or timeout.
"""
with open(file_path, "r") as file:
lines = file.readlines()
new_lines = []
in_method = False
method_found = {
"public getSecretTimeLeft()I": False,
"public isSecretMedia()Z": False,
"public static isSecretPhotoOrVideo(Lorg/telegram/tgnet/TLRPC$Message;)Z": False,
"public static isSecretMedia(Lorg/telegram/tgnet/TLRPC$Message;)Z": False,
}
method_codes = {
"public isSecretMedia()Z": [
".method public isSecretMedia()Z\n",
" .locals 5\n",
" .line 0\n",
" iget-object v0, p0, Lorg/telegram/messenger/MessageObject;->messageOwner:Lorg/telegram/tgnet/TLRPC$Message;\n",
" instance-of v1, v0, Lorg/telegram/tgnet/TLRPC$TL_message_secret;\n",
" const/4 v3, 0x0\n",
" return v3\n",
".end method\n",
],
"public static isSecretPhotoOrVideo(Lorg/telegram/tgnet/TLRPC$Message;)Z": [
".method public static isSecretPhotoOrVideo(Lorg/telegram/tgnet/TLRPC$Message;)Z\n",
" .locals 4\n",
" instance-of v0, p0, Lorg/telegram/tgnet/TLRPC$TL_message_secret;\n",
" const/4 v2, 0x0\n",
" return v2\n",
".end method\n",
],
"public static isSecretMedia(Lorg/telegram/tgnet/TLRPC$Message;)Z": [
".method public static isSecretMedia(Lorg/telegram/tgnet/TLRPC$Message;)Z\n",
" .locals 4\n",
" .line 0\n",
" instance-of v0, p0, Lorg/telegram/tgnet/TLRPC$TL_message_secret;\n",
" const/4 v2, 0x0\n",
" return v2\n",
".end method\n",
],
}
for line in lines:
if any(method_name in line for method_name in method_found.keys()):
in_method = True
method_name = next(
method_name
for method_name in method_found.keys()
if method_name in line
)
method_found[method_name] = True
if method_name == "public getSecretTimeLeft()I":
new_lines.append(line)
continue
else:
new_lines.extend(method_codes[method_name])
continue
if in_method:
if method_name == "public getSecretTimeLeft()I":
if "const/4 v1, 0x0" in line:
new_lines.append(" const/4 v1, 0x1\n")
print(
f"{GREEN}INFO: {NC}Modified const/4 v1, 0x0 to const/4 v1, 0x1 in getSecretTimeLeft method."
)
else:
new_lines.append(line)
if ".end method" in line:
in_method = False
continue
new_lines.append(line)
if all(method_found.values()):
with open(file_path, "w") as file:
file.writelines(new_lines)
print(f"{GREEN}INFO: {NC}Secret Media methods modified successfully.")
else:
print(f"{YELLOW}WARN: {NC}Some Secret Media methods not found in the file.")
def modify_updateParams_method(file_path, method_name):
"""Modify updateParams method for faster downloads"""
with open(file_path, "r") as file:
lines = file.readlines()
new_lines = []
in_method = False
method_found = False
cond_label_pattern = re.compile(r":cond_\d")
for line in lines:
if f".method {method_name}" in line:
in_method = True
method_found = True
new_lines.append(line)
continue
if in_method:
if cond_label_pattern.search(line):
new_lines.append(line)
continue
if "const/high16 v0, 0x20000" in line:
new_lines.append(" const/high16 v0, 0x80000\n")
elif "const/4 v0, 0x4" in line:
new_lines.append(" const/16 v0, 0x8\n")
else:
new_lines.append(line)
if ".end method" in line:
in_method = False
continue
new_lines.append(line)
if method_found:
with open(file_path, "w") as file:
file.writelines(new_lines)
print(f"{GREEN}INFO: {NC}Method {method_name} modified successfully.")
else:
print(f"{YELLOW}WARN: {NC}Method {method_name} not found in the file.")
# ONly apply below one in Plus Messenger, Telegram will crash if you do so
# def modify_markAsReadStories(file_path):
# """Modify the smali file to change the iget-boolean and if-nez lines."""
# with open(file_path, "r") as file:
# lines = file.readlines()
# new_lines = []
# in_method = False
# method_found = False
# cond_label_pattern = re.compile(r":cond_\d")
# register_pattern = re.compile(r"v\d+")
# for line in lines:
# if (
# "iget-boolean" in line
# and "Lorg/telegram/tgnet/TLRPC$User;->premium:Z" in line
# ):
# method_found = True
# new_lines.append(line)
# new_lines.append(" const/4 v9, 0x1\n")
# elif "if-nez" in line and cond_label_pattern.search(line):
# new_lines.append(line)
# else:
# new_lines.append(line)
# # if method_found:
# # with open(file_path, "w") as file:
# # file.writelines(new_lines)
# # new_method_code = [
# # ".method private updateButton(Z)V\n",
# # ".locals 5\n",
# # "const/4 v0, 0x0\n",
# # "const/4 v2, 0x1\n",
# # "iput-boolean v2, p0, Lorg/telegram/ui/Stories/StealthModeAlert;->stealthModeIsActive:Z\n",
# # "iget-object v0, p0, Lorg/telegram/ui/Stories/StealthModeAlert;->button:Lorg/telegram/ui/Components/Premium/PremiumButtonView;\n",
# # "sget v1, Lorg/telegram/messenger/R$string;->StealthModeIsActive:I\n",
# # "invoke-static {v1}, Lorg/telegram/messenger/LocaleController;->getString(I)Ljava/lang/String;\n",
# # "move-result-object v1\n",
# # "invoke-virtual {v0, v1, v2, p1}, Lorg/telegram/ui/Components/Premium/PremiumButtonView;->setOverlayText(Ljava/lang/String;ZZ)V\n",
# # "iget-object p1, p0, Lorg/telegram/ui/Stories/StealthModeAlert;->button:Lorg/telegram/ui/Components/Premium/PremiumButtonView;\n",
# # "iget-object p1, p1, Lorg/telegram/ui/Components/Premium/PremiumButtonView;->overlayTextView:Lorg/telegram/ui/Components/AnimatedTextView;\n",
# # "sget v0, Lorg/telegram/ui/ActionBar/Theme;->key_featuredStickers_buttonText:I\n",
# # "invoke-static {v0}, Lorg/telegram/ui/ActionBar/Theme;->getColor(I)I\n",
# # "move-result v0\n",
# # "invoke-virtual {p1, v0}, Lorg/telegram/ui/Components/AnimatedTextView;->setTextColor(I)V\n",
# # "return-void\n",
# # ".end method\n",
# # ]
# # modify_method(file_path, "private updateButton(Z)V", new_method_code)
# print(
# f"{GREEN}INFO: {NC}MarkAsRead patch applied successfully."
# )
# else:
# print(
# f"{YELLOW}WARN: {NC}MarkAsRead patch not found in the file."
# )
def modify_isChatNoForwards(file_path):
"""Saving Media From Forward Restricted Group/Channels"""
new_method_code1 = [
".method public isChatNoForwards(J)Z\n",
" .registers 3\n",
" const/4 p1, 0x0\n",
" return p1\n",
".end method\n",
]
new_method_code2 = [
".method public isChatNoForwards(Lorg/telegram/tgnet/TLRPC$Chat;)Z\n",
" .registers 4\n",
" const/4 p1, 0x0\n",
" return p1\n",
".end method\n",
]
try:
modify_method(file_path, "public isChatNoForwards(J)Z", new_method_code1)
except NoMethodFoundError as e:
print(e)
try:
modify_method(
file_path,
"public isChatNoForwards(Lorg/telegram/tgnet/TLRPC$Chat;)Z",
new_method_code2,
)
except NoMethodFoundError as e:
print(e)
def modify_checkCanOpenChat(file_path):
"""Access Banned Channels [Main]"""
new_method_code1 = [
".method public checkCanOpenChat(Landroid/os/Bundle;Lorg/telegram/ui/ActionBar/BaseFragment;)Z\n",
" .registers 3\n",
" const/4 p1, 0x1\n",
" return p1\n",
".end method\n",
]
new_method_code2 = [
".method public checkCanOpenChat(Landroid/os/Bundle;Lorg/telegram/ui/ActionBar/BaseFragment;Lorg/telegram/messenger/MessageObject;)Z\n",
" .registers 4\n",
" const/4 p1, 0x1\n",
" return p1\n",
".end method\n",
]
new_method_code3 = [
".method public checkCanOpenChat(Landroid/os/Bundle;Lorg/telegram/ui/ActionBar/BaseFragment;Lorg/telegram/messenger/MessageObject;Lorg/telegram/messenger/browser/Browser$Progress;)Z\n",
" .registers 5\n",
" const/4 p1, 0x1\n",
" return p1\n",
".end method\n",
]
try:
modify_method(
file_path,
"public checkCanOpenChat(Landroid/os/Bundle;Lorg/telegram/ui/ActionBar/BaseFragment;)Z",
new_method_code1,
)
except NoMethodFoundError as e:
print(e)
try:
modify_method(
file_path,
"public checkCanOpenChat(Landroid/os/Bundle;Lorg/telegram/ui/ActionBar/BaseFragment;Lorg/telegram/messenger/MessageObject;)Z",
new_method_code2,
)
except NoMethodFoundError as e:
print(e)
try:
modify_method(
file_path,
"public checkCanOpenChat(Landroid/os/Bundle;Lorg/telegram/ui/ActionBar/BaseFragment;Lorg/telegram/messenger/MessageObject;Lorg/telegram/messenger/browser/Browser$Progress;)Z",
new_method_code3,
)
except NoMethodFoundError as e:
print(e)
def modify_is_sponsored_method(file_path):
"""Disable isSponsored Check"""
new_method_code = [
".method public isSponsored()Z\n",
" .locals 2\n",
" const/4 v0, 0x0\n",
" return v0\n",
".end method\n",