yara-ffi 3.1.0 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/copilot-instructions.md +266 -0
- data/.github/workflows/ruby.yml +69 -17
- data/CHANGELOG.md +90 -1
- data/DEVELOPMENT.md +188 -0
- data/Dockerfile +19 -11
- data/Gemfile.lock +38 -23
- data/README.md +56 -36
- data/USAGE.md +747 -0
- data/lib/yara/compiler.rb +161 -0
- data/lib/yara/ffi.rb +500 -111
- data/lib/yara/pattern_match.rb +178 -0
- data/lib/yara/scan_result.rb +573 -71
- data/lib/yara/scan_results.rb +224 -0
- data/lib/yara/scanner.rb +436 -45
- data/lib/yara/version.rb +5 -1
- data/lib/yara.rb +73 -15
- data/yara-ffi.gemspec +4 -4
- metadata +13 -15
- data/lib/yara/user_data.rb +0 -5
- data/lib/yara/yr_meta.rb +0 -10
- data/lib/yara/yr_namespace.rb +0 -5
- data/lib/yara/yr_rule.rb +0 -11
- data/lib/yara/yr_string.rb +0 -15
data/lib/yara/ffi.rb
CHANGED
@@ -1,116 +1,505 @@
|
|
1
|
-
require_relative "yr_meta"
|
2
|
-
require_relative "yr_namespace"
|
3
|
-
require_relative "yr_string"
|
4
|
-
require_relative "yr_rule"
|
5
|
-
require_relative "user_data"
|
6
|
-
|
7
1
|
module Yara
|
8
|
-
# FFI bindings to
|
2
|
+
# Internal: Low-level FFI bindings to the YARA-X C API.
|
3
|
+
#
|
4
|
+
# This module provides direct Ruby FFI bindings to the yara_x_capi library.
|
5
|
+
# It handles dynamic library loading with multiple fallback paths and exposes
|
6
|
+
# the raw C functions for rule compilation, scanning, and resource management.
|
7
|
+
#
|
8
|
+
# The FFI module is primarily used internally by higher-level classes like
|
9
|
+
# Scanner. Direct usage requires careful memory management and error handling.
|
10
|
+
#
|
11
|
+
# Examples
|
12
|
+
#
|
13
|
+
# # Direct FFI usage (not recommended for normal use)
|
14
|
+
# rules_ptr = FFI::MemoryPointer.new(:pointer)
|
15
|
+
# result = Yara::FFI.yrx_compile("rule test { condition: true }", rules_ptr)
|
16
|
+
# raise "Error: #{Yara::FFI.yrx_last_error}" unless result == Yara::FFI::YRX_SUCCESS
|
9
17
|
module FFI
|
10
18
|
extend ::FFI::Library
|
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
|
-
|
19
|
+
|
20
|
+
# Internal: Library search paths for yara_x_capi shared library.
|
21
|
+
#
|
22
|
+
# These paths are tried in order to locate the YARA-X C API library.
|
23
|
+
# The first successful load is used. This supports various deployment
|
24
|
+
# scenarios including system packages, Docker containers, and CI environments.
|
25
|
+
library_paths = [
|
26
|
+
"yara_x_capi", # System library (preferred)
|
27
|
+
"/usr/local/lib/x86_64-linux-gnu/libyara_x_capi.so", # GitHub Actions/CI
|
28
|
+
"/usr/local/lib/aarch64-linux-gnu/libyara_x_capi.so", # Local Docker (ARM)
|
29
|
+
"/usr/local/lib/libyara_x_capi.so", # Generic fallback
|
30
|
+
"libyara_x_capi" # Final fallback
|
31
|
+
]
|
32
|
+
|
33
|
+
library_loaded = false
|
34
|
+
library_paths.each do |path|
|
35
|
+
begin
|
36
|
+
ffi_lib path
|
37
|
+
library_loaded = true
|
38
|
+
break
|
39
|
+
rescue LoadError
|
40
|
+
next
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
raise LoadError, "Could not load yara_x_capi library from any of: #{library_paths.join(', ')}" unless library_loaded
|
45
|
+
|
46
|
+
# Public: Compile YARA rule source into executable rules object.
|
47
|
+
#
|
48
|
+
# This is the primary compilation function that parses YARA rule source code
|
49
|
+
# and creates an optimized rules object for scanning. The rules object must
|
50
|
+
# be freed with yrx_rules_destroy when no longer needed.
|
51
|
+
#
|
52
|
+
# src - A String containing YARA rule source code
|
53
|
+
# rules - A FFI::MemoryPointer that will receive the rules object pointer
|
54
|
+
#
|
55
|
+
# Examples
|
56
|
+
#
|
57
|
+
# rules_ptr = FFI::MemoryPointer.new(:pointer)
|
58
|
+
# result = Yara::FFI.yrx_compile(rule_source, rules_ptr)
|
59
|
+
#
|
60
|
+
# Returns an Integer result code (YRX_SUCCESS on success).
|
61
|
+
# C Signature: enum YRX_RESULT yrx_compile(const char *src, struct YRX_RULES **rules)
|
62
|
+
attach_function :yrx_compile, [:string, :pointer], :int
|
63
|
+
|
64
|
+
# Public: Get the last error message from YARA-X operations.
|
65
|
+
#
|
66
|
+
# When any YARA-X function returns an error code, this function provides
|
67
|
+
# a human-readable description of what went wrong. The returned string
|
68
|
+
# is managed by YARA-X and should not be freed.
|
69
|
+
#
|
70
|
+
# Examples
|
71
|
+
#
|
72
|
+
# if result != YRX_SUCCESS
|
73
|
+
# error_msg = Yara::FFI.yrx_last_error
|
74
|
+
# raise "YARA Error: #{error_msg}"
|
75
|
+
# end
|
76
|
+
#
|
77
|
+
# Returns a String containing the last error message.
|
78
|
+
# C Signature: const char* yrx_last_error(void)
|
79
|
+
attach_function :yrx_last_error, [], :string
|
80
|
+
|
81
|
+
# Public: Free memory associated with a compiled rules object.
|
82
|
+
#
|
83
|
+
# This function must be called to free the memory allocated by yrx_compile.
|
84
|
+
# After calling this function, the rules pointer becomes invalid and should
|
85
|
+
# not be used.
|
86
|
+
#
|
87
|
+
# rules - A Pointer to the rules object to destroy
|
88
|
+
#
|
89
|
+
# Examples
|
90
|
+
#
|
91
|
+
# Yara::FFI.yrx_rules_destroy(rules_ptr)
|
92
|
+
#
|
93
|
+
# Returns nothing.
|
94
|
+
# C Signature: void yrx_rules_destroy(struct YRX_RULES *rules)
|
95
|
+
attach_function :yrx_rules_destroy, [:pointer], :void
|
96
|
+
|
97
|
+
# Public: Create a scanner object from compiled rules.
|
98
|
+
#
|
99
|
+
# A scanner is needed to perform actual pattern matching against data.
|
100
|
+
# Multiple scanners can be created from the same rules object to enable
|
101
|
+
# concurrent scanning. The scanner must be freed with yrx_scanner_destroy.
|
102
|
+
#
|
103
|
+
# rules - A Pointer to compiled rules object
|
104
|
+
# scanner - A FFI::MemoryPointer that will receive the scanner object pointer
|
105
|
+
#
|
106
|
+
# Examples
|
107
|
+
#
|
108
|
+
# scanner_ptr = FFI::MemoryPointer.new(:pointer)
|
109
|
+
# result = Yara::FFI.yrx_scanner_create(rules_ptr, scanner_ptr)
|
110
|
+
#
|
111
|
+
# Returns an Integer result code (YRX_SUCCESS on success).
|
112
|
+
# C Signature: enum YRX_RESULT yrx_scanner_create(const struct YRX_RULES *rules, struct YRX_SCANNER **scanner)
|
113
|
+
attach_function :yrx_scanner_create, [:pointer, :pointer], :int
|
114
|
+
|
115
|
+
# Public: Free memory associated with a scanner object.
|
116
|
+
#
|
117
|
+
# This function must be called to free the memory allocated by
|
118
|
+
# yrx_scanner_create. After calling this function, the scanner pointer
|
119
|
+
# becomes invalid and should not be used.
|
120
|
+
#
|
121
|
+
# scanner - A Pointer to the scanner object to destroy
|
122
|
+
#
|
123
|
+
# Examples
|
124
|
+
#
|
125
|
+
# Yara::FFI.yrx_scanner_destroy(scanner_ptr)
|
126
|
+
#
|
127
|
+
# Returns nothing.
|
128
|
+
# C Signature: void yrx_scanner_destroy(struct YRX_SCANNER *scanner)
|
129
|
+
attach_function :yrx_scanner_destroy, [:pointer], :void
|
130
|
+
|
131
|
+
# Internal: Callback function type for rule matching events.
|
132
|
+
#
|
133
|
+
# This callback is invoked for each rule that matches during scanning.
|
134
|
+
# The callback receives pointers to the matching rule and optional user data.
|
135
|
+
#
|
136
|
+
# rule - A Pointer to the YRX_RULE structure
|
137
|
+
# user_data - A Pointer to optional user-provided data
|
138
|
+
#
|
139
|
+
# C Signature: typedef void (*YRX_ON_MATCHING_RULE)(const struct YRX_RULE *rule, void *user_data)
|
140
|
+
callback :matching_rule_callback, [:pointer, :pointer], :void
|
141
|
+
|
142
|
+
# Public: Set callback for handling rule matches during scanning.
|
143
|
+
#
|
144
|
+
# This function registers a callback that will be invoked each time a rule
|
145
|
+
# matches during scanning. The callback can extract information about the
|
146
|
+
# matching rule and optionally halt scanning.
|
147
|
+
#
|
148
|
+
# scanner - A Pointer to the scanner object
|
149
|
+
# callback - A Proc matching the matching_rule_callback signature
|
150
|
+
# user_data - A Pointer to optional data passed to callback (can be nil)
|
151
|
+
#
|
152
|
+
# Examples
|
153
|
+
#
|
154
|
+
# callback = proc { |rule_ptr, user_data| puts "Rule matched!" }
|
155
|
+
# result = Yara::FFI.yrx_scanner_on_matching_rule(scanner_ptr, callback, nil)
|
156
|
+
#
|
157
|
+
# Returns an Integer result code (YRX_SUCCESS on success).
|
158
|
+
# C Signature: enum YRX_RESULT yrx_scanner_on_matching_rule(struct YRX_SCANNER *scanner, YRX_ON_MATCHING_RULE callback, void *user_data)
|
159
|
+
attach_function :yrx_scanner_on_matching_rule, [:pointer, :matching_rule_callback, :pointer], :int
|
160
|
+
|
161
|
+
# Public: Scan data using the configured scanner and rules.
|
162
|
+
#
|
163
|
+
# This function performs pattern matching against the provided data using
|
164
|
+
# all rules in the scanner. Any matching rules trigger the registered
|
165
|
+
# callback function. The data is scanned as binary regardless of content.
|
166
|
+
#
|
167
|
+
# scanner - A Pointer to the scanner object
|
168
|
+
# data - A Pointer to the data buffer to scan
|
169
|
+
# len - A size_t indicating the length of data in bytes
|
170
|
+
#
|
171
|
+
# Examples
|
172
|
+
#
|
173
|
+
# data_ptr = FFI::MemoryPointer.new(:char, data.bytesize)
|
174
|
+
# data_ptr.put_bytes(0, data)
|
175
|
+
# result = Yara::FFI.yrx_scanner_scan(scanner_ptr, data_ptr, data.bytesize)
|
176
|
+
#
|
177
|
+
# Returns an Integer result code (YRX_SUCCESS on success).
|
178
|
+
# C Signature: enum YRX_RESULT yrx_scanner_scan(struct YRX_SCANNER *scanner, const uint8_t *data, size_t len)
|
179
|
+
attach_function :yrx_scanner_scan, [:pointer, :pointer, :size_t], :int
|
180
|
+
|
181
|
+
# Public: Set timeout (in milliseconds) for a scanner.
|
182
|
+
#
|
183
|
+
# scanner - A Pointer to the scanner object
|
184
|
+
# timeout - A uint64_t value representing timeout in milliseconds
|
185
|
+
#
|
186
|
+
# Returns an Integer result code (YRX_SUCCESS on success).
|
187
|
+
# C Signature: enum YRX_RESULT yrx_scanner_set_timeout(struct YRX_SCANNER *scanner, uint64_t timeout)
|
188
|
+
attach_function :yrx_scanner_set_timeout, [:pointer, :ulong_long], :int
|
189
|
+
|
190
|
+
# Public: Set a global string variable for the scanner.
|
191
|
+
# C Signature: enum YRX_RESULT yrx_scanner_set_global_str(struct YRX_SCANNER *scanner, const char *ident, const char *value)
|
192
|
+
attach_function :yrx_scanner_set_global_str, [:pointer, :string, :string], :int
|
193
|
+
|
194
|
+
# Public: Set a global boolean variable for the scanner.
|
195
|
+
# C Signature: enum YRX_RESULT yrx_scanner_set_global_bool(struct YRX_SCANNER *scanner, const char *ident, bool value)
|
196
|
+
attach_function :yrx_scanner_set_global_bool, [:pointer, :string, :bool], :int
|
197
|
+
|
198
|
+
# Public: Set a global integer variable for the scanner.
|
199
|
+
# C Signature: enum YRX_RESULT yrx_scanner_set_global_int(struct YRX_SCANNER *scanner, const char *ident, int64_t value)
|
200
|
+
attach_function :yrx_scanner_set_global_int, [:pointer, :string, :long_long], :int
|
201
|
+
|
202
|
+
# Public: Set a global float variable for the scanner.
|
203
|
+
# C Signature: enum YRX_RESULT yrx_scanner_set_global_float(struct YRX_SCANNER *scanner, const char *ident, double value)
|
204
|
+
attach_function :yrx_scanner_set_global_float, [:pointer, :string, :double], :int
|
205
|
+
|
206
|
+
# YRX_COMPILER APIs
|
207
|
+
# Create a compiler: enum YRX_RESULT yrx_compiler_create(uint32_t flags, struct YRX_COMPILER **compiler)
|
208
|
+
attach_function :yrx_compiler_create, [:uint32, :pointer], :int
|
209
|
+
|
210
|
+
# Destroy a compiler: void yrx_compiler_destroy(struct YRX_COMPILER *compiler)
|
211
|
+
attach_function :yrx_compiler_destroy, [:pointer], :void
|
212
|
+
|
213
|
+
# Add source: enum YRX_RESULT yrx_compiler_add_source(struct YRX_COMPILER *compiler, const char *src)
|
214
|
+
attach_function :yrx_compiler_add_source, [:pointer, :string], :int
|
215
|
+
|
216
|
+
# Add source with origin: enum YRX_RESULT yrx_compiler_add_source_with_origin(struct YRX_COMPILER *compiler, const char *src, const char *origin)
|
217
|
+
attach_function :yrx_compiler_add_source_with_origin, [:pointer, :string, :string], :int
|
218
|
+
|
219
|
+
# Define globals on compiler
|
220
|
+
attach_function :yrx_compiler_define_global_str, [:pointer, :string, :string], :int
|
221
|
+
attach_function :yrx_compiler_define_global_bool, [:pointer, :string, :bool], :int
|
222
|
+
attach_function :yrx_compiler_define_global_int, [:pointer, :string, :long_long], :int
|
223
|
+
attach_function :yrx_compiler_define_global_float, [:pointer, :string, :double], :int
|
224
|
+
|
225
|
+
# Build compiler into rules: struct YRX_RULES *yrx_compiler_build(struct YRX_COMPILER *compiler)
|
226
|
+
attach_function :yrx_compiler_build, [:pointer], :pointer
|
227
|
+
|
228
|
+
# YRX_BUFFER utilities
|
229
|
+
# C Signature: void yrx_buffer_destroy(struct YRX_BUFFER *buf)
|
230
|
+
attach_function :yrx_buffer_destroy, [:pointer], :void
|
231
|
+
|
232
|
+
# Compiler diagnostics as JSON
|
233
|
+
# C Signature: enum YRX_RESULT yrx_compiler_errors_json(struct YRX_COMPILER *compiler, struct YRX_BUFFER **buf)
|
234
|
+
attach_function :yrx_compiler_errors_json, [:pointer, :pointer], :int
|
235
|
+
|
236
|
+
# C Signature: enum YRX_RESULT yrx_compiler_warnings_json(struct YRX_COMPILER *compiler, struct YRX_BUFFER **buf)
|
237
|
+
attach_function :yrx_compiler_warnings_json, [:pointer, :pointer], :int
|
238
|
+
|
239
|
+
# Serialize rules into a YRX_BUFFER
|
240
|
+
# C Signature: enum YRX_RESULT yrx_rules_serialize(const struct YRX_RULES *rules, struct YRX_BUFFER **buf)
|
241
|
+
attach_function :yrx_rules_serialize, [:pointer, :pointer], :int
|
242
|
+
|
243
|
+
# Deserialize rules from bytes
|
244
|
+
# C Signature: enum YRX_RESULT yrx_rules_deserialize(const uint8_t *data, size_t len, struct YRX_RULES **rules)
|
245
|
+
attach_function :yrx_rules_deserialize, [:pointer, :size_t, :pointer], :int
|
246
|
+
|
247
|
+
# Struct mapping for YRX_BUFFER
|
248
|
+
class YRX_BUFFER < ::FFI::Struct
|
249
|
+
layout :data, :pointer,
|
250
|
+
:length, :size_t
|
251
|
+
end
|
252
|
+
|
253
|
+
# Struct mapping for YRX_MATCH
|
254
|
+
class YRX_MATCH < ::FFI::Struct
|
255
|
+
layout :offset, :size_t,
|
256
|
+
:length, :size_t
|
257
|
+
end
|
258
|
+
|
259
|
+
# Struct mapping for YRX_METADATA
|
260
|
+
# Note: The value field is a union. We'll use a simple approach
|
261
|
+
# and access the value as a pointer, then interpret based on type.
|
262
|
+
class YRX_METADATA < ::FFI::Struct
|
263
|
+
layout :identifier, :pointer,
|
264
|
+
:value_type, :int,
|
265
|
+
:value, [:char, 16] # Union represented as byte array (largest possible union member)
|
266
|
+
end
|
267
|
+
|
268
|
+
# Struct mapping for YRX_METADATA_BYTES
|
269
|
+
class YRX_METADATA_BYTES < ::FFI::Struct
|
270
|
+
layout :length, :size_t,
|
271
|
+
:data, :pointer
|
272
|
+
end
|
273
|
+
|
274
|
+
# Public: Extract the identifier (name) from a rule object.
|
275
|
+
#
|
276
|
+
# This function retrieves the rule name from a YRX_RULE pointer, typically
|
277
|
+
# called from within a matching rule callback. The identifier is returned
|
278
|
+
# as a pointer and length rather than a null-terminated string.
|
279
|
+
#
|
280
|
+
# rule - A Pointer to the YRX_RULE structure
|
281
|
+
# ident - A FFI::MemoryPointer that will receive the identifier pointer
|
282
|
+
# len - A FFI::MemoryPointer that will receive the identifier length
|
283
|
+
#
|
284
|
+
# Examples
|
285
|
+
#
|
286
|
+
# ident_ptr = FFI::MemoryPointer.new(:pointer)
|
287
|
+
# len_ptr = FFI::MemoryPointer.new(:size_t)
|
288
|
+
# result = Yara::FFI.yrx_rule_identifier(rule_ptr, ident_ptr, len_ptr)
|
289
|
+
#
|
290
|
+
# Returns an Integer result code (YRX_SUCCESS on success).
|
291
|
+
# C Signature: enum YRX_RESULT yrx_rule_identifier(const struct YRX_RULE *rule, const uint8_t **ident, size_t *len)
|
292
|
+
attach_function :yrx_rule_identifier, [:pointer, :pointer, :pointer], :int
|
293
|
+
|
294
|
+
# Internal: Callback function type for metadata iteration.
|
295
|
+
#
|
296
|
+
# This callback is invoked for each metadata entry during rule metadata
|
297
|
+
# iteration. The callback receives pointers to the metadata and user data.
|
298
|
+
#
|
299
|
+
# metadata - A Pointer to the YRX_METADATA structure
|
300
|
+
# user_data - A Pointer to optional user-provided data
|
301
|
+
#
|
302
|
+
# C Signature: typedef void (*YRX_METADATA_CALLBACK)(const struct YRX_METADATA *metadata, void *user_data)
|
303
|
+
callback :metadata_callback, [:pointer, :pointer], :void
|
304
|
+
|
305
|
+
# Public: Iterate through all metadata entries in a rule.
|
306
|
+
#
|
307
|
+
# This function calls the provided callback for each metadata key-value pair
|
308
|
+
# defined in the rule. Metadata includes information like author, description,
|
309
|
+
# and custom tags defined in the rule's meta section.
|
310
|
+
#
|
311
|
+
# rule - A Pointer to the YRX_RULE structure
|
312
|
+
# callback - A Proc matching the metadata_callback signature
|
313
|
+
# user_data - A Pointer to optional data passed to callback (can be nil)
|
314
|
+
#
|
315
|
+
# Examples
|
316
|
+
#
|
317
|
+
# callback = proc { |metadata_ptr, user_data| puts "Found metadata" }
|
318
|
+
# result = Yara::FFI.yrx_rule_iter_metadata(rule_ptr, callback, nil)
|
319
|
+
#
|
320
|
+
# Returns an Integer result code (YRX_SUCCESS on success).
|
321
|
+
# C Signature: enum YRX_RESULT yrx_rule_iter_metadata(const struct YRX_RULE *rule, YRX_METADATA_CALLBACK callback, void *user_data)
|
322
|
+
attach_function :yrx_rule_iter_metadata, [:pointer, :metadata_callback, :pointer], :int
|
323
|
+
|
324
|
+
# Internal: Callback function type for pattern iteration.
|
325
|
+
#
|
326
|
+
# This callback is invoked for each pattern (string) during rule pattern
|
327
|
+
# iteration. The callback receives pointers to the pattern and user data.
|
328
|
+
#
|
329
|
+
# pattern - A Pointer to the YRX_PATTERN structure
|
330
|
+
# user_data - A Pointer to optional user-provided data
|
331
|
+
#
|
332
|
+
# C Signature: typedef void (*YRX_PATTERN_CALLBACK)(const struct YRX_PATTERN *pattern, void *user_data)
|
333
|
+
callback :pattern_callback, [:pointer, :pointer], :void
|
334
|
+
|
335
|
+
# Public: Iterate through all patterns (strings) in a rule.
|
336
|
+
#
|
337
|
+
# This function calls the provided callback for each string pattern defined
|
338
|
+
# in the rule. Patterns are the actual search terms that YARA looks for
|
339
|
+
# during scanning, defined in the rule's strings section.
|
340
|
+
#
|
341
|
+
# rule - A Pointer to the YRX_RULE structure
|
342
|
+
# callback - A Proc matching the pattern_callback signature
|
343
|
+
# user_data - A Pointer to optional data passed to callback (can be nil)
|
344
|
+
#
|
345
|
+
# Examples
|
346
|
+
#
|
347
|
+
# callback = proc { |pattern_ptr, user_data| puts "Found pattern" }
|
348
|
+
# result = Yara::FFI.yrx_rule_iter_patterns(rule_ptr, callback, nil)
|
349
|
+
#
|
350
|
+
# Returns an Integer result code (YRX_SUCCESS on success).
|
351
|
+
# C Signature: enum YRX_RESULT yrx_rule_iter_patterns(const struct YRX_RULE *rule, YRX_PATTERN_CALLBACK callback, void *user_data)
|
352
|
+
attach_function :yrx_rule_iter_patterns, [:pointer, :pattern_callback, :pointer], :int
|
353
|
+
|
354
|
+
# Public: Extract the identifier (name) from a pattern object.
|
355
|
+
#
|
356
|
+
# This function retrieves the pattern identifier from a YRX_PATTERN pointer,
|
357
|
+
# typically called from within a pattern iteration callback. Pattern
|
358
|
+
# identifiers are the variable names like $string1, $hex_pattern, etc.
|
359
|
+
#
|
360
|
+
# pattern - A Pointer to the YRX_PATTERN structure
|
361
|
+
# ident - A FFI::MemoryPointer that will receive the identifier pointer
|
362
|
+
# len - A FFI::MemoryPointer that will receive the identifier length
|
363
|
+
#
|
364
|
+
# Examples
|
365
|
+
#
|
366
|
+
# ident_ptr = FFI::MemoryPointer.new(:pointer)
|
367
|
+
# len_ptr = FFI::MemoryPointer.new(:size_t)
|
368
|
+
# result = Yara::FFI.yrx_pattern_identifier(pattern_ptr, ident_ptr, len_ptr)
|
369
|
+
#
|
370
|
+
# Returns an Integer result code (YRX_SUCCESS on success).
|
371
|
+
# C Signature: enum YRX_RESULT yrx_pattern_identifier(const struct YRX_PATTERN *pattern, const uint8_t **ident, size_t *len)
|
372
|
+
attach_function :yrx_pattern_identifier, [:pointer, :pointer, :pointer], :int
|
373
|
+
|
374
|
+
# Internal: Callback function type for match iteration.
|
375
|
+
#
|
376
|
+
# This callback is invoked for each match found during pattern match
|
377
|
+
# iteration. The callback receives pointers to the match and user data.
|
378
|
+
#
|
379
|
+
# match - A Pointer to the YRX_MATCH structure
|
380
|
+
# user_data - A Pointer to optional user-provided data
|
381
|
+
#
|
382
|
+
# C Signature: typedef void (*YRX_MATCH_CALLBACK)(const struct YRX_MATCH *match, void *user_data)
|
383
|
+
callback :match_callback, [:pointer, :pointer], :void
|
384
|
+
|
385
|
+
# Internal: Callback function type for tag iteration.
|
386
|
+
#
|
387
|
+
# This callback is invoked for each tag during rule tag iteration.
|
388
|
+
# The callback receives a pointer to the tag string and user data.
|
389
|
+
#
|
390
|
+
# tag - A Pointer to null-terminated string representing the tag
|
391
|
+
# user_data - A Pointer to optional user-provided data
|
392
|
+
#
|
393
|
+
# C Signature: typedef void (*YRX_TAG_CALLBACK)(const char *tag, void *user_data)
|
394
|
+
callback :tag_callback, [:pointer, :pointer], :void
|
395
|
+
|
396
|
+
# Public: Iterate through all matches for a specific pattern.
|
397
|
+
#
|
398
|
+
# This function calls the provided callback for each match found for the
|
399
|
+
# given pattern during scanning. Each match provides the offset and length
|
400
|
+
# of where the pattern matched in the scanned data.
|
401
|
+
#
|
402
|
+
# pattern - A Pointer to the YRX_PATTERN structure
|
403
|
+
# callback - A Proc matching the match_callback signature
|
404
|
+
# user_data - A Pointer to optional data passed to callback (can be nil)
|
405
|
+
#
|
406
|
+
# Examples
|
407
|
+
#
|
408
|
+
# callback = proc { |match_ptr, user_data| puts "Found match" }
|
409
|
+
# result = Yara::FFI.yrx_pattern_iter_matches(pattern_ptr, callback, nil)
|
410
|
+
#
|
411
|
+
# Returns an Integer result code (YRX_SUCCESS on success).
|
412
|
+
# C Signature: enum YRX_RESULT yrx_pattern_iter_matches(const struct YRX_PATTERN *pattern, YRX_MATCH_CALLBACK callback, void *user_data)
|
413
|
+
attach_function :yrx_pattern_iter_matches, [:pointer, :match_callback, :pointer], :int
|
414
|
+
|
415
|
+
# Public: Extract the namespace from a rule object.
|
416
|
+
#
|
417
|
+
# This function retrieves the rule namespace from a YRX_RULE pointer.
|
418
|
+
# The namespace is returned as a pointer and length rather than a
|
419
|
+
# null-terminated string.
|
420
|
+
#
|
421
|
+
# rule - A Pointer to the YRX_RULE structure
|
422
|
+
# ns - A FFI::MemoryPointer that will receive the namespace pointer
|
423
|
+
# len - A FFI::MemoryPointer that will receive the namespace length
|
424
|
+
#
|
425
|
+
# Examples
|
426
|
+
#
|
427
|
+
# ns_ptr = FFI::MemoryPointer.new(:pointer)
|
428
|
+
# len_ptr = FFI::MemoryPointer.new(:size_t)
|
429
|
+
# result = Yara::FFI.yrx_rule_namespace(rule_ptr, ns_ptr, len_ptr)
|
430
|
+
#
|
431
|
+
# Returns an Integer result code (YRX_SUCCESS on success).
|
432
|
+
# C Signature: enum YRX_RESULT yrx_rule_namespace(const struct YRX_RULE *rule, const uint8_t **ns, size_t *len)
|
433
|
+
attach_function :yrx_rule_namespace, [:pointer, :pointer, :pointer], :int
|
434
|
+
|
435
|
+
# Public: Iterate through all tags in a rule.
|
436
|
+
#
|
437
|
+
# This function calls the provided callback for each tag defined
|
438
|
+
# in the rule. Tags are used for categorizing and organizing rules.
|
439
|
+
#
|
440
|
+
# rule - A Pointer to the YRX_RULE structure
|
441
|
+
# callback - A Proc matching the tag_callback signature
|
442
|
+
# user_data - A Pointer to optional data passed to callback (can be nil)
|
443
|
+
#
|
444
|
+
# Examples
|
445
|
+
#
|
446
|
+
# callback = proc { |tag_ptr, user_data| puts "Found tag: #{tag_ptr.read_string}" }
|
447
|
+
# result = Yara::FFI.yrx_rule_iter_tags(rule_ptr, callback, nil)
|
448
|
+
#
|
449
|
+
# Returns an Integer result code (YRX_SUCCESS on success).
|
450
|
+
# C Signature: enum YRX_RESULT yrx_rule_iter_tags(const struct YRX_RULE *rule, YRX_TAG_CALLBACK callback, void *user_data)
|
451
|
+
attach_function :yrx_rule_iter_tags, [:pointer, :tag_callback, :pointer], :int
|
452
|
+
|
453
|
+
# Public: YARA-X result codes for operation status.
|
454
|
+
#
|
455
|
+
# These constants represent the possible return values from YARA-X functions.
|
456
|
+
# YRX_SUCCESS (0) indicates successful operation, while other values indicate
|
457
|
+
# various error conditions that can be interpreted using yrx_last_error.
|
458
|
+
|
459
|
+
# Public: Operation completed successfully.
|
460
|
+
YRX_SUCCESS = 0
|
461
|
+
|
462
|
+
# Public: YARA rule syntax error during compilation.
|
463
|
+
YRX_SYNTAX_ERROR = 1
|
464
|
+
|
465
|
+
# Public: Variable definition or reference error.
|
466
|
+
YRX_VARIABLE_ERROR = 2
|
467
|
+
|
468
|
+
# Public: Error during scanning operation.
|
469
|
+
YRX_SCAN_ERROR = 3
|
470
|
+
|
471
|
+
# Public: Scanning operation timed out.
|
472
|
+
YRX_SCAN_TIMEOUT = 4
|
473
|
+
|
474
|
+
# Public: Invalid argument passed to function.
|
475
|
+
YRX_INVALID_ARGUMENT = 5
|
476
|
+
|
477
|
+
# Public: Metadata type constants for YRX_METADATA_TYPE enum.
|
478
|
+
#
|
479
|
+
# These constants represent the possible types of metadata values in YARA-X.
|
480
|
+
# They correspond to the YRX_METADATA_TYPE enum values in the C API.
|
481
|
+
|
482
|
+
# Public: 64-bit signed integer metadata value.
|
483
|
+
YRX_METADATA_TYPE_I64 = 0
|
484
|
+
|
485
|
+
# Public: 64-bit floating point metadata value.
|
486
|
+
YRX_METADATA_TYPE_F64 = 1
|
487
|
+
|
488
|
+
# Public: Boolean metadata value.
|
489
|
+
YRX_METADATA_TYPE_BOOLEAN = 2
|
490
|
+
|
491
|
+
# Public: String metadata value.
|
492
|
+
YRX_METADATA_TYPE_STRING = 3
|
493
|
+
|
494
|
+
# Public: Bytes metadata value.
|
495
|
+
YRX_METADATA_TYPE_BYTES = 4
|
496
|
+
|
497
|
+
# Public: Alternative naming following YARA-X C API documentation.
|
498
|
+
# Maps to the same values as above for compatibility.
|
499
|
+
YRX_I64 = 0
|
500
|
+
YRX_F64 = 1
|
501
|
+
YRX_BOOLEAN = 2
|
502
|
+
YRX_STRING = 3
|
503
|
+
YRX_BYTES = 4
|
115
504
|
end
|
116
505
|
end
|