1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
|
/*
Copyright (c) 2016. The YARA Authors. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _UTIL_H
#define _UTIL_H
#include <yara.h>
#include <yara/globals.h>
#define TEXT_0063_BYTES \
"[ 987654321 987654321 987654321 987654321 987654321 987654321 ]"
#define TEXT_0256_BYTES_001 \
"001" TEXT_0063_BYTES TEXT_0063_BYTES TEXT_0063_BYTES TEXT_0063_BYTES "\n"
#define TEXT_0256_BYTES_002 \
"002" TEXT_0063_BYTES TEXT_0063_BYTES TEXT_0063_BYTES TEXT_0063_BYTES "\n"
#define TEXT_0256_BYTES_003 \
"003" TEXT_0063_BYTES TEXT_0063_BYTES TEXT_0063_BYTES TEXT_0063_BYTES "\n"
#define TEXT_0256_BYTES_004 \
"004" TEXT_0063_BYTES TEXT_0063_BYTES TEXT_0063_BYTES TEXT_0063_BYTES "\n"
#define TEXT_1024_BYTES \
TEXT_0256_BYTES_001 TEXT_0256_BYTES_002 TEXT_0256_BYTES_003 \
TEXT_0256_BYTES_004
extern char compile_error[1024];
extern int warnings;
// For testing, 1024 means split contiguous memory into max 1024 byte blocks.
// See https://github.com/VirusTotal/yara/issues/1356
extern uint64_t yr_test_mem_block_size;
// If yr_test_mem_block_size is non-zero, this specifies the bytes of the
// previous memory block to include in the current memory block.
extern uint64_t yr_test_mem_block_size_overlap;
// Counts calls to 'get first / next block' function for testing purposes.
extern uint64_t yr_test_count_get_block;
typedef struct YR_TEST_ITERATOR_CTX YR_TEST_ITERATOR_CTX;
struct YR_TEST_ITERATOR_CTX
{
const uint8_t* buffer;
size_t buffer_size;
YR_MEMORY_BLOCK current_block;
// Indicates the frequency in which the iterator returns ERROR_BLOCK_NOT_READY
// errors. For example, if the frequency is 2, the iterator will return an
// ERROR_BLOCK_NOT_READY every two blocks succesfully returned. If the value
// is 1, it will return an ERROR_BLOCK_NOT_READY after every block that was
// returned succesfully. With a value of 0 the iterator won't ever return
// ERROR_BLOCK_NOT_READY.
int block_not_ready_frequency;
// Number of blocks that has been returned successfully so far. When this
// reaches block_not_ready_frequency an ERROR_BLOCK_NOT_READY is returned.
int blocks_returned_successfully;
};
extern char* top_srcdir;
extern void init_top_srcdir(void);
extern char* prefix_top_srcdir(char* dir);
struct COUNTERS
{
int rules_matching;
int rules_not_matching;
int rules_warning;
};
int compile_rule(char* string, YR_RULES** rules);
int compile_rule_ex(char* string, YR_RULES** rules, bool strict_escape_flag);
typedef struct SCAN_CALLBACK_CTX SCAN_CALLBACK_CTX;
struct SCAN_CALLBACK_CTX
{
int matches;
void* module_data;
size_t module_data_size;
};
void init_test_iterator(
YR_MEMORY_BLOCK_ITERATOR* iterator,
YR_TEST_ITERATOR_CTX* ctx,
const uint8_t* buffer,
size_t buffer_size);
int _scan_callback(
YR_SCAN_CONTEXT* context,
int message,
void* message_data,
void* user_data);
int count(
YR_SCAN_CONTEXT* context,
int message,
void* message_data,
void* user_data);
int do_nothing(
YR_SCAN_CONTEXT* context,
int message,
void* message_data,
void* user_data);
extern int matches_blob_uses_default_iterator;
int matches_blob(
char* rule,
uint8_t* blob,
size_t blob_size,
uint8_t* module_data,
size_t module_data_size);
int matches_string(char* rule, char* string);
int capture_string(char* rule, char* string, char* expected_string);
int read_file(char* filename, char** buf);
typedef struct atom atom;
struct atom
{
uint8_t length;
uint8_t data[YR_MAX_ATOM_LENGTH];
};
void assert_re_atoms(char* re, int expected_atom_count, atom* expected_atoms);
void assert_hex_atoms(char* hex, int expected_atom_count, atom* expected_atoms);
#define assert_true_expr(expr) \
do \
{ \
if (!(expr)) \
{ \
fprintf(stderr, "%s:%d: expression is not true\n", __FILE__, __LINE__); \
exit(EXIT_FAILURE); \
} \
} while (0);
// Ensure that a particular string is found when scanning. This is useful for
// making sure that unreferenced strings are searched for properly.
// Specifically, making sure they have STRING_FLAGS_FIXED_OFFSET unset.
#define assert_string_capture(rule, string, expected) \
do \
{ \
if (!capture_string(rule, string, expected)) \
{ \
fprintf(stderr, "%s:%d: rule does not match\n", __FILE__, __LINE__); \
exit(EXIT_FAILURE); \
} \
} while (0);
#define assert_match_count(rule, string, count) \
do \
{ \
int result = matches_string(rule, string); \
if (result != count) \
{ \
fprintf( \
stderr, \
"%s:%d: rule does not match count: " \
"expected: %d actual: %d\n", \
__FILE__, \
__LINE__, \
count, \
result); \
exit(EXIT_FAILURE); \
} \
} while (0);
#define assert_true_rule(rule, string) \
do \
{ \
if (!matches_string(rule, string)) \
{ \
fprintf( \
stderr, \
"%s:%d: rule does not match (but should)\n", \
__FILE__, \
__LINE__); \
exit(EXIT_FAILURE); \
} \
} while (0);
#define assert_true_rule_blob_size(rule, blob, size) \
do \
{ \
if (!matches_blob(rule, (uint8_t*) (blob), size, NULL, 0)) \
{ \
fprintf( \
stderr, \
"%s:%d: rule does not match (but should)\n", \
__FILE__, \
__LINE__); \
exit(EXIT_FAILURE); \
} \
} while (0);
#define assert_true_rule_blob(rule, blob) \
assert_true_rule_blob_size(rule, blob, sizeof(blob))
#define assert_true_rule_file(rule, filename) \
do \
{ \
char* buf; \
size_t sz; \
if ((sz = read_file(prefix_top_srcdir(filename), &buf)) == -1) \
{ \
fprintf( \
stderr, \
"%s:%d: cannot read file '%s'\n", \
__FILE__, \
__LINE__, \
filename); \
exit(EXIT_FAILURE); \
} \
if (!matches_blob(rule, (uint8_t*) (buf), sz, NULL, 0)) \
{ \
fprintf( \
stderr, \
"%s:%d: rule does not match contents of" \
"'%s' (but should)\n", \
__FILE__, \
__LINE__, \
filename); \
exit(EXIT_FAILURE); \
} \
free(buf); \
} while (0);
#define assert_false_rule(rule, string) \
do \
{ \
if (matches_string(rule, string)) \
{ \
fprintf( \
stderr, \
"%s:%d: rule matches (but shouldn't)\n", \
__FILE__, \
__LINE__); \
exit(EXIT_FAILURE); \
} \
} while (0);
#define assert_false_rule_blob_size(rule, blob, size) \
do \
{ \
if (matches_blob(rule, (uint8_t*) (blob), size, NULL, 0)) \
{ \
fprintf( \
stderr, \
"%s:%d: rule matches (but shouldn't)\n", \
__FILE__, \
__LINE__); \
exit(EXIT_FAILURE); \
} \
} while (0);
#define assert_false_rule_blob(rule, blob) \
assert_false_rule_blob_size(rule, blob, sizeof(blob))
#define assert_true_rule_module_data_file(rule, filename) \
do \
{ \
char* buf; \
size_t sz; \
if ((sz = read_file(prefix_top_srcdir(filename), &buf)) == -1) \
{ \
fprintf( \
stderr, \
"%s:%d: cannot read file '%s'\n", \
__FILE__, \
__LINE__, \
filename); \
exit(EXIT_FAILURE); \
} \
if (!matches_blob(rule, NULL, 0, (uint8_t*) buf, sz)) \
{ \
fprintf( \
stderr, \
"%s:%d: rule does not matches (but should)\n", \
__FILE__, \
__LINE__); \
exit(EXIT_FAILURE); \
} \
free(buf); \
} while (0);
#define assert_false_rule_file(rule, filename) \
do \
{ \
char* buf; \
size_t sz; \
if ((sz = read_file(prefix_top_srcdir(filename), &buf)) == -1) \
{ \
fprintf( \
stderr, \
"%s:%d: cannot read file '%s'\n", \
__FILE__, \
__LINE__, \
filename); \
exit(EXIT_FAILURE); \
} \
if (matches_blob(rule, (uint8_t*) (buf), sz, NULL, 0)) \
{ \
fprintf( \
stderr, \
"%s:%d: rule matches contents of" \
"'%s' (but shouldn't)\n", \
__FILE__, \
__LINE__, \
filename); \
exit(EXIT_FAILURE); \
} \
free(buf); \
} while (0);
#define assert_error(rule, error) \
do \
{ \
YR_RULES* rules; \
int result = compile_rule(rule, &rules); \
if (result == ERROR_SUCCESS) \
yr_rules_destroy(rules); \
if (result != error) \
{ \
fprintf( \
stderr, \
"%s:%d: expecting error %d but returned %d\n", \
__FILE__, \
__LINE__, \
error, \
result); \
exit(EXIT_FAILURE); \
} \
} while (0);
#define assert_warnings(rule, w) \
do \
{ \
YR_RULES* rules; \
int result = compile_rule(rule, &rules); \
if (result == ERROR_SUCCESS) \
{ \
yr_rules_destroy(rules); \
if (warnings < w) \
{ \
fprintf(stderr, "%s:%d: expecting warning\n", __FILE__, __LINE__); \
exit(EXIT_FAILURE); \
} \
} \
else \
{ \
fprintf( \
stderr, \
"%s:%d: failed to compile << %s >>: %s\n", \
__FILE__, \
__LINE__, \
rule, \
compile_error); \
exit(EXIT_FAILURE); \
} \
} while (0);
#define assert_warnings_strict_escape(rule, w) \
do \
{ \
YR_RULES* rules; \
bool strict_escape = true; \
int result = compile_rule_ex(rule, &rules, strict_escape); \
if (result == ERROR_SUCCESS) \
{ \
yr_rules_destroy(rules); \
if (warnings < w) \
{ \
fprintf(stderr, "%s:%d: expecting warning\n", __FILE__, __LINE__); \
exit(EXIT_FAILURE); \
} \
} \
else \
{ \
fprintf( \
stderr, \
"%s:%d: failed to compile << %s >>: %s\n", \
__FILE__, \
__LINE__, \
rule, \
compile_error); \
exit(EXIT_FAILURE); \
} \
} while (0);
#define assert_no_warnings(rule) \
do \
{ \
YR_RULES* rules; \
int result = compile_rule(rule, &rules); \
if (result == ERROR_SUCCESS) \
{ \
yr_rules_destroy(rules); \
if (warnings > 0) \
{ \
fprintf(stderr, "%s:%d: unexpected warning\n", __FILE__, __LINE__); \
exit(EXIT_FAILURE); \
} \
} \
else \
{ \
fprintf( \
stderr, \
"%s:%d: failed to compile << %s >>: %s\n", \
__FILE__, \
__LINE__, \
rule, \
compile_error); \
exit(EXIT_FAILURE); \
} \
} while (0);
#define assert_warning(rule) assert_warnings(rule, 1)
#define assert_warning_strict_escape(rule) \
assert_warnings_strict_escape(rule, 1)
#define assert_true_regexp(regexp, string, expected) \
do \
{ \
if (!capture_string( \
"rule test { strings: $a = /" regexp "/ condition: $a }", \
string, \
expected)) \
{ \
fprintf(stderr, "%s:%d: regexp does not match\n", __FILE__, __LINE__); \
exit(EXIT_FAILURE); \
} \
} while (0);
#define assert_false_regexp(regexp, string) \
assert_false_rule( \
"rule test { strings: $a = /" regexp "/ condition: $a }", string)
#define assert_regexp_syntax_error(regexp) \
assert_error( \
"rule test { strings: $a = /" regexp "/ condition: $a }", \
ERROR_INVALID_REGULAR_EXPRESSION)
#endif /* _UTIL_H */
|