Coverage Report

Created: 2026-12-01 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/yara/libyara/modules/console/console.c
Line
Count
Source
1
/*
2
Copyright (c) 2021. The YARA Authors. All Rights Reserved.
3
4
Redistribution and use in source and binary forms, with or without modification,
5
are permitted provided that the following conditions are met:
6
7
1. Redistributions of source code must retain the above copyright notice, this
8
list of conditions and the following disclaimer.
9
10
2. Redistributions in binary form must reproduce the above copyright notice,
11
this list of conditions and the following disclaimer in the documentation and/or
12
other materials provided with the distribution.
13
14
3. Neither the name of the copyright holder nor the names of its contributors
15
may be used to endorse or promote products derived from this software without
16
specific prior written permission.
17
18
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
*/
29
30
#include <stdint.h>
31
#include <stdio.h>
32
33
#include <yara/mem.h>
34
#include <yara/modules.h>
35
#include <yara/strutils.h>
36
37
#define MODULE_NAME console
38
39
static int64_t _log_string(
40
    YR_SCAN_CONTEXT* ctx,
41
    const char* prefix,
42
    SIZED_STRING* s)
43
0
{
44
0
  size_t prefix_len = prefix != NULL ? strlen(prefix) : 0;
45
0
  size_t string_len = s->length;
46
47
0
  if (prefix_len == 0 && string_len == 0)
48
0
  {
49
0
    ctx->callback(ctx, CALLBACK_MSG_CONSOLE_LOG, (void*) "", ctx->user_data);
50
0
    return 1;
51
0
  }
52
53
  // Assume the entire string is non-printable, so allocate 4 times the
54
  // space so that we can represent each byte as an escaped value. eg: \x00
55
  // Add an extra byte for the NULL terminator.
56
0
  if (prefix_len == SIZE_MAX || string_len > (SIZE_MAX - prefix_len - 1) / 4)
57
0
    return YR_UNDEFINED;
58
59
0
  size_t msg_len = prefix_len + (string_len * 4) + 1;
60
0
  char* msg = (char*) yr_calloc(msg_len, sizeof(char));
61
62
0
  if (msg == NULL)
63
0
    return YR_UNDEFINED;
64
65
0
  char* p = msg;
66
67
0
  if (prefix_len > 0)
68
0
  {
69
0
    memcpy(p, prefix, prefix_len);
70
0
    p += prefix_len;
71
0
  }
72
73
0
  for (size_t i = 0; i < string_len; i++)
74
0
  {
75
0
    if (isprint((unsigned char) s->c_string[i]))
76
0
    {
77
0
      *p++ = s->c_string[i];
78
0
    }
79
0
    else
80
0
    {
81
0
      sprintf(p, "\\x%02x", (unsigned char) s->c_string[i]);
82
0
      p += 4;
83
0
    }
84
0
  }
85
86
  // result is ignored, as we have no way to signal to the library that it
87
  // should abort or continue.
88
0
  ctx->callback(ctx, CALLBACK_MSG_CONSOLE_LOG, (void*) msg, ctx->user_data);
89
90
0
  yr_free(msg);
91
0
  return 1;
92
0
}
93
94
define_function(log_string)
95
0
{
96
  // We are intentionally using sized strings here as we may be needing to
97
  // output strings with a null character in the middle.
98
0
  SIZED_STRING* s = sized_string_argument(1);
99
100
0
  return_integer(_log_string(yr_scan_context(), NULL, s));
101
0
}
102
103
define_function(log_string_msg)
104
0
{
105
0
  char* m = string_argument(1);
106
  // We are intentionally using sized strings here as we may be needing to
107
  // output strings with a null character in the middle.
108
0
  SIZED_STRING* s = sized_string_argument(2);
109
110
0
  return_integer(_log_string(yr_scan_context(), m, s));
111
0
}
112
113
define_function(log_integer)
114
0
{
115
0
  char* msg = NULL;
116
0
  int64_t i = integer_argument(1);
117
0
  YR_SCAN_CONTEXT* ctx = yr_scan_context();
118
0
  YR_CALLBACK_FUNC callback = ctx->callback;
119
120
0
  yr_asprintf(&msg, "%lli", i);
121
122
0
  if (msg == NULL)
123
0
    return_integer(YR_UNDEFINED);
124
125
  // result is ignored, as we have no way to signal to the library that it
126
  // should abort or continue.
127
0
  callback(ctx, CALLBACK_MSG_CONSOLE_LOG, (void*) msg, ctx->user_data);
128
129
0
  yr_free(msg);
130
0
  return_integer(1);
131
0
}
132
133
define_function(log_integer_msg)
134
0
{
135
0
  char* msg = NULL;
136
0
  char* s = string_argument(1);
137
0
  int64_t i = integer_argument(2);
138
0
  YR_SCAN_CONTEXT* ctx = yr_scan_context();
139
0
  YR_CALLBACK_FUNC callback = ctx->callback;
140
141
0
  yr_asprintf(&msg, "%s%lli", s, i);
142
143
0
  if (msg == NULL)
144
0
    return_integer(YR_UNDEFINED);
145
146
  // result is ignored, as we have no way to signal to the library that it
147
  // should abort or continue.
148
0
  callback(ctx, CALLBACK_MSG_CONSOLE_LOG, (void*) msg, ctx->user_data);
149
150
0
  yr_free(msg);
151
0
  return_integer(1);
152
0
}
153
154
define_function(log_float)
155
0
{
156
0
  char* msg = NULL;
157
0
  double f = float_argument(1);
158
0
  YR_SCAN_CONTEXT* ctx = yr_scan_context();
159
0
  YR_CALLBACK_FUNC callback = ctx->callback;
160
161
0
  yr_asprintf(&msg, "%f", f);
162
163
0
  if (msg == NULL)
164
0
    return_integer(YR_UNDEFINED);
165
166
  // result is ignored, as we have no way to signal to the library that it
167
  // should abort or continue.
168
0
  callback(ctx, CALLBACK_MSG_CONSOLE_LOG, (void*) msg, ctx->user_data);
169
170
0
  yr_free(msg);
171
0
  return_integer(1);
172
0
}
173
174
define_function(log_float_msg)
175
0
{
176
0
  char* msg = NULL;
177
0
  char* s = string_argument(1);
178
0
  double f = float_argument(2);
179
0
  YR_SCAN_CONTEXT* ctx = yr_scan_context();
180
0
  YR_CALLBACK_FUNC callback = ctx->callback;
181
182
0
  yr_asprintf(&msg, "%s%f", s, f);
183
184
0
  if (msg == NULL)
185
0
    return_integer(YR_UNDEFINED);
186
187
  // result is ignored, as we have no way to signal to the library that it
188
  // should abort or continue.
189
0
  callback(ctx, CALLBACK_MSG_CONSOLE_LOG, (void*) msg, ctx->user_data);
190
191
0
  yr_free(msg);
192
0
  return_integer(1);
193
0
}
194
195
define_function(hex_integer)
196
0
{
197
0
  char* msg = NULL;
198
0
  int64_t i = integer_argument(1);
199
0
  YR_SCAN_CONTEXT* ctx = yr_scan_context();
200
0
  YR_CALLBACK_FUNC callback = ctx->callback;
201
202
0
  yr_asprintf(&msg, "0x%llx", i);
203
204
0
  if (msg == NULL)
205
0
    return_integer(YR_UNDEFINED);
206
207
  // result is ignored, as we have no way to signal to the library that it
208
  // should abort or continue.
209
0
  callback(ctx, CALLBACK_MSG_CONSOLE_LOG, (void*) msg, ctx->user_data);
210
211
0
  yr_free(msg);
212
0
  return_integer(1);
213
0
}
214
215
define_function(hex_integer_msg)
216
0
{
217
0
  char* msg = NULL;
218
0
  char* s = string_argument(1);
219
0
  int64_t i = integer_argument(2);
220
0
  YR_SCAN_CONTEXT* ctx = yr_scan_context();
221
0
  YR_CALLBACK_FUNC callback = ctx->callback;
222
223
0
  yr_asprintf(&msg, "%s0x%llx", s, i);
224
225
0
  if (msg == NULL)
226
0
    return_integer(YR_UNDEFINED);
227
228
  // result is ignored, as we have no way to signal to the library that it
229
  // should abort or continue.
230
0
  callback(ctx, CALLBACK_MSG_CONSOLE_LOG, (void*) msg, ctx->user_data);
231
232
0
  yr_free(msg);
233
0
  return_integer(1);
234
0
}
235
236
0
begin_declarations
237
0
  declare_function("log", "s", "i", log_string);
238
0
  declare_function("log", "ss", "i", log_string_msg);
239
0
  declare_function("log", "i", "i", log_integer);
240
0
  declare_function("log", "si", "i", log_integer_msg);
241
0
  declare_function("log", "f", "i", log_float);
242
0
  declare_function("log", "sf", "i", log_float_msg);
243
0
  declare_function("hex", "i", "i", hex_integer);
244
0
  declare_function("hex", "si", "i", hex_integer_msg);
245
0
end_declarations
246
247
int module_initialize(YR_MODULE* module)
248
2
{
249
2
  return ERROR_SUCCESS;
250
2
}
251
252
int module_finalize(YR_MODULE* module)
253
0
{
254
0
  return ERROR_SUCCESS;
255
0
}
256
257
int module_load(
258
    YR_SCAN_CONTEXT* context,
259
    YR_OBJECT* module_object,
260
    void* module_data,
261
    size_t module_data_size)
262
0
{
263
0
  return ERROR_SUCCESS;
264
0
}
265
266
int module_unload(YR_OBJECT* module_object)
267
0
{
268
0
  return ERROR_SUCCESS;
269
0
}