yara 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format nested
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.3.0"
10
+ gem "yard", "~> 0.6.0"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.5.2"
13
+ gem "rcov", ">= 0"
14
+ gem "rake-compiler", ">= 0"
15
+ end
data/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ == 1.4.1 / 2011-01-11
2
+ * Official initial release
3
+
4
+ == 1.4.0 / 2011-01-11
5
+ * Initial version published. C bindings are based on yara v1.4.0.
6
+
data/LICENSE.txt ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README.rdoc ADDED
@@ -0,0 +1,43 @@
1
+ = yara
2
+
3
+ Ruby bindings for the yara malware analysis library.
4
+
5
+ YARA is a tool aimed at helping malware researchers to identify and classify
6
+ malware families. With YARA you can create descriptions of malware families
7
+ based on textual or binary information contained on samples of those families.
8
+ These descriptions, named rules, consist of a set of strings and a Boolean
9
+ expression which determines the rule logic.
10
+
11
+ See http://code.google.com/p/yara-project for more information.
12
+
13
+ == Synopsis
14
+
15
+ # basic example... find all PE files under the current dir
16
+
17
+ require 'yara'
18
+
19
+ ctx = Yara::Rules.new
20
+ ctx.compile_string "rule IsPE { condition: uint16(0) == 0x5A4D and uint32(uint32(0x3C)) == 0x00004550 }"
21
+
22
+ Dir["**/*", "*"].each do |fname|
23
+ begin
24
+ next unless File.file?(fname)
25
+ ctx.scan_file(fname).each {|match| puts "#{fname} -> #{match.rule}" }
26
+ rescue Yara::ScanError => e
27
+ STDERR.puts e
28
+ end
29
+ end
30
+
31
+ == Versioning
32
+ The current version is of libyara at the time of writing 1.4.0.
33
+ The major and minor version numbers of the ruby library are intended
34
+ to be in step with the C api version.
35
+
36
+ == Requirements
37
+ * libyara 1.4 must be installed - http://code.google.com/p/yara-project/
38
+
39
+ == Copyright
40
+
41
+ Copyright (c) 2011 Eric Monti. See LICENSE.txt for
42
+ further details.
43
+
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'rake/extensiontask'
4
+
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "yara"
18
+ gem.homepage = "http://github.com/SpiderLabs/yara-ruby"
19
+ gem.summary = %Q{Ruby Bindings for libyara}
20
+ gem.description = %Q{Ruby Bindings for the yara malware analysis library}
21
+ gem.email = "emonti@trustwave.com"
22
+ gem.authors = ["Eric Monti"]
23
+
24
+ gem.extensions = FileList['ext/**/extconf.rb']
25
+
26
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
27
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
28
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
29
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
30
+ end
31
+ Jeweler::RubygemsDotOrgTasks.new
32
+
33
+ Rake::ExtensionTask.new("yara_native")
34
+
35
+ CLEAN.include("lib/*.bundle")
36
+ CLEAN.include("lib/*.so")
37
+
38
+ require 'rspec/core'
39
+ require 'rspec/core/rake_task'
40
+ RSpec::Core::RakeTask.new(:spec => :compile ) do |spec|
41
+ spec.pattern = FileList['spec/**/*_spec.rb']
42
+ end
43
+
44
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
45
+ spec.pattern = 'spec/**/*_spec.rb'
46
+ spec.rcov = true
47
+ end
48
+
49
+ task :default => :spec
50
+
51
+ require 'yard'
52
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.4.1
@@ -0,0 +1,191 @@
1
+ #include "Match.h"
2
+ #include <strings.h>
3
+ #include <stdlib.h>
4
+
5
+ VALUE class_Match = Qnil;
6
+ VALUE class_MatchString = Qnil;
7
+
8
+ const char * SCAN_ERRORS[] = {
9
+ NULL,
10
+ "insuficient memory",
11
+ "duplicate rule identifier",
12
+ "invalid char in hex string",
13
+ "mismatched bracket",
14
+ "skip at end",
15
+ "invalid skip value",
16
+ "unpaired nibble",
17
+ "consecutive skips",
18
+ "misplaced wildcard or skip",
19
+ "undefined string",
20
+ "undefined identifier",
21
+ "could not open file",
22
+ "invalid regular expression",
23
+ "syntax error",
24
+ "duplicate tag identifier",
25
+ "unreferenced string",
26
+ "duplicate string identifier",
27
+ "callback error",
28
+ "misplaced or operator",
29
+ "invalid or operation syntax",
30
+ "skip inside or operation",
31
+ "nested or operation",
32
+ "misplaced anonymous string",
33
+ "could not map file",
34
+ "zero length file",
35
+ "invalid argument",
36
+ "duplicate meta identifier",
37
+ "includes circular reference",
38
+ "incorrect external variable type",
39
+ };
40
+
41
+
42
+ typedef struct {
43
+ VALUE rule;
44
+ VALUE namespace;
45
+ VALUE tags;
46
+ VALUE strings;
47
+ VALUE meta;
48
+ } match_info;
49
+
50
+ typedef struct {
51
+ VALUE offset;
52
+ VALUE identifier;
53
+ VALUE buffer;
54
+ } match_string;
55
+
56
+ VALUE
57
+ MatchString_NEW(int offset, char *ident, char *buf, size_t buflen) {
58
+ match_string *ms;
59
+
60
+ ms = (match_string *) malloc(sizeof(match_string));
61
+
62
+ if (! ms)
63
+ rb_raise(rb_eNoMemError, "Can't allocate MatchString");
64
+
65
+ ms->offset = INT2NUM(offset);
66
+ ms->identifier = rb_obj_freeze(rb_str_new2(ident));
67
+ ms->buffer = rb_obj_freeze(rb_str_new(buf, buflen));
68
+
69
+ return rb_obj_freeze(Data_Wrap_Struct(class_MatchString, 0, 0, ms));
70
+ }
71
+
72
+ int
73
+ Match_NEW_from_rule(RULE *rule, unsigned char *buffer, VALUE *match) {
74
+ match_info *mi;
75
+
76
+ TAG *tag;
77
+ STRING *string;
78
+ MATCH *m;
79
+ META *meta;
80
+
81
+ if (!(rule->flags & RULE_FLAGS_MATCH))
82
+ return 0;
83
+
84
+ mi = (match_info *) malloc(sizeof(match_info));
85
+ if (! mi )
86
+ return 1;
87
+
88
+ mi->rule = rb_obj_freeze(rb_str_new2(rule->identifier));
89
+ mi->namespace = rb_obj_freeze(rb_str_new2(rule->namespace->name));
90
+ mi->tags = rb_ary_new();
91
+ mi->strings = rb_ary_new();
92
+ mi->meta = rb_hash_new();
93
+
94
+ tag = rule->tag_list_head;
95
+ while (tag) {
96
+ rb_ary_push(mi->tags, rb_obj_freeze(rb_str_new2(tag->identifier)));
97
+ tag = tag->next;
98
+ }
99
+ rb_ary_sort_bang(mi->tags);
100
+ rb_obj_freeze(mi->tags);
101
+
102
+ string = rule->string_list_head;
103
+ while(string) {
104
+ if (string->flags & STRING_FLAGS_FOUND) {
105
+ m = string->matches;
106
+ while (m) {
107
+ rb_ary_push(mi->strings, MatchString_NEW(m->offset, string->identifier, buffer + m->offset, m->length));
108
+ m = m->next;
109
+ }
110
+ }
111
+ string = string->next;
112
+ }
113
+ rb_obj_freeze(mi->strings);
114
+
115
+ meta = rule->meta_list_head;
116
+ while(meta) {
117
+ // ... TODO
118
+ meta = meta->next;
119
+ }
120
+ rb_obj_freeze(mi->meta);
121
+
122
+ *(match) = rb_obj_freeze(Data_Wrap_Struct(class_Match, 0, 0, mi));
123
+
124
+ return 0;
125
+ }
126
+
127
+ VALUE match_rule(VALUE self) {
128
+ match_info *mi;
129
+ Data_Get_Struct(self, match_info, mi);
130
+ return mi->rule;
131
+ }
132
+
133
+ VALUE match_namespace(VALUE self) {
134
+ match_info *mi;
135
+ Data_Get_Struct(self, match_info, mi);
136
+ return mi->namespace;
137
+ }
138
+
139
+ VALUE match_tags(VALUE self) {
140
+ match_info *mi;
141
+ Data_Get_Struct(self, match_info, mi);
142
+ return mi->tags;
143
+ }
144
+
145
+ VALUE match_strings(VALUE self) {
146
+ match_info *mi;
147
+ Data_Get_Struct(self, match_info, mi);
148
+ return mi->strings;
149
+ }
150
+
151
+ VALUE match_meta(VALUE self) {
152
+ match_info *mi;
153
+ Data_Get_Struct(self, match_info, mi);
154
+ return mi->meta;
155
+ }
156
+
157
+ VALUE matchstring_identifier(VALUE self) {
158
+ match_string *ms;
159
+ Data_Get_Struct(self, match_string, ms);
160
+ return ms->identifier;
161
+ }
162
+
163
+ VALUE matchstring_offset(VALUE self) {
164
+ match_string *ms;
165
+ Data_Get_Struct(self, match_string, ms);
166
+ return ms->offset;
167
+ }
168
+
169
+ VALUE matchstring_buffer(VALUE self) {
170
+ match_string *ms;
171
+ Data_Get_Struct(self, match_string, ms);
172
+ return ms->buffer;
173
+ }
174
+
175
+
176
+ void
177
+ init_match(VALUE rb_ns) {
178
+ class_Match = rb_define_class_under(rb_ns, "Match", rb_cObject);
179
+ rb_define_method(class_Match, "rule", match_rule, 0);
180
+ rb_define_method(class_Match, "namespace", match_namespace, 0);
181
+ rb_define_method(class_Match, "tags", match_tags, 0);
182
+ rb_define_method(class_Match, "strings", match_strings, 0);
183
+ rb_define_method(class_Match, "meta", match_meta, 0);
184
+
185
+ class_MatchString = rb_define_class_under(rb_ns, "MatchString", rb_cObject);
186
+ rb_define_method(class_MatchString, "identifier", matchstring_identifier, 0);
187
+ rb_define_method(class_MatchString, "offset", matchstring_offset, 0);
188
+ rb_define_method(class_MatchString, "buffer", matchstring_buffer, 0);
189
+ }
190
+
191
+
@@ -0,0 +1,22 @@
1
+ #ifndef RB_MATCH_H_GUARD
2
+ #define RB_MATCH_H_GUARD
3
+
4
+ #include "ruby.h"
5
+ #include <yara.h>
6
+
7
+ extern VALUE class_Match;
8
+ extern VALUE class_MatchString;
9
+
10
+ extern void
11
+ init_match(VALUE ruby_namespace);
12
+
13
+ extern int
14
+ Match_NEW_from_rule(RULE * rule, unsigned char * buffer, VALUE * match);
15
+
16
+ extern const char * SCAN_ERRORS[];
17
+
18
+ #define MAX_SCAN_ERROR 29
19
+
20
+ #endif
21
+
22
+