| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # string::yara Brik | ||
| 5 | # | ||
| 6 | package Metabrik::String::Yara; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable encode decode) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | commands => { | ||
| 19 | encode => [ qw($data_hash) ], | ||
| 20 | decode => [ qw($data_string) ], | ||
| 21 | }, | ||
| 22 | require_modules => { | ||
| 23 | 'Parse::YARA' => [ ], | ||
| 24 | }, | ||
| 25 | }; | ||
| 26 | } | ||
| 27 | |||
| 28 | # Takes a hash and return a YARA string | ||
| 29 | sub encode { | ||
| 30 | my $self = shift; | ||
| 31 | my ($data) = @_; | ||
| 32 | |||
| 33 | $self->brik_help_run_undef_arg('encode', $data) or return; | ||
| 34 | $self->brik_help_run_invalid_arg('encode', $data, 'HASH') or return; | ||
| 35 | |||
| 36 | my $encoded = ''; | ||
| 37 | eval { | ||
| 38 | $encoded = Parse::YARA->new(rulehash => $data, disable_includes => 0, verbose => 0); | ||
| 39 | }; | ||
| 40 | if ($@) { | ||
| 41 | chomp($@); | ||
| 42 | return $self->log->error("encode: unable to encode YARA: $@"); | ||
| 43 | } | ||
| 44 | |||
| 45 | return $encoded->as_string; | ||
| 46 | } | ||
| 47 | |||
| 48 | # Takes a YARA string and return a hash | ||
| 49 | sub decode { | ||
| 50 | my $self = shift; | ||
| 51 | my ($data) = @_; | ||
| 52 | |||
| 53 | $self->brik_help_run_undef_arg('decode', $data) or return; | ||
| 54 | |||
| 55 | my $decoded = ''; | ||
| 56 | eval { | ||
| 57 | $decoded = Parse::YARA->new(rule => $data, disable_includes => 0, verbose => 0); | ||
| 58 | }; | ||
| 59 | if ($@) { | ||
| 60 | chomp($@); | ||
| 61 | return $self->log->error("decode: unable to decode YARA: $@"); | ||
| 62 | } | ||
| 63 | |||
| 64 | return $decoded->{rules}; | ||
| 65 | } | ||
| 66 | |||
| 67 | 1; | ||
| 68 | |||
| 69 | __END__ | ||