Skip to content

Commit a6f6536

Browse files
authored
Merge pull request #30 from y1z2g3/master
Adapt to yara v4.0.2 version
2 parents b78b7f6 + 50dfde9 commit a6f6536

7 files changed

Lines changed: 64 additions & 36 deletions

File tree

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@ Highlights
99
- Rules can be loaded as strings, files or archives; for archives will recursively look for and load all yara rule files
1010
- Matches are returned with identifier, metadata and tags
1111
- Negate, timeout and limit supported
12+
- Support yara 4.0.2 -- 2021/1/17
1213

1314

1415
How to build
1516
------------
1617

1718
### Get and build yara source code
1819

19-
Example (building from 3.10.0 version)
20+
Example (building from 4.0.2 version)
2021

2122
```
2223
git clone https://github.com/virustotal/yara.git
2324
cd yara
24-
git checkout tags/v3.10.0
25+
git checkout tags/v4.0.2
2526
./bootstrap.sh
26-
./configure --disable-shared
27+
./configure --enable-shared --without-crypto CFLAGS=-fPIC
2728
make
2829
```
2930

@@ -34,11 +35,17 @@ Example (in "yara" folder):
3435
```
3536
git clone https://github.com/p8a/yara-java.git
3637
cd yara-java
37-
git checkout tags/v3.10.0
3838
mvn clean install
3939
```
4040

4141
Usage and examples
4242
------------------
4343

4444
See the unit tests
45+
46+
47+
Notes
48+
----
49+
After you successfully added some sources you can get the compiled rules using the yr_compiler_get_rules() function. You'll get a pointer to a YR_RULES structure which can be used to scan your data as described in Scanning data. Once yr_compiler_get_rules() is invoked you can not add more sources to the compiler, but you can call yr_compiler_get_rules() multiple times. Each time this function is called it returns a pointer to the same YR_RULES structure. Notice that this behaviour is new in YARA 4.0.0, in YARA 3.X and 2.X yr_compiler_get_rules() returned a new copy the YR_RULES structure.Instances of YR_RULES must be destroyed with yr_rules_destroy().
50+
51+
When you call YaraCompilerImpl.createScanner() multiple times. the return YaraScanner will point to the same YR_RULES structure. so, you cann't destroy YaraScanner multiple times!!!

src/main/java/com/github/plusvic/yara/embedded/YaraCompilerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public NativeCompilationCallback(YaraLibrary library, YaraCompilationCallback ca
3636
this.callback = callback;
3737
}
3838

39-
long nativeOnError(long errorLevel, long fileName, long lineNumber, long message, long data) {
39+
long nativeOnError(long errorLevel, long fileName, long lineNumber, long rule, long message, long data) {
4040
callback.onError(YaraCompilationCallback.ErrorLevel.from((int) errorLevel),
4141
library.toString(fileName),
4242
lineNumber,
@@ -71,7 +71,7 @@ public void setCallback(YaraCompilationCallback cbk) {
7171
checkArgument(cbk != null);
7272
checkState(callback == null);
7373

74-
callback = new Callback(new NativeCompilationCallback(library, cbk), "nativeOnError", 5);
74+
callback = new Callback(new NativeCompilationCallback(library, cbk), "nativeOnError", 6);
7575
final long callBackAddress = callback.getAddress();
7676
if(callBackAddress == 0) {
7777
throw new IllegalStateException("Too many concurent callbacks, unable to create.");

src/main/java/com/github/plusvic/yara/embedded/YaraLibrary.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void compilerDestroy(long compiler) {
5555

5656
private final native void yr_compiler_set_callback(
5757
@JniArg(cast = "YR_COMPILER*") long compiler,
58-
@JniArg(cast = "void (*)(int, const char*, int, const char*,void*)", flags = ArgFlag.POINTER_ARG) long callback,
58+
@JniArg(cast = "void (*)(int, const char*, int, const YR_RULE* rule, const char*,void*)", flags = ArgFlag.POINTER_ARG) long callback,
5959
@JniArg(cast = "void *") long data
6060
);
6161
public void compilerSetCallback(long compiler, long callback, long data) {
@@ -227,13 +227,18 @@ public String stringIdentifier(long pv) {
227227
return yara_string_identifier(null, pv);
228228
}
229229

230-
private final native long yara_string_matches(JNIEnv env, @JniArg(cast = "void*") long pv);
231-
public long stringMatches(long pv) {
230+
private final native long yara_string_matches(
231+
JNIEnv env,
232+
@JniArg(cast = "void*") long context,
233+
@JniArg(cast = "void*") long pv);
234+
public long stringMatches(long context, long pv) {
232235
Preconditions.checkState(library != null);
233-
return yara_string_matches(null, pv);
236+
return yara_string_matches(null, context, pv);
234237
}
235238

236-
private final native long yara_string_match_next(JNIEnv env, @JniArg(cast = "void*") long pv);
239+
private final native long yara_string_match_next(
240+
JNIEnv env,
241+
@JniArg(cast = "void*") long pv);
237242
public long stringMatchNext(long pv) {
238243
Preconditions.checkState(library != null);
239244
return yara_string_match_next(null, pv);

src/main/java/com/github/plusvic/yara/embedded/YaraRuleImpl.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
*/
1212
public class YaraRuleImpl implements YaraRule {
1313
private final YaraLibrary library;
14+
private final long context;
1415
private final long peer;
1516

16-
YaraRuleImpl(YaraLibrary library, long peer) {
17+
YaraRuleImpl(YaraLibrary library, long context, long peer) {
1718
checkArgument(library != null);
19+
checkArgument(context != 0);
1820
checkArgument(peer != 0);
1921

2022
this.library = library;
23+
this.context = context;
2124
this.peer = peer;
2225
}
2326

@@ -64,13 +67,13 @@ public Iterator<YaraMeta> getMetadata() {
6467

6568
@Override
6669
protected YaraMetaImpl getNext() {
67-
long last = index;
68-
index = library.ruleMetaNext(index);
69-
70-
if (index == 0 || last == 0) {
70+
if (index == 0){
7171
return null;
7272
}
7373

74+
long last = index;
75+
index = library.ruleMetaNext(index);
76+
7477
return new YaraMetaImpl(library, last);
7578
}
7679
};
@@ -87,14 +90,14 @@ public Iterator<YaraString> getStrings() {
8790

8891
@Override
8992
protected YaraStringImpl getNext() {
90-
long last = index;
91-
index = library.ruleStringNext(index);
92-
93-
if (index == 0 || last == 0) {
93+
if (index == 0){
9494
return null;
9595
}
9696

97-
return new YaraStringImpl(library, last);
97+
long last = index;
98+
index = library.ruleStringNext(index);
99+
100+
return new YaraStringImpl(library, context, last);
98101
}
99102
};
100103
}

src/main/java/com/github/plusvic/yara/embedded/YaraScannerImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,20 @@ public void setMaxRules(int count) {
6161
this.maxRules = count;
6262
}
6363

64-
long nativeOnScan(long type, long message, long data) {
64+
long nativeOnScan(long context, long type, long message, long data) {
6565
if (!negate && type == CALLBACK_MSG_RULE_MATCHING) {
6666
++count;
6767

6868
if (scanCallback != null) {
69-
YaraRuleImpl rule = new YaraRuleImpl(library, message);
69+
YaraRuleImpl rule = new YaraRuleImpl(library, context, message);
7070
scanCallback.onMatch(rule);
7171
}
7272
}
7373
else if(negate && type == CALLBACK_MSG_RULE_NOT_MATCHING) {
7474
++count;
7575

7676
if (scanCallback != null) {
77-
YaraRuleImpl rule = new YaraRuleImpl(library, message);
77+
YaraRuleImpl rule = new YaraRuleImpl(library, context, message);
7878
scanCallback.onMatch(rule);
7979
}
8080
}
@@ -211,7 +211,7 @@ public void scan(File file, Map<String, String> moduleArgs, YaraScanCallback yar
211211
nativeCallback.setMaxRules(maxRules);
212212
nativeCallback.setNegate(notSatisfiedOnly);
213213

214-
Callback callback = new Callback(nativeCallback, "nativeOnScan", 3);
214+
Callback callback = new Callback(nativeCallback, "nativeOnScan", 4);
215215

216216
try {
217217
final long callBackAddress = callback.getAddress();
@@ -284,7 +284,7 @@ public void scan(byte[] buffer, Map<String, String> moduleArgs, YaraScanCallback
284284
nativeCallback.setMaxRules(maxRules);
285285
nativeCallback.setNegate(notSatisfiedOnly);
286286

287-
Callback callback = new Callback(nativeCallback, "nativeOnScan", 3);
287+
Callback callback = new Callback(nativeCallback, "nativeOnScan", 4);
288288

289289
try {
290290
final long callBackAddress = callback.getAddress();

src/main/java/com/github/plusvic/yara/embedded/YaraStringImpl.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
*/
1414
public class YaraStringImpl implements YaraString {
1515
private final YaraLibrary library;
16+
private final long context;
1617
private final long peer;
1718

18-
YaraStringImpl(YaraLibrary library, long peer) {
19+
YaraStringImpl(YaraLibrary library, long context, long peer) {
1920
checkArgument(library != null);
21+
checkArgument(context != 0);
2022
checkArgument(peer != 0);
2123

2224
this.library = library;
25+
this.context = context;
2326
this.peer = peer;
2427
}
2528

@@ -39,7 +42,7 @@ public String getIdentifier() {
3942
*/
4043
public Iterator<YaraMatch> getMatches() {
4144
return new GenericIterator<YaraMatch>() {
42-
private long index = library.stringMatches(peer);
45+
private long index = library.stringMatches(context, peer);
4346

4447
@Override
4548
protected YaraMatchImpl getNext() {

src/main/native-package/src/yara-wrapper.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ static void*
6262
yara_rule_meta_next(JNIEnv *env, void *v) {
6363
YR_META *meta = (YR_META *)v;
6464

65-
if (META_IS_NULL(meta)) {
65+
if (NULL == meta) {
6666
return 0;
6767
}
68-
return ++meta;
68+
return META_IS_LAST_IN_RULE(meta) ? NULL : ++meta;
6969
}
7070

7171
static int
@@ -102,10 +102,10 @@ static void*
102102
yara_rule_string_next(JNIEnv *env, void *v) {
103103
YR_STRING *string = (YR_STRING *)v;
104104

105-
if (STRING_IS_NULL(string)) {
105+
if (NULL == string) {
106106
return 0;
107107
}
108-
return ++string;
108+
return STRING_IS_LAST_IN_RULE(string) ? NULL : ++string;
109109
}
110110

111111
static jstring
@@ -115,15 +115,25 @@ yara_string_identifier(JNIEnv *env, void *v) {
115115
}
116116

117117
static void*
118-
yara_string_matches(JNIEnv *env, void *v) {
118+
yara_string_matches(JNIEnv *env, void *context, void *v) {
119119
YR_STRING *string = (YR_STRING *)v;
120-
return (string ? STRING_MATCHES(string).head : NULL);
120+
YR_MATCHES* matches = ((YR_SCAN_CONTEXT*)context)->matches;
121+
return matches[string->idx].head;
121122
}
122123

123124
static void*
124125
yara_string_match_next(JNIEnv *env, void *v) {
125-
return !v ? 0 :
126-
((YR_MATCH *)v)->next;
126+
YR_MATCH *match = (YR_MATCH *)v;
127+
128+
if(NULL == match) {
129+
return 0;
130+
}
131+
132+
while(match->is_private){
133+
match = match->next;
134+
}
135+
136+
return match->next;
127137
}
128138

129139
static int64_t

0 commit comments

Comments
 (0)