Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Unofficial antivirus definitions using Fangfrisch #212

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
8e394f9
Update Dockerfile
gchamon Feb 14, 2023
9c16448
fix dockerfile missing so
gchamon Feb 14, 2023
94609bf
Merge pull request #1 from waycarbon/fix/dockerfile-missing-so
gchamon Feb 14, 2023
d45cbb7
implement fangfrisch usage
gchamon Feb 24, 2023
3a898f2
update dockerfile and update to reference correct fangfrisch bin
gchamon Feb 24, 2023
fce2f29
installing awscliv2 and add correct pythonpath for fangfrisch
gchamon Feb 24, 2023
abbeee1
revert change to /opt/app
gchamon Feb 24, 2023
efc71b6
add aws-cli to final lambda
gchamon Feb 24, 2023
e21edc6
add lib64 python packages
gchamon Feb 24, 2023
ea1bc0c
centralizing pip installation with --target argument
gchamon Feb 24, 2023
ed91b06
removing cp statement made unecessary with --target argument for pip
gchamon Feb 24, 2023
5df78a7
downgrade awscli to v1
gchamon Feb 24, 2023
ca235e4
return with copying python3.7 site-packages for extra missing deps
gchamon Feb 24, 2023
3e9b2e3
update urllib3
gchamon Feb 24, 2023
cf30411
remove python3.7 site packages, as it doesn't really exist
gchamon Feb 24, 2023
43baf43
run aws from bin folder
gchamon Feb 28, 2023
f59ec40
add pythonpath to awscli invocation
gchamon Feb 28, 2023
cb5f840
separate fangfrisch requirements.txt; optimized docker build image
gchamon Feb 28, 2023
02fef58
sed in-place the shebang for correct python interpreter for lambdas
gchamon Feb 28, 2023
52d4137
fangfrisch.conf absolute path
gchamon Feb 28, 2023
af499af
add missing fangfrisch.conf file
gchamon Feb 28, 2023
0180c60
check shell return; sync without progress
gchamon Feb 28, 2023
b253208
use update_defs_from_s3 to download extra definitions
gchamon Feb 28, 2023
1ec5169
ditch awscli for custom down/upload functions
gchamon Feb 28, 2023
af87467
use sed to reflect AV_DEFINITION_PATH in fangfrisch.conf
gchamon Feb 28, 2023
d1c3ac8
revert changes to md5 from s3 tags function
gchamon Feb 28, 2023
89d6c80
changing fangfrisch.conf in /tmp folder
gchamon Feb 28, 2023
177d3dc
add hardened database with false-positives avoiding configs
gchamon Feb 28, 2023
1ac4456
minor fix for logging download definition
gchamon Feb 28, 2023
ffa3c6b
merging clamdscan for scan.conf support and lower scan timers
gchamon Mar 1, 2023
6c1d36d
fix sqlite db persistence for fangfrisch
gchamon Mar 1, 2023
97037a0
optimize build times
gchamon Mar 1, 2023
64fbd46
optimize dockerfile
gchamon Mar 1, 2023
7962ef5
fix libpcre.so not being added to zip
gchamon Mar 1, 2023
310d112
compress missing files into a single log line
gchamon Mar 1, 2023
6fc216a
fix orphan config in freshclam.conf
gchamon Mar 1, 2023
22d0b4e
fix config in freshclam.conf; separate freshclam and scan conf runs
gchamon Mar 1, 2023
bd196a9
logging freshclam output as a list of strings
gchamon Mar 1, 2023
e6102f9
compressing not downloading and md5 matches into single line
gchamon Mar 1, 2023
d4ebd2d
fix freshclam output split
gchamon Mar 1, 2023
0e95de6
download freshclam defs before running fangfrisch
gchamon Mar 1, 2023
bb8fe9f
breaking updater if freshclam panics
gchamon Mar 1, 2023
d0209e0
downloading ALL extra files definitions, even if same prefix
gchamon Mar 2, 2023
02cc4fa
only print older files and md5 matches if they exist
gchamon Mar 2, 2023
2ba7b19
fix infection deletion log
gchamon Mar 2, 2023
5ac7593
using aws lambda stage image for fangfrisch to avoid shebang rewrite
gchamon Mar 2, 2023
2b76b8c
uploading all extra definition files
gchamon Mar 2, 2023
111af04
refactored upload defs to merge all files into single list
gchamon Mar 2, 2023
c6eb891
fix typo
gchamon Mar 2, 2023
750f7f0
only add extra definition files to databases to upload if necessary
gchamon Mar 2, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .envrc.local.template
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ export TEST_KEY=""
# export AV_STATUS_SNS_PUBLISH_INFECTED
# export AV_TIMESTAMP_METADATA
# export CLAMAVLIB_PATH
# export CLAMSCAN_PATH
# export CLAMDSCAN_PATH
# export FRESHCLAM_PATH
139 changes: 104 additions & 35 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,50 +1,119 @@
FROM public.ecr.aws/lambda/python:3.7 AS cli_deps

COPY requirements-cli.txt requirements-cli.txt
RUN mkdir -p /opt/app/cli \
&& pip3 install --requirement requirements-cli.txt --target /opt/app/cli \
&& rm -rf /root/.cache/pip

FROM amazonlinux:2

# Set up working directories
RUN mkdir -p /opt/app
RUN mkdir -p /opt/app/build
RUN mkdir -p /opt/app/bin/

# Copy in the lambda source
WORKDIR /opt/app
COPY ./*.py /opt/app/
COPY requirements.txt /opt/app/requirements.txt
RUN mkdir -p \
/opt/app \
/opt/app/build \
/opt/app/bin \
/opt/app/python_deps \
/opt/app/cli

# Install packages
RUN yum update -y
RUN yum install -y cpio python3-pip yum-utils zip unzip less
RUN yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

# This had --no-cache-dir, tracing through multiple tickets led to a problem in wheel
RUN pip3 install -r requirements.txt
RUN rm -rf /root/.cache/pip
RUN yum update -y \
&& amazon-linux-extras install epel -y \
&& yum install -y \
cpio \
yum-utils \
tar.x86_64 \
gzip \
zip \
python3-pip \
shadow-utils.x86_64 \
&& yum clean all \
&& rm -rf /var/cache/yum

# Download libraries we need to run in lambda
WORKDIR /tmp
RUN yumdownloader -x \*i686 --archlist=x86_64 clamav clamav-lib clamav-update json-c pcre2 libprelude gnutls libtasn1 lib64nettle nettle
RUN rpm2cpio clamav-0*.rpm | cpio -idmv
RUN rpm2cpio clamav-lib*.rpm | cpio -idmv
RUN rpm2cpio clamav-update*.rpm | cpio -idmv
RUN rpm2cpio json-c*.rpm | cpio -idmv
RUN rpm2cpio pcre*.rpm | cpio -idmv
RUN rpm2cpio gnutls* | cpio -idmv
RUN rpm2cpio nettle* | cpio -idmv
RUN rpm2cpio lib* | cpio -idmv
RUN rpm2cpio *.rpm | cpio -idmv
RUN rpm2cpio libtasn1* | cpio -idmv

# Copy over the binaries and libraries
RUN cp /tmp/usr/bin/clamscan /tmp/usr/bin/freshclam /tmp/usr/lib64/* /opt/app/bin/
RUN yumdownloader -x \*i686 --archlist=x86_64 \
clamav \
clamav-lib \
clamav-update \
clamav-scanner-systemd \
elfutils-libs \
json-c \
lz4 \
pcre2 \
systemd-libs \
libtool-ltdl \
libxml2 \
bzip2-libs \
xz-libs \
libprelude \
gnutls \
nettle \
&& rpm2cpio clamav-0*.rpm | cpio -vimd \
&& rpm2cpio clamav-lib*.rpm | cpio -vimd \
&& rpm2cpio clamav-update*.rpm | cpio -vimd \
&& rpm2cpio json-c*.rpm | cpio -vimd \
&& rpm2cpio pcre*.rpm | cpio -vimd \
&& rpm2cpio libtool-ltdl*.rpm | cpio -vimd \
&& rpm2cpio libxml2*.rpm | cpio -vimd \
&& rpm2cpio bzip2-libs*.rpm | cpio -vimd \
&& rpm2cpio xz-libs*.rpm | cpio -vimd \
&& rpm2cpio libprelude*.rpm | cpio -vimd \
&& rpm2cpio gnutls*.rpm | cpio -vimd \
&& rpm2cpio nettle*.rpm | cpio -vimd \
&& rpm2cpio clamd-0*.rpm | cpio -idmv \
&& rpm2cpio elfutils-libs*.rpm | cpio -idmv \
&& rpm2cpio lz4*.rpm | cpio -idmv \
&& rpm2cpio systemd-libs*.rpm | cpio -idmv \
&& cp -r \
/tmp/usr/bin/clamdscan \
/tmp/usr/sbin/clamd \
/tmp/usr/bin/freshclam \
/tmp/usr/lib64/* \
/usr/lib64/libpcre.so* \
Copy link
Contributor Author

@gchamon gchamon Mar 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

must copy all libpcre.so variants using wildcard, because libpcre.so.1 is a symbolic link sometimes

/opt/app/bin/ \
&& rm -rf /tmp/usr

# Fix the freshclam.conf settings
RUN echo "DatabaseMirror database.clamav.net" > /opt/app/bin/freshclam.conf
RUN echo "CompressLocalDatabase yes" >> /opt/app/bin/freshclam.conf
RUN echo "DatabaseMirror database.clamav.net" > /opt/app/bin/freshclam.conf \
&& echo "CompressLocalDatabase yes" >> /opt/app/bin/freshclam.conf \
&& echo "ScriptedUpdates no" >> /opt/app/bin/freshclam.conf \
&& echo "DatabaseDirectory /var/lib/clamav" >> /opt/app/bin/freshclam.conf
# clamd conf with hardened configs to avoid false positives
RUN echo "DatabaseDirectory /tmp/clamav_defs" > /opt/app/bin/scan.conf \
&& echo "PidFile /tmp/clamd.pid" >> /opt/app/bin/scan.conf \
&& echo "LogFile /tmp/clamd.log" >> /opt/app/bin/scan.conf \
&& echo "LocalSocket /tmp/clamd.sock" >> /opt/app/bin/scan.conf \
&& echo "FixStaleSocket yes" >> /opt/app/bin/scan.conf \
&& echo "DetectPUA yes" >> /opt/app/bin/scan.conf \
&& echo "ExcludePUA PUA.Win.Packer" >> /opt/app/bin/scan.conf \
&& echo "ExcludePUA PUA.Win.Trojan.Packed" >> /opt/app/bin/scan.conf \
&& echo "ExcludePUA PUA.Win.Trojan.Molebox" >> /opt/app/bin/scan.conf \
&& echo "ExcludePUA PUA.Win.Packer.Upx" >> /opt/app/bin/scan.conf \
&& echo "ExcludePUA PUA.Doc.Packed" >> /opt/app/bin/scan.conf

# Create the zip file
RUN groupadd clamav \
&& useradd -g clamav -s /bin/false -c "Clam Antivirus" clamav \
&& useradd -g clamav -s /bin/false -c "Clam Antivirus" clamupdate

ENV LD_LIBRARY_PATH=/opt/app/bin
RUN ldconfig

# Copy in the lambda source
WORKDIR /opt/app
RUN zip -r9 --exclude="*test*" /opt/app/build/lambda.zip *.py bin
COPY requirements.txt /opt/app/requirements.txt

WORKDIR /usr/local/lib/python3.7/site-packages
RUN zip -r9 /opt/app/build/lambda.zip *
# This had --no-cache-dir, tracing through multiple tickets led to a problem in wheel
RUN pip3 install --requirement requirements.txt --target /opt/app/python_deps \
&& rm -rf /root/.cache/pip

# Copy fangfrisch CLI from lambda image
COPY --from=cli_deps /opt/app/cli /opt/app/cli

# Create the zip file
COPY ./*.py /opt/app/
COPY fangfrisch.conf /opt/app/fangfrisch.conf
RUN zip -r9 --exclude="*test*" /opt/app/build/lambda.zip *.py *.conf bin cli \
&& cd /opt/app/python_deps \
&& zip -r9 /opt/app/build/lambda.zip *

WORKDIR /opt/app
51 changes: 26 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,31 +81,32 @@ can cause a continuous loop of scanning if improperly configured.
Runtime configuration is accomplished using environment variables. See
the table below for reference.

| Variable | Description | Default | Required |
| --- | --- | --- | --- |
| AV_DEFINITION_S3_BUCKET | Bucket containing antivirus definition files | | Yes |
| AV_DEFINITION_S3_PREFIX | Prefix for antivirus definition files | clamav_defs | No |
| AV_DEFINITION_PATH | Path containing files at runtime | /tmp/clamav_defs | No |
| AV_SCAN_START_SNS_ARN | SNS topic ARN to publish notification about start of scan | | No |
| AV_SCAN_START_METADATA | The tag/metadata indicating the start of the scan | av-scan-start | No |
| AV_SIGNATURE_METADATA | The tag/metadata name representing file's AV type | av-signature | No |
| AV_STATUS_CLEAN | The value assigned to clean items inside of tags/metadata | CLEAN | No |
| AV_STATUS_INFECTED | The value assigned to clean items inside of tags/metadata | INFECTED | No |
| AV_STATUS_METADATA | The tag/metadata name representing file's AV status | av-status | No |
| AV_STATUS_SNS_ARN | SNS topic ARN to publish scan results (optional) | | No |
| AV_STATUS_SNS_PUBLISH_CLEAN | Publish AV_STATUS_CLEAN results to AV_STATUS_SNS_ARN | True | No |
| AV_STATUS_SNS_PUBLISH_INFECTED | Publish AV_STATUS_INFECTED results to AV_STATUS_SNS_ARN | True | No |
| AV_TIMESTAMP_METADATA | The tag/metadata name representing file's scan time | av-timestamp | No |
| CLAMAVLIB_PATH | Path to ClamAV library files | ./bin | No |
| CLAMSCAN_PATH | Path to ClamAV clamscan binary | ./bin/clamscan | No |
| FRESHCLAM_PATH | Path to ClamAV freshclam binary | ./bin/freshclam | No |
| DATADOG_API_KEY | API Key for pushing metrics to DataDog (optional) | | No |
| AV_PROCESS_ORIGINAL_VERSION_ONLY | Controls that only original version of an S3 key is processed (if bucket versioning is enabled) | False | No |
| AV_DELETE_INFECTED_FILES | Controls whether infected files should be automatically deleted | False | No |
| EVENT_SOURCE | The source of antivirus scan event "S3" or "SNS" (optional) | S3 | No |
| S3_ENDPOINT | The Endpoint to use when interacting wth S3 | None | No |
| SNS_ENDPOINT | The Endpoint to use when interacting wth SNS | None | No |
| LAMBDA_ENDPOINT | The Endpoint to use when interacting wth Lambda | None | No |
| Variable | Description | Default | Required |
|----------------------------------|-------------------------------------------------------------------------------------------------|------------------|----------|
| AV_DEFINITION_S3_BUCKET | Bucket containing antivirus definition files | | Yes |
| AV_DEFINITION_S3_PREFIX | Prefix for antivirus definition files | clamav_defs | No |
| AV_DEFINITION_PATH | Path containing virus definition files at runtime | /tmp/clamav_defs | No |
| AV_SCAN_START_SNS_ARN | SNS topic ARN to publish notification about start of scan | | No |
| AV_SCAN_START_METADATA | The tag/metadata indicating the start of the scan | av-scan-start | No |
| AV_SIGNATURE_METADATA | The tag/metadata name representing file's AV type | av-signature | No |
| AV_STATUS_CLEAN | The value assigned to clean items inside of tags/metadata | CLEAN | No |
| AV_STATUS_INFECTED | The value assigned to clean items inside of tags/metadata | INFECTED | No |
| AV_STATUS_METADATA | The tag/metadata name representing file's AV status | av-status | No |
| AV_STATUS_SNS_ARN | SNS topic ARN to publish scan results (optional) | | No |
| AV_STATUS_SNS_PUBLISH_CLEAN | Publish AV_STATUS_CLEAN results to AV_STATUS_SNS_ARN | True | No |
| AV_STATUS_SNS_PUBLISH_INFECTED | Publish AV_STATUS_INFECTED results to AV_STATUS_SNS_ARN | True | No |
| AV_TIMESTAMP_METADATA | The tag/metadata name representing file's scan time | av-timestamp | No |
| AV_EXTRA_VIRUS_DEFINITIONS | Uses fangfrisch for extra antivirus definitions | False | No |
| CLAMAVLIB_PATH | Path to ClamAV library files | ./bin | No |
| CLAMDSCAN_PATH | Path to ClamAV clamdscan binary | ./bin/clamdscan | No |
| FRESHCLAM_PATH | Path to ClamAV freshclam binary | ./bin/freshclam | No |
| DATADOG_API_KEY | API Key for pushing metrics to DataDog (optional) | | No |
| AV_PROCESS_ORIGINAL_VERSION_ONLY | Controls that only original version of an S3 key is processed (if bucket versioning is enabled) | False | No |
| AV_DELETE_INFECTED_FILES | Controls whether infected files should be automatically deleted | False | No |
| EVENT_SOURCE | The source of antivirus scan event "S3" or "SNS" (optional) | S3 | No |
| S3_ENDPOINT | The Endpoint to use when interacting wth S3 | None | No |
| SNS_ENDPOINT | The Endpoint to use when interacting wth SNS | None | No |
| LAMBDA_ENDPOINT | The Endpoint to use when interacting wth Lambda | None | No |

## S3 Bucket Policy Examples

Expand Down
Loading