YaraFFI.pm

filter: Statementstmt 19.3% 44 / 227 Branchbran 0.0% 0 / 132 Conditioncond 0.0% 0 / 105 MC/DCmcdc 0.0% 0 / 66 Subroutinesub 62.5% 15 / 24 Podpod n/a totaltotal 10.6% 59 / 554 CC 110 SCAR 65.9
CC
110
Cov
9%
__ANON__
1406.0
_extract_metadata_with_records
992.0
_extract_string_matches_with_records
533.1
CRAP 730.6
↑ Summary YaraFFI/Event.pm »
1 package YaraFFI;
2  
3 $YaraFFI::VERSION = '0.07';
4 $YaraFFI::AUTHORITY = 'cpan:MANWAR';
5  
6 =head1 NAME
7  
8 YaraFFI - Minimal Perl FFI bindings for the YARA malware scanning engine
9  
10 =head1 VERSION
11  
12 Version 0.07
13  
14 =head1 SYNOPSIS
15  
16 use YaraFFI;
17  
18 my $rules = <<'YARA';
19 rule HelloWorld {
20 meta:
21 author = "John Doe"
22 description = "Test rule"
23 strings:
24 $a = "hello" ascii
25 condition:
26 $a
27 }
28 YARA
29  
30 my $yara = YaraFFI->new;
31 $yara->compile($rules) or die "compile failed";
32  
33 $yara->scan_buffer("hello hacker", sub {
34 my ($event) = @_;
35 print "Event: $event->{event}\n";
36 print "Rule: $event->{rule}\n" if $event->{rule};
37  
38 if ($event->{event} eq 'rule_match') {
39 if ($event->{metadata}) {
40 print "Metadata:\n";
41 for my $key (keys %{$event->{metadata}}) {
42 print " $key = $event->{metadata}{$key}\n";
43 }
44 }
45 }
46  
47 if ($event->{event} eq 'string_match') {
48 print "String: $event->{string_id}\n";
49 print "Offsets: " . join(", ", @{$event->{offsets}}) . "\n" if $event->{offsets};
50 }
51  
52 if ($event->{event} eq 'rule_not_match') {
53 print "Rule did not match: $event->{rule}\n";
54 }
55  
56 if ($event->{event} eq 'import_module') {
57 print "Module imported: $event->{module_name}\n";
58 }
59  
60 if ($event->{event} eq 'scan_finished') {
61 print "Scan completed\n";
62 }
63 });
64  
65 =head1 DESCRIPTION
66  
67 C<YaraFFI> provides lightweight C<FFI> bindings to C<libyara>, allowing you to compile
68 C<YARA> rules from strings and scan memory buffers or files. It supports all major
69 callback events including C<rule_match>, C<rule_not_match>, C<string_match>,
70 C<import_module>, and C<scan_finished> via a Perl callback.
71  
72 Currently it focuses on correctness and minimal functionality, not full C<YARA>
73 feature coverage.
74  
75 For more information, please follow the L<official documentation|https://yara.readthedocs.io/en/latest>.
76  
77 =head1 FEATURES
78  
79 =over 4
80  
81 =item * Compile C<YARA> rules from string
82  
83 =item * Scan in-memory buffers (C<scan_buffer>)
84  
85 =item * Scan files (C<scan_file>)
86  
87 =item * Events passed to callback: C<rule_match>, C<rule_not_match>, C<string_match>, C<import_module>, C<scan_finished>
88  
89 =item * Match offset reporting for string matches (when enabled)
90  
91 =item * Rule metadata extraction (when enabled)
92  
93 =back
94  
95 =head1 NOT YET IMPLEMENTED
96  
97 =over 4
98  
99 =item * No support for YARA modules or external variables
100  
101 =item * Scan flags currently hardcoded to 0
102  
103 =item * Metadata and offset extraction disabled by default due to YARA version compatibility
104  
105 =back
106  
107 =cut
108  
109 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
111 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 1 ▶use YaraFFI::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
113 1 ▶use YaraFFI::Record::YR_RULE;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 1 ▶use YaraFFI::Record::YR_META;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 1 ▶use YaraFFI::Record::YR_STRING;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 1 ▶use YaraFFI::Record::YR_MATCH;
Subroutine BEGIN: called CC=1 SCAR=0.0
117 1 ▶use FFI::Platypus 2.00;
Subroutine BEGIN: called CC=1 SCAR=0.0
118 1 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
119  
120 my $ffi = FFI::Platypus->new(api => 2);
121 $ffi->lib("libyara.so");
122  
123 # Attach YARA functions
124 $ffi->attach('yr_initialize' => [] => 'int');
125 $ffi->attach('yr_finalize' => [] => 'int');
126 $ffi->attach('yr_compiler_create' => ['opaque*'] => 'int');
127 $ffi->attach('yr_compiler_destroy' => ['opaque' ] => 'void');
128 $ffi->attach('yr_compiler_add_string' => ['opaque', 'string', 'string'] => 'int');
129 $ffi->attach('yr_compiler_get_rules' => ['opaque', 'opaque*'] => 'int');
130 $ffi->attach('yr_rules_destroy' => ['opaque'] => 'void');
131  
132 # Callback signature: int callback(YR_SCAN_CONTEXT* context, int message, void* message_data, void* user_data)
133 $ffi->attach('yr_rules_scan_mem' => ['opaque', 'opaque', 'size_t', 'int', '(opaque, int, opaque, opaque)->int', 'opaque', 'int'] => 'int');
134  
135 # Define constants for YARA callback messages
136 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
137 1 CALLBACK_MSG_RULE_MATCHING => 1,
138 CALLBACK_MSG_RULE_NOT_MATCHING => 2,
139 CALLBACK_MSG_SCAN_FINISHED => 3,
140 CALLBACK_MSG_IMPORT_MODULE => 4,
141 CALLBACK_CONTINUE => 0,
142 ERROR_SUCCESS => 0,
143  
144 # Metadata types
145 META_TYPE_INTEGER => 1,
146 META_TYPE_STRING => 2,
147 META_TYPE_BOOLEAN => 3,
148 1 };
149  
150 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
151 0 my ($class) = @_;
152 0 yr_initialize();
153 0 return bless { rules => undef }, $class;
154 }
155  
156 0 ▶sub compile {
Subroutine compile: not called CC=4 SCAR=30.0
157 0 my ($self, $rule_str) = @_;
158 0 my $compiler;
159 0 my $res = yr_compiler_create(\$compiler);
160 0 ▶ return 0 if $res != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $res != 0 0 0 0
161  
162 0 $res = yr_compiler_add_string($compiler, $rule_str, undef);
163 0 ▶ return 0 if $res != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $res != 0 0 0 0
164  
165 0 my $rules;
166 0 $res = yr_compiler_get_rules($compiler, \$rules);
167 0 ▶ return 0 if $res != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $res != 0 0 0 0
168  
169 0 yr_compiler_destroy($compiler);
170 0 $self->{rules} = $rules;
171 0 return 1;
172 }
173  
174 0 ▶sub scan_file {
Subroutine scan_file: not called CC=2 SCAR=17.9
175 0 my ($self, $filename, $callback, %opts) = @_;
176 0 ▶ die "Compile rules first" unless $self->{rules};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"rules"} 0 0 0
177  
178 0 my $content = read_file($filename, binmode => ':raw');
179 0 return $self->scan_buffer($content, $callback, %opts);
180 }
181  
182 # Safe metadata extraction using FFI records
183 0 ▶sub _extract_metadata_with_records {
Subroutine _extract_metadata_with_records: not called CC=31 SCAR=69.0
184 0 my ($rule_ptr, $enable_extraction) = @_;
185 0 ▶ return undef unless $enable_extraction;
Branch: 1 branch
BranchTrueFalseTotal
unless $enable_extraction 0 0 0
186 0 ▶ return undef unless $rule_ptr && $rule_ptr > 4096;
Branch: 1 branch
BranchTrueFalseTotal
unless $rule_ptr and $rule_ptr > 4096 0 0 0
Condition0%0/3
$rule_ptr and $rule_ptr > 4096
!ll&&!rl&&r
000
MC/DC0%0/2
$rule_ptr and $rule_ptr > 4096
$rule_ptr $rule_ptr > 4096
Truth table0%0/3
A and B
A= $rule_ptrB= $rule_ptr > 4096
ABresult
0X0
100
111
187  
188 0 my %metadata;
189  
190 0 eval {
191 # Cast to YR_RULE record
192 0 my $rule = $ffi->cast('opaque', 'record(YaraFFI::Record::YR_RULE)*', $rule_ptr);
193 0 ▶ return unless $rule && $rule->metas;
Branch: 1 branch
BranchTrueFalseTotal
unless $rule and $rule->metas 0 0 0
Condition0%0/3
$rule and $rule->metas
!ll&&!rl&&r
000
MC/DC0%0/2
$rule and $rule->metas
$rule $rule->metas
Truth table0%0/3
A and B
A= $ruleB= $rule->metas
ABresult
0X0
100
111
194  
195 0 my $metas_ptr = $rule->metas;
196 0 ▶ return unless $metas_ptr > 4096;
Branch: 1 branch
BranchTrueFalseTotal
unless $metas_ptr > 4096 0 0 0
197  
198 # Iterate through metadata entries
199 0 for my $i (0..19) {
200 0 my $meta_ptr = $metas_ptr + ($i * 32); # Approximate YR_META size
201  
202 0 my $type_bytes = $ffi->cast('opaque', 'string(4)', $meta_ptr);
203 0 ▶ last unless defined $type_bytes && length($type_bytes) == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type_bytes and length $type_bytes == 4 0 0 0
Condition0%0/3
defined $type_bytes and length $type_bytes == 4
!ll&&!rl&&r
000
MC/DC0%0/2
defined $type_bytes and length $type_bytes == 4
defined $type_bytes length $type_bytes == 4
Truth table0%0/3
A and B
A= defined $type_bytesB= length $type_bytes == 4
ABresult
0X0
100
111
204  
205 0 my $type = unpack('L', $type_bytes);
206 0 ▶ last if $type == 0 || $type > 3;
Branch: 1 branch
BranchTrueFalseTotal
if $type == 0 or $type > 3 0 0 0
Condition0%0/3
$type == 0 or $type > 3
l!l&&r!l&&!r
000
MC/DC0%0/2
$type == 0 or $type > 3
$type == 0 $type > 3
Truth table0%0/3
A or B
A= $type == 0B= $type > 3
ABresult
1X1
011
000
207  
208 # Read identifier pointer
209 0 my $id_ptr_bytes = $ffi->cast('opaque', 'string(8)', $meta_ptr + 8);
210 0 ▶ next unless defined $id_ptr_bytes && length($id_ptr_bytes) == 8;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id_ptr_bytes and length $id_ptr_bytes == 8 0 0 0
Condition0%0/3
defined $id_ptr_bytes and length $id_ptr_bytes == 8
!ll&&!rl&&r
000
MC/DC0%0/2
defined $id_ptr_bytes and length $id_ptr_bytes == 8
defined $id_ptr_bytes length $id_ptr_bytes == 8
Truth table0%0/3
A and B
A= defined $id_ptr_bytesB= length $id_ptr_bytes == 8
ABresult
0X0
100
111
211  
212 0 my $id_ptr = unpack('Q', $id_ptr_bytes);
213 0 ▶ next unless $id_ptr > 4096;
Branch: 1 branch
BranchTrueFalseTotal
unless $id_ptr > 4096 0 0 0
214  
215 0 my $identifier = $ffi->cast('opaque', 'string', $id_ptr);
216 0 ▶ next unless $identifier && $identifier =~ /^[a-zA-Z_][a-zA-Z0-9_]{0,100}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $identifier and $identifier =~ /^[a-zA-Z_][a-zA-Z0-9_]{0,100}$/u 0 0 0
Condition0%0/3
$identifier and $identifier =~ /^[a-zA-Z_][a-zA-Z0-9_]{0,100}$/u
!ll&&!rl&&r
000
MC/DC0%0/2
$identifier and $identifier =~ /^[a-zA-Z_][a-zA-Z0-9_]{0,100}$/u
$identifier $identifier =~ /^[a-zA-Z_][a-zA-Z0-9_]{0,100}$/u
Truth table0%0/3
A and B
A= $identifierB= $identifier =~ /^[a-zA-Z_][a-zA-Z0-9_]{0,100}$/u
ABresult
0X0
100
111
217  
218 0 ▶ if ($type == META_TYPE_STRING) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type == 2) { } 0 0 0
elsif ($type == 1) { } 0 0 0
elsif ($type == 3) { } 0 0 0
219 0 my $str_ptr_bytes = $ffi->cast('opaque', 'string(8)', $meta_ptr + 16);
220 0 ▶ if (defined $str_ptr_bytes && length($str_ptr_bytes) == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str_ptr_bytes and length $str_ptr_bytes == 8) 0 0 0
Condition0%0/3
defined $str_ptr_bytes and length $str_ptr_bytes == 8
!ll&&!rl&&r
000
MC/DC0%0/2
defined $str_ptr_bytes and length $str_ptr_bytes == 8
defined $str_ptr_bytes length $str_ptr_bytes == 8
Truth table0%0/3
A and B
A= defined $str_ptr_bytesB= length $str_ptr_bytes == 8
ABresult
0X0
100
111
221 0 my $str_ptr = unpack('Q', $str_ptr_bytes);
222 0 ▶ if ($str_ptr > 4096) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str_ptr > 4096) 0 0 0
223 0 my $value = $ffi->cast('opaque', 'string', $str_ptr);
224 0 ▶ $metadata{$identifier} = $value if defined $value && length($value) < 1000;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and length $value < 1000 0 0 0
Condition0%0/3
defined $value and length $value < 1000
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and length $value < 1000
defined $value length $value < 1000
Truth table0%0/3
A and B
A= defined $valueB= length $value < 1000
ABresult
0X0
100
111
225 }
226 }
227 }
228 elsif ($type == META_TYPE_INTEGER) {
229 0 my $int_bytes = $ffi->cast('opaque', 'string(8)', $meta_ptr + 16);
230 0 ▶ if (defined $int_bytes && length($int_bytes) == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $int_bytes and length $int_bytes == 8) 0 0 0
Condition0%0/3
defined $int_bytes and length $int_bytes == 8
!ll&&!rl&&r
000
MC/DC0%0/2
defined $int_bytes and length $int_bytes == 8
defined $int_bytes length $int_bytes == 8
Truth table0%0/3
A and B
A= defined $int_bytesB= length $int_bytes == 8
ABresult
0X0
100
111
231 0 my $value = unpack('q', $int_bytes); # signed 64-bit
232 0 $metadata{$identifier} = $value;
233 }
234 }
235 elsif ($type == META_TYPE_BOOLEAN) {
236 0 my $bool_bytes = $ffi->cast('opaque', 'string(4)', $meta_ptr + 16);
237 0 ▶ if (defined $bool_bytes && length($bool_bytes) == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bool_bytes and length $bool_bytes == 4) 0 0 0
Condition0%0/3
defined $bool_bytes and length $bool_bytes == 4
!ll&&!rl&&r
000
MC/DC0%0/2
defined $bool_bytes and length $bool_bytes == 4
defined $bool_bytes length $bool_bytes == 4
Truth table0%0/3
A and B
A= defined $bool_bytesB= length $bool_bytes == 4
ABresult
0X0
100
111
238 0 my $value = unpack('L', $bool_bytes);
239 0 ▶ $metadata{$identifier} = $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
240 }
241 }
242 }
243 };
244  
245 0 ▶ return keys %metadata > 0 ? \%metadata : undef;
Branch: 1 branch
BranchTrueFalseTotal
keys %metadata > 0 ? : 0 0 0
246 }
247  
248 # Safe string match extraction using FFI records
249 0 ▶sub _extract_string_matches_with_records {
Subroutine _extract_string_matches_with_records: not called CC=25 SCAR=62.8
250 0 my ($rule_ptr, $enable_extraction) = @_;
251 0 ▶ return [] unless $enable_extraction;
Branch: 1 branch
BranchTrueFalseTotal
unless $enable_extraction 0 0 0
252 0 ▶ return [] unless $rule_ptr && $rule_ptr > 4096;
Branch: 1 branch
BranchTrueFalseTotal
unless $rule_ptr and $rule_ptr > 4096 0 0 0
Condition0%0/3
$rule_ptr and $rule_ptr > 4096
!ll&&!rl&&r
000
MC/DC0%0/2
$rule_ptr and $rule_ptr > 4096
$rule_ptr $rule_ptr > 4096
Truth table0%0/3
A and B
A= $rule_ptrB= $rule_ptr > 4096
ABresult
0X0
100
111
253  
254 0 my @strings;
255  
256 0 eval {
257 # Cast to YR_RULE record
258 0 my $rule = $ffi->cast('opaque', 'record(YaraFFI::Record::YR_RULE)*', $rule_ptr);
259 0 ▶ return unless $rule && $rule->strings;
Branch: 1 branch
BranchTrueFalseTotal
unless $rule and $rule->strings 0 0 0
Condition0%0/3
$rule and $rule->strings
!ll&&!rl&&r
000
MC/DC0%0/2
$rule and $rule->strings
$rule $rule->strings
Truth table0%0/3
A and B
A= $ruleB= $rule->strings
ABresult
0X0
100
111
260  
261 0 my $strings_ptr = $rule->strings;
262 0 ▶ return unless $strings_ptr > 4096;
Branch: 1 branch
BranchTrueFalseTotal
unless $strings_ptr > 4096 0 0 0
263  
264 # Iterate through string entries
265 0 for my $i (0..49) {
266 0 my $string_base = $strings_ptr + ($i * 64); # Approximate YR_STRING size
267  
268 # Read identifier pointer
269 0 my $id_ptr_bytes = $ffi->cast('opaque', 'string(8)', $string_base);
270 0 ▶ last unless defined $id_ptr_bytes && length($id_ptr_bytes) == 8;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id_ptr_bytes and length $id_ptr_bytes == 8 0 0 0
Condition0%0/3
defined $id_ptr_bytes and length $id_ptr_bytes == 8
!ll&&!rl&&r
000
MC/DC0%0/2
defined $id_ptr_bytes and length $id_ptr_bytes == 8
defined $id_ptr_bytes length $id_ptr_bytes == 8
Truth table0%0/3
A and B
A= defined $id_ptr_bytesB= length $id_ptr_bytes == 8
ABresult
0X0
100
111
271  
272 0 my $id_ptr = unpack('Q', $id_ptr_bytes);
273 0 ▶ last unless $id_ptr > 4096;
Branch: 1 branch
BranchTrueFalseTotal
unless $id_ptr > 4096 0 0 0
274  
275 0 my $identifier = $ffi->cast('opaque', 'string', $id_ptr);
276 0 ▶ last unless $identifier && $identifier =~ /^\$[a-zA-Z_][a-zA-Z0-9_]{0,100}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $identifier and $identifier =~ /^\$[a-zA-Z_][a-zA-Z0-9_]{0,100}$/u 0 0 0
Condition0%0/3
$identifier and $identifier =~ /^\$[a-zA-Z_][a-zA-Z0-9_]{0,100}$/u
!ll&&!rl&&r
000
MC/DC0%0/2
$identifier and $identifier =~ /^\$[a-zA-Z_][a-zA-Z0-9_]{0,100}$/u
$identifier $identifier =~ /^\$[a-zA-Z_][a-zA-Z0-9_]{0,100}$/u
Truth table0%0/3
A and B
A= $identifierB= $identifier =~ /^\$[a-zA-Z_][a-zA-Z0-9_]{0,100}$/u
ABresult
0X0
100
111
277  
278 # Try to find matches for this string
279 0 my @offsets;
280 0 for my $match_offset (40, 48, 56) {
281 0 my $matches_ptr_bytes = $ffi->cast('opaque', 'string(8)', $string_base + $match_offset);
282 0 ▶ next unless defined $matches_ptr_bytes && length($matches_ptr_bytes) == 8;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matches_ptr_bytes and length $matches_ptr_bytes == 8 0 0 0
Condition0%0/3
defined $matches_ptr_bytes and length $matches_ptr_bytes == 8
!ll&&!rl&&r
000
MC/DC0%0/2
defined $matches_ptr_bytes and length $matches_ptr_bytes == 8
defined $matches_ptr_bytes length $matches_ptr_bytes == 8
Truth table0%0/3
A and B
A= defined $matches_ptr_bytesB= length $matches_ptr_bytes == 8
ABresult
0X0
100
111
283  
284 0 my $matches_ptr = unpack('Q', $matches_ptr_bytes);
285 0 ▶ next unless $matches_ptr > 4096;
Branch: 1 branch
BranchTrueFalseTotal
unless $matches_ptr > 4096 0 0 0
286  
287 # Read match structures
288 0 for my $j (0..199) {
289 0 my $match_base = $matches_ptr + ($j * 24); # YR_MATCH size
290  
291 0 my $offset_bytes = $ffi->cast('opaque', 'string(8)', $match_base + 8);
292 0 ▶ last unless defined $offset_bytes && length($offset_bytes) == 8;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset_bytes and length $offset_bytes == 8 0 0 0
Condition0%0/3
defined $offset_bytes and length $offset_bytes == 8
!ll&&!rl&&r
000
MC/DC0%0/2
defined $offset_bytes and length $offset_bytes == 8
defined $offset_bytes length $offset_bytes == 8
Truth table0%0/3
A and B
A= defined $offset_bytesB= length $offset_bytes == 8
ABresult
0X0
100
111
293  
294 0 my $offset = unpack('Q', $offset_bytes);
295 0 ▶ last if $offset == 0 && $j > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $offset == 0 and $j > 0 0 0 0
Condition0%0/3
$offset == 0 and $j > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$offset == 0 and $j > 0
$offset == 0 $j > 0
Truth table0%0/3
A and B
A= $offset == 0B= $j > 0
ABresult
0X0
100
111
296  
297 1 ▶ use bigint;
Subroutine BEGIN: called CC=1 SCAR=0.0
298 0 my $max_offset = 4294967295;
299 1 ▶ no bigint;
Subroutine BEGIN: called CC=1 SCAR=0.0
300 0 ▶ last if $offset > $max_offset;
Branch: 1 branch
BranchTrueFalseTotal
if $offset > $max_offset 0 0 0
301  
302 0 push @offsets, $offset;
303 }
304  
305 0 ▶ last if @offsets > 0;
Branch: 1 branch
BranchTrueFalseTotal
if @offsets > 0 0 0 0
306 }
307  
308 0 ▶ push @strings, {
Branch: 1 branch
BranchTrueFalseTotal
if @offsets > 0 0 0 0
309 id => $identifier,
310 offsets => \@offsets,
311 } if @offsets > 0;
312 }
313 };
314  
315 0 return \@strings;
316 }
317  
318 # Extract rule name from YR_RULE pointer
319 0 ▶sub _extract_rule_name {
Subroutine _extract_rule_name: not called CC=8 SCAR=38.3
320 0 my ($rule_ptr) = @_;
321 0 ▶ return undef unless $rule_ptr && $rule_ptr > 4096;
Branch: 1 branch
BranchTrueFalseTotal
unless $rule_ptr and $rule_ptr > 4096 0 0 0
Condition0%0/3
$rule_ptr and $rule_ptr > 4096
!ll&&!rl&&r
000
MC/DC0%0/2
$rule_ptr and $rule_ptr > 4096
$rule_ptr $rule_ptr > 4096
Truth table0%0/3
A and B
A= $rule_ptrB= $rule_ptr > 4096
ABresult
0X0
100
111
322  
323 0 my $rule_name;
324 0 eval {
325 0 ▶ OFFSET_LOOP: for (my $offset = 8; $offset < 256; $offset += 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < 256) 0 0 0
326 0 my $identifier_field_addr = $rule_ptr + $offset;
327 0 my $ptr_bytes = $ffi->cast('opaque', 'string(8)', $identifier_field_addr);
328 0 my $name_ptr_value = unpack('Q', $ptr_bytes);
329  
330 0 ▶ next if $name_ptr_value < 4096;
Branch: 1 branch
BranchTrueFalseTotal
if $name_ptr_value < 4096 0 0 0
331 1 ▶ use bigint;
Subroutine BEGIN: called CC=1 SCAR=0.0
332 0 my $max_addr = 140737488355327; # 0x7fffffffffff
333 1 ▶ no bigint;
Subroutine BEGIN: called CC=1 SCAR=0.0
334 0 ▶ next if $name_ptr_value > $max_addr;
Branch: 1 branch
BranchTrueFalseTotal
if $name_ptr_value > $max_addr 0 0 0
335  
336 0 my $candidate = $ffi->cast('opaque', 'string', $name_ptr_value);
337 0 ▶ if (defined $candidate && $candidate =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $candidate and $candidate =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u) 0 0 0
Condition0%0/3
defined $candidate and $candidate =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $candidate and $candidate =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
defined $candidate $candidate =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
Truth table0%0/3
A and B
A= defined $candidateB= $candidate =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
ABresult
0X0
100
111
338 0 $rule_name = $candidate;
339 0 last OFFSET_LOOP;
340 }
341 }
342 };
343  
344 0 return $rule_name;
345 }
346  
347 0 ▶sub scan_buffer {
Subroutine scan_buffer: not called CC=8 SCAR=42.8
348 0 my ($self, $buffer, $callback, %opts) = @_;
349 0 ▶ die "Compile rules first" unless $self->{rules};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"rules"} 0 0 0
350  
351 0 my $len = length($buffer);
352 0 my $ptr = $ffi->cast('string' => 'opaque', $buffer);
353 0 my @events;
354  
355 0 ▶ my $emit_strings = $opts{emit_string_events} // 1;
Condition0%0/2
$opts{"emit_string_events"} // 1
l!l
00
MC/DC0%0/1
$opts{"emit_string_events"} // 1
$opts{"emit_string_events"}
Truth table0%0/2
A
A= $opts{"emit_string_events"}
Aresult
11
00
356 0 ▶ my $emit_not_match = $opts{emit_not_match_events} // 0;
Condition0%0/2
$opts{"emit_not_match_events"} // 0
l!l
00
MC/DC0%0/1
$opts{"emit_not_match_events"} // 0
$opts{"emit_not_match_events"}
Truth table0%0/2
A
A= $opts{"emit_not_match_events"}
Aresult
11
00
357 0 ▶ my $emit_import = $opts{emit_import_events} // 0;
Condition0%0/2
$opts{"emit_import_events"} // 0
l!l
00
MC/DC0%0/1
$opts{"emit_import_events"} // 0
$opts{"emit_import_events"}
Truth table0%0/2
A
A= $opts{"emit_import_events"}
Aresult
11
00
358 0 ▶ my $emit_finished = $opts{emit_finished_events} // 0;
Condition0%0/2
$opts{"emit_finished_events"} // 0
l!l
00
MC/DC0%0/1
$opts{"emit_finished_events"} // 0
$opts{"emit_finished_events"}
Truth table0%0/2
A
A= $opts{"emit_finished_events"}
Aresult
11
00
359 0 ▶ my $enable_metadata = $opts{enable_metadata} // 0;
Condition0%0/2
$opts{"enable_metadata"} // 0
l!l
00
MC/DC0%0/1
$opts{"enable_metadata"} // 0
$opts{"enable_metadata"}
Truth table0%0/2
A
A= $opts{"enable_metadata"}
Aresult
11
00
360 0 ▶ my $enable_offsets = $opts{enable_offsets} // 0;
Condition0%0/2
$opts{"enable_offsets"} // 0
l!l
00
MC/DC0%0/1
$opts{"enable_offsets"} // 0
$opts{"enable_offsets"}
Truth table0%0/2
A
A= $opts{"enable_offsets"}
Aresult
11
00
361  
362 0 ▶ my $callback_sub = $ffi->closure(sub {
Subroutine __ANON__: not called CC=37 SCAR=72.5
363 0 my ($context, $message, $message_data, $user_data) = @_;
364  
365 0 ▶ if ($message == CALLBACK_MSG_RULE_MATCHING) {
Branch: 4 branches
BranchTrueFalseTotal
if ($message == 1) { } 0 0 0
elsif ($message == 2 and $emit_not_match) { } 0 0 0
elsif ($message == 4 and $emit_import) { } 0 0 0
elsif ($message == 3 and $emit_finished) { } 0 0 0
366 0 eval {
367 0 ▶ return unless $message_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $message_data 0 0 0
368  
369 0 my $rule_name = _extract_rule_name($message_data);
370 0 ▶ return unless $rule_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $rule_name 0 0 0
371  
372 # Extract metadata only if enabled
373 0 my $metadata;
374 0 ▶ if ($enable_metadata) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enable_metadata) 0 0 0
375 0 $metadata = eval { _extract_metadata_with_records($message_data, 1) };
376 0 ▶ $metadata = undef if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
377 }
378  
379 # Always emit rule_match event
380 0 my $rule_event = YaraFFI::Event->new(
381 event => 'rule_match',
382 rule => $rule_name,
383 metadata => $metadata,
384 );
385 0 push @events, $rule_event;
386  
387 # Call callback immediately for rule_match
388 0 ▶ if (defined $callback && ref $callback eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callback and ref $callback eq "CODE") 0 0 0
Condition0%0/3
defined $callback and ref $callback eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $callback and ref $callback eq "CODE"
defined $callback ref $callback eq "CODE"
Truth table0%0/3
A and B
A= defined $callbackB= ref $callback eq "CODE"
ABresult
0X0
100
111
389 0 eval { $callback->($rule_event) };
390 }
391  
392 # Emit string_match events if enabled
393 0 ▶ if ($emit_strings) {
Branch: 1 branch
BranchTrueFalseTotal
if ($emit_strings) 0 0 0
394 0 my @string_events;
395  
396 # Try to extract detailed string matches if enabled
397 0 ▶ if ($enable_offsets) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enable_offsets) 0 0 0
398 0 my $strings = eval { _extract_string_matches_with_records($message_data, 1) };
399  
400 0 ▶ if (!$@ && $strings && ref $strings eq 'ARRAY' && @$strings) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@ and $strings and ref $strings eq "ARRAY" and @$strings) 0 0 0
Condition0%0/9
not $@ and $strings and ref $strings eq "ARRAY" and @$strings
!ll&&!rl&&r
000
not $@ and $strings and ref $strings eq "ARRAY"
!ll&&!rl&&r
000
not $@ and $strings
!ll&&!rl&&r
000
MC/DC0%0/4
not $@ and $strings and ref $strings eq "ARRAY" and @$strings
not $@ $strings ref $strings eq "ARRAY" @$strings
Truth table0%0/5
not(A) and B and C and D
A= not $@B= $stringsC= ref $strings eq "ARRAY"D= @$strings
ABCDresult
0XXX0
10XX0
110X0
11100
11111
401 0 for my $string (@$strings) {
402 push @string_events, YaraFFI::Event->new(
403 event => 'string_match',
404 rule => $rule_name,
405 string_id => $string->{id},
406 offsets => $string->{offsets},
407 0 );
408 }
409 }
410 }
411  
412 # If no strings extracted or feature disabled, emit generic event
413 0 ▶ if (@string_events == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@string_events == 0) 0 0 0
414 0 push @string_events, YaraFFI::Event->new(
415 event => 'string_match',
416 rule => $rule_name,
417 string_id => '$',
418 offsets => [],
419 );
420 }
421  
422 # Emit all string match events
423 0 for my $string_event (@string_events) {
424 0 push @events, $string_event;
425 0 ▶ if (defined $callback && ref $callback eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callback and ref $callback eq "CODE") 0 0 0
Condition0%0/3
defined $callback and ref $callback eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $callback and ref $callback eq "CODE"
defined $callback ref $callback eq "CODE"
Truth table0%0/3
A and B
A= defined $callbackB= ref $callback eq "CODE"
ABresult
0X0
100
111
426 0 eval { $callback->($string_event) };
427 }
428 }
429 }
430 };
431 }
432 ▶ elsif ($message == CALLBACK_MSG_RULE_NOT_MATCHING && $emit_not_match) {
Condition0%0/3
$message == 2 and $emit_not_match
!ll&&!rl&&r
000
MC/DC0%0/2
$message == 2 and $emit_not_match
$message == 2 $emit_not_match
Truth table0%0/3
A and B
A= $message == 2B= $emit_not_match
ABresult
0X0
100
111
433 0 eval {
434 0 ▶ return unless $message_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $message_data 0 0 0
435  
436 0 my $rule_name = _extract_rule_name($message_data);
437 0 ▶ return unless $rule_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $rule_name 0 0 0
438  
439 0 my $event = YaraFFI::Event->new(
440 event => 'rule_not_match',
441 rule => $rule_name,
442 );
443 0 push @events, $event;
444  
445 0 ▶ if (defined $callback && ref $callback eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callback and ref $callback eq "CODE") 0 0 0
Condition0%0/3
defined $callback and ref $callback eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $callback and ref $callback eq "CODE"
defined $callback ref $callback eq "CODE"
Truth table0%0/3
A and B
A= defined $callbackB= ref $callback eq "CODE"
ABresult
0X0
100
111
446 0 eval { $callback->($event) };
447 }
448 };
449 }
450 ▶ elsif ($message == CALLBACK_MSG_IMPORT_MODULE && $emit_import) {
Condition0%0/3
$message == 4 and $emit_import
!ll&&!rl&&r
000
MC/DC0%0/2
$message == 4 and $emit_import
$message == 4 $emit_import
Truth table0%0/3
A and B
A= $message == 4B= $emit_import
ABresult
0X0
100
111
451 0 eval {
452 0 ▶ return unless $message_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $message_data 0 0 0
453  
454 # Try to extract module name from message_data
455 0 my $module_name;
456 0 eval {
457 # message_data should point to module name string
458 0 ▶ if ($message_data > 4096) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message_data > 4096) 0 0 0
459 0 $module_name = $ffi->cast('opaque', 'string', $message_data);
460 }
461 };
462  
463 0 ▶ if ($module_name && $module_name =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module_name and $module_name =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u) 0 0 0
Condition0%0/3
$module_name and $module_name =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
!ll&&!rl&&r
000
MC/DC0%0/2
$module_name and $module_name =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
$module_name $module_name =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
Truth table0%0/3
A and B
A= $module_nameB= $module_name =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
ABresult
0X0
100
111
464 0 my $event = YaraFFI::Event->new(
465 event => 'import_module',
466 module_name => $module_name,
467 );
468 0 push @events, $event;
469  
470 0 ▶ if (defined $callback && ref $callback eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callback and ref $callback eq "CODE") 0 0 0
Condition0%0/3
defined $callback and ref $callback eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $callback and ref $callback eq "CODE"
defined $callback ref $callback eq "CODE"
Truth table0%0/3
A and B
A= defined $callbackB= ref $callback eq "CODE"
ABresult
0X0
100
111
471 0 eval { $callback->($event) };
472 }
473 }
474 };
475 }
476 ▶ elsif ($message == CALLBACK_MSG_SCAN_FINISHED && $emit_finished) {
Condition0%0/3
$message == 3 and $emit_finished
!ll&&!rl&&r
000
MC/DC0%0/2
$message == 3 and $emit_finished
$message == 3 $emit_finished
Truth table0%0/3
A and B
A= $message == 3B= $emit_finished
ABresult
0X0
100
111
477 0 eval {
478 0 my $event = YaraFFI::Event->new(
479 event => 'scan_finished',
480 );
481 0 push @events, $event;
482  
483 0 ▶ if (defined $callback && ref $callback eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callback and ref $callback eq "CODE") 0 0 0
Condition0%0/3
defined $callback and ref $callback eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $callback and ref $callback eq "CODE"
defined $callback ref $callback eq "CODE"
Truth table0%0/3
A and B
A= defined $callbackB= ref $callback eq "CODE"
ABresult
0X0
100
111
484 0 eval { $callback->($event) };
485 }
486 };
487 }
488  
489 0 return CALLBACK_CONTINUE;
490 0 });
491  
492 0 my $res = yr_rules_scan_mem($self->{rules}, $ptr, $len, 0, $callback_sub, undef, 0);
493 0 return $res;
494 }
495  
496 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
497 0 my ($self) = @_;
498 0 ▶ yr_rules_destroy($self->{rules}) if $self->{rules};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"rules"} 0 0 0
499 0 yr_finalize();
500 }
501  
502 =head1 METHODS
503  
504 =head2 new
505  
506 Creates a new C<YaraFFI> instance.
507  
508 my $yara = YaraFFI->new();
509  
510 =head2 compile($rules)
511  
512 Compiles C<YARA> rules from a string. Returns C<1> on success, C<0> on failure.
513  
514 my $rules = '...';
515 $yara->compile($rules) or die "Failed to compile";
516  
517 =head2 scan_buffer($data, $callback, %options)
518  
519 Scans a buffer/string for matches.
520  
521 $yara->scan_buffer($data, sub {
522 my ($event) = @_;
523 print "Event: $event->{event}\n";
524 print "Matched: $event->{rule}\n" if $event->{rule};
525 });
526  
527 Options:
528  
529 =over 4
530  
531 =item * C<emit_string_events> - Whether to emit C<string_match> events (default: 1)
532  
533 =item * C<emit_not_match_events> - Whether to emit C<rule_not_match> events (default: 0)
534  
535 =item * C<emit_import_events> - Whether to emit C<import_module> events (default: 0)
536  
537 =item * C<emit_finished_events> - Whether to emit C<scan_finished> event (default: 1)
538  
539 =item * C<enable_metadata> - Enable metadata extraction (default: 0, experimental)
540  
541 =item * C<enable_offsets> - Enable offset extraction (default: 0, experimental)
542  
543 =back
544  
545 =head2 scan_file($filename, $callback, %options)
546  
547 Scans a file for matches. Options are the same as C<scan_buffer>.
548  
549 $yara->scan_file('/path/to/file', sub {
550 my ($event) = @_;
551 print "Event: $event->{event}\n";
552 });
553  
554 =head1 EVENT TYPES
555  
556 The callback receives event objects with the following types:
557  
558 =over 4
559  
560 =item * C<rule_match> - A rule matched the scanned data
561  
562 =item * C<rule_not_match> - A rule did not match (disabled by default)
563  
564 =item * C<string_match> - A string pattern matched (emitted after rule_match)
565  
566 =item * C<import_module> - A YARA module was imported (disabled by default)
567  
568 =item * C<scan_finished> - Scanning completed (enabled by default)
569  
570 =back
571  
572 =head1 EXPERIMENTAL FEATURES
573  
574 Metadata and offset extraction are considered experimental and disabled by default
575 due to C<YARA> version compatibility issues. Enable them at your own risk:
576  
577 $yara->scan_buffer($data, $callback,
578 enable_metadata => 1,
579 enable_offsets => 1
580 );
581  
582 These features use C<FFI::Platypus::Record> to safely access C<YARA> internal structures,
583 but structure layouts may vary between C<YARA> versions.
584  
585 =head1 DEPENDENCIES
586  
587 =over 4
588  
589 =item * L<FFI::Platypus> 2.00 or higher
590  
591 =item * L<FFI::Platypus::Record> (for metadata/offset features)
592  
593 =item * L<File::Slurp>
594  
595 =item * C<libyara.so> (YARA library)
596  
597 =back
598  
599 =head1 AUTHOR
600  
601 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
602  
603 =head1 REPOSITORY
604  
605 L<https://github.com/manwar/YaraFFI>
606  
607 =head1 BUGS
608  
609 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/YaraFFI/issues>.
610 I will be notified and then you'll automatically be notified of progress on your
611 bug as I make changes.
612  
613 =head1 SUPPORT
614  
615 You can find documentation for this module with the perldoc command.
616  
617 perldoc YaraFFI
618  
619 You can also look for information at:
620  
621 =over 4
622  
623 =item * BUG Report
624  
625 L<https://github.com/manwar/YaraFFI/issues>
626  
627 =item * CPAN Ratings
628  
629 L<http://cpanratings.perl.org/d/YaraFFI>
630  
631 =item * Search MetaCPAN
632  
633 L<https://metacpan.org/dist/YaraFFI>
634  
635 =back
636  
637 =head1 LICENSE AND COPYRIGHT
638  
639 Copyright (C) 2025 Mohammad Sajid Anwar.
640  
641 This program is free software; you can redistribute it and / or modify it under
642 the terms of the the Artistic License (2.0). You may obtain a copy of the full
643 license at:
644  
645 L<http://www.perlfoundation.org/artistic_license_2_0>
646  
647 Any use, modification, and distribution of the Standard or Modified Versions is
648 governed by this Artistic License.By using, modifying or distributing the Package,
649 you accept this license. Do not use, modify, or distribute the Package, if you do
650 not accept this license.
651  
652 If your Modified Version has been derived from a Modified Version made by someone
653 other than you,you are nevertheless required to ensure that your Modified Version
654 complies with the requirements of this license.
655  
656 This license does not grant you the right to use any trademark, service mark,
657 tradename, or logo of the Copyright Holder.
658  
659 This license includes the non-exclusive, worldwide, free-of-charge patent license
660 to make, have made, use, offer to sell, sell, import and otherwise transfer the
661 Package with respect to any patent claims licensable by the Copyright Holder that
662 are necessarily infringed by the Package. If you institute patent litigation
663 (including a cross-claim or counterclaim) against any party alleging that the
664 Package constitutes direct or contributory patent infringement,then this Artistic
665 License to you shall terminate on the date that such litigation is filed.
666  
667 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
668 CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
669 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
670 NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
671 REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
672 INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
673 OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
674  
675 =cut
676  
677 1; # End of YaraFFI
↑ Summary YaraFFI/Event.pm »