Bitdefender Hypervisor Memory Introspection
lixcmdline.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Bitdefender
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #include "lixcmdline.h"
6 #include "alerts.h"
7 
20 
21 
22 static INTSTATUS
24  _In_ ENG_NOTIFICATION_CMD_LINE *EngineNotification
25  )
33 {
37  LIX_TASK_OBJECT *pTask = IntLixTaskFindByCr3(EngineNotification->Child.Cr3);
38 
39  memzero(pEvent, sizeof(*pEvent));
40 
41  pEvent->Header.Action = EngineNotification->Header.RequestedAction;
42  pEvent->Header.Reason = reason;
43 
46  pEvent->Header.MitreID = idScripting;
47 
49 
51  memcpy(&pEvent->Header.CurrentProcess, &EngineNotification->Parent, sizeof(INTRO_PROCESS));
52  memcpy(&pEvent->CmdLineViolation.Originator, &EngineNotification->Parent, sizeof(INTRO_PROCESS));
53  memcpy(&pEvent->CmdLineViolation.Victim, &EngineNotification->Child, sizeof(INTRO_PROCESS));
54 
55  memcpy(&pEvent->DetectionName[0], &EngineNotification->Header.DetectionName[0], ALERT_MAX_DETECTION_NAME - 1);
56  memcpy(&pEvent->EnginesVersion[0], &EngineNotification->Header.EnginesVersion[0], ALERT_MAX_ENGINES_VERSION - 1);
57  pEvent->DetectionName[ALERT_MAX_DETECTION_NAME - 1] = 0;
58 
59  status = IntNotifyIntroEvent(introEventEnginesDetectionViolation, pEvent, sizeof(*pEvent));
60  if (!INT_SUCCESS(status))
61  {
62  WARNING("[WARNING] IntNotifyIntroEvent failed: 0x%08x\n", status);
63  }
64 
65  return INT_STATUS_SUCCESS;
66 }
67 
68 
71  _In_ LIX_TASK_OBJECT *Task
72  )
84 {
86  ENG_NOTIFICATION_CMD_LINE *pNotification = NULL;
87  LIX_TASK_OBJECT *pParent = NULL;
88 
90  if (pNotification == NULL)
91  {
93  }
94 
95  pParent = IntLixTaskFindByGva(Task->Parent);
96  IntAlertFillLixProcess(Task, &pNotification->Child);
97  IntAlertFillLixProcess(pParent, &pNotification->Parent);
98 
100  pNotification->Header.OsType = introGuestLinux;
101 
102  pNotification->CmdLineSize = Task->CmdLineLength;
103  pNotification->CmdLine = HpAllocWithTag(pNotification->CmdLineSize, IC_TAG_CMD_LINE);
104  if (NULL == pNotification->CmdLine)
105  {
107  goto _exit;
108  }
109 
110  memcpy(pNotification->CmdLine, Task->CmdLine, Task->CmdLineLength);
111 
112  LOG("[LIX-CMDLINE] Scan request for task '%s' with PID %u using command line '%s' (%u)\n",
113  pNotification->Child.ImageName, pNotification->Child.Pid, pNotification->CmdLine, pNotification->CmdLineSize);
114 
115  status = IntNotifyEngines(pNotification);
116  if (!INT_SUCCESS(status))
117  {
118  ERROR("[ERROR] IntNotifyEngines failed with status: 0x%08x\n", status);
119  goto _exit;
120  }
121 
122  return INT_STATUS_SUCCESS;
123 
124 _exit:
125  if (pNotification != NULL)
126  {
127  if (pNotification->CmdLine)
128  {
129  HpFreeAndNullWithTag(&pNotification->CmdLine, IC_TAG_CMD_LINE);
130  }
131 
132  HpFreeAndNullWithTag(&pNotification, IC_TAG_ENGINE_NOT);
133  }
134 
135  return status;
136 }
137 
138 
139 INTSTATUS
141  _In_ ENG_NOTIFICATION_CMD_LINE *EngineNotification
142  )
150 {
151  INTSTATUS status = INT_STATUS_SUCCESS;
152 
153  if (introGuestNotAllowed != EngineNotification->Header.RequestedAction)
154  {
155  TRACE("[LIX-CMDLINE] Task '%s' with PID %u used a clean command line...\n",
156  EngineNotification->Child.ImageName, EngineNotification->Child.Pid);
157  goto _exit;
158  }
159 
160  LOG("[LIX-CMDLINE] Parent task '%s' (%u) CR3 = 0x%016llx with command line '%s'\n",
161  EngineNotification->Parent.ImageName, EngineNotification->Parent.Pid, EngineNotification->Parent.Cr3,
162  EngineNotification->Parent.CmdLine);
163  LOG("[LIX-CMDLINE] Child task '%s' (%u) CR3 = 0x%016llx with command line '%s'\n",
164  EngineNotification->Child.ImageName, EngineNotification->Child.Pid, EngineNotification->Child.Cr3,
165  EngineNotification->Child.CmdLine);
166  LOG("[LIX-CMDLINE] Detection name: '%s'\n", EngineNotification->Header.DetectionName);
167 
168  LOG("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ MALWARE ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n");
169 
170  status = IntLixCmdLineSendViolationEvent(EngineNotification);
171  if (!INT_SUCCESS(status))
172  {
173  ERROR("[ERROR] IntLixCmdLineSendViolationEvent failed with status: 0x%08x\n", status);
174  }
175 
176 _exit:
177  if (NULL != EngineNotification->CmdLine)
178  {
179  HpFreeAndNullWithTag(&EngineNotification->CmdLine, IC_TAG_CMD_LINE);
180  }
181 
182  HpFreeAndNullWithTag(&EngineNotification, IC_TAG_ENGINE_NOT);
183 
184  return INT_STATUS_SUCCESS;
185 }
186 
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
LIX_TASK_OBJECT * IntLixTaskFindByCr3(QWORD Cr3)
Finds the Linux process having the provided Cr3.
Definition: lixprocess.c:942
#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
#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 IntLixHandleCmdLineCallback(ENG_NOTIFICATION_CMD_LINE *EngineNotification)
Send a command line violation event.
Definition: lixcmdline.c:140
#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
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 TRACE(fmt,...)
Definition: glue.h:58
#define HpFreeAndNullWithTag(Add, Tag)
Definition: glue.h:517
INTSTATUS IntNotifyEngines(void *Parameters)
Definition: glue.c:1004
#define PROC_OPT_PROT_SCAN_CMD_LINE
Uses third party engines to scan the command line of a process.
Definition: intro_types.h:364
#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
Exposes the functions used to schedule an asynchronous command line scan and receives its result...
INTRO_GUEST_TYPE OsType
The guest operating system type.
Definition: intro_types.h:2061
The action was allowed, but it has the BETA flag (Introcore is in log-only mode). ...
Definition: intro_types.h:185
INTSTATUS IntLixCmdLineInspect(LIX_TASK_OBJECT *Task)
Send a command line scan request to the scan engines.
Definition: lixcmdline.c:70
static INTSTATUS IntLixCmdLineSendViolationEvent(ENG_NOTIFICATION_CMD_LINE *EngineNotification)
Send a command line violation event.
Definition: lixcmdline.c:23
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
LIX_TASK_OBJECT * IntLixTaskFindByGva(QWORD TaskStruct)
Finds Linux process with the provided "task_struct" guest virtual address.
Definition: lixprocess.c:1025
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
void IntAlertFillLixProcess(const LIX_TASK_OBJECT *Task, INTRO_PROCESS *EventProcess)
Saves information about a Linux process inside an event.
Definition: alerts.c:1264
#define INT_STATUS_INSUFFICIENT_RESOURCES
Definition: introstatus.h:281