forked from VirusTotal/yara
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotnet.c
1721 lines (1387 loc) · 48.5 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.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#include <time.h>
#include <yara/pe.h>
#include <yara/dotnet.h>
#include <yara/modules.h>
#include <yara/mem.h>
#include <yara/strutils.h>
#include <yara/pe_utils.h>
#define MODULE_NAME dotnet
char* pe_get_dotnet_string(
PE* pe,
uint8_t* string_offset,
DWORD string_index)
{
size_t remaining;
char* start;
char* eos;
// Start of string must be within boundary
if (!(string_offset + string_index >= pe->data &&
string_offset + string_index < pe->data + pe->data_size))
return NULL;
// Calculate how much until end of boundary, don't scan past that.
remaining = (pe->data + pe->data_size) - (string_offset + string_index);
// Search for a NULL terminator from start of string, up to remaining.
start = (char*) (string_offset + string_index);
eos = (char*) memmem((void*) start, remaining, "\0", 1);
if (eos == NULL)
return eos;
return start;
}
uint32_t max_rows(int count, ...)
{
va_list ap;
int i;
uint32_t biggest;
uint32_t x;
if (count == 0)
return 0;
va_start(ap, count);
biggest = va_arg(ap, uint32_t);
for (i = 1; i < count; i++)
{
x = va_arg(ap, uint32_t);
biggest = (x > biggest) ? x : biggest;
}
va_end(ap);
return biggest;
}
void dotnet_parse_guid(
PE* pe,
int64_t metadata_root,
PSTREAM_HEADER guid_header)
{
// GUIDs are 16 bytes each, converted to hex format plus separators and NULL.
char guid[37];
int i = 0;
uint8_t* guid_offset = pe->data + metadata_root + guid_header->Offset;
DWORD guid_size = guid_header->Size;
// Parse GUIDs if we have them.
// GUIDs are 16 bytes each.
while (guid_size >= 16 && fits_in_pe(pe, guid_offset, 16))
{
sprintf(guid, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
*(uint32_t*) guid_offset,
*(uint16_t*) (guid_offset + 4),
*(uint16_t*) (guid_offset + 6),
*(guid_offset + 8),
*(guid_offset + 9),
*(guid_offset + 10),
*(guid_offset + 11),
*(guid_offset + 12),
*(guid_offset + 13),
*(guid_offset + 14),
*(guid_offset + 15));
guid[(16 * 2) + 4] = '\0';
set_string(guid, pe->object, "guids[%i]", i);
i++;
guid_size -= 16;
}
set_integer(i, pe->object, "number_of_guids");
}
// 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.
BLOB_PARSE_RESULT dotnet_parse_blob_entry(
PE* pe,
uint8_t* offset)
{
BLOB_PARSE_RESULT result;
// 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))
{
result.size = 0;
return result;
}
if ((*offset & 0x80) == 0x00)
{
result.length = (DWORD) *offset;
result.size = 1;
}
else if ((*offset & 0xC0) == 0x80)
{
// Make sure we have one more byte.
if (!fits_in_pe(pe, offset, 2))
{
result.size = 0;
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))
{
result.size = 0;
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.
result.size = 0;
}
return result;
}
void dotnet_parse_us(
PE* pe,
int64_t metadata_root,
PSTREAM_HEADER us_header)
{
BLOB_PARSE_RESULT blob_result;
int i = 0;
uint8_t* offset = pe->data + metadata_root + us_header->Offset;
uint8_t* end_of_header = offset + us_header->Size;
// Make sure end of header is not past end of PE, and the first entry MUST be
// a single NULL byte.
if (!fits_in_pe(pe, offset, us_header->Size) || *offset != 0x00)
return;
offset++;
while (offset < end_of_header)
{
blob_result = dotnet_parse_blob_entry(pe, offset);
if (blob_result.size == 0 || !fits_in_pe(pe, offset, blob_result.length))
{
set_integer(i, pe->object, "number_of_user_strings");
return;
}
offset += blob_result.size;
// Avoid empty strings, which usually happen as padding at the end of the
// stream.
if (blob_result.length > 0)
{
set_sized_string(
(char*) offset,
blob_result.length,
pe->object,
"user_strings[%i]",
i);
offset += blob_result.length;
i++;
}
}
set_integer(i, pe->object, "number_of_user_strings");
}
STREAMS dotnet_parse_stream_headers(
PE* pe,
int64_t offset,
int64_t metadata_root,
DWORD num_streams)
{
PSTREAM_HEADER stream_header;
STREAMS headers;
char *start;
char *eos;
char stream_name[DOTNET_STREAM_NAME_SIZE + 1];
int i;
memset(&headers, '\0', sizeof(STREAMS));
stream_header = (PSTREAM_HEADER) (pe->data + offset);
for (i = 0; i < num_streams; i++)
{
if (!struct_fits_in_pe(pe, stream_header, STREAM_HEADER))
break;
start = (char*) stream_header->Name;
if (!fits_in_pe(pe, start, DOTNET_STREAM_NAME_SIZE))
break;
eos = (char*) memmem((void*) start, DOTNET_STREAM_NAME_SIZE, "\0", 1);
if (eos == NULL)
break;
strncpy(stream_name, stream_header->Name, DOTNET_STREAM_NAME_SIZE);
stream_name[DOTNET_STREAM_NAME_SIZE] = '\0';
set_string(stream_name,
pe->object, "streams[%i].name", i);
// Offset is relative to metadata_root.
set_integer(metadata_root + stream_header->Offset,
pe->object, "streams[%i].offset", i);
set_integer(stream_header->Size,
pe->object, "streams[%i].size", i);
// Store necessary bits to parse these later. Not all tables will be
// parsed, but are referenced from others. For example, the #Strings
// stream is referenced from various tables in the #~ heap.
if (strncmp(stream_name, "#GUID", 5) == 0)
headers.guid = stream_header;
// Believe it or not, I have seen at least one binary which has a #- stream
// instead of a #~ (215e1b54ae1aac153e55596e6f1a4350). This isn't in the
// documentation anywhere but the structure is the same. I'm chosing not
// to parse it for now.
else if (strncmp(stream_name, "#~", 2) == 0 && headers.tilde == NULL)
headers.tilde = stream_header;
else if (strncmp(stream_name, "#Strings", 8) == 0 && headers.string == NULL)
headers.string = stream_header;
else if (strncmp(stream_name, "#Blob", 5) == 0)
headers.blob = stream_header;
else if (strncmp(stream_name, "#US", 3) == 0 && headers.us == NULL)
headers.us = stream_header;
// Stream name is padded to a multiple of 4.
stream_header = (PSTREAM_HEADER) ((uint8_t*) stream_header +
sizeof(STREAM_HEADER) +
strlen(stream_name) +
4 - (strlen(stream_name) % 4));
}
set_integer(i, pe->object, "number_of_streams");
return headers;
}
// This is the second pass through the data for #~. The first pass collects
// information on the number of rows for tables which have coded indexes.
// This pass uses that information and the index_sizes to parse the tables
// of interest.
//
// Because the indexes can vary in size depending upon the number of rows in
// other tables it is impossible to use static sized structures. To deal with
// this hardcode the sizes of each table based upon the documentation (for the
// static sized portions) and use the variable sizes accordingly.
void dotnet_parse_tilde_2(
PE* pe,
PTILDE_HEADER tilde_header,
int64_t resource_base,
int64_t metadata_root,
ROWS rows,
INDEX_SIZES index_sizes,
PSTREAMS streams)
{
PMODULE_TABLE module_table;
PASSEMBLY_TABLE assembly_table;
PASSEMBLYREF_TABLE assemblyref_table;
PMANIFESTRESOURCE_TABLE manifestresource_table;
PMODULEREF_TABLE moduleref_table;
PCUSTOMATTRIBUTE_TABLE customattribute_table;
PCONSTANT_TABLE constant_table;
DWORD resource_size, implementation;
char *name;
char typelib[MAX_TYPELIB_SIZE + 1];
int i, bit_check;
int matched_bits = 0;
int64_t resource_offset;
uint32_t row_size, row_count, counter;
uint8_t* string_offset;
uint8_t* blob_offset;
uint32_t num_rows = 0;
uint32_t valid_rows = 0;
uint32_t* row_offset = NULL;
uint8_t* table_offset = NULL;
uint8_t* row_ptr = NULL;
// These are pointers and row sizes for tables of interest to us for special
// parsing. For example, we are interested in pulling out any CustomAttributes
// that are GUIDs so we need to be able to walk these tables. To find GUID
// CustomAttributes you need to walk the CustomAttribute table and look for
// any row with a Parent that indexes into the Assembly table and Type indexes
// into the MemberRef table. Then you follow the index into the MemberRef
// table and check the Class to make sure it indexes into TypeRef table. If it
// does you follow that index and make sure the Name is "GuidAttribute". If
// all that is valid then you can take the Value from the CustomAttribute
// table to find out the index into the Blob stream and parse that.
//
// Luckily we can abuse the fact that the order of the tables is guaranteed
// consistent (though some may not exist, but if they do exist they must exist
// in a certain order). The order is defined by their position in the Valid
// member of the tilde_header structure. By the time we are parsing the
// CustomAttribute table we have already recorded the location of the TypeRef
// and MemberRef tables, so we can follow the chain back up from
// CustomAttribute through MemberRef to TypeRef.
uint8_t* typeref_ptr = NULL;
uint8_t* memberref_ptr = NULL;
uint32_t typeref_row_size = 0;
uint32_t memberref_row_size = 0;
uint8_t* typeref_row = NULL;
uint8_t* memberref_row = NULL;
DWORD type_index;
DWORD class_index;
BLOB_PARSE_RESULT blob_result;
DWORD blob_index;
DWORD blob_length;
// These are used to determine the size of coded indexes, which are the
// dynamically sized columns for some tables. The coded indexes are
// documented in ECMA-335 Section II.24.2.6.
uint8_t index_size, index_size2;
// Number of rows is the number of bits set to 1 in Valid.
// Should use this technique:
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
for (i = 0; i < 64; i++)
valid_rows += ((tilde_header->Valid >> i) & 0x01);
row_offset = (uint32_t*) (tilde_header + 1);
table_offset = (uint8_t*) row_offset;
table_offset += sizeof(uint32_t) * valid_rows;
#define DOTNET_STRING_INDEX(Name) \
index_sizes.string == 2 ? Name.Name_Short : Name.Name_Long
string_offset = pe->data + metadata_root + streams->string->Offset;
// Now walk again this time parsing out what we care about.
for (bit_check = 0; bit_check < 64; bit_check++)
{
// If the Valid bit is not set for this table, skip it...
if (!((tilde_header->Valid >> bit_check) & 0x01))
continue;
// Make sure table_offset doesn't go crazy by inserting a large value
// for num_rows. For example edc05e49dd3810be67942b983455fd43 sets a
// large value for number of rows for the BIT_MODULE section.
if (!fits_in_pe(pe, table_offset, 1))
return;
num_rows = *(row_offset + matched_bits);
// Those tables which exist, but that we don't care about must be
// skipped.
//
// Sadly, given the dynamic sizes of some columns we can not have well
// defined structures for all tables and use them accordingly. To deal
// with this manually move the table_offset pointer by the appropriate
// number of bytes as described in the documentation for each table.
//
// The table structures are documented in ECMA-335 Section II.22.
switch (bit_check)
{
case BIT_MODULE:
module_table = (PMODULE_TABLE) table_offset;
name = pe_get_dotnet_string(pe,
string_offset,
DOTNET_STRING_INDEX(module_table->Name));
if (name != NULL)
set_string(name, pe->object, "module_name");
table_offset += (
2 + index_sizes.string + (index_sizes.guid * 3)) * num_rows;
break;
case BIT_TYPEREF:
row_count = max_rows(4,
rows.module,
rows.moduleref,
rows.assemblyref,
rows.typeref);
if (row_count > (0xFFFF >> 0x02))
index_size = 4;
else
index_size = 2;
row_size = (index_size + (index_sizes.string * 2));
typeref_row_size = row_size;
typeref_ptr = table_offset;
table_offset += row_size * num_rows;
break;
case BIT_TYPEDEF:
row_count = max_rows(3,
rows.typedef_,
rows.typeref,
rows.typespec);
if (row_count > (0xFFFF >> 0x02))
index_size = 4;
else
index_size = 2;
table_offset += (
4 + (index_sizes.string * 2) + index_size +
index_sizes.field + index_sizes.methoddef) * num_rows;
break;
case BIT_FIELDPTR:
// This one is not documented in ECMA-335.
table_offset += (index_sizes.field) * num_rows;
break;
case BIT_FIELD:
table_offset += (
2 + (index_sizes.string) + index_sizes.blob) * num_rows;
break;
case BIT_METHODDEFPTR:
// This one is not documented in ECMA-335.
table_offset += (index_sizes.methoddef) * num_rows;
break;
case BIT_METHODDEF:
table_offset += (
4 + 2 + 2 +
index_sizes.string +
index_sizes.blob +
index_sizes.param) * num_rows;
break;
case BIT_PARAM:
table_offset += (2 + 2 + index_sizes.string) * num_rows;
break;
case BIT_INTERFACEIMPL:
row_count = max_rows(3,
rows.typedef_,
rows.typeref,
rows.typespec);
if (row_count > (0xFFFF >> 0x02))
index_size = 4;
else
index_size = 2;
table_offset += (index_sizes.typedef_ + index_size) * num_rows;
break;
case BIT_MEMBERREF:
row_count = max_rows(4,
rows.methoddef,
rows.moduleref,
rows.typeref,
rows.typespec);
if (row_count > (0xFFFF >> 0x03))
index_size = 4;
else
index_size = 2;
row_size = (index_size + index_sizes.string + index_sizes.blob);
memberref_row_size = row_size;
memberref_ptr = table_offset;
table_offset += row_size * num_rows;
break;
case BIT_CONSTANT:
row_count = max_rows(3, rows.param, rows.field, rows.property);
if (row_count > (0xFFFF >> 0x02))
index_size = 4;
else
index_size = 2;
// Using 'i' is insufficent since we may skip certain constants and
// it would give an inaccurate count in that case.
counter = 0;
row_size = (1 + 1 + index_size + index_sizes.blob);
row_ptr = table_offset;
for (i = 0; i < num_rows; i++)
{
if (!fits_in_pe(pe, row_ptr, row_size))
break;
constant_table = (PCONSTANT_TABLE) row_ptr;
// Only look for constants of type string.
if (constant_table->Type != ELEMENT_TYPE_STRING)
{
row_ptr += row_size;
continue;
}
// Get the blob offset and pull it out of the blob table.
blob_offset = ((uint8_t*) constant_table) + 2 + index_size;
if (index_sizes.blob == 4)
blob_index = *(DWORD*) blob_offset;
else
// Cast the value (index into blob table) to a 32bit value.
blob_index = (DWORD) (*(WORD*) blob_offset);
// Everything checks out. Make sure the index into the blob field
// is valid (non-null and within range).
blob_offset = \
pe->data + metadata_root +
streams->blob->Offset + blob_index;
blob_result = dotnet_parse_blob_entry(pe, blob_offset);
if (blob_result.size == 0)
{
row_ptr += row_size;
continue;
}
blob_length = blob_result.length;
blob_offset += blob_result.size;
// Quick sanity check to make sure the blob entry is within bounds.
if (blob_offset + blob_length >= pe->data + pe->data_size)
{
row_ptr += row_size;
continue;
}
set_sized_string(
(char*) blob_offset,
blob_result.length,
pe->object,
"constants[%i]",
counter);
counter++;
row_ptr += row_size;
}
set_integer(counter, pe->object, "number_of_constants");
table_offset += row_size * num_rows;
break;
case BIT_CUSTOMATTRIBUTE:
// index_size is size of the parent column.
row_count = max_rows(21,
rows.methoddef,
rows.field,
rows.typeref,
rows.typedef_,
rows.param,
rows.interfaceimpl,
rows.memberref,
rows.module,
rows.property,
rows.event,
rows.standalonesig,
rows.moduleref,
rows.typespec,
rows.assembly,
rows.assemblyref,
rows.file,
rows.exportedtype,
rows.manifestresource,
rows.genericparam,
rows.genericparamconstraint,
rows.methodspec);
if (row_count > (0xFFFF >> 0x05))
index_size = 4;
else
index_size = 2;
// index_size2 is size of the type column.
row_count = max_rows(2,
rows.methoddef,
rows.memberref);
if (row_count > (0xFFFF >> 0x03))
index_size2 = 4;
else
index_size2 = 2;
row_size = (index_size + index_size2 + index_sizes.blob);
if (typeref_ptr != NULL && memberref_ptr != NULL)
{
row_ptr = table_offset;
for (i = 0; i < num_rows; i++)
{
if (!fits_in_pe(pe, row_ptr, row_size))
break;
// Check the Parent field.
customattribute_table = (PCUSTOMATTRIBUTE_TABLE) row_ptr;
if (index_size == 4)
{
// Low 5 bits tell us what this is an index into. Remaining bits
// tell us the index value.
// Parent must be an index into the Assembly (0x0E) table.
if ((*(DWORD*) customattribute_table & 0x1F) != 0x0E)
{
row_ptr += row_size;
continue;
}
}
else
{
// Low 5 bits tell us what this is an index into. Remaining bits
// tell us the index value.
// Parent must be an index into the Assembly (0x0E) table.
if ((*(WORD*) customattribute_table & 0x1F) != 0x0E)
{
row_ptr += row_size;
continue;
}
}
// Check the Type field.
customattribute_table = (PCUSTOMATTRIBUTE_TABLE) \
(row_ptr + index_size);
if (index_size2 == 4)
{
// Low 3 bits tell us what this is an index into. Remaining bits
// tell us the index value. Only values 2 and 3 are defined.
// Type must be an index into the MemberRef table.
if ((*(DWORD*) customattribute_table & 0x07) != 0x03)
{
row_ptr += row_size;
continue;
}
type_index = *(DWORD*) customattribute_table >> 3;
}
else
{
// Low 3 bits tell us what this is an index into. Remaining bits
// tell us the index value. Only values 2 and 3 are defined.
// Type must be an index into the MemberRef table.
if ((*(WORD*) customattribute_table & 0x07) != 0x03)
{
row_ptr += row_size;
continue;
}
// Cast the index to a 32bit value.
type_index = (DWORD) ((*(WORD*) customattribute_table >> 3));
}
if (type_index > 0)
type_index--;
// Now follow the Type index into the MemberRef table.
memberref_row = memberref_ptr + (memberref_row_size * type_index);
if (index_sizes.memberref == 4)
{
// Low 3 bits tell us what this is an index into. Remaining bits
// tell us the index value. Class must be an index into the
// TypeRef table.
if ((*(DWORD*) memberref_row & 0x07) != 0x01)
{
row_ptr += row_size;
continue;
}
class_index = *(DWORD*) memberref_row >> 3;
}
else
{
// Low 3 bits tell us what this is an index into. Remaining bits
// tell us the index value. Class must be an index into the
// TypeRef table.
if ((*(WORD*) memberref_row & 0x07) != 0x01)
{
row_ptr += row_size;
continue;
}
// Cast the index to a 32bit value.
class_index = (DWORD) (*(WORD*) memberref_row >> 3);
}
if (class_index > 0)
class_index--;
// Now follow the Class index into the TypeRef table.
typeref_row = typeref_ptr + (typeref_row_size * class_index);
// Skip over the ResolutionScope and check the Name field,
// which is an index into the Strings heap.
row_count = max_rows(4,
rows.module,
rows.moduleref,
rows.assemblyref,
rows.typeref);
if (row_count > (0xFFFF >> 0x02))
typeref_row += 4;
else
typeref_row += 2;
if (index_sizes.string == 4)
{
name = pe_get_dotnet_string(
pe, string_offset, *(DWORD*) typeref_row);
}
else
{
name = pe_get_dotnet_string(
pe, string_offset, *(WORD*) typeref_row);
}
if (name != NULL && strncmp(name, "GuidAttribute", 13) != 0)
{
row_ptr += row_size;
continue;
}
// Get the Value field.
customattribute_table = (PCUSTOMATTRIBUTE_TABLE) \
(row_ptr + index_size + index_size2);
if (index_sizes.blob == 4)
blob_index = *(DWORD*) customattribute_table;
else
// Cast the value (index into blob table) to a 32bit value.
blob_index = (DWORD) (*(WORD*) customattribute_table);
// Everything checks out. Make sure the index into the blob field
// is valid (non-null and within range).
blob_offset = \
pe->data + metadata_root + streams->blob->Offset + blob_index;
// If index into blob is 0 or past the end of the blob stream, skip
// it. We don't know the size of the blob entry yet because that is
// encoded in the start.
if (blob_index == 0x00 || blob_offset >= pe->data + pe->data_size)
{
row_ptr += row_size;
continue;
}
blob_result = dotnet_parse_blob_entry(pe, blob_offset);
if (blob_result.size == 0)
{
row_ptr += row_size;
continue;
}
blob_length = blob_result.length;
blob_offset += blob_result.size;
// Quick sanity check to make sure the blob entry is within bounds.
if (blob_offset + blob_length >= pe->data + pe->data_size)
{
row_ptr += row_size;
continue;
}
// Custom attributes MUST have a 16 bit prolog of 0x0001
if (*(WORD*) blob_offset != 0x0001)
{
row_ptr += row_size;
continue;
}
// The next byte is the length of the string.
blob_offset += 2;
if (blob_offset + *blob_offset >= pe->data + pe->data_size)
{
row_ptr += row_size;
continue;
}
blob_offset += 1;
if (*blob_offset == 0xFF || *blob_offset == 0x00)
{
typelib[0] = '\0';
}
else
{
strncpy(typelib, (char*) blob_offset, MAX_TYPELIB_SIZE);
typelib[MAX_TYPELIB_SIZE] = '\0';
}
set_string(typelib, pe->object, "typelib");
row_ptr += row_size;
}
}
table_offset += row_size * num_rows;
break;
case BIT_FIELDMARSHAL:
row_count = max_rows(2,
rows.field,
rows.param);
if (row_count > (0xFFFF >> 0x01))
index_size = 4;
else
index_size = 2;
table_offset += (index_size + index_sizes.blob) * num_rows;
break;
case BIT_DECLSECURITY:
row_count = max_rows(3,
rows.typedef_,
rows.methoddef,
rows.assembly);
if (row_count > (0xFFFF >> 0x02))
index_size = 4;
else
index_size = 2;
table_offset += (2 + index_size + index_sizes.blob) * num_rows;
break;
case BIT_CLASSLAYOUT:
table_offset += (2 + 4 + index_sizes.typedef_) * num_rows;
break;
case BIT_FIELDLAYOUT:
table_offset += (4 + index_sizes.field) * num_rows;
break;
case BIT_STANDALONESIG:
table_offset += (index_sizes.blob) * num_rows;
break;
case BIT_EVENTMAP:
table_offset += (index_sizes.typedef_ + index_sizes.event) * num_rows;
break;
case BIT_EVENTPTR:
// This one is not documented in ECMA-335.
table_offset += (index_sizes.event) * num_rows;
break;
case BIT_EVENT:
row_count = max_rows(3,
rows.typedef_,
rows.typeref,
rows.typespec);
if (row_count > (0xFFFF >> 0x02))
index_size = 4;
else
index_size = 2;
table_offset += (2 + index_sizes.string + index_size) * num_rows;
break;
case BIT_PROPERTYMAP:
table_offset += (index_sizes.typedef_ + index_sizes.property) * num_rows;
break;
case BIT_PROPERTYPTR:
// This one is not documented in ECMA-335.
table_offset += (index_sizes.property) * num_rows;
break;
case BIT_PROPERTY:
table_offset += (2 + index_sizes.string + index_sizes.blob) * num_rows;
break;
case BIT_METHODSEMANTICS:
row_count = max_rows(2,
rows.event,
rows.property);
if (row_count > (0xFFFF >> 0x01))
index_size = 4;
else
index_size = 2;
table_offset += (2 + index_sizes.methoddef + index_size) * num_rows;
break;
case BIT_METHODIMPL:
row_count = max_rows(2,
rows.methoddef,
rows.memberref);
if (row_count > (0xFFFF >> 0x01))
index_size = 4;
else
index_size = 2;
table_offset += (index_sizes.typedef_ + (index_size * 2)) * num_rows;
break;
case BIT_MODULEREF:
row_ptr = table_offset;
// Can't use 'i' here because we only set the string if it is not
// NULL. Instead use 'counter'.
counter = 0;
for (i = 0; i < num_rows; i++)
{
moduleref_table = (PMODULEREF_TABLE) row_ptr;