| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
set -euo pipefail |
| 29 |
|
| 30 |
RULES_REPO="https://github.com/Neo23x0/signature-base.git" |
| 31 |
WORK_DIR="/opt/makenotwork/yara-rules-src" |
| 32 |
LINK_DIR="/opt/makenotwork/yara-rules" |
| 33 |
CRON_FILE="/etc/cron.d/mnw-yara-rules" |
| 34 |
|
| 35 |
require_root() { |
| 36 |
if [ "$(id -u)" -ne 0 ]; then |
| 37 |
echo "[yara] error: must run as root" >&2 |
| 38 |
exit 1 |
| 39 |
fi |
| 40 |
} |
| 41 |
|
| 42 |
ensure_git() { |
| 43 |
if ! command -v git >/dev/null 2>&1; then |
| 44 |
DEBIAN_FRONTEND=noninteractive apt-get update -qq |
| 45 |
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends git |
| 46 |
fi |
| 47 |
} |
| 48 |
|
| 49 |
pull_or_clone() { |
| 50 |
if [ -d "$WORK_DIR/.git" ]; then |
| 51 |
echo "[yara] updating $WORK_DIR..." |
| 52 |
git -C "$WORK_DIR" fetch --depth=1 origin master |
| 53 |
git -C "$WORK_DIR" reset --hard origin/master |
| 54 |
else |
| 55 |
echo "[yara] cloning $RULES_REPO -> $WORK_DIR..." |
| 56 |
git clone --depth=1 "$RULES_REPO" "$WORK_DIR" |
| 57 |
fi |
| 58 |
} |
| 59 |
|
| 60 |
assemble_link_dir() { |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
echo "[yara] assembling $LINK_DIR..." |
| 65 |
rm -rf "$LINK_DIR" |
| 66 |
mkdir -p "$LINK_DIR" |
| 67 |
|
| 68 |
|
| 69 |
find "$WORK_DIR/yara" -type f \( -name '*.yar' -o -name '*.yara' \) -print0 \ |
| 70 |
| xargs -0 -I {} cp {} "$LINK_DIR/" |
| 71 |
local count |
| 72 |
count=$(ls -1 "$LINK_DIR" | wc -l) |
| 73 |
echo "[yara] $count rule files staged" |
| 74 |
} |
| 75 |
|
| 76 |
write_ruleset_version() { |
| 77 |
|
| 78 |
|
| 79 |
local sha |
| 80 |
sha=$(git -C "$WORK_DIR" rev-parse --short HEAD) |
| 81 |
local date |
| 82 |
date=$(git -C "$WORK_DIR" log -1 --format=%cI) |
| 83 |
cat > "$LINK_DIR/RULESET_VERSION" <<EOF |
| 84 |
repo=$RULES_REPO |
| 85 |
commit=$sha |
| 86 |
date=$date |
| 87 |
synced_at=$(date -u +%Y-%m-%dT%H:%M:%SZ) |
| 88 |
EOF |
| 89 |
echo "[yara] ruleset commit $sha ($date)" |
| 90 |
} |
| 91 |
|
| 92 |
install_cron() { |
| 93 |
cat > "$CRON_FILE" <<EOF |
| 94 |
# Pull upstream YARA ruleset updates weekly. Does not restart makenotwork — |
| 95 |
# the compiled rules in memory keep working until the next service restart. |
| 96 |
SHELL=/bin/bash |
| 97 |
PATH=/usr/sbin:/usr/bin:/sbin:/bin |
| 98 |
30 4 * * 1 root /opt/makenotwork/deploy/setup-yara-rules.sh >> /var/log/mnw-yara-update.log 2>&1 |
| 99 |
EOF |
| 100 |
chmod 644 "$CRON_FILE" |
| 101 |
echo "[yara] cron installed at $CRON_FILE (weekly, Mondays 04:30 UTC)" |
| 102 |
} |
| 103 |
|
| 104 |
main() { |
| 105 |
require_root |
| 106 |
ensure_git |
| 107 |
pull_or_clone |
| 108 |
assemble_link_dir |
| 109 |
write_ruleset_version |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
if [ ! -f "$CRON_FILE" ]; then |
| 114 |
install_cron |
| 115 |
fi |
| 116 |
|
| 117 |
cat <<EOF |
| 118 |
|
| 119 |
[yara] setup complete. Ruleset at $LINK_DIR. |
| 120 |
|
| 121 |
Next step — set the env var so makenotwork uses these rules: |
| 122 |
|
| 123 |
echo 'YARA_RULES_DIR=$LINK_DIR' >> /opt/makenotwork/.env |
| 124 |
systemctl restart makenotwork |
| 125 |
|
| 126 |
The Pipeline Health card for 'yara' will flip from down to ok after the |
| 127 |
first upload passes through the scan worker. Subsequent ruleset bumps land |
| 128 |
via the weekly cron; a manual 'systemctl restart makenotwork' picks up the |
| 129 |
newly-compiled rules. |
| 130 |
|
| 131 |
EOF |
| 132 |
} |
| 133 |
|
| 134 |
main "$@" |
| 135 |
|