-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
dotnet.c
3532 lines (2897 loc) · 96 KB
/
dotnet.c
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
/*
Copyright (c) 2015. The YARA Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <yara/dotnet.h>
#include <yara/mem.h>
#include <yara/modules.h>
#include <yara/pe.h>
#include <yara/pe_utils.h>
#include <yara/simple_str.h>
#include <yara/strutils.h>
#include <yara/unaligned.h>
#define MODULE_NAME dotnet
static uint32_t max_rows(int count, ...)
{
va_list ap;
uint32_t biggest;
uint32_t x;
if (count == 0)
return 0;
va_start(ap, count);
biggest = va_arg(ap, uint32_t);
for (int i = 1; i < count; i++)
{
x = va_arg(ap, uint32_t);
biggest = (x > biggest) ? x : biggest;
}
va_end(ap);
return biggest;
}
static uint32_t read_u32(const uint8_t** data)
{
uint32_t result = yr_le32toh(yr_unaligned_u32(*data));
*data += sizeof(uint32_t);
return result;
}
static uint16_t read_u16(const uint8_t** data)
{
uint16_t result = yr_le16toh(yr_unaligned_u16(*data));
*data += sizeof(uint16_t);
return result;
}
static uint8_t read_u8(const uint8_t** data)
{
uint8_t result = **data;
*data += sizeof(uint8_t);
return result;
}
static uint32_t read_index(const uint8_t** data, uint8_t len)
{
if (len == 2)
return read_u16(data);
else
return read_u32(data);
}
// Returns valid offset within the table or NULL
const uint8_t* get_table_offset(const TABLE_INFO* tbl, uint32_t index)
{
// Indexes to .NET tables are based from 1
if (index < 1 || index > tbl->RowCount)
return NULL;
return tbl->Offset + tbl->RowSize * (index - 1);
}
// Given an offset into a #US or #Blob stream, parse the entry at that position.
// The offset is relative to the start of the PE file.
// if size > 0 then it's valid and readable blob
BLOB_PARSE_RESULT dotnet_parse_blob_entry(PE* pe, const uint8_t* offset)
{
BLOB_PARSE_RESULT result = {.size = 0, .length = 0};
// Blob size is encoded in the first 1, 2 or 4 bytes of the blob.
//
// If the high bit is not set the length is encoded in one byte.
//
// If the high 2 bits are 10 (base 2) then the length is encoded in
// the rest of the bits and the next byte.
//
// If the high 3 bits are 110 (base 2) then the length is encoded
// in the rest of the bits and the next 3 bytes.
//
// See ECMA-335 II.24.2.4 for details.
// Make sure we have at least one byte.
if (!fits_in_pe(pe, offset, 1))
return result;
if ((*offset & 0x80) == 0x00)
{
result.length = (uint32_t) (*offset);
result.size = 1;
}
else if ((*offset & 0xC0) == 0x80)
{
// Make sure we have one more byte.
if (!fits_in_pe(pe, offset, 2))
return result;
// Shift remaining 6 bits left by 8 and OR in the remaining byte.
result.length = ((*offset & 0x3F) << 8) | *(offset + 1);
result.size = 2;
}
else if (offset + 4 < pe->data + pe->data_size && (*offset & 0xE0) == 0xC0)
{
// Make sure we have 3 more bytes.
if (!fits_in_pe(pe, offset, 4))
return result;
result.length = ((*offset & 0x1F) << 24) | (*(offset + 1) << 16) |
(*(offset + 2) << 8) | *(offset + 3);
result.size = 4;
}
else
{
// Return a 0 size as an error.
return result;
}
// Check if the length is actually readable
if (!fits_in_pe(pe, offset, result.size + result.length))
{
result.size = 0;
return result;
}
return result;
}
char* pe_get_dotnet_string(
PE* pe,
const uint8_t* heap_offset,
uint32_t heap_size,
uint32_t string_index)
{
size_t remaining;
char* start;
char* eos;
// Start of string must be within boundary
if (!(heap_offset + string_index >= pe->data &&
heap_offset + string_index < pe->data + pe->data_size &&
string_index < heap_size))
return NULL;
// Calculate how much until end of boundary, don't scan past that.
remaining = (pe->data + pe->data_size) - (heap_offset + string_index);
// Search for a NULL terminator from start of string, up to remaining.
start = (char*) (heap_offset + string_index);
eos = (char*) memmem((void*) start, remaining, "\0", 1);
// If no NULL terminator was found or the string is too large, return NULL.
if (eos == NULL || eos - start > 1024)
return NULL;
return start;
}
static bool is_nested(uint32_t flags)
{
// ECMA 335 II.22.37
// Whether a type is nested can be determined by the value of its
// Flags.Visibility sub-field – it shall be one of the set
// { NestedPublic, NestedPrivate, NestedFamily, NestedAssembly,
// NestedFamANDAssem, NestedFamORAssem }
switch (flags & TYPE_ATTR_VISIBILITY_MASK)
{
case TYPE_ATTR_NESTED_PRIVATE:
case TYPE_ATTR_NESTED_PUBLIC:
case TYPE_ATTR_NESTED_FAMILY:
case TYPE_ATTR_NESTED_ASSEMBLY:
case TYPE_ATTR_NESTED_FAM_AND_ASSEM:
case TYPE_ATTR_NESTED_FAM_OR_ASSEM:
return true;
default:
return false;
}
}
// ECMA 335 II.23.1.15 Flags for types [TypeAttribute]
static const char* get_type_visibility(uint32_t flags)
{
switch (flags & TYPE_ATTR_VISIBILITY_MASK)
{
case TYPE_ATTR_NESTED_PRIVATE:
return "private";
case TYPE_ATTR_PUBLIC:
case TYPE_ATTR_NESTED_PUBLIC:
return "public";
case TYPE_ATTR_NESTED_FAMILY:
return "protected";
case TYPE_ATTR_NOT_PUBLIC:
case TYPE_ATTR_NESTED_ASSEMBLY:
return "internal";
case TYPE_ATTR_NESTED_FAM_AND_ASSEM:
return "private protected";
case TYPE_ATTR_NESTED_FAM_OR_ASSEM:
return "protected internal";
default:
return "private";
}
}
// ECMA 335 II.23.1.10 Flags for methods [MethodAttributes]
static const char* get_method_visibility(uint32_t flags)
{
switch (flags & METHOD_ATTR_ACCESS_MASK)
{
case METHOD_ATTR_PRIVATE:
return "private";
case METHOD_ATTR_FAM_AND_ASSEM:
return "private protected";
case METHOD_ATTR_ASSEM:
return "internal";
case METHOD_ATTR_FAMILY:
return "protected";
case METHOD_ATTR_FAM_OR_ASSEM:
return "protected internal";
case METHOD_ATTR_PUBLIC:
return "public";
default:
return "private";
}
}
// ECMA 335 II.23.1.15 Flags for types [TypeAttribute]
static const char* get_typedef_type(uint32_t flags)
{
switch (flags & TYPE_ATTR_CLASS_SEMANTIC_MASK)
{
case TYPE_ATTR_CLASS:
return "class";
case TYPE_ATTR_INTERFACE:
return "interface";
default:
return NULL;
}
}
// returns allocated string <namespace>.<name>, must be freed
static char* create_full_name(const char* name, const char* namespace)
{
if (!name || !strlen(name))
return namespace ? yr_strdup(namespace) : NULL;
// No namespace -> return name only
if (!namespace || !strlen(namespace))
{
// fix generic names
char* name_copy = yr_strdup(name);
char* end = strchr(name_copy, '`');
if (end)
*end = 0;
return name_copy;
}
size_t name_len = strlen(name);
size_t namespace_len = strlen(namespace);
// <namespace>.<name>
char* full_name = yr_malloc(namespace_len + 1 + name_len + 1);
memcpy(full_name, namespace, namespace_len);
full_name[namespace_len] = '.';
memcpy(full_name + namespace_len + 1, name, name_len + 1);
// fix generic names
char* end = strchr(full_name, '`');
if (end)
*end = 0;
return full_name;
}
static bool read_typedef(
const CLASS_CONTEXT* ctx,
const uint8_t* data,
TYPEDEF_ROW* result)
{
uint32_t row_size = ctx->tables->typedef_.RowSize;
if (fits_in_pe(ctx->pe, data, row_size))
{
uint8_t ext_size = 2;
uint32_t row_count = max_rows(
3,
ctx->tables->typedef_.RowCount,
ctx->tables->typeref.RowCount,
ctx->tables->typespec.RowCount);
if (row_count > (0xFFFF >> 0x02))
ext_size = 4;
result->Flags = read_u32(&data);
result->Name = read_index(&data, ctx->index_sizes->string);
result->Namespace = read_index(&data, ctx->index_sizes->string);
result->Extends = read_index(&data, ext_size);
result->Field = read_index(&data, ctx->index_sizes->field);
result->Method = read_index(&data, ctx->index_sizes->methoddef);
return true;
}
return false;
}
static bool read_typeref(
const CLASS_CONTEXT* ctx,
const uint8_t* data,
TYPEREF_ROW* result)
{
uint32_t row_size = ctx->tables->typeref.RowSize;
if (fits_in_pe(ctx->pe, data, row_size))
{
uint8_t res_size = 2;
uint32_t row_count = max_rows(
4,
ctx->tables->module.RowCount,
ctx->tables->moduleref.RowCount,
ctx->tables->assemblyref.RowCount,
ctx->tables->typeref.RowCount);
if (row_count > (0xFFFF >> 0x02))
res_size = 4;
result->ResolutionScope = read_index(&data, res_size);
result->Name = read_index(&data, ctx->index_sizes->string);
result->Namespace = read_index(&data, ctx->index_sizes->string);
return true;
}
return false;
}
static bool read_interfaceimpl(
const CLASS_CONTEXT* ctx,
const uint8_t* data,
INTERFACEIMPL_ROW* result)
{
uint32_t row_size = ctx->tables->intefaceimpl.RowSize;
if (fits_in_pe(ctx->pe, data, row_size))
{
uint32_t interface_size = 2;
uint32_t row_count = max_rows(
3,
ctx->tables->typedef_.RowCount,
ctx->tables->typeref.RowCount,
ctx->tables->typespec.RowCount);
if (row_count > (0xFFFF >> 0x02))
interface_size = 4;
result->Class = read_index(&data, ctx->index_sizes->typedef_);
result->Interface = read_index(&data, interface_size);
return true;
}
return false;
}
static bool read_methoddef(
const CLASS_CONTEXT* ctx,
const uint8_t* data,
METHODDEF_ROW* result)
{
uint32_t row_size = ctx->tables->methoddef.RowSize;
if (fits_in_pe(ctx->pe, data, row_size))
{
result->Rva = read_u32(&data);
result->ImplFlags = read_u16(&data);
result->Flags = read_u16(&data);
result->Name = read_index(&data, ctx->index_sizes->string);
result->Signature = read_index(&data, ctx->index_sizes->blob);
result->ParamList = read_index(&data, ctx->index_sizes->param);
return true;
}
return false;
}
static bool read_param(
const CLASS_CONTEXT* ctx,
const uint8_t* data,
PARAM_ROW* result)
{
uint32_t row_size = ctx->tables->param.RowSize;
if (fits_in_pe(ctx->pe, data, row_size))
{
result->Flags = read_u16(&data);
result->Sequence = read_u16(&data);
result->Name = read_index(&data, ctx->index_sizes->string);
return true;
}
return false;
}
static bool read_genericparam(
const CLASS_CONTEXT* ctx,
const uint8_t* data,
GENERICPARAM_ROW* result)
{
uint32_t row_size = ctx->tables->genericparam.RowSize;
if (fits_in_pe(ctx->pe, data, row_size))
{
uint32_t owner_idx_size = 2;
uint32_t row_count = max_rows(
2, ctx->tables->typedef_.RowCount, ctx->tables->methoddef.RowCount);
if (row_count > (0xFFFF >> 0x01))
owner_idx_size = 4;
result->Number = read_u16(&data);
result->Flags = read_u16(&data);
result->Owner = read_index(&data, owner_idx_size);
result->Name = read_index(&data, ctx->index_sizes->string);
return true;
}
return false;
}
static bool read_typespec(
const CLASS_CONTEXT* ctx,
const uint8_t* data,
TYPESPEC_ROW* result)
{
uint32_t row_size = ctx->tables->typespec.RowSize;
if (fits_in_pe(ctx->pe, data, row_size))
{
result->Signature = read_index(&data, ctx->index_sizes->blob);
return true;
}
return false;
}
static bool read_nestedclass(
const CLASS_CONTEXT* ctx,
const uint8_t* data,
NESTEDCLASS_ROW* result)
{
uint32_t row_size = ctx->tables->nestedclass.RowSize;
if (fits_in_pe(ctx->pe, data, row_size))
{
result->NestedClass = read_index(&data, ctx->index_sizes->typedef_);
result->EnclosingClass = read_index(&data, ctx->index_sizes->typedef_);
return true;
}
return false;
}
// ECMA-335 II.23.2 blob heap uses variable length encoding of integers
static uint32_t read_blob_unsigned(const uint8_t** data, uint32_t* len)
{
if (*len < 1)
return 0;
// first byte is enough to decode the length
// without worrying about endiannity
// Compressed integers use big-endian order
uint8_t first_byte = *(*data);
// If the value lies between 0 (0x00) and 127 (0x7F), inclusive, encode as a
// one-byte integer (bit 7 is clear, value held in bits 6 through 0)
if (!(first_byte & 0x80))
{
*data += sizeof(uint8_t);
*len -= sizeof(uint8_t);
return first_byte;
}
if (*len < 2)
return 0;
// If the value lies between 2^8 (0x80) and 2^14 – 1 (0x3FFF), inclusive,
// encode as a 2-byte integer with bit 15 set, bit 14 clear (value held in
// bits 13 through 0)
if ((first_byte & 0xC0) == 0x80)
{
uint32_t result = yr_be16toh(yr_unaligned_u16(*data));
*data += sizeof(uint16_t);
*len -= sizeof(uint16_t);
// value is in lower 14 bits
return result & 0x3FFF;
}
if (*len < 4)
return 0;
// Otherwise, encode as a 4-byte integer, with bit 31 set, bit 30 set,
// bit 29 clear (value held in bits 28 through 0)
if ((first_byte & 0xE0) == 0xC0)
{
uint32_t result = yr_be32toh(yr_unaligned_u32(*data));
*data += sizeof(uint32_t);
*len -= sizeof(uint32_t);
// Uses last 29 bits for the result
return result & 0x1FFFFFFF;
}
return 0;
}
// ECMA-335 II.23.2 blob heap uses variable length encoding of integers
// Probably wouldn't work on non 2's complement arches?
static int32_t read_blob_signed(const uint8_t** data, uint32_t* len)
{
// Compressed integers use big-endian order!
if (*len < 1)
return 0;
// first byte is enough to decode the length
// without worrying about endiannity
int8_t first_byte = *(*data);
// Encode as a one-byte integer, bit 7 clear, rotated value in bits 6
// through 0, giving 0x01 (-2^6) to 0x7E (2^6-1).
if (!(first_byte & 0x80))
{
int8_t tmp = first_byte >> 1;
// sign extension in case of negative number
if (first_byte & 0x1)
tmp |= 0xC0;
*data += sizeof(uint8_t);
*len -= sizeof(uint8_t);
return (int32_t) tmp;
}
if (*len < 2)
return 0;
// Encode as a two-byte integer: bit 15 set, bit 14 clear, rotated value
// in bits 13 through 0, giving 0x8001 (-2^13) to 0xBFFE (2^13-1).
if ((first_byte & 0xC0) == 0x80)
{
uint16_t tmp1 = yr_be16toh(yr_unaligned_u16(*data));
// shift and leave top 2 bits clear
uint16_t tmp2 = (tmp1 >> 1) & 0x3FFF;
// sign extension in case of negative number
if (tmp1 & 0x1)
tmp2 |= 0xC000;
*data += sizeof(uint16_t);
*len -= sizeof(uint16_t);
return (int32_t) tmp2;
}
if (*len < 4)
return 0;
// Encode as a four-byte integer: bit 31 set, 30 set, bit 29 clear,
// rotated value in bits 28 through 0, giving 0xC0000001 (-2^28) to
// 0xDFFFFFFE (2^28-1).
if ((first_byte & 0xE0) == 0xC0)
{
uint32_t tmp1 = yr_be32toh(yr_unaligned_u32(*data));
// shift and leave top 3 bits clear
uint32_t tmp2 = (tmp1 >> 1) & 0x1FFFFFFF;
// sign extension in case of negative number
if (tmp1 & 0x1)
tmp2 |= 0xE0000000;
*data += sizeof(uint32_t);
*len -= sizeof(uint32_t);
return (int32_t) tmp2;
}
return 0;
}
// Forward declarations
static char* parse_signature_type(
const CLASS_CONTEXT* ctx,
const uint8_t** data,
uint32_t* len,
GENERIC_PARAMETERS* class_gen_params,
GENERIC_PARAMETERS* method_gen_params,
uint32_t depth);
static char* parse_enclosing_types(
const CLASS_CONTEXT* ctx,
uint32_t nested_idx,
uint32_t depth);
static char* get_type_def_or_ref_fullname(
const CLASS_CONTEXT* ctx,
uint32_t coded_index,
GENERIC_PARAMETERS* class_gen_params,
GENERIC_PARAMETERS* method_gen_params,
uint32_t depth) // against loops
{
// first 2 bits define table, index starts with third bit
uint32_t index = coded_index >> 2;
if (!index)
return NULL;
const uint8_t* str_heap = ctx->str_heap;
uint32_t str_size = ctx->str_size;
uint8_t table = coded_index & 0x3;
if (table == 0) // TypeDef
{
const uint8_t* data = get_table_offset(&ctx->tables->typedef_, index);
if (!data)
return NULL;
TYPEDEF_ROW def_row;
bool result = read_typedef(ctx, data, &def_row);
if (result)
{
const char* name = pe_get_dotnet_string(
ctx->pe, str_heap, str_size, def_row.Name);
const char* namespace = pe_get_dotnet_string(
ctx->pe, str_heap, str_size, def_row.Namespace);
char* result = NULL;
// Type might be nested, try to find correct namespace
if (is_nested(def_row.Flags))
{
char* nested_namespace = parse_enclosing_types(ctx, index, 1);
char* tmp = create_full_name(namespace, nested_namespace);
result = create_full_name(name, tmp);
yr_free(nested_namespace);
yr_free(tmp);
}
else
result = create_full_name(name, namespace);
return result;
}
}
else if (table == 1) // TypeRef
{
const uint8_t* data = get_table_offset(&ctx->tables->typeref, index);
if (!data)
return NULL;
TYPEREF_ROW ref_row;
bool result = read_typeref(ctx, data, &ref_row);
if (result)
{
const char* name = pe_get_dotnet_string(
ctx->pe, str_heap, str_size, ref_row.Name);
const char* namespace = pe_get_dotnet_string(
ctx->pe, str_heap, str_size, ref_row.Namespace);
return create_full_name(name, namespace);
}
}
else if (table == 2) // TypeSpec
{
const uint8_t* data = get_table_offset(&ctx->tables->typespec, index);
if (!data)
return NULL;
TYPESPEC_ROW spec_row;
bool result = read_typespec(ctx, data, &spec_row);
if (result)
{
const uint8_t* sig_data = ctx->blob_heap + spec_row.Signature;
// Read the blob entry with the data
BLOB_PARSE_RESULT blob_res = dotnet_parse_blob_entry(ctx->pe, sig_data);
sig_data += blob_res.size;
uint32_t sig_len = blob_res.length;
// Valid blob
if (blob_res.size)
return parse_signature_type(
ctx, &sig_data, &sig_len, class_gen_params, NULL, depth);
}
}
return NULL;
}
static char* parse_signature_type(
const CLASS_CONTEXT* ctx,
const uint8_t** data,
uint32_t* len,
GENERIC_PARAMETERS* class_gen_params,
GENERIC_PARAMETERS* method_gen_params,
uint32_t depth // against loops
)
{
// If at least first type fits and we are not too nested
if (*len < 1 || !fits_in_pe(ctx->pe, *data, 1) || depth > MAX_TYPE_DEPTH)
return NULL;
bool class = false;
uint32_t coded_index, index;
char* tmp = NULL;
char* ret_type = NULL;
uint8_t type = read_u8(data);
*len -= 1;
switch (type)
{
case TYPE_VOID:
ret_type = "void";
break;
case TYPE_BOOL:
ret_type = "bool";
break;
case TYPE_CHAR:
ret_type = "char";
break;
case TYPE_I1:
ret_type = "sbyte";
break;
case TYPE_U1:
ret_type = "byte";
break;
case TYPE_I2:
ret_type = "short";
break;
case TYPE_U2:
ret_type = "ushort";
break;
case TYPE_I4:
ret_type = "int";
break;
case TYPE_U4:
ret_type = "uint";
break;
case TYPE_I8:
ret_type = "long";
break;
case TYPE_U8:
ret_type = "ulong";
break;
case TYPE_R4:
ret_type = "float";
break;
case TYPE_R8:
ret_type = "double";
break;
case TYPE_STRING:
ret_type = "string";
break;
case TYPE_TYPEDREF:
ret_type = "TypedReference";
break;
case TYPE_I:
ret_type = "IntPtr";
break;
case TYPE_U:
ret_type = "UIntPtr";
break;
case TYPE_PTR: // Ptr followed by type
tmp = parse_signature_type(
ctx, data, len, class_gen_params, method_gen_params, depth + 1);
if (tmp)
{
SIMPLE_STR* ss = sstr_new(NULL);
if (!ss)
{
yr_free(tmp);
break;
}
bool res = sstr_appendf(ss, "Ptr<%s>", tmp);
if (res)
ret_type = sstr_move(ss);
yr_free(tmp);
sstr_free(ss);
return ret_type;
}
break;
case TYPE_BYREF:
// ByRef followed by type
tmp = parse_signature_type(
ctx, data, len, class_gen_params, method_gen_params, depth + 1);
if (tmp)
{
SIMPLE_STR* ss = sstr_new(NULL);
if (!ss)
{
yr_free(tmp);
break;
}
bool res = sstr_appendf(ss, "ref %s", tmp);
if (res)
ret_type = sstr_move(ss);
yr_free(tmp);
sstr_free(ss);
return ret_type;
}
break;
case TYPE_VALUETYPE: // ValueType
case TYPE_CLASS: // Class
// followed by TypeDefOrRefOrSpecEncoded index
coded_index = read_blob_unsigned(data, len);
return get_type_def_or_ref_fullname(
ctx, coded_index, class_gen_params, method_gen_params, depth + 1);
break;
case TYPE_VAR: // Generic class var
case TYPE_MVAR: // Generic method var
index = read_blob_unsigned(data, len);
class = type == TYPE_VAR;
// return class generic var or method generic var
if (class && class_gen_params && index < class_gen_params->len)
ret_type = class_gen_params->names[index];
else if (!class && method_gen_params && index < method_gen_params->len)
ret_type = method_gen_params->names[index];
break;
case TYPE_ARRAY:
{
// Array -> Type -> Rank -> NumSizes -> Size -> NumLobound -> LoBound
char* tmp = parse_signature_type(
ctx, data, len, class_gen_params, method_gen_params, depth + 1);
if (!tmp)
break;
int32_t* sizes = NULL;
int32_t* lo_bounds = NULL;
// Read number of dimensions
uint32_t rank = read_blob_unsigned(data, len);
if (!rank || rank > MAX_ARRAY_RANK)
goto cleanup;
// Read number of specified sizes
uint32_t num_sizes = read_blob_unsigned(data, len);
sizes = yr_malloc(sizeof(uint32_t) * num_sizes);
if (!sizes || num_sizes > rank)
goto cleanup;
for (uint32_t i = 0; i < num_sizes; ++i)
{
sizes[i] = read_blob_unsigned(data, len);
}
// Read number of specified lower bounds
uint32_t num_lowbounds = read_blob_unsigned(data, len);
lo_bounds = yr_malloc(sizeof(int32_t) * num_lowbounds);
if (!lo_bounds || num_lowbounds > rank)
goto cleanup;
for (uint32_t i = 0; i < num_lowbounds; ++i)
{
lo_bounds[i] = read_blob_signed(data, len);
// Adjust higher bound according to lower bound
if (num_sizes > i)
sizes[i] += lo_bounds[i];
}
// Build the resulting array type
SIMPLE_STR* ss = sstr_new(NULL);
if (!ss)
goto cleanup;
sstr_appendf(ss, "%s[", tmp);
for (uint32_t i = 0; i < rank; ++i)
{
if (num_sizes > i || num_lowbounds > i)
{
if (num_lowbounds > i && lo_bounds[i] != 0)
sstr_appendf(ss, "%d...", lo_bounds[i]);
if (num_sizes > i && sizes[i] != 0)
sstr_appendf(ss, "%d", sizes[i]);
}
if (i + 1 != rank)
sstr_appendf(ss, ",");
}
bool res = sstr_appendf(ss, "]");
if (res)
ret_type = sstr_move(ss);
yr_free(sizes);
yr_free(lo_bounds);
yr_free(tmp);
sstr_free(ss);
return ret_type;
cleanup:
yr_free(sizes);
yr_free(lo_bounds);
yr_free(tmp);
}
break;
case TYPE_GENERICINST:
{
tmp = parse_signature_type(
ctx, data, len, class_gen_params, method_gen_params, depth + 1);
if (!tmp)
break;
uint32_t gen_count = read_blob_unsigned(data, len);
// Sanity check for corrupted files
if (gen_count > MAX_GEN_PARAM_COUNT)
{
yr_free(tmp);
break;
}
SIMPLE_STR* ss = sstr_new(NULL);
if (!ss)
{
yr_free(tmp);
break;
}
sstr_appendf(ss, "%s<", tmp);
yr_free(tmp);
for (int i = 0; i < gen_count; i++)
{
char* param_type = parse_signature_type(
ctx, data, len, class_gen_params, method_gen_params, depth + 1);
if (param_type != NULL)
{
if (i > 0)
sstr_appendf(ss, ",");
sstr_appendf(ss, "%s", param_type);
yr_free(param_type);
}
}
bool res = sstr_appendf(ss, ">");
if (res)
ret_type = sstr_move(ss);
sstr_free(ss);
return ret_type;