FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
frida-python/frida/core.py at main · feicong/frida-python · GitHub
feicong
/
frida-python
Public
forked from
frida/frida-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
frida-python
/
frida
/
core.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
1830 lines (1412 loc) · 52.1 KB
Breadcrumbs
frida-python
/
frida
/
core.py
Copy path
File metadata and controls
1830 lines (1412 loc) · 52.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
from
__future__
import
annotations
import
asyncio
import
dataclasses
import
fnmatch
import
functools
import
json
import
sys
import
threading
import
traceback
import
warnings
from
types
import
TracebackType
from
typing
import
(
Any
,
AnyStr
,
Awaitable
,
Callable
,
Dict
,
List
,
Mapping
,
MutableMapping
,
Optional
,
Sequence
,
Tuple
,
Type
,
TypeVar
,
Union
,
overload
,
)
if
sys
.
version_info
>=
(
3
,
8
):
from
typing
import
Literal
,
TypedDict
else
:
from
typing_extensions
import
Literal
,
TypedDict
if
sys
.
version_info
>=
(
3
,
10
):
from
typing
import
ParamSpec
else
:
from
typing_extensions
import
ParamSpec
if
sys
.
version_info
>=
(
3
,
11
):
from
typing
import
NotRequired
,
cast
else
:
from
typing_extensions
import
NotRequired
,
cast
from
.
import
_frida
_device_manager
=
None
_Cancellable
=
_frida
.
Cancellable
ProcessTarget
=
Union
[
int
,
str
]
Spawn
=
_frida
.
Spawn
@
dataclasses
.
dataclass
class
RPCResult
:
finished
:
bool
=
False
value
:
Any
=
None
error
:
Optional
[
Exception
]
=
None
def
get_device_manager
()
->
"DeviceManager"
:
"""
Get or create a singleton DeviceManager that let you manage all the devices
"""
global
_device_manager
if
_device_manager
is
None
:
_device_manager
=
DeviceManager
(
_frida
.
DeviceManager
())
return
_device_manager
def
_filter_missing_kwargs
(
d
:
MutableMapping
[
Any
,
Any
])
->
None
:
for
key
in
list
(
d
.
keys
()):
if
d
[
key
]
is
None
:
d
.
pop
(
key
)
P
=
ParamSpec
(
"P"
)
R
=
TypeVar
(
"R"
,
covariant
=
True
)
def
cancellable
(
f
:
Callable
[
P
,
R
])
->
Callable
[
P
,
R
]:
# currently there is no way to type properly the extended callable with optional cancellable parameter
# ref: https://github.com/python/typing/discussions/1905#discussioncomment-11696995
@
functools
.
wraps
(
f
)
def
wrapper
(
*
args
:
P
.
args
,
**
kwargs
:
P
.
kwargs
)
->
R
:
cancellable
=
kwargs
.
pop
(
"cancellable"
,
None
)
if
cancellable
is
not
None
:
with
cast
(
Cancellable
,
cancellable
):
return
f
(
*
args
,
**
kwargs
)
return
f
(
*
args
,
**
kwargs
)
return
wrapper
class
IOStream
:
"""
Frida's own implementation of an input/output stream
"""
def
__init__
(
self
,
impl
:
_frida
.
IOStream
)
->
None
:
self
.
_impl
=
impl
def
__repr__
(
self
)
->
str
:
return
repr
(
self
.
_impl
)
@
property
def
is_closed
(
self
)
->
bool
:
"""
Query whether the stream is closed
"""
return
self
.
_impl
.
is_closed
()
@
cancellable
def
close
(
self
)
->
None
:
"""
Close the stream.
"""
self
.
_impl
.
close
()
@
cancellable
def
read
(
self
,
count
:
int
)
->
bytes
:
"""
Read up to the specified number of bytes from the stream
"""
return
self
.
_impl
.
read
(
count
)
@
cancellable
def
read_all
(
self
,
count
:
int
)
->
bytes
:
"""
Read exactly the specified number of bytes from the stream
"""
return
self
.
_impl
.
read_all
(
count
)
@
cancellable
def
write
(
self
,
data
:
bytes
)
->
int
:
"""
Write as much as possible of the provided data to the stream
"""
return
self
.
_impl
.
write
(
data
)
@
cancellable
def
write_all
(
self
,
data
:
bytes
)
->
None
:
"""
Write all of the provided data to the stream
"""
self
.
_impl
.
write_all
(
data
)
class
PortalMembership
:
def
__init__
(
self
,
impl
:
_frida
.
PortalMembership
)
->
None
:
self
.
_impl
=
impl
@
cancellable
def
terminate
(
self
)
->
None
:
"""
Terminate the membership
"""
self
.
_impl
.
terminate
()
class
ScriptExportsSync
:
"""
Proxy object that expose all the RPC exports of a script as attributes on this class
A method named exampleMethod in a script will be called with instance.example_method on this object
"""
def
__init__
(
self
,
script
:
"Script"
)
->
None
:
self
.
_script
=
script
def
__getattr__
(
self
,
name
:
str
)
->
Callable
[...,
Any
]:
script
=
self
.
_script
js_name
=
_to_camel_case
(
name
)
def
method
(
*
args
:
Any
,
**
kwargs
:
Any
)
->
Any
:
request
,
data
=
make_rpc_call_request
(
js_name
,
args
)
return
script
.
_rpc_request
(
request
,
data
,
**
kwargs
)
return
method
def
__dir__
(
self
)
->
List
[
str
]:
return
self
.
_script
.
list_exports_sync
()
ScriptExports
=
ScriptExportsSync
class
ScriptExportsAsync
:
"""
Proxy object that expose all the RPC exports of a script as attributes on this class
A method named exampleMethod in a script will be called with instance.example_method on this object
"""
def
__init__
(
self
,
script
:
"Script"
)
->
None
:
self
.
_script
=
script
def
__getattr__
(
self
,
name
:
str
)
->
Callable
[...,
Awaitable
[
Any
]]:
script
=
self
.
_script
js_name
=
_to_camel_case
(
name
)
async
def
method
(
*
args
:
Any
,
**
kwargs
:
Any
)
->
Any
:
request
,
data
=
make_rpc_call_request
(
js_name
,
args
)
return
await
script
.
_rpc_request_async
(
request
,
data
,
**
kwargs
)
return
method
def
__dir__
(
self
)
->
List
[
str
]:
return
self
.
_script
.
list_exports_sync
()
def
make_rpc_call_request
(
js_name
:
str
,
args
:
Sequence
[
Any
])
->
Tuple
[
List
[
Any
],
Optional
[
bytes
]]:
if
args
and
isinstance
(
args
[
-
1
],
bytes
):
raw_args
=
args
[:
-
1
]
data
=
args
[
-
1
]
else
:
raw_args
=
args
data
=
None
return
([
"call"
,
js_name
,
raw_args
],
data
)
class
ScriptErrorMessage
(
TypedDict
):
type
:
Literal
[
"error"
]
description
:
str
stack
:
NotRequired
[
str
]
fileName
:
NotRequired
[
str
]
lineNumber
:
NotRequired
[
int
]
columnNumber
:
NotRequired
[
int
]
class
ScriptPayloadMessage
(
TypedDict
):
type
:
Literal
[
"send"
]
payload
:
NotRequired
[
Any
]
ScriptMessage
=
Union
[
ScriptPayloadMessage
,
ScriptErrorMessage
]
ScriptMessageCallback
=
Callable
[[
ScriptMessage
,
Optional
[
bytes
]],
None
]
ScriptDestroyedCallback
=
Callable
[[],
None
]
class
RPCException
(
Exception
):
"""
Wraps remote errors from the script RPC
"""
def
__str__
(
self
)
->
str
:
return
str
(
self
.
args
[
2
])
if
len
(
self
.
args
)
>=
3
else
str
(
self
.
args
[
0
])
class
Script
:
def
__init__
(
self
,
impl
:
_frida
.
Script
)
->
None
:
self
.
exports_sync
=
ScriptExportsSync
(
self
)
self
.
exports_async
=
ScriptExportsAsync
(
self
)
self
.
_impl
=
impl
self
.
_on_message_callbacks
:
List
[
ScriptMessageCallback
]
=
[]
self
.
_log_handler
:
Callable
[[
str
,
str
],
None
]
=
self
.
default_log_handler
self
.
_pending
:
Dict
[
int
,
Callable
[[
Optional
[
Any
],
Optional
[
Union
[
RPCException
,
_frida
.
InvalidOperationError
]]],
None
]
]
=
{}
self
.
_next_request_id
=
1
self
.
_cond
=
threading
.
Condition
()
impl
.
on
(
"destroyed"
,
self
.
_on_destroyed
)
impl
.
on
(
"message"
,
self
.
_on_message
)
@
property
def
exports
(
self
)
->
ScriptExportsSync
:
"""
The old way of retrieving the synchronous exports caller
"""
warnings
.
warn
(
"Script.exports will become asynchronous in the future, use the explicit Script.exports_sync instead"
,
DeprecationWarning
,
stacklevel
=
2
,
)
return
self
.
exports_sync
def
__repr__
(
self
)
->
str
:
return
repr
(
self
.
_impl
)
@
property
def
is_destroyed
(
self
)
->
bool
:
"""
Query whether the script has been destroyed
"""
return
self
.
_impl
.
is_destroyed
()
@
cancellable
def
load
(
self
)
->
None
:
"""
Load the script.
"""
self
.
_impl
.
load
()
@
cancellable
def
unload
(
self
)
->
None
:
"""
Unload the script
"""
self
.
_impl
.
unload
()
@
cancellable
def
eternalize
(
self
)
->
None
:
"""
Eternalize the script
"""
self
.
_impl
.
eternalize
()
def
post
(
self
,
message
:
Any
,
data
:
Optional
[
AnyStr
]
=
None
)
->
None
:
"""
Post a JSON-encoded message to the script
"""
raw_message
=
json
.
dumps
(
message
)
kwargs
=
{
"data"
:
data
}
_filter_missing_kwargs
(
kwargs
)
self
.
_impl
.
post
(
raw_message
,
**
kwargs
)
@
cancellable
def
enable_debugger
(
self
,
port
:
Optional
[
int
]
=
None
)
->
None
:
"""
Enable the Node.js compatible script debugger
"""
kwargs
=
{
"port"
:
port
}
_filter_missing_kwargs
(
kwargs
)
self
.
_impl
.
enable_debugger
(
**
kwargs
)
@
cancellable
def
disable_debugger
(
self
)
->
None
:
"""
Disable the Node.js compatible script debugger
"""
self
.
_impl
.
disable_debugger
()
@
overload
def
on
(
self
,
signal
:
Literal
[
"destroyed"
],
callback
:
ScriptDestroyedCallback
)
->
None
: ...
@
overload
def
on
(
self
,
signal
:
Literal
[
"message"
],
callback
:
ScriptMessageCallback
)
->
None
: ...
def
on
(
self
,
signal
:
str
,
callback
:
Callable
[...,
Any
])
->
None
:
"""
Add a signal handler
"""
if
signal
==
"message"
:
self
.
_on_message_callbacks
.
append
(
callback
)
else
:
self
.
_impl
.
on
(
signal
,
callback
)
@
overload
def
off
(
self
,
signal
:
Literal
[
"destroyed"
],
callback
:
ScriptDestroyedCallback
)
->
None
: ...
@
overload
def
off
(
self
,
signal
:
Literal
[
"message"
],
callback
:
ScriptMessageCallback
)
->
None
: ...
def
off
(
self
,
signal
:
str
,
callback
:
Callable
[...,
Any
])
->
None
:
"""
Remove a signal handler
"""
if
signal
==
"message"
:
self
.
_on_message_callbacks
.
remove
(
callback
)
else
:
self
.
_impl
.
off
(
signal
,
callback
)
def
get_log_handler
(
self
)
->
Callable
[[
str
,
str
],
None
]:
"""
Get the method that handles the script logs
"""
return
self
.
_log_handler
def
set_log_handler
(
self
,
handler
:
Callable
[[
str
,
str
],
None
])
->
None
:
"""
Set the method that handles the script logs
:param handler: a callable that accepts two parameters:
1. the log level name
2. the log message
"""
self
.
_log_handler
=
handler
def
default_log_handler
(
self
,
level
:
str
,
text
:
str
)
->
None
:
"""
The default implementation of the log handler, prints the message to stdout
or stderr, depending on the level
"""
if
level
==
"info"
:
print
(
text
,
file
=
sys
.
stdout
)
else
:
print
(
text
,
file
=
sys
.
stderr
)
async
def
list_exports_async
(
self
)
->
List
[
str
]:
"""
Asynchronously list all the exported attributes from the script's rpc
"""
result
=
await
self
.
_rpc_request_async
([
"list"
])
assert
isinstance
(
result
,
list
)
return
result
def
list_exports_sync
(
self
)
->
List
[
str
]:
"""
List all the exported attributes from the script's rpc
"""
result
=
self
.
_rpc_request
([
"list"
])
assert
isinstance
(
result
,
list
)
return
result
def
list_exports
(
self
)
->
List
[
str
]:
"""
List all the exported attributes from the script's rpc
"""
warnings
.
warn
(
"Script.list_exports will become asynchronous in the future, use the explicit Script.list_exports_sync instead"
,
DeprecationWarning
,
stacklevel
=
2
,
)
return
self
.
list_exports_sync
()
def
_rpc_request_async
(
self
,
args
:
Any
,
data
:
Optional
[
bytes
]
=
None
)
->
asyncio
.
Future
[
Any
]:
loop
=
asyncio
.
get_event_loop
()
future
:
asyncio
.
Future
[
Any
]
=
asyncio
.
Future
()
def
on_complete
(
value
:
Any
,
error
:
Optional
[
Union
[
RPCException
,
_frida
.
InvalidOperationError
]])
->
None
:
if
error
is
not
None
:
loop
.
call_soon_threadsafe
(
future
.
set_exception
,
error
)
else
:
loop
.
call_soon_threadsafe
(
future
.
set_result
,
value
)
request_id
=
self
.
_append_pending
(
on_complete
)
if
not
self
.
is_destroyed
:
self
.
_send_rpc_call
(
request_id
,
args
,
data
)
else
:
self
.
_on_destroyed
()
return
future
@
cancellable
def
_rpc_request
(
self
,
args
:
Any
,
data
:
Optional
[
bytes
]
=
None
)
->
Any
:
result
=
RPCResult
()
def
on_complete
(
value
:
Any
,
error
:
Optional
[
Union
[
RPCException
,
_frida
.
InvalidOperationError
]])
->
None
:
with
self
.
_cond
:
result
.
finished
=
True
result
.
value
=
value
result
.
error
=
error
self
.
_cond
.
notify_all
()
def
on_cancelled
()
->
None
:
self
.
_pending
.
pop
(
request_id
,
None
)
on_complete
(
None
,
None
)
request_id
=
self
.
_append_pending
(
on_complete
)
if
not
self
.
is_destroyed
:
self
.
_send_rpc_call
(
request_id
,
args
,
data
)
cancellable
=
Cancellable
.
get_current
()
cancel_handler
=
cancellable
.
connect
(
on_cancelled
)
try
:
with
self
.
_cond
:
while
not
result
.
finished
:
self
.
_cond
.
wait
()
finally
:
cancellable
.
disconnect
(
cancel_handler
)
cancellable
.
raise_if_cancelled
()
else
:
self
.
_on_destroyed
()
if
result
.
error
is
not
None
:
raise
result
.
error
return
result
.
value
def
_append_pending
(
self
,
callback
:
Callable
[[
Any
,
Optional
[
Union
[
RPCException
,
_frida
.
InvalidOperationError
]]],
None
]
)
->
int
:
with
self
.
_cond
:
request_id
=
self
.
_next_request_id
self
.
_next_request_id
+=
1
self
.
_pending
[
request_id
]
=
callback
return
request_id
def
_send_rpc_call
(
self
,
request_id
:
int
,
args
:
Any
,
data
:
Optional
[
bytes
])
->
None
:
self
.
post
([
"frida:rpc"
,
request_id
,
*
args
],
data
)
def
_on_rpc_message
(
self
,
request_id
:
int
,
operation
:
str
,
params
:
List
[
Any
],
data
:
Optional
[
Any
])
->
None
:
if
operation
in
(
"ok"
,
"error"
):
callback
=
self
.
_pending
.
pop
(
request_id
,
None
)
if
callback
is
None
:
return
value
=
None
error
=
None
if
operation
==
"ok"
:
if
data
is
not
None
:
value
=
(
params
[
1
],
data
)
if
len
(
params
)
>
1
else
data
else
:
value
=
params
[
0
]
else
:
error
=
RPCException
(
*
params
[
0
:
3
])
callback
(
value
,
error
)
def
_on_destroyed
(
self
)
->
None
:
while
True
:
next_pending
=
None
with
self
.
_cond
:
pending_ids
=
list
(
self
.
_pending
.
keys
())
if
len
(
pending_ids
)
>
0
:
next_pending
=
self
.
_pending
.
pop
(
pending_ids
[
0
])
if
next_pending
is
None
:
break
next_pending
(
None
,
_frida
.
InvalidOperationError
(
"script has been destroyed"
))
def
_on_message
(
self
,
raw_message
:
str
,
data
:
Optional
[
bytes
])
->
None
:
message
=
json
.
loads
(
raw_message
)
mtype
=
message
[
"type"
]
payload
=
message
.
get
(
"payload"
,
None
)
if
mtype
==
"log"
:
level
=
message
[
"level"
]
text
=
payload
self
.
_log_handler
(
level
,
text
)
elif
mtype
==
"send"
and
isinstance
(
payload
,
list
)
and
len
(
payload
)
>
0
and
payload
[
0
]
==
"frida:rpc"
:
request_id
=
payload
[
1
]
operation
=
payload
[
2
]
params
=
payload
[
3
:]
self
.
_on_rpc_message
(
request_id
,
operation
,
params
,
data
)
else
:
for
callback
in
self
.
_on_message_callbacks
[:]:
try
:
callback
(
message
,
data
)
except
:
traceback
.
print_exc
()
SessionDetachedCallback
=
Callable
[
[
Literal
[
"application-requested"
,
"process-replaced"
,
"process-terminated"
,
"connection-terminated"
,
"device-lost"
],
Optional
[
_frida
.
Crash
],
],
None
,
]
class
Session
:
def
__init__
(
self
,
impl
:
_frida
.
Session
)
->
None
:
self
.
_impl
=
impl
def
__repr__
(
self
)
->
str
:
return
repr
(
self
.
_impl
)
@
property
def
is_detached
(
self
)
->
bool
:
"""
Query whether the session is detached
"""
return
self
.
_impl
.
is_detached
()
@
cancellable
def
detach
(
self
)
->
None
:
"""
Detach session from the process
"""
self
.
_impl
.
detach
()
@
cancellable
def
resume
(
self
)
->
None
:
"""
Resume session after network error
"""
self
.
_impl
.
resume
()
@
cancellable
def
enable_child_gating
(
self
)
->
None
:
"""
Enable child gating
"""
self
.
_impl
.
enable_child_gating
()
@
cancellable
def
disable_child_gating
(
self
)
->
None
:
"""
Disable child gating
"""
self
.
_impl
.
disable_child_gating
()
@
cancellable
def
create_script
(
self
,
source
:
str
,
name
:
Optional
[
str
]
=
None
,
snapshot
:
Optional
[
bytes
]
=
None
,
runtime
:
Optional
[
str
]
=
None
)
->
Script
:
"""
Create a new script
"""
kwargs
=
{
"name"
:
name
,
"snapshot"
:
snapshot
,
"runtime"
:
runtime
}
_filter_missing_kwargs
(
kwargs
)
return
Script
(
self
.
_impl
.
create_script
(
source
,
**
kwargs
))
# type: ignore
@
cancellable
def
create_script_from_bytes
(
self
,
data
:
bytes
,
name
:
Optional
[
str
]
=
None
,
snapshot
:
Optional
[
bytes
]
=
None
,
runtime
:
Optional
[
str
]
=
None
)
->
Script
:
"""
Create a new script from bytecode
"""
kwargs
=
{
"name"
:
name
,
"snapshot"
:
snapshot
,
"runtime"
:
runtime
}
_filter_missing_kwargs
(
kwargs
)
return
Script
(
self
.
_impl
.
create_script_from_bytes
(
data
,
**
kwargs
))
# type: ignore
@
cancellable
def
compile_script
(
self
,
source
:
str
,
name
:
Optional
[
str
]
=
None
,
runtime
:
Optional
[
str
]
=
None
)
->
bytes
:
"""
Compile script source code to bytecode
"""
kwargs
=
{
"name"
:
name
,
"runtime"
:
runtime
}
_filter_missing_kwargs
(
kwargs
)
return
self
.
_impl
.
compile_script
(
source
,
**
kwargs
)
@
cancellable
def
snapshot_script
(
self
,
embed_script
:
str
,
warmup_script
:
Optional
[
str
],
runtime
:
Optional
[
str
]
=
None
)
->
bytes
:
"""
Evaluate script and snapshot the resulting VM state
"""
kwargs
=
{
"warmup_script"
:
warmup_script
,
"runtime"
:
runtime
}
_filter_missing_kwargs
(
kwargs
)
return
self
.
_impl
.
snapshot_script
(
embed_script
,
**
kwargs
)
@
cancellable
def
setup_peer_connection
(
self
,
stun_server
:
Optional
[
str
]
=
None
,
relays
:
Optional
[
Sequence
[
_frida
.
Relay
]]
=
None
)
->
None
:
"""
Set up a peer connection with the target process
"""
kwargs
=
{
"stun_server"
:
stun_server
,
"relays"
:
relays
}
_filter_missing_kwargs
(
kwargs
)
self
.
_impl
.
setup_peer_connection
(
**
kwargs
)
# type: ignore
@
cancellable
def
join_portal
(
self
,
address
:
str
,
certificate
:
Optional
[
str
]
=
None
,
token
:
Optional
[
str
]
=
None
,
acl
:
Union
[
None
,
List
[
str
],
Tuple
[
str
]]
=
None
,
)
->
PortalMembership
:
"""
Join a portal
"""
kwargs
:
Dict
[
str
,
Any
]
=
{
"certificate"
:
certificate
,
"token"
:
token
,
"acl"
:
acl
}
_filter_missing_kwargs
(
kwargs
)
return
PortalMembership
(
self
.
_impl
.
join_portal
(
address
,
**
kwargs
))
def
on
(
self
,
signal
:
Literal
[
"detached"
],
callback
:
SessionDetachedCallback
,
)
->
None
:
"""
Add a signal handler
"""
self
.
_impl
.
on
(
signal
,
callback
)
def
off
(
self
,
signal
:
Literal
[
"detached"
],
callback
:
SessionDetachedCallback
,
)
->
None
:
"""
Remove a signal handler
"""
self
.
_impl
.
off
(
signal
,
callback
)
BusDetachedCallback
=
Callable
[[],
None
]
BusMessageCallback
=
Callable
[[
Mapping
[
Any
,
Any
],
Optional
[
bytes
]],
None
]
class
Bus
:
def
__init__
(
self
,
impl
:
_frida
.
Bus
)
->
None
:
self
.
_impl
=
impl
self
.
_on_message_callbacks
:
List
[
Callable
[...,
Any
]]
=
[]
impl
.
on
(
"message"
,
self
.
_on_message
)
@
cancellable
def
attach
(
self
)
->
None
:
"""
Attach to the bus
"""
self
.
_impl
.
attach
()
def
post
(
self
,
message
:
Any
,
data
:
Optional
[
Union
[
str
,
bytes
]]
=
None
)
->
None
:
"""
Post a JSON-encoded message to the bus
"""
raw_message
=
json
.
dumps
(
message
)
kwargs
=
{
"data"
:
data
}
_filter_missing_kwargs
(
kwargs
)
self
.
_impl
.
post
(
raw_message
,
**
kwargs
)
@
overload
def
on
(
self
,
signal
:
Literal
[
"detached"
],
callback
:
BusDetachedCallback
)
->
None
: ...
@
overload
def
on
(
self
,
signal
:
Literal
[
"message"
],
callback
:
BusMessageCallback
)
->
None
: ...
def
on
(
self
,
signal
:
str
,
callback
:
Callable
[...,
Any
])
->
None
:
"""
Add a signal handler
"""
if
signal
==
"message"
:
self
.
_on_message_callbacks
.
append
(
callback
)
else
:
self
.
_impl
.
on
(
signal
,
callback
)
@
overload
def
off
(
self
,
signal
:
Literal
[
"detached"
],
callback
:
BusDetachedCallback
)
->
None
: ...
@
overload
def
off
(
self
,
signal
:
Literal
[
"message"
],
callback
:
BusMessageCallback
)
->
None
: ...
def
off
(
self
,
signal
:
str
,
callback
:
Callable
[...,
Any
])
->
None
:
"""
Remove a signal handler
"""
if
signal
==
"message"
:
self
.
_on_message_callbacks
.
remove
(
callback
)
else
:
self
.
_impl
.
off
(
signal
,
callback
)
def
_on_message
(
self
,
raw_message
:
str
,
data
:
Any
)
->
None
:
message
=
json
.
loads
(
raw_message
)
for
callback
in
self
.
_on_message_callbacks
[:]:
try
:
callback
(
message
,
data
)
except
:
traceback
.
print_exc
()
ServiceCloseCallback
=
Callable
[[],
None
]
ServiceMessageCallback
=
Callable
[[
Any
],
None
]
class
Service
:
def
__init__
(
self
,
impl
:
_frida
.
Service
)
->
None
:
self
.
_impl
=
impl
@
cancellable
def
activate
(
self
)
->
None
:
"""
Activate the service
"""
self
.
_impl
.
activate
()
@
cancellable
def
cancel
(
self
)
->
None
:
"""
Cancel the service
"""
self
.
_impl
.
cancel
()
def
request
(
self
,
parameters
:
Any
)
->
Any
:
"""
Perform a request
"""
return
self
.
_impl
.
request
(
parameters
)
@
overload
def
on
(
self
,
signal
:
Literal
[
"close"
],
callback
:
ServiceCloseCallback
)
->
None
: ...
@
overload
def
on
(
self
,
signal
:
Literal
[
"message"
],
callback
:
ServiceMessageCallback
)
->
None
: ...
def
on
(
self
,
signal
:
str
,
callback
:
Callable
[...,
Any
])
->
None
:
"""
Add a signal handler
"""
self
.
_impl
.
on
(
signal
,
callback
)
@
overload
def
off
(
self
,
signal
:
Literal
[
"close"
],
callback
:
ServiceCloseCallback
)
->
None
: ...
@
overload
def
off
(
self
,
signal
:
Literal
[
"message"
],
callback
:
ServiceMessageCallback
)
->
None
: ...
def
off
(
self
,
signal
:
str
,
callback
:
Callable
[...,
Any
])
->
None
:
"""
Remove a signal handler
"""
self
.
_impl
.
off
(
signal
,
callback
)
DeviceSpawnAddedCallback
=
Callable
[[
_frida
.
Spawn
],
None
]
DeviceSpawnRemovedCallback
=
Callable
[[
_frida
.
Spawn
],
None
]
DeviceChildAddedCallback
=
Callable
[[
_frida
.
Child
],
None
]
DeviceChildRemovedCallback
=
Callable
[[
_frida
.
Child
],
None
]
DeviceProcessCrashedCallback
=
Callable
[[
_frida
.
Crash
],
None
]
DeviceOutputCallback
=
Callable
[[
int
,
int
,
bytes
],
None
]
DeviceUninjectedCallback
=
Callable
[[
int
],
None
]
DeviceLostCallback
=
Callable
[[],
None
]
class
Device
:
"""
Represents a device that Frida connects to
"""
def
__init__
(
self
,
device
:
_frida
.
Device
)
->
None
:
assert
device
.
bus
is
not
None
self
.
id
=
device
.
id
self
.
name
=
device
.
name
self
.
icon
=
device
.
icon
self
.
type
=
device
.
type
self
.
bus
=
Bus
(
device
.
bus
)
self
.
_impl
=
device
def
__repr__
(
self
)
->
str
:
return
repr
(
self
.
_impl
)
@
property
def
is_lost
(
self
)
->
bool
:
"""
Query whether the device has been lost
"""
return
self
.
_impl
.
is_lost
()
@
cancellable
def
override_option
(
self
,
name
:
str
,
value
:
Any
)
->
None
:
"""
Override a backend-specific option
"""
self
.
_impl
.
override_option
(
name
,
value
)
@
cancellable
def
query_system_parameters
(
self
)
->
Dict
[
str
,
Any
]:
"""
Returns a dictionary of information about the host system
"""
return
self
.
_impl
.
query_system_parameters
()
@
cancellable
def
get_frontmost_application
(
self
,
scope
:
Optional
[
str
]
=
None
)
->
Optional
[
_frida
.
Application
]:
"""
Get details about the frontmost application
"""
kwargs
=
{
"scope"
:
scope
}
_filter_missing_kwargs
(
kwargs
)
return
self
.
_impl
.
get_frontmost_application
(
**
kwargs
)
@
cancellable
def
enumerate_applications
(
self
,
identifiers
:
Optional
[
Sequence
[
str
]]
=
None
,
scope
:
Optional
[
str
]
=
None
)
->
List
[
_frida
.
Application
]:
"""
Enumerate applications
"""
kwargs
=
{
"identifiers"
:
identifiers
,
"scope"
:
scope
}
_filter_missing_kwargs
(
kwargs
)
return
self
.
_impl
.
enumerate_applications
(
**
kwargs
)
# type: ignore
@
cancellable
def
enumerate_processes
(
self
,
pids
:
Optional
[
Sequence
[
int
]]
=
None
,
scope
:
Optional
[
str
]
=
None
)
->
List
[
_frida
.
Process
]:
"""
Enumerate processes
"""
kwargs
=
{
"pids"
:
pids
,
"scope"
:
scope
}
_filter_missing_kwargs
(
kwargs
)
return
self
.
_impl
.
enumerate_processes
(
**
kwargs
)
# type: ignore
@
cancellable
def
get_process
(
self
,
process_name
:
str
)
->
_frida
.
Process
:
"""
Get the process with the given name
:raises ProcessNotFoundError: if the process was not found or there were more than one process with the given name
"""
process_name_lc
=
process_name
.
lower
()
matching
=
[
process
for
process
in
self
.
_impl
.
enumerate_processes
()
if
fnmatch
.
fnmatchcase
(
process
.
name
.
lower
(),
process_name_lc
)
]
if
len
(
matching
)
==
1
:
return
matching
[
0
]
elif
len
(
matching
)
>
1
:
matches_list
=
", "
.
join
([
f"
{
process
.
name
}
(pid:
{
process
.
pid
}
)"
for
process
in
matching
])
raise
_frida
.
ProcessNotFoundError
(
f"ambiguous name; it matches:
{
matches_list
}
"
)
else
:
raise
_frida
.
ProcessNotFoundError
(
f"unable to find process with name '
{
process_name
}
'"
)
@
cancellable
def
enable_spawn_gating
(
self
)
->
None
:
"""
Enable spawn gating
"""
self
.
_impl
.
enable_spawn_gating
()
@
cancellable
def
disable_spawn_gating
(
self
)
->
None
:
"""
Disable spawn gating
"""
self
.
_impl
.
disable_spawn_gating
()
@
cancellable
def
enumerate_pending_spawn
(
self
)
->
List
[
_frida
.
Spawn
]:
"""
Enumerate pending spawn
"""
return
self
.
_impl
.
enumerate_pending_spawn
()
@
cancellable
def
enumerate_pending_children
(
self
)
->
List
[
_frida
.
Child
]:
"""
Enumerate pending children
"""
return
self
.
_impl
.
enumerate_pending_children
()
@
cancellable
def
spawn
(
self
,
program
:
Union
[
str
,
List
[
Union
[
str
,
bytes
]],
Tuple
[
Union
[
str
,
bytes
]]],
argv
:
Union
[
None
,
List
[
Union
[
str
,
bytes
]],
Tuple
[
Union
[
str
,
bytes
]]]
=
None
,
envp
:
Optional
[
Dict
[
str
,
str
]]
=
None
,
env
:
Optional
[
Dict
[
str
,
str
]]
=
None
,
cwd
:
Optional
[
str
]
=
None
,
stdio
:
Optional
[
str
]
=
None
,
**
kwargs
:
Any
,
)
->
int
:
"""
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL