-
Notifications
You must be signed in to change notification settings - Fork 5
/
broyara.cc
139 lines (97 loc) · 2.23 KB
/
broyara.cc
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
// See the file "COPYING" in the main distribution directory for copyright.
#include <string>
#include <iostream>
#include "broyara.h"
#include "util.h"
#include "Event.h"
#include "file_analysis/Manager.h"
using namespace file_analysis;
int callback_function(
int message,
void* message_data,
void* user_data) {
if (0 == user_data) {
return 1;
}
Yara* yara_callback_obj = static_cast<Yara*>(user_data);
yara_callback_obj->raiseEvent(message,message_data);
return ERROR_SUCCESS; // what are you meant to return from a callback function??
}
Yara::Yara(RecordVal* args, File* file, const char* arg_kind)
: file_analysis::Analyzer(
file_mgr->GetComponentTag(to_upper(arg_kind).c_str()),
args,
file),
fed(false),
yr_rules_(0)
{
if (ERROR_SUCCESS != yr_initialize()) {
throw "unable to initialize yara";
}
const u_char* str1 = args->Lookup("yara_rules_file")->AsStringVal()->Bytes();
const char* rules_file_name = (const char*) str1;//"
if (ERROR_SUCCESS != yr_rules_load(rules_file_name,&yr_rules_)) {
throw "unable to load yara rules";
}
}
Yara::~Yara()
{
if (0 != yr_rules_) {
yr_rules_destroy(yr_rules_);
}
yr_finalize();
}
bool Yara::DeliverStream(const u_char* data, uint64 len)
{
ostream_.write((const char *) data,len);
return true;
}
bool Yara::EndOfFile()
{
Finalize();
}
bool Yara::Undelivered(uint64 offset, uint64 len)
{
return false;
}
void Yara::Finalize()
{
if (!ostream_) {
return;
}
std::string result_file = ostream_.str();
if (0 == result_file.size()) {
return;
}
int res = yr_rules_scan_mem(
yr_rules_,
(uint8_t *) result_file.c_str(),
result_file.size(),
SCAN_FLAGS_FAST_MODE,
callback_function,
this,
0 // flags
);
if (ERROR_SUCCESS != res) {
//TODO: bro event??
return;
}
}
void Yara::raiseEvent(
int message,
void* message_data)
{
if (CALLBACK_MSG_RULE_MATCHING != message) {
return;
}
if (0 == message_data) {
return;
}
if (0 != file_yaraalert) {
val_list* vl = new val_list();
vl->append(GetFile()->GetVal()->Ref());
YR_RULE* yrrule = static_cast<YR_RULE*>(message_data);
vl->append(new StringVal(yrrule->identifier));
mgr.QueueEvent(file_yaraalert, vl);
}
}