forked from cumulodev/yara
/
yara.go
249 lines (195 loc) · 5.47 KB
/
yara.go
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
package yara
// #include <stdio.h>
// #include <yara.h>
// #include "cgo.h"
import "C"
import (
"fmt"
"io"
"log"
"unsafe"
)
const RequiredVersion = "3.4.0"
var callback = (C.YR_CALLBACK_FUNC)(unsafe.Pointer(C.callback))
// Callback is a callback that gets called during a scan with a matching rule.
type Callback func(*Rule) CallbackStatus
// CallbackStatus is flag to indicate to libyara if it should continue or abort a scanning
// process.
type CallbackStatus int
const (
// Coninue tells libyara to continue the scanning.
Continue = CallbackStatus(C.CALLBACK_CONTINUE)
// Abort tells libyara to abort the scanning.
Abort = CallbackStatus(C.CALLBACK_ABORT)
// Fail tells libyara that an error occured and it should abort the scanning.
Fail = CallbackStatus(C.CALLBACK_ERROR)
)
func init() {
code := C.yr_initialize()
if code != C.ERROR_SUCCESS {
log.Fatalf("failed to initialize libyara!")
}
}
func Finalize() error {
code := C.yr_finalize()
if code != C.ERROR_SUCCESS {
return Error(code)
}
return nil
}
type Compiler struct {
handle *C.YR_COMPILER
}
func NewCompiler() (*Compiler, error) {
var handle *C.YR_COMPILER
code := C.yr_compiler_create(&handle)
if code != C.ERROR_SUCCESS {
return nil, Error(code)
}
return &Compiler{handle}, nil
}
func (c *Compiler) Destroy() {
C.yr_compiler_destroy(c.handle)
}
func (c *Compiler) AddFile(ns, path string) error {
cpath := C.CString(path)
cmode := C.CString("r")
cns := C.CString(ns)
defer C.free(unsafe.Pointer(cpath))
defer C.free(unsafe.Pointer(cmode))
defer C.free(unsafe.Pointer(cns))
fd := C.fopen(cpath, cmode)
if fd == nil {
return fmt.Errorf("libyara: failed to open %q", path)
}
defer C.fclose(fd)
errors := C.yr_compiler_add_file(c.handle, fd, nil, cpath)
if errors > 0 {
return fmt.Errorf("libyara: failed to compile %q", path)
}
return nil
}
func (c *Compiler) AddString(ns, rule string) error {
cns := C.CString(ns)
crule := C.CString(rule)
errors := C.yr_compiler_add_string(c.handle, crule, cns)
C.free(unsafe.Pointer(crule))
C.free(unsafe.Pointer(cns))
if errors > 0 {
return fmt.Errorf("libyara: failed to compile rule")
}
return nil
}
func (c *Compiler) Rules() (*Rules, error) {
var handle *C.YR_RULES
code := C.yr_compiler_get_rules(c.handle, &handle)
if code != C.ERROR_SUCCESS {
return nil, Error(code)
}
return &Rules{
handle: handle,
}, nil
}
type Rules struct {
handle *C.YR_RULES
}
func Load(r io.Reader) (*Rules, error) {
var handle *C.YR_RULES
code := C.yr_rules_load_stream(readStream(r), &handle)
if code != C.ERROR_SUCCESS {
return nil, Error(code)
}
return &Rules{handle}, nil
}
func LoadFromFile(path string) (*Rules, error) {
var handle *C.YR_RULES
cpath := C.CString(path)
code := C.yr_rules_load(cpath, &handle)
C.free(unsafe.Pointer(cpath))
if code != C.ERROR_SUCCESS {
return nil, Error(code)
}
return &Rules{
handle: handle,
}, nil
}
func (r *Rules) Destroy() {
C.yr_rules_destroy(r.handle)
}
func (r *Rules) Write(w io.Writer) error {
code := C.yr_rules_save_stream(r.handle, writeStream(w))
if code != C.ERROR_SUCCESS {
return Error(code)
}
return nil
}
func (r *Rules) Save(path string) error {
cpath := C.CString(path)
code := C.yr_rules_save(r.handle, cpath)
C.free(unsafe.Pointer(cpath))
if code != C.ERROR_SUCCESS {
return Error(code)
}
return nil
}
func (r *Rules) ScanMemory(buffer []byte, fn Callback) error {
data := (*C.uint8_t)(unsafe.Pointer(&buffer[0]))
size := C.size_t(len(buffer))
code := C.yr_rules_scan_mem(r.handle, data, size, 0, callback, *(*unsafe.Pointer)(unsafe.Pointer(&fn)), 0)
if code != C.ERROR_SUCCESS {
return Error(code)
}
return nil
}
func (r *Rules) ScanFile(path string, fn Callback) error {
cpath := C.CString(path)
code := C.yr_rules_scan_file(r.handle, cpath, 0, callback, *(*unsafe.Pointer)(unsafe.Pointer(&fn)), 0)
C.free(unsafe.Pointer(cpath))
if code != C.ERROR_SUCCESS {
return Error(code)
}
return nil
}
type Rule struct {
Identifier string
Tags []string
Metadata map[string]interface{}
}
func NewRule() *Rule {
return &Rule{
Metadata: make(map[string]interface{}),
}
}
var errorcodes = map[Error]string{
C.ERROR_COULD_NOT_OPEN_FILE: "File could not be opened",
C.ERROR_COULD_NOT_MAP_FILE: "File could not be mapped into memory",
C.ERROR_INVALID_FILE: "File is not a valid rules file",
C.ERROR_CORRUPT_FILE: "Rules file is corrupt",
C.ERROR_UNSUPPORTED_FILE_VERSION: "File was generated by a different YARA and can’t be loaded by this version",
C.ERROR_TOO_MANY_SCAN_THREADS: "Too many threads trying to use the same YR_RULES object simultaneosly",
C.ERROR_SCAN_TIMEOUT: "Scan timed out",
C.ERROR_CALLBACK_ERROR: "Callback returned an error",
C.ERROR_TOO_MANY_MATCHES: "Too many matches for some string in your rules",
}
// Error is an libyara error.
type Error int
// Error returns an human readable repsentation of the libyara error.
func (e Error) Error() string {
if msg, ok := errorcodes[e]; ok {
return fmt.Sprintf("libyara: %s (%d)", msg, e)
} else {
return fmt.Sprintf("libyara: Unknown error code (%d)", e)
}
}
func readStream(r io.Reader) *C.YR_STREAM {
stream := new(C.YR_STREAM)
stream.user_data = unsafe.Pointer(&r)
stream.read = (C.YR_STREAM_READ_FUNC)(C.goStreamRead)
return stream
}
func writeStream(w io.Writer) *C.YR_STREAM {
stream := new(C.YR_STREAM)
stream.user_data = unsafe.Pointer(&w)
stream.write = (C.YR_STREAM_WRITE_FUNC)(C.goStreamWrite)
return stream
}