Skip to content

Commit c16f355

Browse files
authored
Refresh rules at runtime for Yara Analyser Job (#1550)
* Add yara dynamic update rules * Add tests for yara dynamic update rules * Add linux/amd64 spec to docker build fules.
1 parent e086875 commit c16f355

File tree

8 files changed

+110
-13
lines changed

8 files changed

+110
-13
lines changed

docker/api_server/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ COPY web/. .
77
RUN npm run build
88

99
# Build Turbinia API Server, copy from build, and setup rest of requirements
10-
FROM ubuntu:22.04 as build-stage2
10+
FROM --platform=linux/amd64 ubuntu:22.04 as build-stage2
1111

1212
ENV DEBIAN_FRONTEND=noninteractive \
1313
PIP_NO_CACHE_DIR=1

docker/server/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM ubuntu:22.04
1+
FROM --platform=linux/amd64 ubuntu:22.04
22

33
ENV DEBIAN_FRONTEND=noninteractive
44
ENV PIP_NO_CACHE_DIR=1

docker/tests/Dockerfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM ubuntu:22.04
1+
FROM --platform=linux/amd64 ubuntu:22.04
22
ENV DEBIAN_FRONTEND=noninteractive \
33
PIP_NO_CACHE_DIR=1
44
ARG PPA_TRACK=stable
@@ -57,9 +57,10 @@ RUN echo "Defaults secure_path=\"/home/turbinia/.venv/bin:/usr/local/sbin:/usr/l
5757

5858
# Install yara rules and fraken binary.
5959
RUN cd /opt \
60-
&& git clone https://github.com/Neo23x0/signature-base.git \
60+
&& git clone https://github.com/Neo23x0/signature-base.git --depth=1\
6161
&& sudo chown -R turbinia:turbinia /opt/signature-base \
62-
&& find /opt/signature-base -type f -not -iname '*.yar' -not -iname '*.yara' -not -iname 'file-type-signatures.txt' -delete
62+
&& sudo git config --global --add safe.directory /opt/signature-base \
63+
&& find /opt/signature-base -type f -not -path '*.git/*' -not -iname '*.yar' -not -iname '*.yara' -not -iname 'file-type-signatures.txt' -delete
6364
COPY turbinia/config/rules/*.yar /opt/signature-base/yara/
6465
RUN mkdir -p /opt/fraken && chown -R turbinia:turbinia /opt/fraken
6566
COPY --from=us-docker.pkg.dev/osdfir-registry/turbinia/release/fraken:latest --chown=turbinia:turbinia /bin/fraken /opt/fraken/fraken

docker/worker/Dockerfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build 1 - Turbinia Worker
2-
FROM ubuntu:22.04
2+
FROM --platform=linux/amd64 ubuntu:22.04
33
ENV DEBIAN_FRONTEND=noninteractive \
44
PIP_NO_CACHE_DIR=1
55
ARG PPA_TRACK=stable
@@ -52,9 +52,10 @@ RUN echo "Defaults secure_path=\"/home/turbinia/.venv/bin:/opt/fraken:/usr/local
5252

5353
# Install yara rules and fraken binary.
5454
RUN cd /opt \
55-
&& git clone https://github.com/Neo23x0/signature-base.git \
55+
&& git clone https://github.com/Neo23x0/signature-base.git --depth=1\
5656
&& sudo chown -R turbinia:turbinia /opt/signature-base \
57-
&& find /opt/signature-base -type f -not -iname '*.yar' -not -iname '*.yara' -not -iname 'file-type-signatures.txt' -delete
57+
&& sudo git config --global --add safe.directory /opt/signature-base \
58+
&& find /opt/signature-base -type f -not -path '*.git/*' -not -iname '*.yar' -not -iname '*.yara' -not -iname 'file-type-signatures.txt' -delete
5859
COPY turbinia/config/rules/*.yar /opt/signature-base/yara/
5960
RUN mkdir -p /opt/fraken && chown -R turbinia:turbinia /opt/fraken
6061
COPY --from=us-docker.pkg.dev/osdfir-registry/turbinia/release/fraken:latest --chown=turbinia:turbinia /bin/fraken /opt/fraken/fraken

poetry.lock

Lines changed: 48 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ urllib3 = [
4141
]
4242
debugpy = "^1.8.0"
4343
jurigged = "^0.5.7"
44+
gitpython = "^3.1.43"
4445

4546
[tool.poetry.group.test]
4647
optional = true

turbinia/workers/analysis/yara.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
# limitations under the License.
1515
"""Task for running Yara on drives & directories."""
1616

17+
import git
1718
import json
19+
import logging
1820
import os
1921
import re
2022

@@ -28,6 +30,8 @@
2830
from turbinia.workers import Priority
2931
from turbinia.workers import TurbiniaTask
3032

33+
log = logging.getLogger(__name__)
34+
3135

3236
class YaraAnalysisTask(TurbiniaTask):
3337
"""Task to use Yara to analyse files."""
@@ -43,6 +47,39 @@ class YaraAnalysisTask(TurbiniaTask):
4347
'minscore': None
4448
}
4549

50+
RULES = {
51+
'https://github.com/Neo23x0/signature-base.git': '/opt/signature-base'
52+
}
53+
54+
def update_rules(self, rules):
55+
"""Update the Yara rules.
56+
57+
Args:
58+
rules (dict): dict with repo url -> path mapping
59+
60+
Returns:
61+
bool: True if success, False if error
62+
"""
63+
if rules is None:
64+
rules = self.RULES
65+
66+
log.debug('Updating Yara rules')
67+
for repo, path in rules.items():
68+
try:
69+
repository = git.Repo(path)
70+
origin = repository.remotes.origin
71+
origin.pull(depth=1)
72+
log.debug('Successfully updated rules from %s in %s', repo, path)
73+
except git.exc.InvalidGitRepositoryError as e:
74+
log.debug(
75+
'InvalidGitRepositoryError updating rules in %s: %s', path, str(e))
76+
return False
77+
except Exception as e:
78+
log.debug('Unknown error updating rules in %s: %s', path, str(e))
79+
return False
80+
81+
return True
82+
4683
def run(self, evidence, result):
4784
"""Run the Yara worker.
4885
@@ -52,6 +89,9 @@ def run(self, evidence, result):
5289
Returns:
5390
TurbiniaTaskResult object.
5491
"""
92+
# Let's update the Yara rules
93+
self.update_rules(rules=self.RULES)
94+
5595
# Where to store the resulting output file.
5696
output_file_name = 'yara_analysis.txt'
5797
output_file_path = os.path.join(self.output_dir, output_file_name)

turbinia/workers/analysis/yara_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515
"""Tests for the Yara analysis task."""
1616

17+
import git
1718
import logging
1819
import os
1920
import mock
@@ -74,6 +75,16 @@ def test_yara_no_stderr(self):
7475
TurbiniaException, '.*Unknown \(no stderr\).*', self.task.runFraken,
7576
self.result, self.evidence)
7677

78+
def test_update_rules(self):
79+
"""Tests the update_rules method"""
80+
ret = self.task.update_rules(None)
81+
self.assertEqual(ret, True)
82+
83+
error_rules = {'http://dummy': '/'}
84+
ret = self.task.update_rules(error_rules)
85+
self.assertRaises(git.exc.InvalidGitRepositoryError)
86+
self.assertEqual(ret, False)
87+
7788

7889
if __name__ == '__main__':
7990
unittest.main()

0 commit comments

Comments
 (0)