NAME

YaraFFI - Minimal Perl FFI bindings for the YARA malware scanning engine

VERSION

Version 0.07

SYNOPSIS

use YaraFFI;

my $rules = <<'YARA';
rule HelloWorld {
    meta:
        author = "John Doe"
        description = "Test rule"
    strings:
        $a = "hello" ascii
    condition:
        $a
}
YARA

my $yara = YaraFFI->new;
$yara->compile($rules) or die "compile failed";

$yara->scan_buffer("hello hacker", sub {
    my ($event) = @_;
    print "Event: $event->{event}\n";
    print "Rule: $event->{rule}\n" if $event->{rule};

    if ($event->{event} eq 'rule_match') {
        if ($event->{metadata}) {
            print "Metadata:\n";
            for my $key (keys %{$event->{metadata}}) {
                print "  $key = $event->{metadata}{$key}\n";
            }
        }
    }

    if ($event->{event} eq 'string_match') {
        print "String: $event->{string_id}\n";
        print "Offsets: " . join(", ", @{$event->{offsets}}) . "\n" if $event->{offsets};
    }

    if ($event->{event} eq 'rule_not_match') {
        print "Rule did not match: $event->{rule}\n";
    }

    if ($event->{event} eq 'import_module') {
        print "Module imported: $event->{module_name}\n";
    }

    if ($event->{event} eq 'scan_finished') {
        print "Scan completed\n";
    }
});

DESCRIPTION

YaraFFI provides lightweight FFI bindings to libyara, allowing you to compile YARA rules from strings and scan memory buffers or files. It supports all major callback events including rule_match, rule_not_match, string_match, import_module, and scan_finished via a Perl callback.

Currently it focuses on correctness and minimal functionality, not full YARA feature coverage.

For more information, please follow the official documentation.

FEATURES

  • Compile YARA rules from string

  • Scan in-memory buffers (scan_buffer)

  • Scan files (scan_file)

  • Events passed to callback: rule_match, rule_not_match, string_match, import_module, scan_finished

  • Match offset reporting for string matches (when enabled)

  • Rule metadata extraction (when enabled)

NOT YET IMPLEMENTED

  • No support for YARA modules or external variables

  • Scan flags currently hardcoded to 0

  • Metadata and offset extraction disabled by default due to YARA version compatibility

METHODS

new

Creates a new YaraFFI instance.

my $yara = YaraFFI->new();

compile($rules)

Compiles YARA rules from a string. Returns 1 on success, 0 on failure.

my $rules = '...';
$yara->compile($rules) or die "Failed to compile";

scan_buffer($data, $callback, %options)

Scans a buffer/string for matches.

$yara->scan_buffer($data, sub {
    my ($event) = @_;
    print "Event: $event->{event}\n";
    print "Matched: $event->{rule}\n" if $event->{rule};
});

Options:

  • emit_string_events - Whether to emit string_match events (default: 1)

  • emit_not_match_events - Whether to emit rule_not_match events (default: 0)

  • emit_import_events - Whether to emit import_module events (default: 0)

  • emit_finished_events - Whether to emit scan_finished event (default: 1)

  • enable_metadata - Enable metadata extraction (default: 0, experimental)

  • enable_offsets - Enable offset extraction (default: 0, experimental)

scan_file($filename, $callback, %options)

Scans a file for matches. Options are the same as scan_buffer.

$yara->scan_file('/path/to/file', sub {
    my ($event) = @_;
    print "Event: $event->{event}\n";
});

EVENT TYPES

The callback receives event objects with the following types:

  • rule_match - A rule matched the scanned data

  • rule_not_match - A rule did not match (disabled by default)

  • string_match - A string pattern matched (emitted after rule_match)

  • import_module - A YARA module was imported (disabled by default)

  • scan_finished - Scanning completed (enabled by default)

EXPERIMENTAL FEATURES

Metadata and offset extraction are considered experimental and disabled by default due to YARA version compatibility issues. Enable them at your own risk:

$yara->scan_buffer($data, $callback,
    enable_metadata => 1,
    enable_offsets => 1
);

These features use FFI::Platypus::Record to safely access YARA internal structures, but structure layouts may vary between YARA versions.

DEPENDENCIES

AUTHOR

Mohammad Sajid Anwar, <mohammad.anwar at yahoo.com>

REPOSITORY

https://github.com/manwar/YaraFFI

BUGS

Please report any bugs or feature requests through the web interface at https://github.com/manwar/YaraFFI/issues. I will be notified and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc YaraFFI

You can also look for information at:

LICENSE AND COPYRIGHT

Copyright (C) 2025 Mohammad Sajid Anwar.

This program is free software; you can redistribute it and / or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0

Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License.By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made by someone other than you,you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement,then this Artistic License to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.