-
Notifications
You must be signed in to change notification settings - Fork 71
/
plugin.cpp
1563 lines (1389 loc) · 43.4 KB
/
plugin.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
#include <hexsuite.hpp>
#include <intel.hpp>
#include <optional>
#include "nt_syscalls.hpp"
// NT constants.
//
static constexpr size_t index_self_ref = 0x1ED;
static constexpr ea_t make_pte_base( size_t level, size_t max_levels )
{
ea_t result = ~0ull << ( max_levels * 9 + 12 );
for ( size_t l = level; l != max_levels; l++ )
result |= index_self_ref << ( 9 * l + 12 );
return result;
}
static constexpr ea_t pfn_list_base_va48 = 0xFFFFFA8000000000;
static constexpr ea_t pfn_list_base_la57 = 0xFFFFDE0000000000;
static tinfo_t convert_dtype( uint32_t dtype )
{
switch ( dtype )
{
case dt_byte: return tinfo_t{ BT_INT8 };
case dt_word: return tinfo_t{ BT_INT16 };
case dt_dword: return tinfo_t{ BT_INT32 };
case dt_qword: return tinfo_t{ BT_INT64 };
case dt_byte16: return tinfo_t{ BT_INT128 };
case dt_float: return tinfo_t{ BT_FLOAT };
case dt_double: return tinfo_t{ BTMT_DOUBLE };
default: return tinfo_t{ BT_VOID };
}
}
// Optimizes out blocks generated due to inlined scheduler hints, hv enlightenments or instrumentations.
//
hex::insn_optimizer global_optimizer = [ ] ( mblock_t* blk, minsn_t* ins, auto )
{
// Skip if it isn't a conditional jump.
//
if ( !is_mcode_jcond( ins->opcode ) )
return 0;
// For each operand and sub-operand:
//
int res = ins->for_all_ops( hex::mop_visitor( [ ] ( mop_t* op, const tinfo_t* type, bool is_target )
{
std::pair<size_t, const char*> force_zero_list[] = {
{ 4, "KiIrqlFlags" },
{ 4, "HvlEnlightenments" },
{ 4, "HvlLongSpinCountMask" },
{ 0x10, "PerfGlobalGroupMask" },
};
// If referencing any of the globals above at any offset [0-8], assume constant zero.
//
for ( int delta = 0; delta <= 0x10; delta++ )
{
auto name = get_name( op->g - delta );
for ( auto& [sz, item] : force_zero_list )
{
if ( delta >= sz )
continue;
if ( name == item )
{
msg( "Ignoring %s\n", item );
op->make_number( 0, op->size );
return 1;
}
}
}
return 0;
} ) );
if ( res )
blk->mark_lists_dirty();
return res;
};
// Optimizes out blocks generated from PTE writing macro handling shadow ranges.
//
hex::insn_optimizer shadow_pte_update_optimizer = [ ] ( mblock_t* blk, minsn_t* ins, auto )
{
// Skip if it isn't a conditional jump.
//
if ( !is_mcode_jcond( ins->opcode ) )
return 0;
// For each operand and sub-operand:
//
int res = ins->for_all_ops( hex::mop_visitor( [ ] ( mop_t* op, const tinfo_t* type, bool is_target )
{
// If call type:
//
if ( op->t == mop_d && op->d->opcode == m_call && op->d->l.t == mop_v )
{
// If checking shadow PTE, assume 0 return.
//
auto callee = get_name( op->d->l.g );
if ( callee == "MiPteHasShadow" || callee == "MiPteInShadowRange" )
{
msg( "Ignoring %s\n", callee.c_str() );
op->make_number( 0, 4 );
return 1;
}
}
return 0;
} ) );
// If we changed anything, declare lists dirty.
//
if ( res )
blk->mark_lists_dirty();
return res;
};
// Optimizes out blocks generated from PTE reading macro handling shadow ranges.
//
hex::insn_optimizer shadow_pte_read_optimizer = [ ] ( mblock_t* blk, minsn_t* ins, auto )
{
// Skip if it isn't a conditional jump.
//
if ( !is_mcode_jcond( ins->opcode ) )
return 0;
// For each operand and sub-operand:
//
int res = ins->for_all_ops( hex::mop_visitor( [ ] ( mop_t* op, const tinfo_t* type, bool is_target )
{
// If and type:
//
if ( op->t == mop_d && op->d->opcode == m_and )
{
auto o1 = &op->d->l;
auto o2 = &op->d->r;
if ( o1->t == mop_n )
std::swap( o1, o2 );
// If Op1 is 0xC00000:
//
if ( o2->t == mop_n && o2->nnn->value == 0xC00000 )
{
// If Op2 is MiFlags:
//
if ( o1->t == mop_v && get_name( o1->g ) == "MiFlags" )
{
// Replace with number.
//
op->make_number( 0, 4 );
return 1;
}
}
}
return 0;
} ) );
// If we changed anything, declare lists dirty.
//
if ( res )
blk->mark_lists_dirty();
return res;
};
// Optimizes out system priority management on IRQL change.
//
hex::block_optimizer scheduler_hint_optimizer = [ ] ( mblock_t* blk )
{
int changes = 0;
for( minsn_t* ins : hex::instructions( blk ) )
{
// Skip if it does not match scheduler hint:
//
if ( ins->opcode != m_call || ins->l.t != mop_v || get_name( ins->l.g ) != "KiRemoveSystemWorkPriorityKick" )
continue;
msg( "Ignoring KiRemoveSystemWorkPriorityKick\n" );
// Clear the call.
//
blk->make_nop( ins );
changes++;
// Find predecessors.
//
for ( mblock_t* pred : hex::predecessors( blk ) )
for ( minsn_t* ins : hex::instructions( pred ) )
if ( ins != pred->tail )
pred->make_nop( ins );
}
return changes;
};
// Lifts int2c as assert failure.
//
hex::microcode_filter nt_assert_lifter = [ ] ( codegen_t& cg )
{
if ( cg.insn.itype != NN_int ||
cg.insn.ops[ 0 ].value != 0x2C )
return false;
auto ci = hex::call_info(
tinfo_t{ BT_VOID },
hex::call_arg( hex::phys_reg( R_cx, 4 ), tinfo_t{ BT_INT32 }, "code" )
);
ci->flags |= FCI_NORET;
cg.mb->insert_into_block(
hex::make_call( cg.insn.ea, hex::helper{ "__assert_fail" }, std::move( ci ) ).release(),
cg.mb->tail
);
return true;
};
// Lifts MOVABS on dynamic relocations to Mm intrinsics.
//
hex::microcode_filter mm_dyn_reloc_lifter = [ ] ( codegen_t& cg )
{
if ( cg.insn.itype == NN_mov &&
cg.insn.ops[ 0 ].type == o_reg &&
cg.insn.ops[ 1 ].type == o_imm )
{
ea_t imm = cg.insn.ops[ 1 ].value;
const char* intrinsic_getter = nullptr;
const char* intrinsic_rtype = nullptr;
size_t intrinsic_offset = 0;
// Handle PFN list:
//
if ( !intrinsic_getter )
{
for ( auto base : { pfn_list_base_va48, pfn_list_base_la57 } )
{
if ( base <= imm && imm <= ( base + 48 ) )
{
intrinsic_getter = "MmGetPfnDb";
intrinsic_rtype = "_MMPFN";
intrinsic_offset = imm - base;
break;
}
}
}
// Handle page tables:
//
if ( !intrinsic_getter )
{
for ( size_t paging_depth : { 4, 5 } )
{
constexpr const char* bnames[] = { "MmGetPml5eBase", "MmGetPml4eBase", "MmGetPdpteBase", "MmGetPdeBase", "MmGetPteBase" };
constexpr const char* lnames[] = { "MmGetPml5eLimit", "MmGetPml4eLimit", "MmGetPdpteLimit", "MmGetPdeLimit", "MmGetPteLimit" };
for ( size_t level = 0; level != paging_depth; level++ )
{
auto pmin = make_pte_base( level, paging_depth );
auto pmax = pmin + ( ( 1ull << ( 12 + 9 * level ) ) - 1 );
if ( !level && imm == ( pmin + 0x7F8 ) )
{
intrinsic_getter = "MmGetPxeUserLimit";
intrinsic_offset = 0;
intrinsic_rtype = "_MMPTE";
break;
}
else if ( !level && imm == ( pmin + ( index_self_ref ) * 8 ) )
{
intrinsic_getter = "MmGetPxeSelfRef";
intrinsic_offset = 0;
intrinsic_rtype = "_MMPTE";
break;
}
else if ( imm == pmax )
{
intrinsic_getter = lnames[ level + ( 5 - paging_depth ) ];
intrinsic_offset = 0;
intrinsic_rtype = "_MMPTE";
break;
}
else if ( pmin <= imm && imm <= pmax )
{
intrinsic_getter = bnames[ level + ( 5 - paging_depth ) ];
intrinsic_offset = imm - pmin;
intrinsic_rtype = "_MMPTE";
break;
}
}
if ( intrinsic_getter )
break;
}
}
// If we did find a match:
//
if ( intrinsic_getter )
{
tinfo_t type = {};
if ( tinfo_t ttype; ttype.get_named_type( get_idati(), intrinsic_rtype ) )
type.create_ptr( ttype );
else
type = tinfo_t{ BTF_UINT64 };
qstring types = {};
type.print( &types );
msg( "Found relocation: %s %s()\n", types.c_str(), intrinsic_getter );
auto call_info = hex::call_info( hex::pure_t{}, type );
auto call = hex::make_call( cg.insn.ea, hex::helper{ intrinsic_getter }, std::move( call_info ) );
auto adj = hex::make_add( cg.insn.ea, { intrinsic_offset, 8 }, std::move( call ), hex::phys_reg( cg.insn.ops[ 0 ].reg, 8 ) );
cg.mb->insert_into_block( adj.release(), cg.mb->tail);
cg.mb->mark_lists_dirty();
return true;
}
}
return false;
};
// Lifts SYSCALL.
//
static bool is_ea_syscall( ea_t ea )
{
insn_t out{};
return decode_insn( &out, ea ) > 0 && out.itype == NN_syscall;
}
struct syscall_netnode : netnode
{
union info_t
{
struct
{
uint16_t id; // syscall id + 1, else 0
uint16_t api_id; // syscall_signatures index + 1, else 0
};
nodeidx_t value;
explicit info_t( nodeidx_t value ) : value( value ) {}
};
inline static std::optional<nt_syscall_map_t> idb_scm {};
syscall_netnode() : netnode( "$ ntrays syscall", 0, true ) {}
info_t get_info( ea_t ea ) const
{
return info_t( altval_ea( ea ) );
}
void set_info( ea_t ea, info_t info )
{
QASSERT( 60801, is_ea_syscall( ea ) );
altset_ea( ea, info.value );
}
size_t get_scm_value() const { return altval( 0 ); }
void set_scm_value( size_t value ) { altset( 0, value ); }
const std::pair<const char *, nt_syscall_map_t> *get_scm_preset() const
{
if ( auto value = get_scm_value() )
if ( value > 1 && value - 2 < std::size( nt_syscall_maps ) )
return &nt_syscall_maps[ value - 2 ];
return nullptr;
}
void set_scm_preset( size_t index )
{
set_scm_value( index + 2 );
}
const nt_syscall_map_t *get_scm()
{
if ( !get_scm_value() )
return nullptr;
if ( auto *pair = get_scm_preset() )
return &pair->second;
if ( !idb_scm )
load_scm();
return &*idb_scm;
}
void set_scm( nt_syscall_map_t value )
{
set_scm_value( 1 );
idb_scm = std::move( value );
store_scm();
}
void apply_scm( nt_syscall_map_t value )
{
set_scm_value( 1 );
if ( idb_scm ) idb_scm->apply( std::move( value ) );
else idb_scm = std::move( value );
store_scm();
}
const char *get_scm_name() const
{
if ( !get_scm_value() )
return "None";
if ( auto *pair = get_scm_preset() )
return pair->first;
return "Custom";
}
void load_scm()
{
std::vector<uint8_t> blob{};
blob.resize( blobsize( 0, 'B' ) );
size_t tmp = blob.size();
getblob( blob.data(), &tmp, 0, 'B' );
nt_syscall_map_t map{};
map.deserialize( blob );
idb_scm = std::move( map );
msg( "Loaded %u NT and %u WIN32K syscall IDs from IDB\n", idb_scm->nt.size(), idb_scm->win32k.size() );
}
void store_scm()
{
if ( idb_scm )
{
auto blob = idb_scm->serialize();
setblob( blob.data(), blob.size(), 0, 'B' );
msg( "Saved %u NT and %u WIN32K syscall IDs to IDB\n", idb_scm->nt.size(), idb_scm->win32k.size() );
}
}
};
static size_t inf_get_pointer_size()
{
return inf_is_64bit() ? sizeof( uint64_t ) : sizeof( uint32_t );
}
static tinfo_t tinfo_from_argument_descriptor( const nt_api_descriptor::argument_descriptor &descriptor, ea_t ea )
{
tinfo_t tif{};
qstring tmp{};
std::string decl = descriptor.type_name;
decl += ";";
if ( parse_decl( &tif, &tmp, nullptr, decl.c_str(), PT_SIL | PT_TYP ) )
{
return tif;
}
else
{
msg( "%zX: could not parse type '%s'\n", ea, descriptor.type_name );
return tinfo_t{ inf_is_64bit() ? BT_INT64 : BT_INT32 };
}
}
static hex::call_arg load_stack_value( codegen_t &cg, tinfo_t typ, size_t offset, const char *name )
{
auto ltmp = hex::reg{ cg.mba->alloc_kreg( 8 ), 8 };
auto tmp = hex::reg{ cg.mba->alloc_kreg( typ.get_size() ), ( int ) typ.get_size() };
cg.mb->insert_into_block( hex::make_add( cg.insn.ea, hex::phys_reg( R_sp, 8 ), hex::operand{ offset, 8 }, ltmp ).release(), cg.mb->tail );
cg.mb->insert_into_block( hex::make_ldx( cg.insn.ea, hex::phys_reg( R_ss, 2 ), ltmp, tmp ).release(), cg.mb->tail );
return hex::call_arg( tmp, typ, name );
};
static std::unique_ptr<mcallinfo_t> create_call_info_from_nt_signature( codegen_t &cg, const nt_api_descriptor &descriptor )
{
auto ci = hex::call_info( tinfo_t{ BT_INT64 } );
QASSERT( 60802, inf_is_64bit() );
for ( size_t i = 0; i < descriptor.arguments.size(); i++ )
{
const auto &argument = descriptor.arguments[ i ];
auto type = tinfo_from_argument_descriptor( argument, cg.insn.ea );
if ( i < 4 )
{
size_t regs[ 4 ] = { R_r10, R_dx, R_r8, R_r9 };
ci->args.push_back( hex::call_arg( hex::phys_reg( regs[ i ], type.get_size() ), type, argument.name ) );
}
else
{
ci->args.push_back( load_stack_value( cg, type, 8 + i * 8, argument.name ) );
}
}
return ci;
}
hex::microcode_filter syscall_lifter = [ ] ( codegen_t& cg )
{
if ( cg.insn.itype != NN_syscall )
return false;
if ( inf_get_filetype() == f_ELF )
return false;
if ( !inf_is_64bit() )
return false;
syscall_netnode snn{};
const auto info = snn.get_info( cg.insn.ea );
auto *syscall_map = snn.get_scm();
const nt_api_descriptor *signature{};
// User set API?
//
if ( info.api_id )
{
signature = &nt_api_descriptors[ info.api_id - 1 ];
}
// Syscall ID is known?
//
else if ( info.id && syscall_map )
{
const auto api_id = syscall_map->get_api_id( info.id - 1 );
if ( signature = api_id.get_descriptor() )
{
msg( "%zX: inferred syscall signature %s from ID %u and syscall map '%s'\n", cg.insn.ea, signature->api_name, info.id - 1, snn.get_scm_name() );
}
else if ( const char *api_name = api_id.get_missing() )
{
msg( "%zX: inferred syscall API %s from ID %u and syscall map '%s', but no signature is available\n", cg.insn.ea, api_name, info.id - 1, snn.get_scm_name() );
}
else
{
msg( "%zX: failed to infer syscall signature from ID %u\n", cg.insn.ea, info.id - 1 );
}
}
const char *helper_name;
std::unique_ptr<mcallinfo_t> syscall_ci;
if ( signature )
{
helper_name = signature->api_name;
syscall_ci = create_call_info_from_nt_signature( cg, *signature );
}
else
{
if ( info.id && !syscall_map ) msg( "%zX: syscall ID known but no syscall map is defined\n", cg.insn.ea );
helper_name = "__syscall";
syscall_ci = hex::call_info(
tinfo_t{ BT_INT64 },
hex::call_arg( hex::phys_reg( R_ax, 4 ), tinfo_t{ BT_INT32 }, "id" ),
hex::call_arg( hex::phys_reg( R_r10, 8 ), tinfo_t{ BT_INT64 }, "arg0" ),
hex::call_arg( hex::phys_reg( R_dx, 8 ), tinfo_t{ BT_INT64 }, "arg1" ),
hex::call_arg( hex::phys_reg( R_r8, 8 ), tinfo_t{ BT_INT64 }, "arg2" ),
hex::call_arg( hex::phys_reg( R_r9, 8 ), tinfo_t{ BT_INT64 }, "arg3" ),
load_stack_value( cg, tinfo_t{ BT_INT64 }, 0x28 + 0 * 8, "arg4" ),
load_stack_value( cg, tinfo_t{ BT_INT64 }, 0x28 + 1 * 8, "arg5" )
);
}
// syscall_ci->return_regs.add( reg2mreg( R_ax ), 64 ); // this line triggers internal error 50406 with some samples
syscall_ci->spoiled.add( reg2mreg( R_ax ), 64 );
syscall_ci->spoiled.add( reg2mreg( R_cx ), 64 );
syscall_ci->spoiled.add( reg2mreg( R_r11 ), 64 );
// everything else should be spoiled too, not sure if there's a nice way to do this
syscall_ci->flags |= FCI_HASCALL;
auto syscall_call = hex::make_call( cg.insn.ea, hex::helper{ helper_name }, std::move( syscall_ci ) );
cg.mb->insert_into_block(
hex::make_mov( cg.insn.ea, std::move( syscall_call ), hex::phys_reg( R_ax, 8 ) ).release(),
cg.mb->tail
);
cg.mb->mark_lists_dirty();
return true;
};
hex::insn_optimizer syscall_optimizer = []( mblock_t *blk, minsn_t *ins, auto )
{
if ( !ins->contains_opcode( m_call ) )
return 0;
auto *call = ins->find_call( true );
if ( call->is_helper( "__syscall" ) )
{
QASSERT( 60800, ins->ea != BADADDR && call->d.t == mop_f && !call->d.f->args.empty() );
auto *ci = call->d.f;
uint64_t syscall_id = 0;
if ( ci->args[ 0 ].is_constant( &syscall_id, false ) )
{
syscall_netnode snn{};
auto info = snn.get_info( ins->ea );
if ( !info.id )
{
info.id = syscall_id + 1;
snn.set_info( ins->ea, info );
// TODO: triggering a refresh at this point would be nice
msg( "%zX: deduced ID=%u for syscall (maturity=%u), regenerate microcode to update signature\n", ins->ea, syscall_id, blk->mba->maturity );
}
}
}
return 0;
};
// used by vdui_t::ctree_to_disasm() in hexx64.dll
static ea_t get_vdui_ea(vdui_t *vu)
{
vu->get_current_item( USE_KEYBOARD );
ea_t fict_ea = vu->cfunc->entry_ea;
if ( vu->item.citype == VDI_EXPR )
{
fict_ea = vu->item.it->ea;
if ( fict_ea == BADADDR )
{
struct ea_searcher_t : public ctree_visitor_t
{
const citem_t *result;
ea_searcher_t( const citem_t *start ) : ctree_visitor_t( CV_PARENTS ), result( start ) {}
int visit_item( citem_t *item )
{
if ( item != result ) return 0;
for ( size_t i = parents.size(); i-- > 0;)
{
if ( parents[ i ]->ea != BADADDR )
{
result = parents[ i ];
break;
}
}
return 1;
}
int idaapi visit_insn( cinsn_t *insn ) override { return visit_item( insn ); }
int idaapi visit_expr( cexpr_t *expr ) override { return visit_item( expr ); }
};
ea_searcher_t searcher( vu->item.it );
searcher.apply_to( &vu->cfunc->body, nullptr );
fict_ea = searcher.result->ea;
}
}
else if ( vu->tail.citype == VDI_TAIL )
{
fict_ea = vu->tail.loc.ea;
}
else if ( vu->head.citype == VDI_TAIL )
{
fict_ea = vu->head.loc.ea;
}
return vu->mba->map_fict_ea( fict_ea );
}
struct api_chooser_t : chooser_t
{
static constexpr int WIDTHS[] = { 30, 50 };
static constexpr const char *COLUMNS[] = { "Name", "Arguments" };
api_chooser_t() : chooser_t( CH_KEEP | CH_MODAL, std::size( WIDTHS ), WIDTHS, COLUMNS, "Select an API", 0 ) {}
void idaapi get_row( qstrvec_t *out, int *out_icon, chooser_item_attrs_t *out_attrs, size_t n ) const override
{
if ( n == 0 )
{
( *out )[ 0 ] = "Automatic";
}
else
{
( *out )[ 0 ] = nt_api_descriptors[ n - 1 ].api_name;
( *out )[ 1 ] = nt_api_descriptors[ n - 1 ].arguments_to_string().c_str();
}
}
size_t idaapi get_count() const override
{
return std::size( nt_api_descriptors ) + 1;
}
};
struct winver_chooser_t : chooser_t
{
static constexpr int WIDTHS[] = { 50 };
static constexpr const char *COLUMNS[] = { "Version" };
winver_chooser_t() : chooser_t( CH_KEEP | CH_MODAL, std::size( WIDTHS ), WIDTHS, COLUMNS, "Select a Windows Version", 0 ) {}
void idaapi get_row( qstrvec_t *out, int *out_icon, chooser_item_attrs_t *out_attrs, size_t n ) const override
{
if ( n == 0 )
{
( *out )[ 0 ] = "Disable automatic deduction";
}
else if ( n == 1 )
{
( *out )[ 0 ] = "Select a ntdll.dll or win32u.dll";
}
else
{
( *out )[ 0 ] = "Preset: ";
( *out )[ 0 ] += nt_syscall_maps[ n - 2 ].first;
}
}
size_t idaapi get_count() const override
{
return std::size( nt_syscall_maps ) + 2;
}
};
static bool idaapi menu_set_syscall_api(vdui_t *vdui)
{
syscall_netnode snn{};
auto ea = get_vdui_ea( vdui );
auto info = snn.get_info( ea );
api_chooser_t chooser{};
auto choice = chooser.choose( info.api_id );
if ( choice >= 0 )
{
info.api_id = choice;
snn.set_info( ea, info );
vdui->refresh_view( true );
}
return false;
}
static bool idaapi menu_set_winver(vdui_t *vdui)
{
syscall_netnode snn{};
winver_chooser_t chooser{};
auto choice = chooser.choose( snn.get_scm_value() );
if ( choice >= 0 )
{
if ( choice == 1 )
{
if ( const char *nt_binary_path = ask_file( false, "PE files|*.dll", "Select a ntdll.dll or win32u.dll" ) )
{
auto map = extract_syscall_ids( nt_binary_path );
if ( map.valid() )
{
snn.apply_scm( std::move( map ) );
}
else
{
msg( "Failed to parse %s\n", nt_binary_path );
}
}
}
else
{
snn.set_scm_preset( choice - 2 );
}
vdui->refresh_view( true );
}
return false;
}
namespace
{
class menu_action_handler : public action_handler_t
{
public:
typedef std::function<bool( vdui_t * )> handler_t;
bool is_enabled;
explicit menu_action_handler( handler_t handler )
: is_enabled( true ), handler( std::move( handler ) )
{
}
menu_action_handler( handler_t handler, const bool enabled )
: is_enabled( enabled ), handler( std::move( handler ) )
{
}
int idaapi activate( action_activation_ctx_t *ctx ) override
{
const auto vdui = get_widget_vdui( ctx->widget );
return handler( vdui ) ? 1 : 0;
}
action_state_t idaapi update( action_update_ctx_t *ctx ) override
{
return ctx->widget_type == BWN_PSEUDOCODE ? AST_ENABLE_FOR_WIDGET : AST_DISABLE_FOR_WIDGET;
}
private:
handler_t handler;
};
static menu_action_handler set_syscall_api_handler{ menu_set_syscall_api };
static menu_action_handler set_winver_handler{ menu_set_winver };
static action_desc_t action_descs[] = {
ACTION_DESC_LITERAL( "ntrays:set_syscall_api", "Set syscall ~A~PI", &set_syscall_api_handler, nullptr, nullptr, -1 ),
ACTION_DESC_LITERAL( "ntrays:set_winver", "Set ~W~indows version", &set_winver_handler, nullptr, nullptr, -1 )
};
}
hex::hexrays_callback hexrays_popup_event = hex::hexrays_callback_for<hxe_populating_popup>( []( TWidget *widget, TPopupMenu *popup_menu, vdui_t *vu )
{
if ( is_ea_syscall( get_vdui_ea( vu ) ) )
attach_action_to_popup( vu->ct, popup_menu, "ntrays:set_syscall_api" );
attach_action_to_popup( vu->ct, popup_menu, "ntrays:set_winver" );
return 0;
} );
// Lifts CPUID.
//
hex::microcode_filter cpuid_lifter = [ ] ( codegen_t& cg )
{
if ( cg.insn.itype != NN_cpuid )
return false;
// Emit the cpuid intrinsic with a "magic" return.
//
auto cpuid_ci = hex::call_info(
tinfo_t{ BT_INT64 },
hex::call_arg( hex::phys_reg( R_ax, 4 ), tinfo_t{ BT_INT32 }, "leaf" ),
hex::call_arg( hex::phys_reg( R_cx, 4 ), tinfo_t{ BT_INT32 }, "subleaf" )
);
auto cpuid_call = hex::make_call( cg.insn.ea, hex::helper{ "__cpuid" }, std::move( cpuid_ci ) );
auto cpuid_res = hex::reg( cg.mba->alloc_kreg( 8 ), 8 );
cg.mb->insert_into_block(
hex::make_mov( cg.insn.ea, std::move( cpuid_call ), cpuid_res ).release(),
cg.mb->tail
);
// Create movs to each register.
//
std::pair<mreg_t, const char*> parts[] = {
{ reg2mreg( R_ax ), "EAX" },
{ reg2mreg( R_bx ), "EBX" },
{ reg2mreg( R_cx ), "ECX" },
{ reg2mreg( R_dx ), "EDX" }
};
for ( auto [reg, extr] : parts )
{
auto extract_ci = hex::call_info(
hex::pure_t{},
tinfo_t{ BT_INT32 },
hex::call_arg{ cpuid_res, tinfo_t{ BT_INT64 } }
);
auto extract_call = hex::make_call( cg.insn.ea, hex::helper( extr ), std::move( extract_ci ) );
auto mov_ins = hex::make_mov( cg.insn.ea, std::move( extract_call ), hex::reg( reg, 4 ) );
cg.mb->insert_into_block( mov_ins.release(), cg.mb->tail );
}
cg.mb->mark_lists_dirty();
return true;
};
// Lifts XGETBV.
//
hex::microcode_filter xgetbv_lifter = [ ] ( codegen_t& cg )
{
if ( cg.insn.itype != NN_xgetbv )
return false;
// Emit the XGETBV intrinsic.
//
auto xgetbv_ci = hex::call_info(
tinfo_t{ BT_INT64 },
hex::call_arg( hex::phys_reg( R_cx, 4 ), tinfo_t{ BT_INT32 }, "xcr" )
);
auto xgetbv_call = hex::make_call( cg.insn.ea, hex::helper{ "_xgetbv" }, std::move( xgetbv_ci ) );
auto xgetbv_res = hex::reg( cg.mba->alloc_kreg( 8 ), 8 );
cg.mb->insert_into_block(
hex::make_mov( cg.insn.ea, std::move( xgetbv_call ), xgetbv_res ).release(),
cg.mb->tail
);
// Create movs to each register.
//
cg.mb->insert_into_block( hex::make_low( cg.insn.ea, xgetbv_res, hex::phys_reg( R_ax, 4 ) ).release(), cg.mb->tail );
cg.mb->insert_into_block( hex::make_high( cg.insn.ea, xgetbv_res, hex::phys_reg( R_cx, 4 ) ).release(), cg.mb->tail );
cg.mb->mark_lists_dirty();
return true;
};
// Lifts XSETBV.
//
hex::microcode_filter xsetbv_lifter = [ ] ( codegen_t& cg )
{
if ( cg.insn.itype != NN_xsetbv )
return false;
// Emit the intrinsic.
//
auto xsetbv_ci = hex::call_info(
tinfo_t{ BT_VOID },
hex::call_arg( hex::phys_reg( R_cx, 4 ), tinfo_t{ BT_INT32 }, "xcr" ),
hex::call_arg( { hex::phys_reg( R_ax, 4 ), hex::phys_reg( R_dx, 4 ) }, tinfo_t( BT_INT64 ), "value" )
);
cg.mb->insert_into_block(
hex::make_call( cg.insn.ea, hex::helper{ "_xsetbv" }, std::move( xsetbv_ci ) ).release(),
cg.mb->tail
);
cg.mb->mark_lists_dirty();
return true;
};
// Lifts simple instructions.
//
constexpr std::pair<uint16_t, const char*> simple_instruction_list[] =
{
{ NN_clac, "__clac" },
{ NN_stac, "__stac" },
{ NN_swapgs, "__swapgs" },
{ NN_saveprevssp, "__saveprevssp" },
{ NN_setssbsy, "__setssbsy" },
{ NN_endbr64, "__endbr64" },
{ NN_endbr32, "__endbr32" },
{ NN_incsspq, "__incsspq" },
{ NN_incsspd, "__incsspd" },
{ NN_rstorssp, "__rstorssp" },
{ NN_wrssd, "__wrssd" },
{ NN_wrssq, "__wrssq" },
{ NN_wrussd, "__wrussd" },
{ NN_wrussq, "__wrussq" },
{ NN_clrssbsy, "__clrssbsy" },
{ NN_clflushopt, "_mm_clflushopt" },
{ NN_clwb, "_mm_clwb" },
{ NN_vmclear, "__vmclear" },
{ NN_vmlaunch, "__vmlaunch" },
{ NN_vmptrld, "__vmptrld" },
{ NN_vmptrst, "__vmptrst" },
{ NN_vmwrite, "__vmwrite" },
{ NN_vmxoff, "__vmxoff" },
{ NN_vmxon, "__vmxon" },
{ NN_invept, "_invept" },
{ NN_invvpid, "_invvpid" },
{ NN_invpcid, "_invpcid" },
{ NN_invlpga, "_invlpga" },
{ NN_xsaves, "_xsaves" },
{ NN_xrstors, "_xrstors" },
{ NN_prefetcht0, "_mm_prefetcht0" },
{ NN_prefetcht1, "_mm_prefetcht1" },
{ NN_prefetcht2, "_mm_prefetcht2" },
{ NN_prefetchnta, "_mm_prefetchnta" },
// TODO: vmfunc.
};
hex::microcode_filter simple_instruction_lifter = [ ] ( codegen_t& cg )
{
// Pick the intrinsic.
//
auto it = std::find_if( std::begin( simple_instruction_list ), std::end( simple_instruction_list ), [ & ] ( auto& pair )
{
return cg.insn.itype == pair.first;
} );
if ( it == std::end( simple_instruction_list ) )
return false;
hex::helper helper{ it->second };
// Create the call information.
//
auto ci = hex::call_info( tinfo_t{ BT_VOID } );
for ( auto& ops : cg.insn.ops )
{
if ( ops.type == o_void )
break;
if ( ops.type == o_imm )
{
tinfo_t t = convert_dtype( ops.dtype );
if ( t.is_void() ) return false;
ci->args.push_back( hex::call_arg{ hex::operand{ ops.value, ( int ) t.get_size() }, t } );
}
else if ( ops.type == o_reg )
{
tinfo_t t = convert_dtype( ops.dtype );
if ( t.is_void() ) return false;
ci->args.push_back( hex::call_arg{ hex::phys_reg( ops.reg, t.get_size() ), t } );
}
else
{
tinfo_t t{};
t.create_ptr( tinfo_t{ BT_VOID } );
ci->args.push_back( hex::call_arg{ hex::reg( cg.load_effective_address( &ops - &cg.insn.ops[ 0 ] ), 8 ), t } );
}
}
// Emit the intrinsic.
//
cg.mb->insert_into_block(
hex::make_call( cg.insn.ea, helper, std::move( ci ) ).release(),
cg.mb->tail
);
cg.mb->mark_lists_dirty();
return true;
};
// Lifts RDRAND/RDSEED.
//
hex::microcode_filter rdrand_rdseed_lifter = [ ] ( codegen_t& cg )