Bitdefender Hypervisor Memory Introspection
patsig.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Bitdefender
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #include "patsig.h"
6 
7 
8 DWORD
10  _In_ const BYTE *Buffer,
11  _In_ DWORD SigCount,
12  _In_ const PATTERN_SIGNATURE *Sigs
13  )
25 {
26  if (Buffer == NULL || Sigs == NULL)
27  {
28  return SIG_NOT_FOUND;
29  }
30 
31  for (DWORD i = 0; i < SigCount; i++)
32  {
33  BOOLEAN matched = TRUE;
34 
35  for (DWORD j = 0; j < Sigs[i].Length; j++)
36  {
37  if (Sigs[i].Pattern[j] != 0x100 &&
38  Sigs[i].Pattern[j] != Buffer[Sigs[i].Offset + j])
39  {
40  matched = FALSE;
41  break;
42  }
43  }
44 
45  if (matched)
46  {
47  return i;
48  }
49  }
50 
51  return SIG_NOT_FOUND;
52 }
53 
54 
55 DWORD
57  _In_ const BYTE *Buffer,
58  _In_ const DWORD BufferSize,
59  _In_ DWORD SigCount,
60  _In_ const PATTERN_SIGNATURE *Sigs
61  )
73 {
74  if (Buffer == NULL || Sigs == NULL)
75  {
76  return SIG_NOT_FOUND;
77  }
78 
79  for (DWORD i = 0; i < SigCount; i++)
80  {
81  // check at each offset of buffer
82  for (DWORD bufferOffset = 0; bufferOffset + Sigs[i].Length <= BufferSize; bufferOffset++)
83  {
84  DWORD foundSigId = IntPatternMatch(Buffer + bufferOffset, 1, &Sigs[i]);
85  if (0 == foundSigId) // only one element, can be SIG_NOT_FOUND or 0
86  {
87  return i;
88  }
89  }
90  }
91 
92  return SIG_NOT_FOUND;
93 }
DWORD IntPatternMatch(const BYTE *Buffer, DWORD SigCount, const PATTERN_SIGNATURE *Sigs)
Matches one of the given signatures on the given buffer.
Definition: patsig.c:9
_Bool BOOLEAN
Definition: intro_types.h:58
uint8_t BYTE
Definition: intro_types.h:47
#define _In_
Definition: intro_sal.h:21
#define SIG_NOT_FOUND
Signals that a signature was not matched.
Definition: patsig.h:13
#define TRUE
Definition: intro_types.h:30
uint32_t DWORD
Definition: intro_types.h:49
DWORD IntPatternMatchAllOffsets(const BYTE *Buffer, const DWORD BufferSize, DWORD SigCount, const PATTERN_SIGNATURE *Sigs)
Matches one of the given signatures on the given buffer at any offset inside the given buffer...
Definition: patsig.c:56
Describes a signature that can be used for searching or matching guest contents.
Definition: patsig.h:23
#define FALSE
Definition: intro_types.h:34