Skip to content

Commit 820c1e9

Browse files
ljblancoredborderpperezredborderrgomezborder
authored
Release 6.18.0 (#315)
* Feature/#22929 adapt yara scripts (#311) * control rvm gemset and collapse into validation function * add better explanation * Update pyyara.py to python 3 * move pyyara script to this repo --------- Co-authored-by: ljblancoredborder <ljblanco@redborder.com> Co-authored-by: Rafa Gómez <rgomez@redborder.com> * bump version * reduce version to patch, this is not blocking manager to run * Revert "reduce version to patch, this is not blocking manager to run" This reverts commit efc0a55. --------- Co-authored-by: Pablo Pérez <pperez@redborder.com> Co-authored-by: Rafa Gómez <rgomez@redborder.com>
1 parent d479107 commit 820c1e9

3 files changed

Lines changed: 109 additions & 3 deletions

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.17.2
1+
6.18.0

resources/scripts/rb_yara.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99

1010
# Usage help
1111
def usage
12+
puts "rb_yara.rb - Script to manage Yara rules in redBorder. It can import or clear rules."
1213
puts "Usage: rb_yara.rb [import|clear]"
14+
puts " import : Create a tarball with Yara rules in #{PATH_YARA_RULES} as .yara format."
15+
puts " clear : Remove all yara rules from rails database #{PATH_YARA_RULES}."
16+
puts "User must ensure that rvm gemset is web, otherwise rake version will be inconsistent."
1317
exit 1
1418
end
1519

@@ -62,9 +66,24 @@ def clear_yara_rules
6266
log("Yara rules cleared everywhere.")
6367
end
6468

65-
# Main
66-
usage if ARGV.length != 1
69+
def validation
70+
current = `rvm current`.strip
71+
unless current == 'ruby-2.7.5@web'
72+
log("ERROR: Wrong Ruby version of gemset: #{current}")
73+
usage
74+
return false
75+
end
76+
if ARGV.length != 1
77+
log('ERROR: Check number of arguments')
78+
usage
79+
return false
80+
end
81+
return true
82+
end
6783

84+
exit 1 unless validation
85+
86+
# Main
6887
case get_action(ARGV[0])
6988
when "import"
7089
create_yara_rules_tar

resources/tools/pyyara.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/python
2+
3+
# Licensed to the Apache Software Foundation (ASF) under one or more
4+
# contributor license agreements. See the NOTICE file distributed with this
5+
# work for additional information regarding copyright ownership. The ASF
6+
# licenses this file to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
# License for the specific language governing permissions and limitations under
16+
# the License.
17+
#
18+
# Copyright 2013 Endgame Inc.
19+
20+
import sys
21+
import os
22+
import glob
23+
import yara
24+
import json
25+
import datetime
26+
27+
def log(msg):
28+
sys.stderr.write("[%s] " % datetime.datetime.now())
29+
sys.stderr.write(msg + "\n")
30+
sys.stderr.flush()
31+
32+
def output(msg):
33+
sys.stdout.write(msg + "\n")
34+
sys.stdout.flush()
35+
36+
def die():
37+
log("Process timed out, about to exit ...")
38+
print(json.dumps({"_error":"timed out"}))
39+
sys.exit(1)
40+
41+
log("PID=%d" % os.getpid())
42+
log("PARENT PID=%d" % os.getppid())
43+
log("CWD=%s" % os.getcwd())
44+
45+
if len(sys.argv) > 2:
46+
path_yara_rules = sys.argv[2]
47+
else:
48+
path_yara_rules = "yara_rules/"
49+
50+
start = datetime.datetime.now()
51+
sigs = dict([(name.replace(".yara", "").split("/")[-1], name) for name in glob.glob(path_yara_rules+"*.yar*")])
52+
rules = yara.compile(filepaths=sigs)
53+
end = datetime.datetime.now()
54+
log("Loaded yara rules in %s: %s"%( end - start, json.dumps(sigs, indent=4)))
55+
56+
matches = {}
57+
58+
def match_callback(data):
59+
if data.get("matches", False):
60+
data.pop("matches")
61+
if "strings" in data:
62+
data.pop("strings")
63+
if "rule" in data and isinstance(data["rule"], bytes):
64+
data["rule"] = data["rule"].decode('utf-8', errors='ignore')
65+
if "tags" in data and not data["tags"]:
66+
data.pop("tags")
67+
if "meta" in data and "description" in data["meta"] and isinstance(data["meta"]["description"], bytes):
68+
data["meta"]["description"] = data["meta"]["description"].decode('utf-8', errors='ignore')
69+
matches['matches'].append(data)
70+
return yara.CALLBACK_CONTINUE
71+
72+
# Open and read the target file
73+
target_file = sys.argv[1]
74+
75+
log("Openning %s for reading ..."%(target_file))
76+
data = open(target_file, 'rb').read()
77+
78+
log("Performing matching on %d bytes of data ..."%len(data))
79+
matches = {'filename': os.path.basename(target_file), 'matches':[]}
80+
81+
start = datetime.datetime.now()
82+
rules.match(data=data, callback=match_callback)
83+
end = datetime.datetime.now()
84+
85+
log("Done matching %d bytes in %s, printing results ..."%(len(data), end - start))
86+
output(json.dumps(matches))
87+
log("Process Exiting")

0 commit comments

Comments
 (0)