NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
yara_inspection.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023-2024 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
26 #include <assert.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 
33 #include <doca_apsh.h>
34 #include <doca_argp.h>
35 #include <doca_log.h>
37 
38 #include "utils.h"
39 
40 #include "yara_inspection_core.h"
41 
43 
44 static bool running = true; /* True/False - should we continue YARA scanning or not */
45 
46 /*
47  * YARA agent application main function
48  *
49  * @argc [in]: command line arguments size
50  * @argv [in]: array of command line arguments
51  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
52  */
53 int main(int argc, char **argv)
54 {
56  struct doca_log_backend *sdk_log;
57  struct yara_config yara_conf;
59  struct doca_apsh_process **processes;
60  struct doca_apsh_yara **yara_matches;
62  int num_processes, i, yara_matches_size;
64  uint32_t yara_rules_arr_size = 2;
65  struct doca_telemetry_exporter_schema *telemetry_schema;
66  struct doca_telemetry_exporter_source *telemetry_source;
68  struct yara_event yara_match_event;
69  bool telemetry_enabled;
70  const char *str;
71  uint32_t pid;
72 
73  /* Register a logger backend */
75  if (result != DOCA_SUCCESS)
76  return EXIT_FAILURE;
77 
78  /* Register a logger backend for internal SDK errors and warnings */
80  if (result != DOCA_SUCCESS)
81  return EXIT_FAILURE;
83  if (result != DOCA_SUCCESS)
84  return EXIT_FAILURE;
85 
86  /* Parse cmdline/json arguments */
87  result = doca_argp_init(NULL, &yara_conf);
88  if (result != DOCA_SUCCESS) {
89  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
90  return EXIT_FAILURE;
91  }
93  if (result != DOCA_SUCCESS) {
94  DOCA_LOG_ERR("Failed to parse application input: %s", doca_error_get_descr(result));
96  return EXIT_FAILURE;
97  }
98  result = doca_argp_start(argc, argv);
99  if (result != DOCA_SUCCESS) {
100  DOCA_LOG_ERR("Failed to parse application input: %s", doca_error_get_descr(result));
102  return EXIT_FAILURE;
103  }
104 
105  /* Init the yara inspection app */
106  result = yara_inspection_init(&yara_conf, &resources);
107  if (result != DOCA_SUCCESS) {
108  DOCA_LOG_ERR("Failed to init application: %s", doca_error_get_descr(result));
110  return EXIT_FAILURE;
111  }
112 
113  /* Creating telemetry schema */
114  telemetry_enabled = (telemetry_start(&telemetry_schema, &telemetry_source, &yara_index) == DOCA_SUCCESS);
115 
116  do {
117  result = doca_apsh_processes_get(resources.sys, &processes, &num_processes);
118  if (result != DOCA_SUCCESS)
119  return result;
120  for (i = 0; i < num_processes; i++) {
121  result = doca_apsh_yara_get(processes[i],
122  yara_rules_arr,
123  yara_rules_arr_size,
125  &yara_matches,
126  &yara_matches_size);
127 
128  if (yara_matches_size != 0) {
129  for (i = 0; i < yara_matches_size; i++) {
130  pid = doca_apsh_yara_info_get(yara_matches[i], DOCA_APSH_YARA_PID);
131  str = doca_apsh_yara_info_get(yara_matches[i], DOCA_APSH_YARA_RULE);
132  DOCA_LOG_INFO("Got match for Yara rule %s in process id %d", str, pid);
133 
134  if (!telemetry_enabled)
135  continue;
136 
138  if (result != DOCA_SUCCESS)
139  DOCA_LOG_ERR("Failed to get timestamp, error code: %d", result);
140  yara_match_event.timestamp = timestamp;
141  yara_match_event.pid =
143  yara_match_event.vad =
144  doca_apsh_yara_info_get(yara_matches[i],
146  str = doca_apsh_yara_info_get(yara_matches[i], DOCA_APSH_YARA_COMM);
147  if (strlcpy(yara_match_event.process_name, str, MAX_PROCESS_NAME_LEN) >=
149  yara_match_event.process_name[MAX_PROCESS_NAME_LEN - 2] = '+';
150  str = doca_apsh_yara_info_get(yara_matches[i], DOCA_APSH_YARA_RULE);
151  if (strlcpy(yara_match_event.yara_rule_name, str, MAX_PATH_LEN) >= MAX_PATH_LEN)
152  yara_match_event.yara_rule_name[MAX_PATH_LEN - 2] = '+';
153 
154  /* Send telemetry data */
155  if (doca_telemetry_exporter_source_report(telemetry_source,
156  yara_index,
157  &yara_match_event,
158  1) != DOCA_SUCCESS)
159  DOCA_LOG_ERR("Cannot report to telemetry");
160  }
161  doca_apsh_yara_free(yara_matches);
162  running = false;
163  break;
164  }
165  }
166  if (running) {
167  DOCA_LOG_INFO("No match for any Yara rule");
168  sleep(yara_conf.time_interval);
169  }
170  doca_apsh_processes_free(processes);
171  } while (running);
172 
173  /* Destroy */
174  if (telemetry_enabled)
175  telemetry_destroy(telemetry_schema, telemetry_source);
176 
178 
180 
181  return DOCA_SUCCESS;
182 }
#define NULL
Definition: __stddef_null.h:26
doca_error_t telemetry_start(struct doca_telemetry_exporter_schema **telemetry_schema, struct doca_telemetry_exporter_source **telemetry_source, struct event_indexes *indexes)
void telemetry_destroy(struct doca_telemetry_exporter_schema *telemetry_schema, struct doca_telemetry_exporter_source *telemetry_source)
int32_t pid
doca_telemetry_exporter_timestamp_t timestamp
int32_t result
#define MAX_PATH_LEN
struct rdma_resources resources
doca_apsh_yara_rule
available doca app shield yara rules
@ DOCA_APSH_YARA_RULE_MIMIKATZ
@ DOCA_APSH_YARA_RULE_HELLO_WORLD
@ DOCA_APSH_YARA_SCAN_HEAP
@ DOCA_APSH_YARA_RULE
@ DOCA_APSH_YARA_PID
@ DOCA_APSH_YARA_MATCH_WINDOW_ADDR
@ DOCA_APSH_YARA_COMM
#define doca_apsh_yara_info_get(yara, attr)
Get attribute value for a yara.
Definition: doca_apsh.h:1588
DOCA_EXPERIMENTAL doca_error_t doca_apsh_processes_get(struct doca_apsh_system *system, struct doca_apsh_process ***processes, int *processes_size)
Get array of current processes running on the system.
DOCA_EXPERIMENTAL doca_error_t doca_apsh_yara_get(struct doca_apsh_process *process, enum doca_apsh_yara_rule *yara_rules_arr, uint32_t yara_rules_arr_size, uint64_t scan_type, struct doca_apsh_yara ***yara_matches, int *yara_matches_size)
Scan current process with yara rules. The scanning is done with a window size and step that are set b...
DOCA_EXPERIMENTAL void doca_apsh_yara_free(struct doca_apsh_yara **yara_matches)
Destroys a yara context.
DOCA_EXPERIMENTAL void doca_apsh_processes_free(struct doca_apsh_process **processes)
Destroys a process context.
DOCA_EXPERIMENTAL doca_error_t doca_argp_start(int argc, char **argv)
Parse incoming arguments (cmd line/json).
DOCA_EXPERIMENTAL doca_error_t doca_argp_init(const char *program_name, void *program_config)
Initialize the parser interface.
DOCA_EXPERIMENTAL doca_error_t doca_argp_destroy(void)
ARG Parser destroy.
enum doca_error doca_error_t
DOCA API return codes.
DOCA_STABLE const char * doca_error_get_descr(doca_error_t error)
Returns the description string of an error code.
@ DOCA_SUCCESS
Definition: doca_error.h:38
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_standard(void)
Create default, non configurable backend for application messages.
#define DOCA_LOG_ERR(format,...)
Generates an ERROR application log message.
Definition: doca_log.h:466
#define DOCA_LOG_INFO(format,...)
Generates an INFO application log message.
Definition: doca_log.h:486
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_with_file_sdk(FILE *fptr, struct doca_log_backend **backend)
Create a logging backend with a FILE* stream for SDK messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_set_sdk_level(struct doca_log_backend *backend, uint32_t level)
Set the log level limit for SDK logging backends.
@ DOCA_LOG_LEVEL_WARNING
Definition: doca_log.h:47
uint8_t doca_telemetry_exporter_type_index_t
DOCA schema field type index.
uint64_t doca_telemetry_exporter_timestamp_t
DOCA schema type index type.
DOCA_EXPERIMENTAL doca_error_t doca_telemetry_exporter_get_timestamp(doca_telemetry_exporter_timestamp_t *timestamp)
Get timestamp in the proper format.
DOCA_EXPERIMENTAL doca_error_t doca_telemetry_exporter_source_report(struct doca_telemetry_exporter_source *doca_source, doca_telemetry_exporter_type_index_t index, void *data, int count)
Report events data of the same type via DOCA source.
char yara_rule_name[MAX_PATH_LEN+1]
doca_telemetry_exporter_timestamp_t timestamp
char process_name[MAX_PROCESS_NAME_LEN+1]
size_t strlcpy(char *dst, const char *src, size_t size)
Definition: utils.c:123
static bool running
int main(int argc, char **argv)
DOCA_LOG_REGISTER(YARA_APP)
doca_error_t register_yara_params(void)
void yara_inspection_cleanup(struct yara_resources *resources)
doca_error_t yara_inspection_init(struct yara_config *conf, struct yara_resources *resources)
#define MAX_PROCESS_NAME_LEN