Bitdefender Hypervisor Memory Introspection
wincmdline.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Bitdefender
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #include "wincmdline.h"
6 #include "winprocesshp.h"
7 #include "alerts.h"
8 
21 
22 
26  )
37 {
38  INTSTATUS status;
39  WIN_PROCESS_OBJECT *pParent;
40  ENG_NOTIFICATION_CMD_LINE *pNotification;
41 
43  if (NULL == pNotification)
44  {
46  }
47 
49  pNotification->Header.OsType = introGuestWindows;
50 
51  // It could be NULL (in case the parent process no longer exists).
52  pParent = IntWinProcFindObjectByEprocess(Process->ParentEprocess);
53 
54  IntAlertFillWinProcess(Process, &pNotification->Child);
55  IntAlertFillWinProcess(pParent, &pNotification->Parent);
56 
57  pNotification->CmdLineSize = Process->CommandLineSize;
58  pNotification->CmdLine = HpAllocWithTag(pNotification->CmdLineSize, IC_TAG_CMD_LINE);
59  if (NULL == pNotification->CmdLine)
60  {
61  HpFreeAndNullWithTag(&pNotification, IC_TAG_ENGINE_NOT);
63  }
64 
65  memcpy(pNotification->CmdLine, Process->CommandLine, Process->CommandLineSize);
66 
67  LOG("[CMDLINE] Asking the engines to scan process:%s with PID:%u command line:%s - with size:%u\n",
68  pNotification->Child.ImageName, pNotification->Child.Pid,
69  pNotification->CmdLine, pNotification->CmdLineSize
70  );
71 
72  status = IntNotifyEngines(pNotification);
73  if (!INT_SUCCESS(status))
74  {
75  ERROR("[ERROR] IntNotifyEngines failed: 0x%x\n", status);
76  }
77 
78  // Something went wrong. We are going to free the memory.
79  if (!INT_SUCCESS(status))
80  {
81  if (pNotification)
82  {
83  if (pNotification->CmdLine)
84  {
86  }
87 
88  HpFreeAndNullWithTag(&pNotification, IC_TAG_ENGINE_NOT);
89  }
90  }
91 
92  return status;
93 }
94 
95 
96 static INTSTATUS
98  _In_ PENG_NOTIFICATION_CMD_LINE EngineNotification
99  )
107 {
108  INTSTATUS status;
110  INTRO_ACTION_REASON reason;
111 
112  pEvent = &gAlert.EngineDetection;
113  memzero(pEvent, sizeof(*pEvent));
114 
116 
117  pEvent->Header.Action = EngineNotification->Header.RequestedAction;
118  pEvent->Header.Reason = reason;
119 
120  pEvent->Header.Flags = IntAlertProcGetFlags(0, NULL, reason, ALERT_FLAG_FROM_ENGINES);
121  pEvent->Header.Flags |= ALERT_FLAG_NOT_RING0;
122  pEvent->Header.MitreID = idScripting;
123 
124  if (0 == strcasecmp("powershell.exe", EngineNotification->Child.ImageName))
125  {
126  pEvent->Header.MitreID = idPowerShell;
127  }
128 
130 
132  memcpy(&pEvent->Header.CurrentProcess, &EngineNotification->Parent, sizeof(INTRO_PROCESS));
133  memcpy(&pEvent->CmdLineViolation.Originator, &EngineNotification->Parent, sizeof(INTRO_PROCESS));
134  memcpy(&pEvent->CmdLineViolation.Victim, &EngineNotification->Child, sizeof(INTRO_PROCESS));
135 
136  memcpy(&pEvent->DetectionName[0], &EngineNotification->Header.DetectionName[0], ALERT_MAX_DETECTION_NAME - 1);
137  memcpy(&pEvent->EnginesVersion[0], &EngineNotification->Header.EnginesVersion[0], ALERT_MAX_ENGINES_VERSION - 1);
138  pEvent->DetectionName[ALERT_MAX_DETECTION_NAME - 1] = 0;
139 
140  status = IntNotifyIntroEvent(introEventEnginesDetectionViolation, pEvent, sizeof(*pEvent));
141  if (!INT_SUCCESS(status))
142  {
143  WARNING("[WARNING] IntNotifyIntroEvent failed: 0x%08x\n", status);
144  }
145 
146  return INT_STATUS_SUCCESS;
147 }
148 
149 
150 INTSTATUS
152  _In_ PENG_NOTIFICATION_CMD_LINE EngineNotification
153  )
161 {
162  INTSTATUS status;
163 
164  if (introGuestNotAllowed != EngineNotification->Header.RequestedAction)
165  {
166  LOG("[CMDLINE] process:%s with PID:%u used a clean command line\n",
167  EngineNotification->Child.ImageName, EngineNotification->Child.Pid);
168  goto free_memory;
169  }
170 
171  LOG("[CMDLINE] [command line violation] process:%s with PID:%u, CR3:0x%llx and command line:%s created "
172  "process:%s with PID:%u, CR3:0x%llx using the malicious command line:%s\n",
173  EngineNotification->Parent.ImageName,
174  EngineNotification->Parent.Pid,
175  EngineNotification->Parent.Cr3,
176  EngineNotification->Parent.CmdLine,
177  EngineNotification->Child.ImageName,
178  EngineNotification->Child.Pid,
179  EngineNotification->Child.Cr3,
180  EngineNotification->Child.CmdLine
181  );
182  LOG("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ MALWARE ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n");
183 
184  status = IntWinSendCmdLineViolation(EngineNotification);
185  if (!INT_SUCCESS(status))
186  {
187  ERROR("[ERROR] IntWinPsSendCmdLineViolation failed with status: 0x%08x\n", status);
188  }
189 
190 free_memory:
191  if (NULL != EngineNotification->CmdLine)
192  {
193  HpFreeAndNullWithTag(&EngineNotification->CmdLine, IC_TAG_CMD_LINE);
194  }
195 
196  HpFreeAndNullWithTag(&EngineNotification, IC_TAG_ENGINE_NOT);
197 
198  return INT_STATUS_SUCCESS;
199 }
BYTE * CmdLine
The command line to be scanned.
Definition: intro_types.h:2085
enum _INTRO_ACTION_REASON INTRO_ACTION_REASON
The reason for which an INTRO_ACTION was taken.
INTRO_PROCESS Parent
The parent process that provided the command line.
Definition: intro_types.h:2083
#define _In_
Definition: intro_sal.h:21
MITRE_ID MitreID
The Mitre ID that corresponds to this attack.
Definition: intro_types.h:1199
#define INT_STATUS_SUCCESS
Definition: introstatus.h:54
INTRO_ENG_NOTIF_TYPE Type
The type of the alert.
Definition: intro_types.h:1868
#define IC_TAG_CMD_LINE
Windows command line.
Definition: memtags.h:131
void IntAlertFillWinProcess(const WIN_PROCESS_OBJECT *Process, INTRO_PROCESS *EventProcess)
Saves information about a windows process inside an alert.
Definition: alerts.c:689
#define INT_SUCCESS(Status)
Definition: introstatus.h:42
QWORD Flags
A combination of ALERT_FLAG_* values describing the alert.
Definition: intro_types.h:1198
Sent for third party engines detections. See EVENT_ENGINES_DETECTION_VIOLATION.
Definition: intro_types.h:119
INTSTATUS IntWinHandleCmdLineCallback(PENG_NOTIFICATION_CMD_LINE EngineNotification)
Handle a command line scan response.
Definition: wincmdline.c:151
#define ERROR(fmt,...)
Definition: glue.h:62
#define HpAllocWithTag(Len, Tag)
Definition: glue.h:516
int INTSTATUS
The status data type.
Definition: introstatus.h:24
INTRO_VIOLATION_HEADER Header
The alert header.
Definition: intro_types.h:1867
INTRO_ENG_NOTIF_TYPE Type
The type of the notification.
Definition: intro_types.h:2059
PowerShell.
Definition: intro_types.h:1149
ENG_NOTIFICATION_HEADER Header
Notification header.
Definition: intro_types.h:2082
#define ALERT_MAX_DETECTION_NAME
The maximum size of a detection name as given by a third party scan engine.
Definition: intro_types.h:709
#define LOG(fmt,...)
Definition: glue.h:61
void IntAlertFillVersionInfo(INTRO_VIOLATION_HEADER *Header)
Fills version information for an alert.
Definition: alerts.c:327
QWORD IntAlertProcGetFlags(QWORD ProtectionFlag, const void *Process, INTRO_ACTION_REASON Reason, QWORD AdditionalFlags)
Returns the flags for an alert.
Definition: alerts.c:425
INTRO_ACTION_REASON Reason
The reason for which Action was taken.
Definition: intro_types.h:1195
Command line notification for scan engines.
Definition: intro_types.h:2080
GENERIC_ALERT gAlert
Global alert buffer.
Definition: alerts.c:27
DWORD CmdLineSize
The size of the command line buffer.
Definition: intro_types.h:2086
Event structure for detections provided by additional scan engines.
Definition: intro_types.h:1865
INTSTATUS IntNotifyIntroEvent(INTRO_EVENT_TYPE EventClass, void *Param, size_t EventSize)
Notifies the integrator about an introspection alert.
Definition: glue.c:1042
#define memzero(a, s)
Definition: introcrt.h:35
#define HpFreeAndNullWithTag(Add, Tag)
Definition: glue.h:517
INTSTATUS IntNotifyEngines(void *Parameters)
Definition: glue.c:1004
#define ALERT_FLAG_FROM_ENGINES
If set, the alert was generated due to a third party scan engines detection.
Definition: intro_types.h:678
#define WARNING(fmt,...)
Definition: glue.h:60
Command line scan results.
Definition: intro_types.h:135
struct _EVENT_ENGINES_DETECTION_VIOLATION::@320::@322 CmdLineViolation
Command line of the process.
#define ALERT_FLAG_NOT_RING0
If set, the alert was triggered in ring 1, 2 or 3.
Definition: intro_types.h:674
INTRO_GUEST_TYPE OsType
The guest operating system type.
Definition: intro_types.h:2061
static INTSTATUS IntWinSendCmdLineViolation(PENG_NOTIFICATION_CMD_LINE EngineNotification)
Send a command line violation event.
Definition: wincmdline.c:97
The action was allowed, but it has the BETA flag (Introcore is in log-only mode). ...
Definition: intro_types.h:185
PWIN_PROCESS_OBJECT IntWinProcFindObjectByEprocess(QWORD Eprocess)
Finds a process by the address of its _EPROCESS structure.
Definition: winprocesshp.c:96
CHAR EnginesVersion[ALERT_MAX_ENGINES_VERSION]
A NULL-terminated string with the engines versions.
Definition: intro_types.h:1873
CHAR DetectionName[ALERT_MAX_DETECTION_NAME]
A NULL-terminated string with the detection name, as provided by the engines.
Definition: intro_types.h:1871
#define IC_TAG_ENGINE_NOT
Used for asynchronous engine notifications.
Definition: memtags.h:132
INTRO_ACTION Action
The action that was taken as the result of this alert.
Definition: intro_types.h:1194
INTRO_PROCESS Child
The child process that received the command line.
Definition: intro_types.h:2084
CHAR ImageName[ALERT_IMAGE_NAME_LEN]
Image base name of the current process..
Definition: intro_types.h:909
INTRO_PROCESS CurrentProcess
The current process.
Definition: intro_types.h:1197
Scripting.
Definition: intro_types.h:1147
#define ALERT_MAX_ENGINES_VERSION
The maximum size of the third party scan engines version.
Definition: intro_types.h:710
DWORD Pid
The PID of the process.
Definition: intro_types.h:905
Describes a guest process.
Definition: intro_types.h:901
EVENT_ENGINES_DETECTION_VIOLATION EngineDetection
Definition: alerts.h:33
Exposes the functions used to schedule an asynchronous command line scan and receives its result...
INTSTATUS IntWinInspectCommandLine(PWIN_PROCESS_OBJECT Process)
Send a command line scan request to the scan engines.
Definition: wincmdline.c:24
This structure describes a running process inside the guest.
Definition: winprocess.h:83
#define INT_STATUS_INSUFFICIENT_RESOURCES
Definition: introstatus.h:281