-
-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathsession.vala
2104 lines (1693 loc) · 58.4 KB
/
session.vala
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
namespace Frida {
[DBus (name = "re.frida.HostSession16")]
public interface HostSession : Object {
public abstract async void ping (uint interval_seconds, Cancellable? cancellable) throws GLib.Error;
public abstract async HashTable<string, Variant> query_system_parameters (Cancellable? cancellable) throws GLib.Error;
public abstract async HostApplicationInfo get_frontmost_application (HashTable<string, Variant> options,
Cancellable? cancellable) throws GLib.Error;
public abstract async HostApplicationInfo[] enumerate_applications (HashTable<string, Variant> options,
Cancellable? cancellable) throws GLib.Error;
public abstract async HostProcessInfo[] enumerate_processes (HashTable<string, Variant> options,
Cancellable? cancellable) throws GLib.Error;
public abstract async void enable_spawn_gating (Cancellable? cancellable) throws GLib.Error;
public abstract async void disable_spawn_gating (Cancellable? cancellable) throws GLib.Error;
public abstract async HostSpawnInfo[] enumerate_pending_spawn (Cancellable? cancellable) throws GLib.Error;
public abstract async HostChildInfo[] enumerate_pending_children (Cancellable? cancellable) throws GLib.Error;
public abstract async uint spawn (string program, HostSpawnOptions options, Cancellable? cancellable) throws GLib.Error;
public abstract async void input (uint pid, uint8[] data, Cancellable? cancellable) throws GLib.Error;
public abstract async void resume (uint pid, Cancellable? cancellable) throws GLib.Error;
public abstract async void kill (uint pid, Cancellable? cancellable) throws GLib.Error;
public abstract async AgentSessionId attach (uint pid, HashTable<string, Variant> options,
Cancellable? cancellable) throws GLib.Error;
public abstract async void reattach (AgentSessionId id, Cancellable? cancellable) throws GLib.Error;
public abstract async InjectorPayloadId inject_library_file (uint pid, string path, string entrypoint, string data,
Cancellable? cancellable) throws GLib.Error;
public abstract async InjectorPayloadId inject_library_blob (uint pid, uint8[] blob, string entrypoint, string data,
Cancellable? cancellable) throws GLib.Error;
public signal void spawn_added (HostSpawnInfo info);
public signal void spawn_removed (HostSpawnInfo info);
public signal void child_added (HostChildInfo info);
public signal void child_removed (HostChildInfo info);
public signal void process_crashed (CrashInfo crash);
public signal void output (uint pid, int fd, uint8[] data);
public signal void agent_session_detached (AgentSessionId id, SessionDetachReason reason, CrashInfo crash);
public signal void uninjected (InjectorPayloadId id);
}
[DBus (name = "re.frida.AgentSessionProvider16")]
public interface AgentSessionProvider : Object {
public abstract async void open (AgentSessionId id, HashTable<string, Variant> options,
Cancellable? cancellable) throws GLib.Error;
#if !WINDOWS
public abstract async void migrate (AgentSessionId id, GLib.Socket to_socket, Cancellable? cancellable) throws GLib.Error;
#endif
public abstract async void unload (Cancellable? cancellable) throws GLib.Error;
public signal void opened (AgentSessionId id);
public signal void closed (AgentSessionId id);
public signal void eternalized ();
public signal void child_gating_changed (uint subscriber_count);
}
[DBus (name = "re.frida.AgentSession16")]
public interface AgentSession : Object {
public abstract async void close (Cancellable? cancellable) throws GLib.Error;
public abstract async void interrupt (Cancellable? cancellable) throws GLib.Error;
public abstract async void resume (uint rx_batch_id, Cancellable? cancellable, out uint tx_batch_id) throws GLib.Error;
public abstract async void enable_child_gating (Cancellable? cancellable) throws GLib.Error;
public abstract async void disable_child_gating (Cancellable? cancellable) throws GLib.Error;
public abstract async AgentScriptId create_script (string source, HashTable<string, Variant> options,
Cancellable? cancellable) throws GLib.Error;
public abstract async AgentScriptId create_script_from_bytes (uint8[] bytes, HashTable<string, Variant> options,
Cancellable? cancellable) throws GLib.Error;
public abstract async uint8[] compile_script (string source, HashTable<string, Variant> options,
Cancellable? cancellable) throws GLib.Error;
public abstract async uint8[] snapshot_script (string embed_script, HashTable<string, Variant> options,
Cancellable? cancellable) throws GLib.Error;
public abstract async void destroy_script (AgentScriptId script_id, Cancellable? cancellable) throws GLib.Error;
public abstract async void load_script (AgentScriptId script_id, Cancellable? cancellable) throws GLib.Error;
public abstract async void eternalize_script (AgentScriptId script_id, Cancellable? cancellable) throws GLib.Error;
public abstract async void enable_debugger (AgentScriptId script_id, Cancellable? cancellable) throws GLib.Error;
public abstract async void disable_debugger (AgentScriptId script_id, Cancellable? cancellable) throws GLib.Error;
public abstract async void post_messages (AgentMessage[] messages, uint batch_id,
Cancellable? cancellable) throws GLib.Error;
public abstract async PortalMembershipId join_portal (string address, HashTable<string, Variant> options,
Cancellable? cancellable) throws GLib.Error;
public abstract async void leave_portal (PortalMembershipId membership_id, Cancellable? cancellable) throws GLib.Error;
public abstract async void offer_peer_connection (string offer_sdp, HashTable<string, Variant> peer_options,
Cancellable? cancellable, out string answer_sdp) throws GLib.Error;
public abstract async void add_candidates (string[] candidate_sdps, Cancellable? cancellable) throws GLib.Error;
public abstract async void notify_candidate_gathering_done (Cancellable? cancellable) throws GLib.Error;
public abstract async void begin_migration (Cancellable? cancellable) throws GLib.Error;
public abstract async void commit_migration (Cancellable? cancellable) throws GLib.Error;
public signal void new_candidates (string[] candidate_sdps);
public signal void candidate_gathering_done ();
}
[DBus (name = "re.frida.AgentController16")]
public interface AgentController : Object {
#if !WINDOWS
public abstract async HostChildId prepare_to_fork (uint parent_pid, Cancellable? cancellable, out uint parent_injectee_id,
out uint child_injectee_id, out GLib.Socket child_socket) throws GLib.Error;
#endif
public abstract async HostChildId prepare_to_specialize (uint pid, string identifier, Cancellable? cancellable,
out uint specialized_injectee_id, out string specialized_pipe_address) throws GLib.Error;
public abstract async void recreate_agent_thread (uint pid, uint injectee_id, Cancellable? cancellable) throws GLib.Error;
public abstract async void wait_for_permission_to_resume (HostChildId id, HostChildInfo info,
Cancellable? cancellable) throws GLib.Error;
public abstract async void prepare_to_exec (HostChildInfo info, Cancellable? cancellable) throws GLib.Error;
public abstract async void cancel_exec (uint pid, Cancellable? cancellable) throws GLib.Error;
public abstract async void acknowledge_spawn (HostChildInfo info, SpawnStartState start_state,
Cancellable? cancellable) throws GLib.Error;
}
[DBus (name = "re.frida.AgentMessageSink16")]
public interface AgentMessageSink : Object {
public abstract async void post_messages (AgentMessage[] messages, uint batch_id,
Cancellable? cancellable) throws GLib.Error;
}
public struct AgentMessage {
public AgentMessageKind kind;
public AgentScriptId script_id;
public string text;
public bool has_data;
public uint8[] data;
public AgentMessage (AgentMessageKind kind, AgentScriptId script_id, string text, bool has_data, uint8[] data) {
this.kind = kind;
this.script_id = script_id;
this.text = text;
this.has_data = has_data;
this.data = data;
}
}
public enum AgentMessageKind {
SCRIPT = 1,
DEBUGGER
}
public class AgentMessageTransmitter : Object {
public signal void closed ();
public signal void new_candidates (string[] candidate_sdps);
public signal void candidate_gathering_done ();
public weak AgentSession agent_session {
get;
construct;
}
public uint persist_timeout {
get;
construct;
}
public AgentMessageSink? message_sink {
get;
set;
}
public MainContext frida_context {
get;
construct;
}
public MainContext dbus_context {
get;
construct;
}
private Promise<bool>? close_request;
private State state = LIVE;
private TimeoutSource? expiry_timer;
private uint last_rx_batch_id = 0;
private Gee.LinkedList<PendingMessage> pending_messages = new Gee.LinkedList<PendingMessage> ();
private int next_serial = 1;
private uint pending_deliveries = 0;
private Cancellable delivery_cancellable = new Cancellable ();
#if HAVE_NICE
private Nice.Agent? nice_agent;
private uint nice_stream_id;
private uint nice_component_id;
private SctpConnection? nice_iostream;
private DBusConnection? nice_connection;
private uint nice_registration_id;
#endif
private AgentMessageSink? nice_message_sink;
private Cancellable nice_cancellable = new Cancellable ();
private enum State {
LIVE,
INTERRUPTED
}
public AgentMessageTransmitter (AgentSession agent_session, uint persist_timeout, MainContext frida_context,
MainContext dbus_context) {
Object (
agent_session: agent_session,
persist_timeout: persist_timeout,
frida_context: frida_context,
dbus_context: dbus_context
);
}
construct {
assert (frida_context != null);
assert (dbus_context != null);
}
public async void close (Cancellable? cancellable) throws IOError {
while (close_request != null) {
try {
yield close_request.future.wait_async (cancellable);
return;
} catch (GLib.Error e) {
assert (e is IOError.CANCELLED);
cancellable.set_error_if_cancelled ();
}
}
close_request = new Promise<bool> ();
nice_cancellable.cancel ();
delivery_cancellable.cancel ();
yield teardown_peer_connection_and_emit_closed ();
message_sink = null;
close_request.resolve (true);
}
public void check_okay_to_receive () throws Error {
if (state == INTERRUPTED)
throw new Error.INVALID_OPERATION ("Cannot receive messages while interrupted");
}
public void interrupt () throws Error {
if (persist_timeout == 0 || expiry_timer != null)
throw new Error.INVALID_OPERATION ("Invalid operation");
state = INTERRUPTED;
delivery_cancellable.cancel ();
expiry_timer = new TimeoutSource.seconds (persist_timeout);
expiry_timer.set_callback (() => {
close.begin (null);
return false;
});
expiry_timer.attach (frida_context);
}
public void resume (uint rx_batch_id, out uint tx_batch_id) throws Error {
if (persist_timeout == 0 || expiry_timer == null)
throw new Error.INVALID_OPERATION ("Invalid operation");
if (rx_batch_id != 0) {
PendingMessage? m;
while ((m = pending_messages.peek ()) != null && m.delivery_attempts > 0 && m.serial <= rx_batch_id) {
pending_messages.poll ();
}
}
expiry_timer.destroy ();
expiry_timer = null;
delivery_cancellable = new Cancellable ();
state = LIVE;
schedule_on_frida_thread (() => {
maybe_deliver_pending_messages ();
return false;
});
tx_batch_id = last_rx_batch_id;
}
public void notify_rx_batch_id (uint batch_id) throws Error {
if (state == INTERRUPTED)
throw new Error.INVALID_OPERATION ("Cannot receive messages while interrupted");
last_rx_batch_id = batch_id;
}
#if HAVE_NICE
public async void offer_peer_connection (string offer_sdp, HashTable<string, Variant> peer_options,
Cancellable? cancellable, out string answer_sdp) throws Error, IOError {
var offer = PeerSessionDescription.parse (offer_sdp);
var agent = new Nice.Agent.full (dbus_context, Nice.Compatibility.RFC5245, ICE_TRICKLE);
agent.set_software ("Frida");
agent.controlling_mode = false;
agent.ice_tcp = false;
uint stream_id = agent.add_stream (1);
if (stream_id == 0)
throw new Error.NOT_SUPPORTED ("Unable to add stream");
uint component_id = 1;
agent.set_stream_name (stream_id, "application");
agent.set_remote_credentials (stream_id, offer.ice_ufrag, offer.ice_pwd);
yield PeerConnection.configure_agent (agent, stream_id, component_id, PeerOptions._deserialize (peer_options),
cancellable);
uint8[] cert_der;
string cert_pem, key_pem;
yield generate_certificate (out cert_der, out cert_pem, out key_pem);
TlsCertificate certificate;
try {
certificate = new TlsCertificate.from_pem (cert_pem + key_pem, -1);
} catch (GLib.Error e) {
assert_not_reached ();
}
var answer = new PeerSessionDescription ();
answer.session_id = PeerSessionId.generate ();
agent.get_local_credentials (stream_id, out answer.ice_ufrag, out answer.ice_pwd);
answer.ice_trickle = offer.ice_trickle;
answer.fingerprint = PeerConnection.compute_certificate_fingerprint (cert_der);
answer.setup = (offer.setup != ACTIVE) ? PeerSetup.ACTIVE : PeerSetup.ACTPASS;
answer.sctp_port = offer.sctp_port;
answer.max_message_size = offer.max_message_size;
answer_sdp = answer.to_sdp ();
if (nice_agent != null)
throw new Error.INVALID_OPERATION ("Peer connection already exists");
nice_agent = agent;
nice_stream_id = stream_id;
nice_component_id = component_id;
schedule_on_dbus_thread (() => {
open_peer_connection.begin (certificate, offer, cancellable);
return false;
});
}
private async void teardown_peer_connection_and_emit_closed () {
schedule_on_frida_thread (() => {
if (nice_agent != null)
close_nice_resources_and_emit_closed.begin ();
else
closed ();
return Source.REMOVE;
});
}
private async void close_nice_resources_and_emit_closed () {
yield close_nice_resources (true);
closed ();
}
private async void close_nice_resources (bool connection_still_alive) {
Nice.Agent? agent = nice_agent;
DBusConnection? conn = nice_connection;
discard_nice_resources ();
if (conn != null && connection_still_alive) {
try {
yield conn.flush ();
yield conn.close ();
} catch (GLib.Error e) {
}
}
if (agent != null) {
schedule_on_dbus_thread (() => {
agent.close_async.begin ();
schedule_on_frida_thread (() => {
close_nice_resources.callback ();
return false;
});
return false;
});
yield;
}
}
private void discard_nice_resources () {
nice_cancellable.cancel ();
nice_cancellable = new Cancellable ();
nice_message_sink = null;
if (nice_registration_id != 0) {
nice_connection.unregister_object (nice_registration_id);
nice_registration_id = 0;
}
if (nice_connection != null) {
nice_connection.on_closed.disconnect (on_nice_connection_closed);
nice_connection = null;
}
nice_iostream = null;
nice_component_id = 0;
nice_stream_id = 0;
nice_agent = null;
}
private async void open_peer_connection (TlsCertificate certificate, PeerSessionDescription offer,
Cancellable? cancellable) {
Nice.Agent agent = nice_agent;
DtlsConnection? tc = null;
ulong candidate_handler = 0;
ulong gathering_handler = 0;
ulong accept_handler = 0;
try {
agent.component_state_changed.connect (on_component_state_changed);
var pending_candidates = new Gee.ArrayList<string> ();
candidate_handler = agent.new_candidate_full.connect (candidate => {
string candidate_sdp = agent.generate_local_candidate_sdp (candidate);
pending_candidates.add (candidate_sdp);
if (pending_candidates.size == 1) {
schedule_on_dbus_thread (() => {
var stolen_candidates = pending_candidates;
pending_candidates = new Gee.ArrayList<string> ();
schedule_on_frida_thread (() => {
int n = stolen_candidates.size;
var sdps = new string[n + 1];
for (int i = 0; i != n; i++)
sdps[i] = stolen_candidates[i];
new_candidates (sdps[0:n]);
return false;
});
return false;
});
}
});
gathering_handler = agent.candidate_gathering_done.connect (stream_id => {
schedule_on_dbus_thread (() => {
schedule_on_frida_thread (() => {
candidate_gathering_done ();
return false;
});
return false;
});
});
if (!agent.gather_candidates (nice_stream_id))
throw new Error.NOT_SUPPORTED ("Unable to gather local candidates");
var socket = new PeerSocket (agent, nice_stream_id, nice_component_id);
if (offer.setup == ACTIVE) {
tc = DtlsServerConnection.new (socket, certificate);
} else {
tc = DtlsClientConnection.new (socket, null);
tc.set_certificate (certificate);
}
tc.set_database (null);
accept_handler = tc.accept_certificate.connect ((peer_cert, errors) => {
return PeerConnection.compute_certificate_fingerprint (peer_cert.certificate.data) == offer.fingerprint;
});
yield tc.handshake_async (Priority.DEFAULT, nice_cancellable);
nice_iostream = new SctpConnection (tc, offer.setup, offer.sctp_port, offer.max_message_size);
schedule_on_frida_thread (() => {
complete_peer_connection.begin ();
return false;
});
} catch (GLib.Error e) {
schedule_on_frida_thread (() => {
close_nice_resources.begin (false);
return false;
});
} finally {
if (accept_handler != 0)
tc.disconnect (accept_handler);
if (gathering_handler != 0)
agent.disconnect (gathering_handler);
if (candidate_handler != 0)
agent.disconnect (candidate_handler);
}
}
private async void complete_peer_connection () {
try {
nice_connection = yield new DBusConnection (nice_iostream, null, DELAY_MESSAGE_PROCESSING, null,
nice_cancellable);
nice_connection.on_closed.connect (on_nice_connection_closed);
try {
nice_registration_id = nice_connection.register_object (ObjectPath.AGENT_SESSION, agent_session);
} catch (IOError io_error) {
assert_not_reached ();
}
nice_connection.start_message_processing ();
nice_message_sink = yield nice_connection.get_proxy (null, ObjectPath.AGENT_MESSAGE_SINK,
DO_NOT_LOAD_PROPERTIES, null);
} catch (GLib.Error e) {
close_nice_resources.begin (false);
}
}
private void on_component_state_changed (uint stream_id, uint component_id, Nice.ComponentState state) {
switch (state) {
case FAILED:
nice_cancellable.cancel ();
break;
default:
break;
}
}
public void add_candidates (string[] candidate_sdps) throws Error {
Nice.Agent? agent = nice_agent;
if (agent == null)
throw new Error.INVALID_OPERATION ("No peer connection in progress");
string[] candidate_sdps_copy = candidate_sdps;
schedule_on_dbus_thread (() => {
var candidates = new SList<Nice.Candidate> ();
foreach (unowned string sdp in candidate_sdps_copy) {
var candidate = agent.parse_remote_candidate_sdp (nice_stream_id, sdp);
if (candidate != null)
candidates.append (candidate);
}
agent.set_remote_candidates (nice_stream_id, nice_component_id, candidates);
return false;
});
}
public void notify_candidate_gathering_done () throws Error {
Nice.Agent? agent = nice_agent;
if (agent == null)
throw new Error.INVALID_OPERATION ("No peer connection in progress");
schedule_on_dbus_thread (() => {
agent.peer_candidate_gathering_done (nice_stream_id);
return false;
});
}
private void on_nice_connection_closed (DBusConnection connection, bool remote_peer_vanished, GLib.Error? error) {
handle_nice_connection_closure.begin ();
}
private async void handle_nice_connection_closure () {
yield close_nice_resources (false);
if (persist_timeout != 0) {
try {
interrupt ();
} catch (Error e) {
}
} else {
close.begin (null);
}
}
#else
public async void offer_peer_connection (string offer_sdp, HashTable<string, Variant> peer_options,
Cancellable? cancellable, out string answer_sdp) throws Error, IOError {
throw new Error.NOT_SUPPORTED ("Peer-to-peer support not available due to build configuration");
}
private async void teardown_peer_connection_and_emit_closed () {
schedule_on_frida_thread (() => {
closed ();
return Source.REMOVE;
});
}
public void add_candidates (string[] candidate_sdps) throws Error {
}
public void notify_candidate_gathering_done () throws Error {
}
#endif
public void begin_migration () {
state = INTERRUPTED;
}
public void commit_migration () {
if (expiry_timer != null)
return;
state = LIVE;
maybe_deliver_pending_messages ();
}
public void post_message_from_script (AgentScriptId script_id, string json, Bytes? data) {
pending_messages.offer (new PendingMessage (next_serial++, AgentMessageKind.SCRIPT, script_id, json, data));
maybe_deliver_pending_messages ();
}
public void post_message_from_debugger (AgentScriptId script_id, string message) {
pending_messages.offer (new PendingMessage (next_serial++, AgentMessageKind.DEBUGGER, script_id, message));
maybe_deliver_pending_messages ();
}
private void maybe_deliver_pending_messages () {
if (state != LIVE)
return;
AgentMessageSink? sink = (nice_message_sink != null) ? nice_message_sink : message_sink;
if (sink == null)
return;
if (pending_messages.is_empty)
return;
var batch = new Gee.ArrayList<PendingMessage> ();
void * items = null;
int n_items = 0;
size_t total_size = 0;
size_t max_size = 4 * 1024 * 1024;
PendingMessage? m;
while ((m = pending_messages.peek ()) != null) {
size_t message_size = m.estimate_size_in_bytes ();
if (total_size + message_size > max_size && !batch.is_empty)
break;
pending_messages.poll ();
batch.add (m);
n_items++;
items = realloc (items, n_items * sizeof (AgentMessage));
AgentMessage * am = (AgentMessage *) items + n_items - 1;
am->kind = m.kind;
am->script_id = m.script_id;
*((void **) &am->text) = m.text;
unowned Bytes? data = m.data;
am->has_data = data != null;
*((void **) &am->data) = am->has_data ? data.get_data () : null;
am->data.length = am->has_data ? data.length : 0;
total_size += message_size;
}
if (persist_timeout == 0)
emit_batch (sink, batch, items);
else
deliver_batch.begin (sink, batch, items);
}
private void emit_batch (AgentMessageSink sink, Gee.ArrayList<PendingMessage> messages, void * items) {
unowned AgentMessage[] items_arr = (AgentMessage[]) items;
items_arr.length = messages.size;
sink.post_messages.begin (items_arr, 0, delivery_cancellable);
free (items);
}
private async void deliver_batch (AgentMessageSink sink, Gee.ArrayList<PendingMessage> messages, void * items) {
bool success = false;
pending_deliveries++;
try {
int n = messages.size;
foreach (var message in messages)
message.delivery_attempts++;
unowned AgentMessage[] items_arr = (AgentMessage[]) items;
items_arr.length = n;
uint batch_id = messages[n - 1].serial;
yield sink.post_messages (items_arr, batch_id, delivery_cancellable);
success = true;
} catch (GLib.Error e) {
pending_messages.add_all (messages);
pending_messages.sort ((a, b) => a.serial - b.serial);
} finally {
pending_deliveries--;
if (pending_deliveries == 0 && success)
next_serial = 1;
free (items);
}
}
protected void schedule_on_frida_thread (owned SourceFunc function) {
var source = new IdleSource ();
source.set_callback ((owned) function);
source.attach (frida_context);
}
protected void schedule_on_dbus_thread (owned SourceFunc function) {
var source = new IdleSource ();
source.set_callback ((owned) function);
source.attach (dbus_context);
}
private class PendingMessage {
public int serial;
public AgentMessageKind kind;
public AgentScriptId script_id;
public string text;
public Bytes? data;
public uint delivery_attempts;
public PendingMessage (int serial, AgentMessageKind kind, AgentScriptId script_id, string text,
Bytes? data = null) {
this.serial = serial;
this.kind = kind;
this.script_id = script_id;
this.text = text;
this.data = data;
}
public size_t estimate_size_in_bytes () {
return sizeof (AgentMessage) + text.length + 1 + ((data != null) ? data.length : 0);
}
}
}
[DBus (name = "re.frida.TransportBroker16")]
public interface TransportBroker : Object {
public abstract async void open_tcp_transport (AgentSessionId id, Cancellable? cancellable, out uint16 port,
out string token) throws GLib.Error;
}
[DBus (name = "re.frida.PortalSession16")]
public interface PortalSession : Object {
public abstract async void join (HostApplicationInfo app, SpawnStartState current_state,
AgentSessionId[] interrupted_sessions, HashTable<string, Variant> options, Cancellable? cancellable,
out SpawnStartState next_state) throws GLib.Error;
public signal void resume ();
public signal void kill ();
}
[DBus (name = "re.frida.BusSession16")]
public interface BusSession : Object {
public abstract async void attach (Cancellable? cancellable) throws GLib.Error;
public abstract async void post (string json, bool has_data, uint8[] data, Cancellable? cancellable) throws GLib.Error;
public signal void message (string json, bool has_data, uint8[] data);
}
[DBus (name = "re.frida.AuthenticationService16")]
public interface AuthenticationService : Object {
public abstract async string authenticate (string token, Cancellable? cancellable) throws GLib.Error;
}
public class StaticAuthenticationService : Object, AuthenticationService {
public string token_hash {
get;
construct;
}
public StaticAuthenticationService (string token) {
Object (token_hash: Checksum.compute_for_string (SHA256, token));
}
public async string authenticate (string token, Cancellable? cancellable) throws Error, IOError {
string input_hash = Checksum.compute_for_string (SHA256, token);
uint accumulator = 0;
for (uint i = 0; i != input_hash.length; i++) {
accumulator |= input_hash[i] ^ token_hash[i];
}
if (accumulator != 0)
throw new Error.INVALID_ARGUMENT ("Incorrect token");
return "{}";
}
}
public class NullAuthenticationService : Object, AuthenticationService {
public async string authenticate (string token, Cancellable? cancellable) throws Error, IOError {
throw new Error.INVALID_OPERATION ("Authentication not expected");
}
}
public class UnauthorizedHostSession : Object, HostSession {
public async void ping (uint interval_seconds, Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
public async HashTable<string, Variant> query_system_parameters (Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
public async HostApplicationInfo get_frontmost_application (HashTable<string, Variant> options,
Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
public async HostApplicationInfo[] enumerate_applications (HashTable<string, Variant> options,
Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
public async HostProcessInfo[] enumerate_processes (HashTable<string, Variant> options,
Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
public async void enable_spawn_gating (Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
public async void disable_spawn_gating (Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
public async HostSpawnInfo[] enumerate_pending_spawn (Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
public async HostChildInfo[] enumerate_pending_children (Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
public async uint spawn (string program, HostSpawnOptions options, Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
public async void input (uint pid, uint8[] data, Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
public async void resume (uint pid, Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
public async void kill (uint pid, Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
public async AgentSessionId attach (uint pid, HashTable<string, Variant> options,
Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
public async void reattach (AgentSessionId id, Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
public async InjectorPayloadId inject_library_file (uint pid, string path, string entrypoint, string data,
Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
public async InjectorPayloadId inject_library_blob (uint pid, uint8[] blob, string entrypoint, string data,
Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
}
public class UnauthorizedPortalSession : Object, PortalSession {
public async void join (HostApplicationInfo app, SpawnStartState current_state,
AgentSessionId[] interrupted_sessions, HashTable<string, Variant> options,
Cancellable? cancellable, out SpawnStartState next_state) throws Error, IOError {
throw_not_authorized ();
}
}
public class UnauthorizedBusSession : Object, BusSession {
public async void attach (Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
public async void post (string json, bool has_data, uint8[] data, Cancellable? cancellable) throws Error, IOError {
throw_not_authorized ();
}
}
[NoReturn]
private void throw_not_authorized () throws Error {
throw new Error.PERMISSION_DENIED ("Not authorized, authentication required");
}
public enum Realm {
NATIVE,
EMULATED;
public static Realm from_nick (string nick) throws Error {
return Marshal.enum_from_nick<Realm> (nick);
}
public string to_nick () {
return Marshal.enum_to_nick<Realm> (this);
}
}
public enum SpawnStartState {
RUNNING,
SUSPENDED;
public static SpawnStartState from_nick (string nick) throws Error {
return Marshal.enum_from_nick<SpawnStartState> (nick);
}
public string to_nick () {
return Marshal.enum_to_nick<SpawnStartState> (this);
}
}
public enum UnloadPolicy {
IMMEDIATE,
RESIDENT,
DEFERRED;
public static UnloadPolicy from_nick (string nick) throws Error {
return Marshal.enum_from_nick<UnloadPolicy> (nick);
}
public string to_nick () {
return Marshal.enum_to_nick<UnloadPolicy> (this);
}
}
public struct InjectorPayloadId {
public uint handle;
public InjectorPayloadId (uint handle) {
this.handle = handle;
}
public static uint hash (InjectorPayloadId? id) {
return direct_hash ((void *) id.handle);
}
public static bool equal (InjectorPayloadId? a, InjectorPayloadId? b) {
return a.handle == b.handle;
}
}
public struct MappedLibraryBlob {
public uint64 address;
public uint size;
public uint allocated_size;
public MappedLibraryBlob (uint64 address, uint size, uint allocated_size) {
this.address = address;
this.size = size;
this.allocated_size = allocated_size;
}
}
#if DARWIN
public struct DarwinInjectorState {
public Gum.MemoryRange? mapped_range;
}
#endif
#if LINUX
public struct LinuxInjectorState {
public int frida_ctrlfd;
public int agent_ctrlfd;
}
#endif
#if LINUX || FREEBSD
public struct PosixInjectorState {
public int fifo_fd;
}
#endif
public enum SessionDetachReason {
APPLICATION_REQUESTED = 1,
PROCESS_REPLACED,
PROCESS_TERMINATED,
CONNECTION_TERMINATED,
DEVICE_LOST;
public static SessionDetachReason from_nick (string nick) throws Error {
return Marshal.enum_from_nick<SessionDetachReason> (nick);
}