-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyara-insp.c
113 lines (88 loc) · 2.4 KB
/
yara-insp.c
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
#include <yara.h>
#include "args.h"
#include "config.h"
#include "error.h"
#define MAX_ARGS_TAG 32
int show_version = FALSE;
int show_help = FALSE;
int show_tags = FALSE;
#define USAGE_STRING \
"Usage: ytags [OPTION]... RULES_FILE"
args_option_t options[] =
{
OPT_BOOLEAN('v', "version", &show_version,
"show version information"),
OPT_BOOLEAN('h', "help", &show_help,
"show this help and exit"),
OPT_END()
};
#define exit_with_code(code) { result = code; goto _exit; }
int main(int argc, const char** argv) {
YR_RULES* rules = NULL;
YR_RULE* cur_rule = NULL;
YR_META* meta = NULL;
const char* tag;
argc = args_parse(options, argc, argv);
int result;
if (show_version)
{
printf("%s\n", PACKAGE_STRING);
return EXIT_SUCCESS;
}
if (show_help)
{
printf(
"YARA %s, the pattern matching swiss army knife.\n"
"%s\n\n"
"Mandatory arguments to long options are mandatory for "
"short options too.\n\n", PACKAGE_VERSION, USAGE_STRING);
args_print_usage(options, 35);
printf("\nSend bug reports and suggestions to: %s.\n", PACKAGE_BUGREPORT);
return EXIT_SUCCESS;
}
if (argc != 1)
{
// After parsing the command-line options we expect two additional
// arguments, the rules file and the target file, directory or pid to
// be scanned.
fprintf(stderr, "yara: wrong number of arguments\n");
fprintf(stderr, "%s\n\n", USAGE_STRING);
fprintf(stderr, "Try `--help` for more options\n");
return EXIT_FAILURE;
}
result = yr_initialize();
if (result != ERROR_SUCCESS)
{
fprintf(stderr, "error: initialization error (%d)\n", result);
exit_with_code(EXIT_FAILURE);
}
result = yr_rules_load(argv[0], &rules);
// Accepted result are ERROR_SUCCESS or ERROR_INVALID_FILE
// if we are passing the rules in source form, if result is
// different from those exit with error.
if (result != ERROR_SUCCESS &&
result != ERROR_INVALID_FILE)
{
print_error(result);
exit_with_code(EXIT_FAILURE);
}
if (result == ERROR_SUCCESS)
{
yr_rules_foreach(rules, cur_rule)
{
printf("rule %s :", cur_rule->identifier);
yr_rule_tags_foreach(cur_rule, tag)
{
printf(" %s", tag);
}
printf("\n");
}
} else {
print_error(result);
exit_with_code(EXIT_FAILURE);
}
_exit:
if (rules != NULL)
yr_rules_destroy(rules);
return result;
}