YaraFFI/Event.pm

filter: Statementstmt 26.9% 14 / 52 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 18.1% 4 / 22 Podpod 70.5% 12 / 17 totaltotal 22.3% 30 / 134 CC 14 SCAR 17.3
CC
14
Cov
17%
has_metadata
12.0
has_offsets
12.0
offsets
6.0
CRAP 5.6
« YaraFFI.pm ↑ Summary YaraFFI/Record/YR_MATCH.pm »
1 package YaraFFI::Event;
2  
3 1 $YaraFFI::Event::VERSION = '0.07';
4 1 $YaraFFI::Event::AUTHORITY = 'cpan:MANWAR';
5  
6 =head1 NAME
7  
8 YaraFFI::Event - Event class that stringifies to rule name but also works as a hash
9  
10 =head1 VERSION
11  
12 Version 0.07
13  
14 =head1 SYNOPSIS
15  
16 use YaraFFI::Event;
17  
18 # Create a rule match event with metadata
19 my $event = YaraFFI::Event->new(
20 event => 'rule_match',
21 rule => 'MalwareRule',
22 metadata => {
23 author => 'Security Team',
24 description => 'Detects malware',
25 severity => 5,
26 active => 1,
27 },
28 );
29  
30 print "Matched: $event\n"; # Stringifies to rule name
31 print "Author: ", $event->metadata->{author}, "\n";
32 print "Severity: ", $event->severity, "\n";
33  
34 # Create a string match event with offsets
35 my $str_event = YaraFFI::Event->new(
36 event => 'string_match',
37 rule => 'MalwareRule',
38 string_id => '$suspicious',
39 offsets => [0, 42, 128],
40 );
41  
42 print "String: ", $str_event->string_id, "\n";
43 print "Found at: ", join(", ", @{$str_event->offsets}), "\n";
44  
45 =head1 DESCRIPTION
46  
47 C<YaraFFI::Event> represents a scanning event from C<YARA>. It can be used as a string
48 (stringifies to the rule name) or as a hash/object to access event details.
49  
50 Events are created during C<YARA> scans and passed to callback functions.
51  
52 =head1 EVENT TYPES
53  
54 =head2 rule_match
55  
56 Emitted when a C<YARA> rule matches the scanned data.
57  
58 Fields:
59 event => 'rule_match'
60 rule => 'RuleName'
61 metadata => { key => value, ... } # Optional, rule metadata
62  
63 =head2 string_match
64  
65 Emitted when a string pattern within a rule matches.
66  
67 Fields:
68 event => 'string_match'
69 rule => 'RuleName'
70 string_id => '$string_identifier'
71 offsets => [offset1, offset2, ...] # Byte offsets where string matched
72  
73 =head1 METHODS
74  
75 =head2 new(%args)
76  
77 Creates a new event object.
78  
79 my $event = YaraFFI::Event->new(
80 event => 'rule_match',
81 rule => 'TestRule',
82 );
83  
84 =cut
85  
86 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 0 ▶use overload '""' => sub { $_[0]->{rule} }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
91  
92 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
93 0 my ($class, %args) = @_;
94 0 return bless \%args, $class;
95 }
96  
97 =head2 event
98  
99 Returns the event type (C<rule_match> or C<string_match>).
100  
101 my $type = $event->event;
102  
103 =cut
104  
105 0 ▶sub event {
Subroutine event: not called CC=1 SCAR=6.9
Pod documented
106 0 my ($self) = @_;
107 0 return $self->{event};
108 }
109  
110 =head2 rule
111  
112 Returns the name of the matched rule.
113  
114 my $rule_name = $event->rule;
115  
116 =cut
117  
118 0 ▶sub rule {
Subroutine rule: not called CC=1 SCAR=6.9
Pod documented
119 0 my ($self) = @_;
120 0 return $self->{rule};
121 }
122  
123 =head2 metadata
124  
125 Returns the rule metadata as a hash reference (C<rule_match> events only).
126  
127 my $meta = $event->metadata;
128 if ($meta) {
129 print "Author: $meta->{author}\n";
130 print "Severity: $meta->{severity}\n";
131 }
132  
133 =cut
134  
135 0 ▶sub metadata {
Subroutine metadata: not called CC=1 SCAR=6.9
Pod documented
136 0 my ($self) = @_;
137 0 return $self->{metadata};
138 }
139  
140 =head2 string_id
141  
142 Returns the string identifier (C<string_match> events only).
143  
144 my $id = $event->string_id; # e.g., '$suspicious'
145  
146 =cut
147  
148 0 ▶sub string_id {
Subroutine string_id: not called CC=1 SCAR=6.9
Pod documented
149 0 my ($self) = @_;
150 0 return $self->{string_id};
151 }
152  
153 =head2 offsets
154  
155 Returns an array reference of byte offsets where the string matched (C<string_match> events only).
156  
157 my $offsets = $event->offsets;
158 for my $offset (@$offsets) {
159 print "Match at byte $offset\n";
160 }
161  
162 =cut
163  
164 0 ▶sub offsets {
Subroutine offsets: not called CC=2 SCAR=17.9
Pod documented
165 0 my ($self) = @_;
166 0 ▶ return $self->{offsets} || [];
Condition0%0/2
$self->{'offsets'} || []
l!l
00
MC/DC0%0/1
$self->{'offsets'} || []
$self->{'offsets'}
Truth table0%0/2
A
A= $self->{'offsets'}
Aresult
11
00
167 }
168  
169 =head2 Convenience Metadata Accessors
170  
171 For common metadata fields, you can access them directly:
172  
173 my $author = $event->author;
174 my $desc = $event->description;
175 my $sev = $event->severity;
176  
177 These return undef if the metadata doesn't exist or the field isn't present.
178  
179 =cut
180  
181 0 ▶sub author {
Subroutine author: not called CC=2 SCAR=17.9
Pod undocumented
182 0 my ($self) = @_;
183 0 ▶ return $self->{metadata} ? $self->{metadata}{author} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'metadata'} ? : 0 0 0
184 }
185  
186 0 ▶sub description {
Subroutine description: not called CC=2 SCAR=17.9
Pod undocumented
187 0 my ($self) = @_;
188 0 ▶ return $self->{metadata} ? $self->{metadata}{description} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'metadata'} ? : 0 0 0
189 }
190  
191 0 ▶sub severity {
Subroutine severity: not called CC=2 SCAR=17.9
Pod undocumented
192 0 my ($self) = @_;
193 0 ▶ return $self->{metadata} ? $self->{metadata}{severity} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'metadata'} ? : 0 0 0
194 }
195  
196 0 ▶sub reference {
Subroutine reference: not called CC=2 SCAR=17.9
Pod undocumented
197 0 my ($self) = @_;
198 0 ▶ return $self->{metadata} ? $self->{metadata}{reference} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'metadata'} ? : 0 0 0
199 }
200  
201 0 ▶sub date {
Subroutine date: not called CC=2 SCAR=17.9
Pod undocumented
202 0 my ($self) = @_;
203 0 ▶ return $self->{metadata} ? $self->{metadata}{date} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'metadata'} ? : 0 0 0
204 }
205  
206 =head2 has_metadata
207  
208 Returns true if the event has metadata.
209  
210 if ($event->has_metadata) {
211 # Process metadata
212 }
213  
214 =cut
215  
216 0 ▶sub has_metadata {
Subroutine has_metadata: not called CC=3 SCAR=24.8
Pod documented
217 0 my ($self) = @_;
218 0 ▶ return defined $self->{metadata} && ref $self->{metadata} eq 'HASH' && keys %{$self->{metadata}} > 0;
Condition0%0/6
defined $self->{'metadata'} && ref $self->{'metadata'} eq 'HASH' && keys %{$$self{'metadata'};} > 0
!ll&&!rl&&r
000
defined $self->{"metadata"} and ref $self->{"metadata"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $self->{'metadata'} && ref $self->{'metadata'} eq 'HASH' && keys %{$$self{'metadata'};} > 0
defined $self->{"metadata"} ref $self->{"metadata"} eq "HASH" keys %{$$self{'metadata'};} > 0
Truth table0%0/4
A and B && C
A= defined $self->{"metadata"}B= ref $self->{"metadata"} eq "HASH"C= keys %{$$self{'metadata'};} > 0
ABCresult
0XX0
10X0
1100
1111
219 }
220  
221 =head2 has_offsets
222  
223 Returns true if the event has match offsets.
224  
225 if ($event->has_offsets) {
226 print "Matches at: ", join(", ", @{$event->offsets}), "\n";
227 }
228  
229 =cut
230  
231 0 ▶sub has_offsets {
Subroutine has_offsets: not called CC=3 SCAR=24.8
Pod documented
232 0 my ($self) = @_;
233 0 ▶ return defined $self->{offsets} && ref $self->{offsets} eq 'ARRAY' && @{$self->{offsets}} > 0;
Condition0%0/6
defined $self->{'offsets'} && ref $self->{'offsets'} eq 'ARRAY' && @{$self->{'offsets'};} > 0
!ll&&!rl&&r
000
defined $self->{"offsets"} and ref $self->{"offsets"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $self->{'offsets'} && ref $self->{'offsets'} eq 'ARRAY' && @{$self->{'offsets'};} > 0
defined $self->{"offsets"} ref $self->{"offsets"} eq "ARRAY" @{$self->{'offsets'};} > 0
Truth table0%0/4
A and B && C
A= defined $self->{"offsets"}B= ref $self->{"offsets"} eq "ARRAY"C= @{$self->{'offsets'};} > 0
ABCresult
0XX0
10X0
1100
1111
234 }
235  
236 =head2 match_count
237  
238 Returns the number of times the string matched (for C<string_match> events).
239  
240 my $count = $event->match_count;
241  
242 =cut
243  
244 0 ▶sub match_count {
Subroutine match_count: not called CC=2 SCAR=17.9
Pod documented
245 0 my ($self) = @_;
246 0 ▶ return $self->{offsets} ? scalar(@{$self->{offsets}}) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'offsets'} ? : 0 0 0
247 }
248  
249 =head2 is_rule_match
250  
251 Returns true if this is a rule_match event.
252  
253 if ($event->is_rule_match) {
254 # Handle rule match
255 }
256  
257 =cut
258  
259 0 ▶sub is_rule_match {
Subroutine is_rule_match: not called CC=2 SCAR=17.9
Pod documented
260 0 my ($self) = @_;
261 0 ▶ return $self->{event} && $self->{event} eq 'rule_match';
Condition0%0/3
$self->{'event'} && $self->{'event'} eq 'rule_match'
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{'event'} && $self->{'event'} eq 'rule_match'
$self->{'event'} $self->{'event'} eq 'rule_match'
Truth table0%0/3
A && B
A= $self->{'event'}B= $self->{'event'} eq 'rule_match'
ABresult
0X0
100
111
262 }
263  
264 =head2 is_string_match
265  
266 Returns true if this is a C<string_match> event.
267  
268 if ($event->is_string_match) {
269 # Handle string match
270 }
271  
272 =cut
273  
274 0 ▶sub is_string_match {
Subroutine is_string_match: not called CC=2 SCAR=17.9
Pod documented
275 0 my ($self) = @_;
276 0 ▶ return $self->{event} && $self->{event} eq 'string_match';
Condition0%0/3
$self->{'event'} && $self->{'event'} eq 'string_match'
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{'event'} && $self->{'event'} eq 'string_match'
$self->{'event'} $self->{'event'} eq 'string_match'
Truth table0%0/3
A && B
A= $self->{'event'}B= $self->{'event'} eq 'string_match'
ABresult
0X0
100
111
277 }
278  
279 =head2 to_hash
280  
281 Returns the event as a plain hash reference.
282  
283 my $hash = $event->to_hash;
284  
285 =cut
286  
287 0 ▶sub to_hash {
Subroutine to_hash: not called CC=1 SCAR=6.9
Pod documented
288 0 my ($self) = @_;
289 0 return { %$self };
290 }
291  
292 =head1 OVERLOADING
293  
294 The event object overloads string concatenation, so it can be used directly
295 in print statements and will display the rule name:
296  
297 print "Matched: $event\n"; # Prints "Matched: RuleName"
298  
299 =head1 AUTHOR
300  
301 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
302  
303 =head1 REPOSITORY
304  
305 L<https://github.com/manwar/YaraFFI>
306  
307 =head1 BUGS
308  
309 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/YaraFFI/issues>.
310 I will be notified and then you'll automatically be notified of progress on your
311 bug as I make changes.
312  
313 =head1 SUPPORT
314  
315 You can find documentation for this module with the perldoc command.
316  
317 perldoc YaraFFI::Event
318  
319 You can also look for information at:
320  
321 =over 4
322  
323 =item * BUG Report
324  
325 L<https://github.com/manwar/YaraFFI/issues>
326  
327 =item * CPAN Ratings
328  
329 L<http://cpanratings.perl.org/d/YaraFFI>
330  
331 =item * Search MetaCPAN
332  
333 L<https://metacpan.org/dist/YaraFFI>
334  
335 =back
336  
337 =head1 LICENSE AND COPYRIGHT
338  
339 Copyright (C) 2025 Mohammad Sajid Anwar.
340  
341 This program is free software; you can redistribute it and / or modify it under
342 the terms of the the Artistic License (2.0). You may obtain a copy of the full
343 license at:
344  
345 L<http://www.perlfoundation.org/artistic_license_2_0>
346  
347 Any use, modification, and distribution of the Standard or Modified Versions is
348 governed by this Artistic License.By using, modifying or distributing the Package,
349 you accept this license. Do not use, modify, or distribute the Package, if you do
350 not accept this license.
351  
352 If your Modified Version has been derived from a Modified Version made by someone
353 other than you,you are nevertheless required to ensure that your Modified Version
354 complies with the requirements of this license.
355  
356 This license does not grant you the right to use any trademark, service mark,
357 tradename, or logo of the Copyright Holder.
358  
359 This license includes the non-exclusive, worldwide, free-of-charge patent license
360 to make, have made, use, offer to sell, sell, import and otherwise transfer the
361 Package with respect to any patent claims licensable by the Copyright Holder that
362 are necessarily infringed by the Package. If you institute patent litigation
363 (including a cross-claim or counterclaim) against any party alleging that the
364 Package constitutes direct or contributory patent infringement,then this Artistic
365 License to you shall terminate on the date that such litigation is filed.
366  
367 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
368 CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
369 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
370 NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
371 REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
372 INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
373 OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
374  
375 =cut
376  
377 1 1; # End of YaraFFI::Event
« YaraFFI.pm ↑ Summary YaraFFI/Record/YR_MATCH.pm »