forked from cseagle/sk3wldbg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sk3wldbg.cpp
1338 lines (1229 loc) · 44.8 KB
/
sk3wldbg.cpp
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
/*
Source for Sk3wlDbg IdaPro plugin
Copyright (c) 2016 Chris Eagle
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* This is the Sk3wlDbg plugin module
*
* It is known to compile with
*
* - Visual Studio 2010, Linux g++, OS X - clang
*
*/
#ifdef __NT__
#include <windows.h>
#include <winnt.h>
#include <wincrypt.h>
#else
//#ifndef __NT__
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#endif
#ifdef PACKED
#undef PACKED
#endif
#define USE_STANDARD_FILE_FUNCTIONS
#include <unicorn/unicorn.h>
#include <pro.h>
#include <ida.hpp>
#include <idp.hpp>
#include <bytes.hpp>
#include <auto.hpp>
#include <loader.hpp>
#include <kernwin.hpp>
#include <typeinf.hpp>
#include <nalt.hpp>
#include <segment.hpp>
#include <srarea.hpp>
#include <typeinf.hpp>
#include <struct.hpp>
#include <entry.hpp>
#include <dbg.hpp>
#include <idd.hpp>
#include <ua.hpp>
#include "sk3wldbg.h"
#include "loader.h"
static unsigned int ida_to_uc_perms_map[] = {
UC_PROT_NONE, UC_PROT_EXEC, UC_PROT_WRITE, UC_PROT_EXEC | UC_PROT_WRITE,
UC_PROT_READ, UC_PROT_EXEC | UC_PROT_READ, UC_PROT_READ | UC_PROT_WRITE, UC_PROT_ALL
};
static unsigned int uc_to_ida_perms_map[] = {
0, SEGPERM_READ, SEGPERM_WRITE, SEGPERM_READ | SEGPERM_WRITE,
SEGPERM_EXEC, SEGPERM_EXEC | SEGPERM_READ, SEGPERM_EXEC | SEGPERM_WRITE, SEGPERM_EXEC | SEGPERM_WRITE | SEGPERM_WRITE
};
/// Initialize debugger.
/// This function is called from the main thread.
/// \return success
bool idaapi uni_init_debugger(const char * /*hostname*/, int /*portnum*/, const char * /*password*/) {
sk3wldbg *uc = (sk3wldbg*)dbg;
msg("uni_init_debugger called\n");
// bool result = uc->open();
msg("uni_init_debugger complete\n");
// return result;
return true;
}
/// Terminate debugger.
/// This function is called from the main thread.
/// \return success
bool idaapi uni_term_debugger(void) {
sk3wldbg *uc = (sk3wldbg*)dbg;
msg("uni_term_debugger called\n");
if (uc->uc) {
uc_emu_stop(uc->uc);
uc->emu_state = RS_TERM;
qsem_post(uc->run_sem);
//***synchronize here to make sure execution thread has stopped
qthread_join(uc->process_thread);
msg("uni_term_debugger thread joined\n");
uc->close();
}
msg("uni_term_debugger complete\n");
return true;
}
bool sk3wldbg::queue_dbg_event(bool is_hardware) {
debug_event_t brk;
brk.eid = ::BREAKPOINT;
brk.pid = the_process;
brk.tid = the_threads.front();
brk.ea = (ea_t)get_pc();
msg("x86 breakpoint hit at: 0x%llx\n", (uint64_t)brk.ea);
brk.handled = true;
brk.bpt.hea = is_hardware ? brk.ea : BADADDR;
brk.bpt.kea = BADADDR;
enqueue_debug_evt(brk);
return true;
}
int idaapi processRunner(void *unicorn) {
sk3wldbg *uc = (sk3wldbg*)unicorn;
//this is going to have to run in a separate thread otherwise other
//debthread functions will never get called
bool done = false;
while (!done) {
qsem_wait(uc->run_sem, -1);
//pick this up every time we start in case user changed the PC manually
uint64_t _pc = uc->get_pc();
switch (uc->emu_state) {
case RS_INIT:
break;
case RS_RUN:
uc->start(_pc);
break;
case RS_PAUSE:
break;
case RS_STEP_INTO:
// msg("processRunner RS_STEP_INTO\n");
uc->step(_pc);
break;
case RS_STEP_OVER:
// msg("processRunner RS_STEP_OVER\n");
uc->step(_pc);
break;
case RS_STEP_OUT:
// msg("processRunner RS_STEP_OUT\n");
uc->step(_pc);
break;
case RS_TERM:
done = true;
continue;
}
}
return 0;
}
/// Return information about the n-th "compatible" running process.
/// If n is 0, the processes list is reinitialized.
/// This function is called from the main thread.
/// \retval 1 ok
/// \retval 0 failed
/// \retval -1 network error
int idaapi uni_process_get_info(int n, process_info_t *info) {
if (n) {
return 0;
}
msg("uni_process_get_info called\n");
sk3wldbg *uc = (sk3wldbg*)dbg;
qstrncpy(info->name, "Unicorn Process", sizeof(info->name));
info->pid = uc->the_process;
return 1;
}
/// Start an executable to debug.
/// This function is called from debthread.
/// \param path path to executable
/// \param args arguments to pass to executable
/// \param startdir current directory of new process
/// \param dbg_proc_flags \ref DBG_PROC_
/// \param input_path path to database input file.
/// (not always the same as 'path' - e.g. if you're analyzing
/// a dll and want to launch an executable that loads it)
/// \param input_file_crc32 CRC value for 'input_path'
/// \retval 1 ok
/// \retval 0 failed
/// \retval -2 file not found (ask for process options)
/// \retval 1 | #CRC32_MISMATCH ok, but the input file crc does not match
/// \retval -1 network error
int idaapi uni_start_process(const char * /*path*/,
const char * /*args*/,
const char * /*startdir*/,
int /*dbg_proc_flags*/,
const char *input_path,
uint32 /*input_file_crc32*/) {
msg("uni_start_process called\n");
sk3wldbg *uc = (sk3wldbg*)dbg;
ea_t init_pc = get_screen_ea();
uc->check_mode(init_pc);
if (!uc->open()) {
//failed to open unicorn instance
return 0;
}
uc->clear_memory();
uc->getRandomBytes(&uc->the_process, 2);
uc->the_process = (uc->the_process % 40000) + 1000;
thid_t a_thread = 0;
do {
uc->getRandomBytes(&a_thread, 2);
a_thread = (a_thread % 40000) + 1000;
} while (a_thread == (thid_t)uc->the_process);
uc->the_threads.push_back(a_thread);
FILE *bin = fopen(input_path, "rb");
bool loaded = false;
if (bin != NULL) {
msg("found input file %s\n", input_path);
if (fseek(bin, 0, SEEK_END) != 0) {
//HUH?
msg("SEEK_END fail\n");
}
else {
long sz = ftell(bin);
void *img = malloc(sz);
if (img == NULL) {
//OOM
msg("image allocate fail\n");
}
else {
msg("reading file of %u bytes\n", (uint32_t)sz);
fseek(bin, 0, SEEK_SET);
if (fread(img, sz, 1, bin) != 1) {
//fail
msg("fread fail\n");
}
else {
loaded = loadImage(uc, img, sz);
}
free(img);
}
}
fclose(bin);
}
if (!loaded) {
//didn't know format, let's try for what we need from IDA
//init memory, by copying from IDA
//May prefer instead to init from file, in which case we need loaders
//Also need to map in a stack and init stack pointer, this will be
//architecture dependent since each arch has its own SP register
//arch specific unicorns also need to set initial register state
segment_t *seg;
msg("uni_start_process copying memory called\n");
for (seg = get_first_seg(); seg != NULL; seg = get_next_seg(seg->startEA)) {
uint64_t exact = seg->endEA - seg->startEA;
uc->map_mem_zero(seg->startEA, seg->endEA, ida_to_uc_perms_map[seg->perm]);
void *buf = qcalloc(exact, 1);
get_many_bytes_ex(seg->startEA, buf, exact, NULL);
uc_err err = uc_mem_write(uc->uc, seg->startEA, buf, (size_t)exact);
if (err != UC_ERR_OK) {
msg("Failed on uc_mem_write() with error returned %u: %s\n", err, uc_strerror(err));
}
qfree(buf);
}
}
if (uc->get_sp() == 0) {
//need a stack too, just sling it somewhere
//add it to uc->memory
unsigned int stack_top = 0xc0000000;
uc->map_mem_zero(stack_top - 0x100000, stack_top, UC_PROT_READ | UC_PROT_WRITE);
stack_top -= 16;
uc->set_sp(stack_top);
}
//init registers
//register unicorn hooks
//need other ways to set PC, from start, user specified
uc->set_pc(init_pc);
uc->emu_state = RS_INIT;
//this is going to have to run in a separate thread otherwise other
//debthread functions will never get called
uc->process_thread = qthread_create(processRunner, uc);
if (uc->process_thread == NULL) {
//error failed to create thread
msg(PLUGIN_NAME": Failed to start process.\n");
return 0;
}
debug_event_t start;
start.eid = PROCESS_START;
start.pid = uc->the_process;
start.tid = uc->the_threads.front();
start.ea = BADADDR;
start.handled = true;
qstrncpy(start.modinfo.name, "Unicorn Process", sizeof(start.modinfo.name));
start.modinfo.base = inf.minEA;
start.modinfo.size = inf.maxEA - inf.minEA;
start.modinfo.rebase_to = BADADDR;
uc->enqueue_debug_evt(start);
msg("uni_start_process complete\n");
return 1;
}
/// Attach to an existing running process.
/// event_id should be equal to -1 if not attaching to a crashed process.
/// This function is called from debthread.
/// \retval 1 ok
/// \retval 0 failed
/// \retval -1 network error
int idaapi uni_attach_process(pid_t /*pid*/, int /*event_id*/, int /*dbg_proc_flags*/) {
//can't do this with unicorn
msg("uni_attach_process called\n");
return 0;
}
/// Detach from the debugged process.
/// May be called while the process is running or suspended.
/// Must detach from the process in any case.
/// The kernel will repeatedly call get_debug_event() and until ::PROCESS_DETACH.
/// In this mode, all other events will be automatically handled and process will be resumed.
/// This function is called from debthread.
/// \retval 1 ok
/// \retval 0 failed
/// \retval -1 network error
int idaapi uni_detach_process(void) {
msg("uni_detach_process called\n");
//for unicorn we will just terminate session if user wants to detach
sk3wldbg *uc = (sk3wldbg*)dbg;
uc_emu_stop(uc->uc);
uc->emu_state = RS_TERM;
qsem_post(uc->run_sem);
//***synchronize here to make sure execution thread has stopped
qthread_join(uc->process_thread);
msg("uni_detach_process thread joined\n");
debug_event_t detach;
detach.eid = PROCESS_DETACH;
detach.pid = uc->the_process;
detach.tid = uc->the_threads.front();
detach.ea = BADADDR;
detach.handled = true;
uc->enqueue_debug_evt(detach);
msg("uni_detach_process complete\n");
return 1;
}
/// Rebase database if the debugged program has been rebased by the system.
/// This function is called from the main thread.
void idaapi uni_rebase_if_required_to(ea_t /*new_base*/) {
msg("uni_rebase_if_required_to called: NOT IMPLEMENTED\n");
}
/// Prepare to pause the process.
/// Normally the next get_debug_event() will pause the process
/// If the process is sleeping then the pause will not occur
/// until the process wakes up. The interface should take care of
/// this situation.
/// If this function is absent, then it won't be possible to pause the program.
/// This function is called from debthread.
/// \retval 1 ok
/// \retval 0 failed
/// \retval -1 network error
int idaapi uni_prepare_to_pause_process(void) {
sk3wldbg *uc = (sk3wldbg*)dbg;
msg("uni_prepare_to_pause_process called\n");
uc->pause();
debug_event_t pause;
pause.eid = ::PROCESS_SUSPEND;
pause.pid = uc->the_process;
pause.tid = uc->the_threads.front();
pause.ea = inf.minEA;
pause.info[0] = 0;
uc->enqueue_debug_evt(pause);
msg("uni_prepare_to_pause_process complete\n");
return 1;
}
/// Stop the process.
/// May be called while the process is running or suspended.
/// Must terminate the process in any case.
/// The kernel will repeatedly call get_debug_event() and until ::PROCESS_EXIT.
/// In this mode, all other events will be automatically handled and process will be resumed.
/// This function is called from debthread.
/// \retval 1 ok
/// \retval 0 failed
/// \retval -1 network error
int idaapi uni_exit_process(void) {
sk3wldbg *uc = (sk3wldbg*)dbg;
msg("uni_exit_process called\n");
uc_emu_stop(uc->uc);
uc->emu_state = RS_TERM;
qsem_post(uc->run_sem);
//***synchronize here to make sure execution thread has stopped
qthread_join(uc->process_thread);
msg("uni_exit_process thread joined\n");
debug_event_t stop;
stop.eid = ::PROCESS_EXIT;
stop.pid = uc->the_process;
stop.tid = uc->the_threads.front();
stop.ea = inf.minEA;
stop.exit_code = 0;
uc->enqueue_debug_evt(stop);
return 1;
}
/// Get a pending debug event and suspend the process.
/// This function will be called regularly by IDA.
/// This function is called from debthread.
/// IMPORTANT: commdbg does not expect immediately after a BPT-related event
/// any other event with the same thread/IP - this can cause erroneous
/// restoring of a breakpoint before resume
/// (the bug was encountered 24.02.2015 in pc_linux_upx.elf)
gdecode_t idaapi uni_get_debug_event(debug_event_t *event, int /*timeout_ms*/) {
sk3wldbg *uc = (sk3wldbg*)dbg;
if (uc->debug_queue_len() == 0) {
return GDE_NO_EVENT;
}
else {
uc->dequeue_debug_evt(event);
msg("uni_get_debug_event called returning: eid = 0x%08x\n", event->eid);
//should we ever act on event->eid here?
return uc->debug_queue_len() > 0 ? GDE_MANY_EVENTS : GDE_ONE_EVENT;
}
}
/// Continue after handling the event.
/// This function is called from debthread.
/// \retval 1 ok
/// \retval 0 failed
/// \retval -1 network error
int idaapi uni_continue_after_event(const debug_event_t *event) {
sk3wldbg *uc = (sk3wldbg*)dbg;
msg("uni_continue_after_event called: eid = 0x%08x\n", event->eid);
if (event == NULL || event->eid == 2) {// || uc->dbg_evt_list.size() == 0) {
return 1;
}
switch (event->eid) {
case PROCESS_START:
msg("uni_continue_after_event resuming execution\n");
uc->emu_state = RS_RUN;
qsem_post(uc->run_sem);
break;
case PROCESS_EXIT:
msg("uni_continue_after_event PROCESS_EXIT\n");
uc->emu_state = RS_TERM;
qsem_post(uc->run_sem);
break;
case THREAD_START:
msg("uni_continue_after_event THREAD_START\n");
break;
case THREAD_EXIT:
msg("uni_continue_after_event THREAD_EXIT\n");
break;
case BREAKPOINT:
msg("uni_continue_after_event BREAKPOINT\n");
//resume from breakpoint, replace instruction in memory, single step resume again?
qsem_post(uc->run_sem);
break;
case STEP:
msg("uni_continue_after_event trying to step\n");
//state should have been set in set_resume_mode
qsem_post(uc->run_sem);
/*
uc_err err = uc_emu_start(uc->uc, uc->get_pc(), 0, 0, 1);
if (err != UC_ERR_OK) {
return false;
}
debug_event_t cont;
cont.eid = ::STEP;
cont.pid = uc->the_process;
cont.tid = uc->the_threads.front();
cont.ea = (ea_t)uc->get_pc();
cont.handled = true;
uc->enqueue_debug_evt(cont);
*/
break;
case EXCEPTION:
msg("uni_continue_after_event EXCEPTION\n");
break;
case LIBRARY_LOAD:
msg("uni_continue_after_event LIBRARY_LOAD\n");
break;
case LIBRARY_UNLOAD:
msg("uni_continue_after_event LIBRARY_UNLOAD\n");
break;
case INFORMATION:
msg("uni_continue_after_event INFORMATION\n");
break;
case SYSCALL:
msg("uni_continue_after_event SYSCALL\n");
break;
case WINMESSAGE:
msg("uni_continue_after_event WINMESSAGE\n");
break;
case PROCESS_ATTACH:
msg("uni_continue_after_event PRICESS_ATTACH\n");
break;
case PROCESS_DETACH:
msg("uni_continue_after_event PROCESS_DETACH\n");
break;
case PROCESS_SUSPEND:
msg("uni_continue_after_event PROCESS_SUSPEND\n");
break;
case TRACE_FULL:
msg("uni_continue_after_event TRACE_FULL\n");
break;
case NO_EVENT:
break;
}
return 1;
}
/// Set exception handling.
/// This function is called from debthread or the main thread.
void idaapi uni_set_exception_info(const exception_info_t *info, int qty) {
msg("uni_set_exception_info called\n");
for (int i = 0; i < qty; i++) {
msg("Exception #%d\n", i);
msg(" Code: 0x%x, flags: 0x%x\n", info[i].code, info[i].flags);
msg(" Name: %s, Desc: %s\n", info[i].name.c_str(), info[i].desc.c_str());
}
}
/// This function will be called by the kernel each time
/// it has stopped the debugger process and refreshed the database.
/// The debugger module may add information to the database if it wants.
///
/// The reason for introducing this function is that when an event line
/// LOAD_DLL happens, the database does not reflect the memory state yet
/// and therefore we can't add information about the dll into the database
/// in the get_debug_event() function.
/// Only when the kernel has adjusted the database we can do it.
/// Example: for imported PE DLLs we will add the exported function
/// names to the database.
///
/// This function pointer may be absent, i.e. NULL.
/// This function is called from the main thread.
void idaapi uni_stopped_at_debug_event(bool /*dlls_added*/) {
msg("uni_stopped_at_debug_event called\n");
}
/// \name Threads
/// The following functions manipulate threads.
/// These functions are called from debthread.
/// \retval 1 ok
/// \retval 0 failed
/// \retval -1 network error
int idaapi uni_thread_suspend(thid_t /*tid*/) { ///< Suspend a running thread
sk3wldbg *uc = (sk3wldbg*)dbg;
msg("uni_thread_suspend called\n");
uc->pause();
uc->do_suspend = true;
return 1;
}
int idaapi uni_thread_continue(thid_t /*tid*/) { ///< Resume a suspended thread
sk3wldbg *uc = (sk3wldbg*)dbg;
msg("uni_thread_continue called\n");
//this is going to have to run in a separate thread otherwise other
//debthread functions will never get called
if (uc->do_suspend) {
uc->do_suspend = false;
}
uc->resume();
return 1;
}
int idaapi uni_set_resume_mode(thid_t /*tid*/, resume_mode_t resmod) { ///< Specify resume action
sk3wldbg *uc = (sk3wldbg*)dbg;
msg("uni_set_resume_mode called. resmod = %d\n", resmod);
//*** figure out how best to handle all resume modes
switch (resmod) {
case RESMOD_OVER:
uc->emu_state = RS_STEP_OVER;
break;
case RESMOD_OUT:
uc->emu_state = RS_STEP_OUT;
break;
case RESMOD_INTO:
uc->emu_state = RS_STEP_INTO;
break;
case RESMOD_NONE:
uc->emu_state = RS_RUN; //????
break;
default:
break;
}
return 1;
}
/// Read thread registers.
/// This function is called from debthread.
/// \param tid thread id
/// \param clsmask bitmask of register classes to read
/// \param regval pointer to vector of regvals for all registers.
/// regval is assumed to have debugger_t::registers_size elements
/// \retval 1 ok
/// \retval 0 failed
/// \retval -1 network error
int idaapi uni_read_registers(thid_t /*tid*/, int clsmask, regval_t *values) {
msg("uni_read_registers called\n");
sk3wldbg *uc = (sk3wldbg*)dbg;
uc_err err = UC_ERR_OK;
//need to figure out how to do this across all unicorn archs
for (int i = 0; i < uc->registers_size; i++) {
if (uc->_registers[i].register_class & clsmask) {
int32_t rtype = RVT_INT;
if (uc->_registers[i].dtyp == dt_float || uc->_registers[i].dtyp == dt_double) {
rtype = RVT_FLOAT;
}
values[i].rvtype = rtype;
err = uc_reg_read(uc->uc, uc->reg_map[i], &values[i].ival);
if (err != UC_ERR_OK) {
break;
}
}
}
return 1;
}
/// Write one thread register.
/// This function is called from debthread.
/// \param tid thread id
/// \param regidx register index
/// \param regval new value of the register
/// \retval 1 ok
/// \retval 0 failed
/// \retval -1 network error
int idaapi uni_write_register(thid_t /*tid*/, int regidx, const regval_t *value) {
msg("uni_write_register called\n");
//need to figure out how to do this across all unicorn archs
sk3wldbg *uc = (sk3wldbg*)dbg;
uc_err err = uc_reg_write(uc->uc, uc->reg_map[regidx], &value->ival);
return err == UC_ERR_OK;
}
/// Get information about the base of a segment register.
/// Currently used by the IBM PC module to resolve references like fs:0.
/// This function is called from debthread.
/// \param tid thread id
/// \param sreg_value value of the segment register (returned by get_reg_val())
/// \param answer pointer to the answer. can't be NULL.
/// \retval 1 ok
/// \retval 0 failed
/// \retval -1 network error
int idaapi uni_thread_get_sreg_base(thid_t /*tid*/, int /*sreg_value*/, ea_t *answer) {
msg("uni_thread_get_sreg_base called\n");
//right now unicorn has no way to get this information since base is not a register
//would need to read decide whether seg is local or global, then UC_X86_REG_L/GDTR,
//the uc_read_mem to read correct descriptor, then parse out the base address
*answer = 0;
return 1;
}
/// \name Memory manipulation
/// The following functions manipulate bytes in the memory.
/// Get information on the memory areas.
/// The debugger module fills 'areas'. The returned vector MUST be sorted.
/// This function is called from debthread.
/// \retval -3 use idb segmentation
/// \retval -2 no changes
/// \retval -1 the process does not exist anymore
/// \retval 0 failed
/// \retval 1 new memory layout is returned
int idaapi uni_get_memory_info(meminfo_vec_t &areas) {
sk3wldbg *uc = (sk3wldbg*)dbg;
msg("uni_get_memory_info called\n");
uc_mem_region *regions;
uint32_t count;
uc_err err = uc_mem_regions(uc->uc, ®ions, &count);
if (err == UC_ERR_OK) {
for (uint32_t i = 0; i < count; i++) {
memory_info_t mem;
mem.startEA = (ea_t)regions[i].begin;
mem.endEA = (ea_t)regions[i].end - 1; //-1 because uc_mem_region is inclusive
mem.perm = uc_to_ida_perms_map[regions[i].perms];
areas.push_back(mem);
msg(" region %d: 0x%llx-0x%llx (%d)\n", i, (uint64_t)mem.startEA, (uint64_t)mem.endEA, mem.perm);
msg(" region %d: 0x%llx-0x%llx (%d)\n", i, regions[i].begin, regions[i].end - 1, mem.perm);
}
// free(regions);
}
return 1;
}
/// Read process memory.
/// Returns number of read bytes.
/// This function is called from debthread.
/// \retval 0 read error
/// \retval -1 process does not exist anymore
ssize_t idaapi uni_read_memory(ea_t ea, void *buffer, size_t size) {
sk3wldbg *uc = (sk3wldbg*)dbg;
// msg("uni_read_memory called for 0x%llx:%d\n", (uint64_t)ea, size);
uc_err err = uc_mem_read(uc->uc, ea, buffer, size);
if (err != UC_ERR_OK) {
msg("Failed on uc_mem_read() with error returned %u: %s\n", err, uc_strerror(err));
}
return size;
}
/// Write process memory.
/// This function is called from debthread.
/// \return number of written bytes, -1 if fatal error
ssize_t idaapi uni_write_memory(ea_t ea, const void *buffer, size_t size) {
sk3wldbg *uc = (sk3wldbg*)dbg;
msg("uni_write_memory called for 0x%llx:%u\n", (uint64_t)ea, (uint32_t)size);
uc_err err = uc_mem_write(uc->uc, ea, buffer, size);
if (err != UC_ERR_OK) {
msg("Failed on uc_mem_write() with error returned %u: %s\n", err, uc_strerror(err));
}
return size;
}
/// Is it possible to set breakpoint?.
/// This function is called from debthread or from the main thread if debthread
/// is not running yet.
/// It is called to verify hardware breakpoints.
/// \return ref BPT_
int idaapi uni_is_ok_bpt(bpttype_t type, ea_t ea, int /*len*/) {
msg("uni_is_ok_bpt called for 0x%llx\n", (uint64_t)ea);
//*** test type and setup appropriate actions in hook functions to break
// when appropriate
switch (type) {
case BPT_EXEC: //hardware bpt
case BPT_SOFT:
//code bpt
return BPT_OK;
case BPT_RDWR:
case BPT_WRITE:
//data wpt
//need to make sure read/write hook is installed
default:
return BPT_BAD_TYPE;
}
return BPT_OK;
}
/// Add/del breakpoints.
/// bpts array contains nadd bpts to add, followed by ndel bpts to del.
/// This function is called from debthread.
/// \return number of successfully modified bpts, -1 if network error
int idaapi uni_update_bpts(update_bpt_info_t *bpts, int nadd, int ndel) {
msg("uni_update_bpts called\n");
int processed = 0;
sk3wldbg *uc = (sk3wldbg*)dbg;
for (int i = 0; i < nadd; i++) {
uint8_t orig;
uc_err err = uc_mem_read(uc->uc, bpts[i].ea, &orig, 1);
if (err == UC_ERR_OK) {
bpts[i].orgbytes.push_back(orig);
processed++;
uc->add_bpt(bpts[i].ea);
}
}
for (int i = nadd; i < (nadd + ndel); i++) {
uc->del_bpt(bpts[i].ea);
processed++;
}
return processed;
}
/// Update low-level (server side) breakpoint conditions.
/// This function is called from debthread.
/// \return nlowcnds. -1-network error
int idaapi uni_update_lowcnds(const lowcnd_t * /*lowcnds*/, int nlowcnds) {
msg("uni_update_lowcnds called\n");
warning("TITLE Under Construction\nICON INFO\nAUTOHIDE NONE\nHIDECANCEL\nConditional breakpoints are currently unimplemented");
return nlowcnds;
}
/// \name Remote file
/// Open/close/read a remote file.
/// These functions are called from the main thread
/// -1-error
int idaapi uni_open_file(const char *file, uint32 * /*fsize*/, bool /*readonly*/) {
msg("uni_open_file called (%s)\n", file);
return -1;
}
void idaapi uni_close_file(int /*fn*/) {
msg("uni_close_file called\n");
return;
}
ssize_t idaapi uni_read_file(int /*fn*/, uint32 /*off*/, void * /*buf*/, size_t /*size*/) {
msg("uni_read_file called\n");
return -1;
}
/// Map process address.
/// This function may be absent.
/// This function is called from debthread.
/// \param off offset to map
/// \param regs current register values. if regs == NULL, then perform
/// global mapping, which is independent on used registers
/// usually such a mapping is a trivial identity mapping
/// \param regnum required mapping. maybe specified as a segment register number
/// or a regular register number if the required mapping can be deduced
/// from it. for example, esp implies that ss should be used.
/// \return mapped address or #BADADDR
ea_t idaapi uni_map_address(ea_t off, const regval_t * /*regs*/, int /*regnum*/) {
ea_t res = BADADDR;
/* TODO
Lots to be done here. Need to lookup associated segment bas, then add offset
and check against limit. Return valuse is either base + offset or BADADDR
For this and other functions, need some helpers that read segment descriptors
*/
// msg("uni_map_address called for 0x%llx\n", (uint64_t)off);
/*
meminfo_vec_t mv;
uni_get_memory_info(mv);
for (int i = 0; i < mv.size(); i++) {
if (mv[i].contains(off)) {
res = off;
break;
}
}
*/
return off;
}
/// Set debugger options (parameters that are specific to the debugger module).
/// See the definition of ::set_options_t for arguments.
/// See the convenience function in dbg.hpp if you need to call it.
/// The kernel will call this function after reading the debugger specific
/// config file (arguments are: keyword="", type=#IDPOPT_STR, value="")
/// This function is optional.
/// This function is called from the main thread
//Called with keyword == NULL indicates user has selected "Set specific options" button
// in IDA's Debugger setup dialog
const char *idaapi uni_set_dbg_options(const char *keyword, int /*pri*/,
int value_type, const void *value) {
msg("uni_set_dbg_options called: %s\n", keyword);
if (value_type == IDPOPT_STR) {
msg(" option value: %s\n", (char*)value);
}
return IDPOPT_OK;
}
/// Get pointer to debugger specific functions.
/// This function returns a pointer to a structure that holds pointers to
/// debugger module specific functions. For information on the structure
/// layout, please check the corresponding debugger module. Most debugger
/// modules return NULL because they do not have any extensions. Available
/// extensions may be called from plugins.
/// This function is called from the main thread.
const void *idaapi uni_get_debmod_extensions(void) {
msg("uni_get_debmod_extensions called\n");
return NULL;
}
/// Calculate the call stack trace.
/// This function is called when the process is suspended and should fill
/// the 'trace' object with the information about the current call stack.
/// If this function is missing or returns false, IDA will use the standard
/// mechanism (based on the frame pointer chain) to calculate the stack trace
/// This function is called from the main thread.
/// \return success
bool idaapi uni_update_call_stack(thid_t /*tid*/, call_stack_t * /*trace*/) {
msg("uni_update_call_stack called\n");
warning("TITLE Under Construction\nICON INFO\nAUTOHIDE NONE\nHIDECANCEL\nStack trace is currently unimplemented");
return false;
}
/// Call application function.
/// This function calls a function from the debugged application.
/// This function is called from debthread
/// \param func_ea address to call
/// \param tid thread to use
/// \param fti type information for the called function
/// \param nargs number of actual arguments
/// \param regargs information about register arguments
/// \param stkargs memory blob to pass as stack arguments (usually contains pointed data)
/// it must be relocated by the callback but not changed otherwise
/// \param retregs function return registers.
/// \param[out] errbuf the error message. if empty on failure, see 'event'.
/// should not be filled if an appcall exception
/// happened but #APPCALL_DEBEV is set
/// \param[out] event the last debug event that occurred during appcall execution
/// filled only if the appcall execution fails and #APPCALL_DEBEV is set
/// \param options appcall options, usually taken from \inf{appcall_options}.
/// possible values: combination of \ref APPCALL_ or 0
/// \return ea of stkargs blob, #BADADDR if failed and errbuf is filled
ea_t idaapi uni_appcall(
ea_t /*func_ea*/,
thid_t /*tid*/,
const struct func_type_data_t * /*fti*/,
int /*nargs*/,
const struct regobjs_t * /*regargs*/,
struct relobj_t * /*stkargs*/,
struct regobjs_t * /*retregs*/,
qstring * /*errbuf*/,
debug_event_t * /*event*/,
int /*options*/) {
msg("uni_appcall called\n");
warning("TITLE Under Construction\nICON INFO\nAUTOHIDE NONE\nHIDECANCEL\nappcall is currently unimplemented");
return BADADDR;
}
/// Cleanup after appcall().
/// The debugger module must keep the stack blob in the memory until this function
/// is called. It will be called by the kernel for each successful appcall().
/// There is an exception: if #APPCALL_MANUAL, IDA may not call cleanup_appcall.
/// If the user selects to terminate a manual appcall, then cleanup_appcall will be called.
/// Otherwise, the debugger module should terminate the appcall when the called
/// function returns.
/// This function is called from debthread.
/// \retval 2 ok, there are pending events
/// \retval 1 ok
/// \retval 0 failed
/// \retval -1 network error
int idaapi uni_cleanup_appcall(thid_t /*tid*/) {
msg("uni_cleanup_appcall called\n");
return 1;
}
/// Evaluate a low level breakpoint condition at 'ea'.
/// Other evaluation errors are displayed in a dialog box.
/// This call is rarely used by IDA when the process has already been suspended
/// for some reason and it has to decide whether the process should be resumed
/// or definitely suspended because of a breakpoint with a low level condition.
/// This function is called from debthread.
/// \retval 1 condition is satisfied
/// \retval 0 not satisfied
/// \retval -1 network error
int idaapi uni_eval_lowcnd(thid_t /*tid*/, ea_t ea) {
msg("uni_eval_lowcnd called: 0x%llx\n", (uint64_t)ea);
// warning("TITLE Under Construction\nICON INFO\nAUTOHIDE NONE\nHIDECANCEL\nConditional breakpoints are currently unimplemented");
return 1;
}
/// This function is called from main thread
ssize_t idaapi uni_write_file(int /*fn*/, uint32 /*off*/, const void * /*buf*/, size_t /*size*/) {
msg("uni_write_file called\n");
return -1;
}
/// Perform a debugger-specific function.
/// This function is called from debthread
int idaapi uni_send_ioctl(int /*fn*/, const void * /*buf*/, size_t /*size*/, void ** /*poutbuf*/, ssize_t * /*poutsize*/) {
msg("uni_send_ioctl called\n");
return -1;
}
/// Enable/Disable tracing.
/// "trace_flags" can be a set of STEP_TRACE, INSN_TRACE, BBLK_TRACE or FUNC_TRACE.
/// See thread_t::trace_mode in debugger.h.
/// This function is called from the main thread.
bool idaapi uni_dbg_enable_trace(thid_t /*tid*/, bool /*enable*/, int /*trace_flags*/) {
msg("uni_dbg_enable_trace called\n");
warning("TITLE Under Construction\nICON INFO\nAUTOHIDE NONE\nHIDECANCEL\naTracing is currently unimplemented");
return false;
}
/// Is tracing enabled? ONLY used for tracers.
/// "trace_bit" can be one of the following: STEP_TRACE, INSN_TRACE, BBLK_TRACE or FUNC_TRACE
bool idaapi uni_is_tracing_enabled(thid_t /*tid*/, int /*tracebit*/) {
msg("uni_is_tracing_enabled called\n");
return false;
}
/// Execute a command on the remote computer.
/// \return exit code
int idaapi uni_rexec(const char *cmdline) {
msg("uni_rexec called (%s)\n", cmdline);
return 0;
}
/// Get (store to out_pattrs) process/debugger-specific runtime attributes.
/// This function is called from main thread.
void idaapi uni_get_debapp_attrs(debapp_attrs_t *out_pattrs) {
msg("uni_get_debapp_attrs called\n");
out_pattrs->addrsize = (inf.lflags & LFLG_64BIT) ? 8 :4;
if (inf.filetype == f_PE) {
if (inf.lflags & LFLG_64BIT) {