Coverage Report

Created: 2025-07-12 06:20

/src/yara/libyara/modules/console/console.c
Line
Count
Source (jump to first uncovered line)
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 <stdio.h>
31
32
#include <yara/mem.h>
33
#include <yara/modules.h>
34
#include <yara/strutils.h>
35
36
#define MODULE_NAME console
37
38
define_function(log_string)
39
0
{
40
  // We are intentionally using sized strings here as we may be needing to
41
  // output strings with a null character in the middle.
42
0
  SIZED_STRING* s = sized_string_argument(1);
43
0
  YR_SCAN_CONTEXT* ctx = yr_scan_context();
44
0
  YR_CALLBACK_FUNC callback = ctx->callback;
45
46
  // Assume the entire string is non-printable, so allocate 4 times the
47
  // space so that we can represent each byte as an escaped value. eg: \x00
48
  // Add an extra byte for the NULL terminator.
49
0
  char* msg;
50
0
  if (s->length == 0)
51
0
  {
52
0
    callback(ctx, CALLBACK_MSG_CONSOLE_LOG, (void*) "", ctx->user_data);
53
0
    return_integer(1);
54
0
  }
55
0
  else
56
0
  {
57
0
    msg = (char*) yr_calloc((s->length * 4) + 1, sizeof(char));
58
0
    if (msg == NULL)
59
0
      return_integer(YR_UNDEFINED);
60
0
  }
61
62
0
  char* p = msg;
63
0
  for (size_t i = 0; i < s->length; i++)
64
0
  {
65
0
    if (isprint((unsigned char) s->c_string[i]))
66
0
    {
67
0
      *p++ = s->c_string[i];
68
0
    }
69
0
    else
70
0
    {
71
0
      sprintf(p, "\\x%02x", (unsigned char) s->c_string[i]);
72
0
      p += 4;
73
0
    }
74
0
  }
75
76
  // result is ignored, as we have no way to signal to the library that it
77
  // should abort or continue.
78
0
  callback(ctx, CALLBACK_MSG_CONSOLE_LOG, (void*) msg, ctx->user_data);
79
80
0
  yr_free(msg);
81
0
  return_integer(1);
82
0
}
83
84
define_function(log_string_msg)
85
0
{
86
0
  char* m = string_argument(1);
87
  // We are intentionally using sized strings here as we may be needing to
88
  // output strings with a null character in the middle.
89
0
  SIZED_STRING* s = sized_string_argument(2);
90
0
  YR_SCAN_CONTEXT* ctx = yr_scan_context();
91
0
  YR_CALLBACK_FUNC callback = ctx->callback;
92
93
  // Assume the entire string is non-printable, so allocate 4 times the
94
  // space so that we can represent each byte as an escaped value. eg: \x00
95
  // Add an extra byte for the NULL terminator.
96
0
  size_t msg_len = strlen(m) + (s->length * 4) + 1;
97
0
  char* msg = (char*) yr_calloc(msg_len, sizeof(char));
98
0
  if (msg == NULL && msg_len > 0)
99
0
    return_integer(YR_UNDEFINED);
100
101
0
  char* p = msg;
102
0
  strlcpy(msg, m, msg_len);
103
0
  p += strlen(m);
104
0
  for (size_t i = 0; i < s->length; i++)
105
0
  {
106
0
    if (isprint((unsigned char) s->c_string[i]))
107
0
    {
108
0
      *p++ = s->c_string[i];
109
0
    }
110
0
    else
111
0
    {
112
0
      sprintf(p, "\\x%02x", (unsigned char) s->c_string[i]);
113
0
      p += 4;
114
0
    }
115
0
  }
116
117
  // result is ignored, as we have no way to signal to the library that it
118
  // should abort or continue.
119
0
  callback(ctx, CALLBACK_MSG_CONSOLE_LOG, (void*) msg, ctx->user_data);
120
121
0
  yr_free(msg);
122
0
  return_integer(1);
123
0
}
124
125
define_function(log_integer)
126
0
{
127
0
  char* msg = NULL;
128
0
  int64_t i = integer_argument(1);
129
0
  YR_SCAN_CONTEXT* ctx = yr_scan_context();
130
0
  YR_CALLBACK_FUNC callback = ctx->callback;
131
132
0
  yr_asprintf(&msg, "%lli", i);
133
134
0
  if (msg == NULL)
135
0
    return_integer(YR_UNDEFINED);
136
137
  // result is ignored, as we have no way to signal to the library that it
138
  // should abort or continue.
139
0
  callback(ctx, CALLBACK_MSG_CONSOLE_LOG, (void*) msg, ctx->user_data);
140
141
0
  yr_free(msg);
142
0
  return_integer(1);
143
0
}
144
145
define_function(log_integer_msg)
146
0
{
147
0
  char* msg = NULL;
148
0
  char* s = string_argument(1);
149
0
  int64_t i = integer_argument(2);
150
0
  YR_SCAN_CONTEXT* ctx = yr_scan_context();
151
0
  YR_CALLBACK_FUNC callback = ctx->callback;
152
153
0
  yr_asprintf(&msg, "%s%lli", s, i);
154
155
0
  if (msg == NULL)
156
0
    return_integer(YR_UNDEFINED);
157
158
  // result is ignored, as we have no way to signal to the library that it
159
  // should abort or continue.
160
0
  callback(ctx, CALLBACK_MSG_CONSOLE_LOG, (void*) msg, ctx->user_data);
161
162
0
  yr_free(msg);
163
0
  return_integer(1);
164
0
}
165
166
define_function(log_float)
167
0
{
168
0
  char* msg = NULL;
169
0
  double f = float_argument(1);
170
0
  YR_SCAN_CONTEXT* ctx = yr_scan_context();
171
0
  YR_CALLBACK_FUNC callback = ctx->callback;
172
173
0
  yr_asprintf(&msg, "%f", f);
174
175
0
  if (msg == NULL)
176
0
    return_integer(YR_UNDEFINED);
177
178
  // result is ignored, as we have no way to signal to the library that it
179
  // should abort or continue.
180
0
  callback(ctx, CALLBACK_MSG_CONSOLE_LOG, (void*) msg, ctx->user_data);
181
182
0
  yr_free(msg);
183
0
  return_integer(1);
184
0
}
185
186
define_function(log_float_msg)
187
0
{
188
0
  char* msg = NULL;
189
0
  char* s = string_argument(1);
190
0
  double f = float_argument(2);
191
0
  YR_SCAN_CONTEXT* ctx = yr_scan_context();
192
0
  YR_CALLBACK_FUNC callback = ctx->callback;
193
194
0
  yr_asprintf(&msg, "%s%f", s, f);
195
196
0
  if (msg == NULL)
197
0
    return_integer(YR_UNDEFINED);
198
199
  // result is ignored, as we have no way to signal to the library that it
200
  // should abort or continue.
201
0
  callback(ctx, CALLBACK_MSG_CONSOLE_LOG, (void*) msg, ctx->user_data);
202
203
0
  yr_free(msg);
204
0
  return_integer(1);
205
0
}
206
207
define_function(hex_integer)
208
0
{
209
0
  char* msg = NULL;
210
0
  int64_t i = integer_argument(1);
211
0
  YR_SCAN_CONTEXT* ctx = yr_scan_context();
212
0
  YR_CALLBACK_FUNC callback = ctx->callback;
213
214
0
  yr_asprintf(&msg, "0x%llx", i);
215
216
0
  if (msg == NULL)
217
0
    return_integer(YR_UNDEFINED);
218
219
  // result is ignored, as we have no way to signal to the library that it
220
  // should abort or continue.
221
0
  callback(ctx, CALLBACK_MSG_CONSOLE_LOG, (void*) msg, ctx->user_data);
222
223
0
  yr_free(msg);
224
0
  return_integer(1);
225
0
}
226
227
define_function(hex_integer_msg)
228
0
{
229
0
  char* msg = NULL;
230
0
  char* s = string_argument(1);
231
0
  int64_t i = integer_argument(2);
232
0
  YR_SCAN_CONTEXT* ctx = yr_scan_context();
233
0
  YR_CALLBACK_FUNC callback = ctx->callback;
234
235
0
  yr_asprintf(&msg, "%s0x%llx", s, i);
236
237
0
  if (msg == NULL)
238
0
    return_integer(YR_UNDEFINED);
239
240
  // result is ignored, as we have no way to signal to the library that it
241
  // should abort or continue.
242
0
  callback(ctx, CALLBACK_MSG_CONSOLE_LOG, (void*) msg, ctx->user_data);
243
244
0
  yr_free(msg);
245
0
  return_integer(1);
246
0
}
247
248
0
begin_declarations
249
0
  declare_function("log", "s", "i", log_string);
250
0
  declare_function("log", "ss", "i", log_string_msg);
251
0
  declare_function("log", "i", "i", log_integer);
252
0
  declare_function("log", "si", "i", log_integer_msg);
253
0
  declare_function("log", "f", "i", log_float);
254
0
  declare_function("log", "sf", "i", log_float_msg);
255
0
  declare_function("hex", "i", "i", hex_integer);
256
0
  declare_function("hex", "si", "i", hex_integer_msg);
257
0
end_declarations
258
259
int module_initialize(YR_MODULE* module)
260
1
{
261
1
  return ERROR_SUCCESS;
262
1
}
263
264
int module_finalize(YR_MODULE* module)
265
0
{
266
0
  return ERROR_SUCCESS;
267
0
}
268
269
int module_load(
270
    YR_SCAN_CONTEXT* context,
271
    YR_OBJECT* module_object,
272
    void* module_data,
273
    size_t module_data_size)
274
0
{
275
0
  return ERROR_SUCCESS;
276
0
}
277
278
int module_unload(YR_OBJECT* module_object)
279
0
{
280
0
  return ERROR_SUCCESS;
281
0
}