forked from Cisco-Talos/clamav-bytecode-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytecode_local.h
1406 lines (1290 loc) · 44.6 KB
/
bytecode_local.h
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) 2009-2014 Cisco Systems, Inc.
* Copyright (C) 2009-2013 Sourcefire, Inc.
* All rights reserved.
* Authors: Török Edvin, Kevin Lin
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/** @file */
#define NULL (void *)0x0
#define force_inline inline __attribute__((always_inline))
#define overloadable_func __attribute__((overloadable))
/* DOXYGEN defined() must come first */
#if defined(DOXYGEN) || __has_feature(attribute_overloadable)
/* Yes, clang supports overloading functions in C! */
/**
\group_debug
* @fn debug(const char * str)
* Prints \p str to clamscan's --debug output.
* @overload
* @param[in] str null terminated string
*/
static force_inline void overloadable_func debug(const char *str)
{
debug_print_str((const uint8_t *)str, 0);
}
/**
\group_debug
* @fn debug(const uint8_t* str)
* Prints \p str to clamscan's --debug output.
* @overload
* @param[in] str null terminated string
*/
static force_inline void overloadable_func debug(const uint8_t *str)
{
debug_print_str((const uint8_t *)str, 0);
}
/**
\group_debug
* @fn debug(uint32_t a)
* Prints \p a integer to clamscan's --debug output.
* @overload
* @param[in] a integer
*/
static force_inline void overloadable_func debug(uint32_t a)
{
debug_print_uint(a);
}
/**
\group_debug
* debug is an overloaded function (yes clang supports that in C!), but it only
* works on strings, and integers. Give an error on any other type.
* @sa debug(const char * str),
* @sa debug(const uint8_t* str),
* @sa debug(uint32_t a)
*/
void debug(...) __attribute__((overloadable, unavailable));
#endif
/* Virusname definition handling */
/**
\group_config
* Declares the virusname prefix.
* @param[in] name the prefix common to all viruses reported by this bytecode
*/
#define VIRUSNAME_PREFIX(name) const char __clambc_virusname_prefix[] = name;
/**
\group_config
* Declares all the virusnames that this bytecode can report.
* @param[in] ... a comma-separated list of strings interpreted as virusnames
*/
#define VIRUSNAMES(...) const char *const __clambc_virusnames[] = {__VA_ARGS__};
/* Logical signature handling */
typedef struct signature {
uint64_t id;
} __Signature;
/**
\group_config
* Like \p PE_HOOK_DECLARE, but it is not run for packed files that pe.c can
* unpack (only on the unpacked file).
*/
#define PE_UNPACKER_DECLARE const uint16_t __clambc_kind = BC_PE_UNPACKER;
/**
\group_config
* Make the current bytecode a PDF hook.
* @details Having a logical signature doesn't make sense here, since the logical
* signature is evaluated AFTER these hooks run.
* @details This hook is called several times, use pdf_get_phase() to find out in which
* phase you got called.
*/
#define PDF_HOOK_DECLARE const uint16_t __clambc_kind = BC_PDF;
/**
* entrypoint() return code that tells hook invoker that it should skip
* executing, probably because it'd trigger a bug in it
*/
#define BYTECODE_ABORT_HOOK 0xcea5e
/**
\group_config
* Make the current bytecode a PE hook.
* @details Bytecode will be called once the logical signature trigger matches
* (or always if there is none), and if you have access to all the PE information.
* By default you only have access to execs.h information, and not to PE field
* information (even for PE files).
*/
#define PE_HOOK_DECLARE const uint16_t __clambc_kind = BC_PE_ALL;
/**
\group_config
* Make the current bytecode a PRECLASS hook.
* @details Bytecode will be called once the logical signature trigger matches
* (or always if there is none), and if you have access to all PRECLASS information.
*/
#define PRECLASS_HOOK_DECLARE const uint16_t __clambc_kind = BC_PRECLASS;
/**
\group_config
* Like \p PE_UNPACKER_DECLARE, but for ELF files.
*/
#define ELF_UNPACKER_DECLARE const uint16_t __clambc_kind = BC_ELF_UNPACKER;
/**
\group_config
* Like \p PE_UNPACKER_DECLARE, but for Mach-O files.
*/
#define MACHO_UNPACKER_DECLARE const uint16_t __clambc_kind = BC_MACHO_UNPACKER;
/**
\group_config
* Marks the beginning of the subsignature name declaration section.
*/
#define SIGNATURES_DECL_BEGIN \
struct __Signatures {
/**
\group_config
* Declares a name for a subsignature.
*/
#define DECLARE_SIGNATURE(name) \
const char *name##_sig; \
__Signature name;
/**
\group_config
* Marks the end of the subsignature name declaration section.
*/
#define SIGNATURES_DECL_END \
} \
;
/**
\group_config
* Defines the ClamAV file target.
* @param[in] tgt ClamAV signature type (0 - raw, 1 - PE, etc.)
*/
#define TARGET(tgt) const unsigned short __Target = (tgt);
/**
\group_config
* Defines an alternative copyright for this bytecode.
* @details This will also prevent the sourcecode from being embedded into the bytecode.
*/
#define COPYRIGHT(c) const char *const __Copyright = (c);
/**
\group_config
* Define IconGroup1 for logical signature.
* @details See logical signature documentation for what it is.
*/
#define ICONGROUP1(group) const char *const __IconGroup1 = (group);
/**
\group_config
* Define IconGroup2 for logical signature.
* @details See logical signature documentation for what it is.
*/
#define ICONGROUP2(group) const char *const __IconGroup2 = (group);
/**
\group_config
* Define the minimum engine functionality level required for this
* bytecode/logical signature.
* @details Engines older than this will skip loading the bytecode.
* You can use the #FunctionalityLevels enumeration here.
*/
#define FUNCTIONALITY_LEVEL_MIN(m) const unsigned short __FuncMin = (m);
/**
\group_config
* Define the maximum engine functionality level required for this
* bytecode/logical signature.
* @details Engines newer than this will skip loading the bytecode.
* You can use the #FunctionalityLevels enumeration here.
*/
#define FUNCTIONALITY_LEVEL_MAX(m) const unsigned short __FuncMax = (m);
#define LDB_ADDATTRIBUTES(x) const char *__ldb_rawattrs = (x);
#define CONTAINER(x) const char *__ldb_container = (x);
/**
\group_config
* Marks the beginning of subsignature pattern definitions.
* @sa SIGNATURES_DECL_BEGIN
*/
/* some other macro may use __COUNTER__, so we need to subtract its current\
* value to obtain zero-based indices */
#define SIGNATURES_DEF_BEGIN \
static const unsigned __signature_bias = __COUNTER__ + 1; \
const struct __Signatures Signatures = {/** \
\group_config \
* Defines the pattern for a previously declared subsignature. \
* @sa DECLARE_SIGNATURE \
* @param name the name of a previously declared subsignature \
* @param hex the pattern for this subsignature \
*/
#define DEFINE_SIGNATURE(name, hex) \
.name##_sig = (hex), \
.name = {__COUNTER__ - __signature_bias},
/**
* Old macro used to mark the end of the subsignature pattern definitions.
*/
#define SIGNATURES_END \
} \
;
/**
\group_config
* Marks the end of the subsignature pattern definitions.\n
* Alternative: SIGNATURES_END
*/
#define SIGNATURES_DEF_END \
} \
;
/**
\group_engine
* Returns how many times the specified signature matched.
* @param[in] sig name of subsignature queried
* @return number of times this subsignature matched in the entire file
* @details This is a constant-time operation, the counts for all subsignatures are
* already computed.
*/
static force_inline uint32_t count_match(__Signature sig)
{
return __clambc_match_counts[sig.id];
}
/**
\group_engine
* Returns whether the specified subsignature has matched at least once.
* @param[in] sig name of subsignature queried
* @return 1 if subsignature one or more times, 0 otherwise
*/
static force_inline uint32_t matches(__Signature sig)
{
return __clambc_match_counts[sig.id] != 0;
}
/**
\group_engine
* Returns the offset of the match.
* @param[in] sig - Signature
* @param[in] goback - max length of signature
* @return offset of match
*/
static force_inline uint32_t match_location(__Signature sig, uint32_t goback)
{
int32_t pos = __clambc_match_offsets[sig.id];
if (engine_functionality_level() <= FUNC_LEVEL_096_1) {
/* bug, it returns offset of last subsig, not offset of first */
pos -= goback;
if (pos <= 0) pos = 0;
}
return pos;
}
/**
\group_engine
* Like match_location(), but also checks that the match starts with
* the specified hex string.
* @details It is recommended to use this for safety and compatibility with 0.96.1
* @param[in] sig - signature
* @param[in] goback - maximum length of signature (till start of last subsig)
* @param[in] static_start - static string that sig must begin with
* @param[in] static_len - static string that sig must begin with - length
* @return >=0 - offset of match
* @return -1 - no match
*/
static force_inline int32_t match_location_check(__Signature sig,
uint32_t goback,
const char *static_start,
uint32_t static_len)
{
int32_t pos = match_location(sig, goback);
if (seek(pos, SEEK_SET) != pos)
return -1;
int32_t cpos = file_find_limit((const uint8_t *)static_start, static_len, pos + goback);
if (cpos == -1) {
debug("Engine reported match, but we couldn't find it! Engine reported (after fixup):");
debug(pos);
return -1;
}
if (seek(cpos, SEEK_SET) != cpos)
return -1;
if (cpos != pos && engine_functionality_level() >= FUNC_LEVEL_096_1_dev) {
debug("wrong match pos reported by engine, real match pos:");
debug(cpos);
debug("reported by engine:");
debug(pos);
debug("but goback fixed it up!");
}
return cpos;
}
/**
\group_scan
* Sets the specified virusname as the virus detected by this bytecode.
* @param[in] virusname the name of the virus, excluding the prefix, must be one of
* the virusnames declared in \p VIRUSNAMES.
* \sa VIRUSNAMES
*/
static force_inline overloadable_func void foundVirus(const char *virusname)
{
setvirusname((const uint8_t *)virusname, 0);
}
#if defined(DOXYGEN) || __has_feature(attribute_overloadable)
/** Like foundVirus() but just use the prefix as virusname */
static force_inline void overloadable_func foundVirus(void)
{
foundVirus("");
}
#endif
/**
\group_file
* Returns the currently scanned file's size.
* @return file size as 32-bit unsigned integer
*/
static force_inline uint32_t getFilesize(void)
{
return __clambc_filesize[0];
}
union unaligned_32 {
uint32_t una_u32;
int32_t una_s32;
} __attribute__((packed));
union unaligned_16 {
uint16_t una_u16;
int16_t una_s16;
} __attribute__((packed));
/**
\group_env
* Returns true if the bytecode is executing on a big-endian CPU.
* @return true if executing on bigendian CPU, false otherwise
*
* @details This will be optimized away in libclamav, but it must be used when dealing
* with endianess for portability reasons.\n
* For example whenever you read a 32-bit integer from a file, it can be written
* in little-endian convention (x86 CPU for example), or big-endian convention
* (PowerPC CPU for example).\n
* If the file always contains little-endian integers, then conversion might be
* needed.\n
* ClamAV bytecodes by their nature must only handle known-endian integers, if
* endianness can change, then both situations must be taken into account (based
* on a 1-byte field for example).
*/
bool __is_bigendian(void) __attribute__((const)) __attribute__((nothrow));
/**
\group_env
* Converts the specified value if needed, knowing it is in little endian
* order.
* @param[in] v 32-bit integer as read from a file
* @return integer converted to host's endianess
*/
static uint32_t force_inline le32_to_host(uint32_t v)
{
/* calculate bswap always, so compiler can use a select,
and doesn't need to create a branch.
This will get optimized away at bytecode load time anyway */
uint32_t swapped = __builtin_bswap32(v);
return __is_bigendian() ? swapped : v;
}
/**
\group_env
* Converts the specified value if needed, knowing it is in big endian
* order.
* @param[in] v 32-bit integer as read from a file
* @return integer converted to host's endianess
*/
static uint32_t force_inline be32_to_host(uint32_t v)
{
/* calculate bswap always, so compiler can use a select,
and doesn't need to create a branch.
This will get optimized away at bytecode load time anyway */
uint32_t swapped = __builtin_bswap32(v);
return __is_bigendian() ? v : swapped;
}
/**
\group_env
* Converts the specified value if needed, knowing it is in little endian
* order.
* @param[in] v 64-bit integer as read from a file
* @return integer converted to host's endianess
*/
static uint64_t force_inline le64_to_host(uint64_t v)
{
uint64_t swapped = __builtin_bswap64(v);
return __is_bigendian() ? swapped : v;
}
/**
\group_env
* Converts the specified value if needed, knowing it is in big endian
* order.
* @param[in] v 64-bit integer as read from a file
* @return integer converted to host's endianess
*/
static uint64_t force_inline be64_to_host(uint64_t v)
{
uint64_t swapped = __builtin_bswap64(v);
return __is_bigendian() ? v : swapped;
}
/**
\group_env
* Converts the specified value if needed, knowing it is in little endian
* order.
* @param[in] v 16-bit integer as read from a file
* @return integer converted to host's endianess
*/
static uint16_t force_inline le16_to_host(uint16_t v)
{
uint16_t swapped = ((v & 0xff) << 8) | ((v >> 8) & 0xff);
return __is_bigendian() ? swapped : v;
}
/**
\group_env
* Converts the specified value if needed, knowing it is in big endian
* order.
* @param[in] v 16-bit integer as read from a file
* @return integer converted to host's endianess
*/
static uint16_t force_inline be16_to_host(uint16_t v)
{
uint16_t swapped = ((v & 0xff) << 8) | ((v >> 8) & 0xff);
return __is_bigendian() ? v : swapped;
}
/**
\group_env
* Reads from the specified buffer a 32-bit of little-endian integer.
* @param[in] buff pointer to buffer
* @return 32-bit little-endian integer converted to host endianness
*/
static uint32_t force_inline cli_readint32(const void *buff)
{
uint32_t v = ((const union unaligned_32 *)buff)->una_s32;
return le32_to_host(v);
}
/**
\group_env
* Reads from the specified buffer a 16-bit of little-endian integer.
* @param[in] buff pointer to buffer
* @return 16-bit little-endian integer converted to host endianness
*/
static uint16_t force_inline cli_readint16(const void *buff)
{
uint16_t v = ((const union unaligned_16 *)buff)->una_s16;
return le16_to_host(v);
}
/**
\group_env
* Writes the specified value into the specified buffer in little-endian order
* @param[out] offset pointer to buffer to write to
* @param[in] v value to write
*/
static void force_inline cli_writeint32(void *offset, uint32_t v)
{
((union unaligned_32 *)offset)->una_u32 = le32_to_host(v);
}
/* --------------------- PE helper functions ------------------------ */
/**
\group_pe
* Returns whether the current file has executable information.
* @return true if the file has exe info, false otherwise
*/
static force_inline bool hasExeInfo(void)
{
return __clambc_pedata.offset != -1;
}
/**
\group_pe
* Returns whether PE information is available
* @return true if PE information is available (in PE hooks)
*/
static force_inline bool hasPEInfo(void)
{
return (__clambc_kind == BC_PE_ALL ||
__clambc_kind == BC_PE_UNPACKER);
}
#define NEED_PE_INFO \
{ /* only available in PE hooks */ \
if (!hasPEInfo()) \
__fail_missing_PE_HOOK_DECLARE__or__PE_UNPACKER_DECLARE(); \
}
/**
\group_pe
* Returns whether this is a PE32+ executable.
* @return true if this is a PE32+ executable
*/
static force_inline bool isPE64(void)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return le16_to_host(__clambc_pedata.opt64.Magic) == 0x020b;
}
/**
\group_pe
* Returns MajorLinkerVersion for this PE file.
* @return PE MajorLinkerVersion or 0 if not in PE hook
*/
static force_inline uint8_t getPEMajorLinkerVersion(void)
{
return isPE64() ? __clambc_pedata.opt64.MajorLinkerVersion : __clambc_pedata.opt32.MajorLinkerVersion;
}
/**
\group_pe
* Returns MinorLinkerVersion for this PE file.
* @return PE MinorLinkerVersion or 0 if not in PE hook
*/
static force_inline uint8_t getPEMinorLinkerVersion(void)
{
return isPE64() ? __clambc_pedata.opt64.MinorLinkerVersion : __clambc_pedata.opt32.MinorLinkerVersion;
}
/**
\group_pe
* Return the PE SizeOfCode.
* @return PE SizeOfCode or 0 if not in PE hook
*/
static force_inline uint32_t getPESizeOfCode(void)
{
return le32_to_host(isPE64() ? __clambc_pedata.opt64.SizeOfCode : __clambc_pedata.opt32.SizeOfCode);
}
/**
\group_pe
* Return the PE SizeofInitializedData.
* @return PE SizeOfInitializeData or 0 if not in PE hook
*/
static force_inline uint32_t getPESizeOfInitializedData(void)
{
return le32_to_host(isPE64() ? __clambc_pedata.opt64.SizeOfInitializedData : __clambc_pedata.opt32.SizeOfInitializedData);
}
/**
\group_pe
* Return the PE SizeofUninitializedData.
* @return PE SizeofUninitializedData or 0 if not in PE hook
*/
static force_inline uint32_t getPESizeOfUninitializedData(void)
{
return le32_to_host(isPE64() ? __clambc_pedata.opt64.SizeOfUninitializedData : __clambc_pedata.opt32.SizeOfUninitializedData);
}
/**
\group_pe
* Return the PE BaseOfCode.
* @return PE BaseOfCode, or 0 if not in PE hook
*/
static force_inline uint32_t getPEBaseOfCode(void)
{
return le32_to_host(isPE64() ? __clambc_pedata.opt64.BaseOfCode : __clambc_pedata.opt32.BaseOfCode);
}
/**
\group_pe
* Return the PE BaseOfData.
* @return PE BaseOfData, or 0 if not in PE hook
*/
static force_inline uint32_t getPEBaseOfData(void)
{
return le32_to_host(isPE64() ? 0 : __clambc_pedata.opt32.BaseOfData);
}
/**
\group_pe
* Return the PE ImageBase as 64-bit integer.
* @return PE ImageBase as 64-bit int, or 0 if not in PE hook
*/
static force_inline uint64_t getPEImageBase(void)
{
return le64_to_host(isPE64() ? __clambc_pedata.opt64.ImageBase : __clambc_pedata.opt32.ImageBase);
}
/**
\group_pe
* Return the PE SectionAlignment.
* @return PE SectionAlignment, or 0 if not in PE hook
*/
static force_inline uint32_t getPESectionAlignment(void)
{
return le32_to_host(isPE64() ? __clambc_pedata.opt64.SectionAlignment : __clambc_pedata.opt32.SectionAlignment);
}
/**
\group_pe
* Return the PE FileAlignment.
* @return PE FileAlignment, or 0 if not in PE hook
*/
static force_inline uint32_t getPEFileAlignment(void)
{
return le32_to_host(isPE64() ? __clambc_pedata.opt64.FileAlignment : __clambc_pedata.opt32.FileAlignment);
}
/**
\group_pe
* Return the PE MajorOperatingSystemVersion.
* @return PE MajorOperatingSystemVersion, or 0 if not in PE hook
*/
static force_inline uint16_t getPEMajorOperatingSystemVersion(void)
{
return le16_to_host(isPE64() ? __clambc_pedata.opt64.MajorOperatingSystemVersion : __clambc_pedata.opt32.MajorOperatingSystemVersion);
}
/**
\group_pe
* Return the PE MinorOperatingSystemVersion.
* @return PE MinorOperatingSystemVersion, or 0 if not in PE hook
*/
static force_inline uint16_t getPEMinorOperatingSystemVersion(void)
{
return le16_to_host(isPE64() ? __clambc_pedata.opt64.MinorOperatingSystemVersion : __clambc_pedata.opt32.MinorOperatingSystemVersion);
}
/**
\group_pe
* Return the PE MajorImageVersion.
* @return PE MajorImageVersion, or 0 if not in PE hook
*/
static force_inline uint16_t getPEMajorImageVersion(void)
{
return le16_to_host(isPE64() ? __clambc_pedata.opt64.MajorImageVersion : __clambc_pedata.opt32.MajorImageVersion);
}
/**
\group_pe
* Return the PE MinorImageVersion.
* @return PE MinorrImageVersion, or 0 if not in PE hook */
static force_inline uint16_t getPEMinorImageVersion(void)
{
return le16_to_host(isPE64() ? __clambc_pedata.opt64.MinorImageVersion : __clambc_pedata.opt32.MinorImageVersion);
}
/**
\group_pe
* Return the PE MajorSubsystemVersion.
* @return PE MajorSubsystemVersion or 0 if not in PE hook
*/
static force_inline uint16_t getPEMajorSubsystemVersion(void)
{
return le16_to_host(isPE64() ? __clambc_pedata.opt64.MajorSubsystemVersion : __clambc_pedata.opt32.MajorSubsystemVersion);
}
/**
\group_pe
* Return the PE MinorSubsystemVersion.
* @return PE MinorSubsystemVersion, or 0 if not in PE hook
*/
static force_inline uint16_t getPEMinorSubsystemVersion(void)
{
return le16_to_host(isPE64() ? __clambc_pedata.opt64.MinorSubsystemVersion : __clambc_pedata.opt32.MinorSubsystemVersion);
}
/**
\group_pe
* Return the PE Win32VersionValue.
* @return PE Win32VersionValue, or 0 if not in PE hook
*/
static force_inline uint32_t getPEWin32VersionValue(void)
{
return le32_to_host(isPE64() ? __clambc_pedata.opt64.Win32VersionValue : __clambc_pedata.opt32.Win32VersionValue);
}
/**
\group_pe
* Return the PE SizeOfImage.
* @return PE SizeOfImage, or 0 if not in PE hook
*/
static force_inline uint32_t getPESizeOfImage(void)
{
return le32_to_host(isPE64() ? __clambc_pedata.opt64.SizeOfImage : __clambc_pedata.opt32.SizeOfImage);
}
/**
\group_pe
* Return the PE SizeOfHeaders.
* @return PE SizeOfHeaders, or 0 if not in PE hook
*/
static force_inline uint32_t getPESizeOfHeaders(void)
{
return le32_to_host(isPE64() ? __clambc_pedata.opt64.SizeOfHeaders : __clambc_pedata.opt32.SizeOfHeaders);
}
/**
\group_pe
* Return the PE CheckSum.
* @return PE CheckSum, or 0 if not in PE hook
*/
static force_inline uint32_t getPECheckSum(void)
{
return le32_to_host(isPE64() ? __clambc_pedata.opt64.CheckSum : __clambc_pedata.opt32.CheckSum);
}
/**
\group_pe
* Return the PE Subsystem.
* @return PE subsystem, or 0 if not in PE hook
*/
static force_inline uint16_t getPESubsystem(void)
{
return le16_to_host(isPE64() ? __clambc_pedata.opt64.Subsystem : __clambc_pedata.opt32.Subsystem);
}
/**
\group_pe
* Return the PE DllCharacteristics.
* @return PE DllCharacteristics, or 0 if not in PE hook
*/
static force_inline uint16_t getPEDllCharacteristics(void)
{
return le16_to_host(isPE64() ? __clambc_pedata.opt64.DllCharacteristics : __clambc_pedata.opt32.DllCharacteristics);
}
/**
\group_pe
* Return the PE SizeOfStackReserve.
* @return PE SizeOfStackReserver, or 0 if not in PE hook
*/
static force_inline uint32_t getPESizeOfStackReserve(void)
{
return le32_to_host(isPE64() ? __clambc_pedata.opt64.SizeOfStackReserve : __clambc_pedata.opt32.SizeOfStackReserve);
}
/**
\group_pe
* Return the PE SizeOfStackCommit.
* @return PE SizeOfStackCommit, or 0 if not in PE hook
*/
static force_inline uint32_t getPESizeOfStackCommit(void)
{
return le32_to_host(isPE64() ? __clambc_pedata.opt64.SizeOfStackCommit : __clambc_pedata.opt32.SizeOfStackCommit);
}
/**
\group_pe
* Return the PE SizeOfHeapReserve.
* @return PE SizeOfHeapReserve, or 0 if not in PE hook
*/
static force_inline uint32_t getPESizeOfHeapReserve(void)
{
return le32_to_host(isPE64() ? __clambc_pedata.opt64.SizeOfHeapReserve : __clambc_pedata.opt32.SizeOfHeapReserve);
}
/**
\group_pe
* Return the PE SizeOfHeapCommit.
* @return PE SizeOfHeapCommit, or 0 if not in PE hook
*/
static force_inline uint32_t getPESizeOfHeapCommit(void)
{
return le32_to_host(isPE64() ? __clambc_pedata.opt64.SizeOfHeapCommit : __clambc_pedata.opt32.SizeOfHeapCommit);
}
/**
\group_pe
* Return the PE LoaderFlags.
* @return PE LoaderFlags or 0 if not in PE hook
*/
static force_inline uint32_t getPELoaderFlags(void)
{
return le32_to_host(isPE64() ? __clambc_pedata.opt64.LoaderFlags : __clambc_pedata.opt32.LoaderFlags);
}
/**
\group_pe
* Returns the CPU this executable runs on, see libclamav/pe.c for possible
* values.
* @return PE Machine or 0 if not in PE hook
*/
static force_inline uint16_t getPEMachine()
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return le16_to_host(__clambc_pedata.file_hdr.Machine);
}
/**
\group_pe
* Returns the PE TimeDateStamp from headers
* @return PE TimeDateStamp or 0 if not in PE hook
*/
static force_inline uint32_t getPETimeDateStamp()
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return le32_to_host(__clambc_pedata.file_hdr.TimeDateStamp);
}
/**
\group_pe
* Returns pointer to the PE debug symbol table
* @return PE PointerToSymbolTable or 0 if not in PE hook
*/
static force_inline uint32_t getPEPointerToSymbolTable()
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return le32_to_host(__clambc_pedata.file_hdr.PointerToSymbolTable);
}
/**
\group_pe
* Returns the PE number of debug symbols
* @return PE NumberOfSymbols or 0 if not in PE hook
*/
static force_inline uint32_t getPENumberOfSymbols()
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return le32_to_host(__clambc_pedata.file_hdr.NumberOfSymbols);
}
/**
\group_pe
* Returns the size of PE optional header.
* @return size of PE optional header, or 0 if not in PE hook
*/
static force_inline uint16_t getPESizeOfOptionalHeader()
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return le16_to_host(__clambc_pedata.file_hdr.SizeOfOptionalHeader);
}
/**
\group_pe
* Returns PE characteristics.
* @details For example you can use this to check whether it is a DLL (0x2000).
* @return characteristic of PE file, or 0 if not in PE hook*/
static force_inline uint16_t getPECharacteristics()
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return le16_to_host(__clambc_pedata.file_hdr.Characteristics);
}
/**
\group_pe
* Returns whether this is a DLL.
* Use this only in a PE hook!
* @return true - the file is a DLL
* @return false - file is not a DLL
*/
static force_inline bool getPEisDLL()
{
return getPECharacteristics() & 0x2000;
}
/**
\group_pe
* Gets the virtual address of specified image data directory.
* @param[in] n image directory requested
* @return Virtual Address of requested image directory
*/
static force_inline uint32_t getPEDataDirRVA(unsigned n)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
const struct pe_image_data_dir *p = &__clambc_pedata.opt64_dirs[n];
const struct pe_image_data_dir *p32 = &__clambc_pedata.opt32_dirs[n];
return n < 16 ? le32_to_host(isPE64() ? p->VirtualAddress : p32->VirtualAddress)
: 0;
}
/**
\group_pe
* Gets the size of the specified image data directory.
* @param[in] n image directory requested
* @return Size of requested image directory
*/
static force_inline uint32_t getPEDataDirSize(unsigned n)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return n < 16 ? le32_to_host(isPE64() ? __clambc_pedata.opt64_dirs[n].Size : __clambc_pedata.opt32_dirs[n].Size)
: 0;
}
/**
\group_pe
* Returns the number of sections in this executable file.
* @return number of sections as 16-bit unsigned integer
*/
static force_inline uint16_t getNumberOfSections(void)
{
/* available in non-PE hooks too */
return __clambc_pedata.nsections;
}
/**
\group_pe
* Gets the offset to the PE header.
* @return offset to the PE header, or 0 if not in PE hook
*/
static uint32_t getPELFANew(void)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return le32_to_host(__clambc_pedata.e_lfanew);
}
/**
\group_pe
* Read name of requested PE section.
* @param[out] name name of PE section
* @param[in] n PE section requested
* @return 0 if successful,
* @return <0 otherwise
*/
static force_inline int readPESectionName(unsigned char name[8], unsigned n)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop