diff --git a/Dockerfile b/Dockerfile index 4bfc0ce..0e104da 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,22 +1,96 @@ -# v0.4.5 -FROM vuls/go-exploitdb@sha256:4738ca739083d41b89aab11012e283f89645f5cdbb5c021f2fbb6584bd5fe423 +FROM alpine:3.18 -# Make directory to store DB if not volume mounted -RUN mkdir /vuls +ARG TARGETPLATFORM -# Set up a cron job to update the database files every 3 hours -RUN echo "0 */3 * * * /update.sh" >> /var/spool/cron/crontabs/root +RUN apk --no-cache add git ca-certificates -# Make sure the cron job file has proper permissions -RUN chmod 0600 /var/spool/cron/crontabs/root +ADD --link --chmod=600 crontabs/root /var/spool/cron/crontabs/root -# Copy updater script and make it executable -COPY update.sh /update.sh -RUN chmod +x /update.sh +RUN </exploit-db-server: +docker run -d -p 1326:1326 -v /opt/exploit-db:/var/lib/go-exploitdb -name exploit-db-server /exploit-db-server: ``` ## Querying diff --git a/crontabs/root b/crontabs/root new file mode 100644 index 0000000..e718f3f --- /dev/null +++ b/crontabs/root @@ -0,0 +1 @@ +0 */3 * * * go-exploitdb-update diff --git a/entrypoint.sh b/entrypoint.sh deleted file mode 100644 index 8e49657..0000000 --- a/entrypoint.sh +++ /dev/null @@ -1,67 +0,0 @@ -#!/bin/sh - -set -euo pipefail - -cronPid=0 -serverPid=0 -firstUpdatePid=0 - -# gracefully handlei SIGINT and SIGTERM -term_handler() { - set +e - echo "Terminating..." - - if [ $firstUpdatePid -ne 0 ]; then - echo "Ending firstUpdate..." - kill -SIGTERM "$firstUpdatePid" - wait $firstUpdatePid - echo "firstUpdate ended." - fi - - if [ $cronPid -ne 0 ]; then - echo "Ending cron..." - kill -SIGTERM "$cronPid" - wait $cronPid - echo "Cron ended." - fi - - if [ $serverPid -ne 0 ]; then - echo "Ending server..." - kill -SIGTERM "$serverPid" - wait $serverPid - echo "Server ended." - fi - - exit 143 -} - -trap 'term_handler' SIGTERM -trap 'term_handler' SIGINT - -# Start go-exploitdb server listening and setup/migrate exploit db file -echo "Starting server listening on 0.0.0.0:1326..." -go-exploitdb server --bind 0.0.0.0 --dbpath /vuls/go-exploitdb.sqlite3 & -serverPid=$! - -# Wait until the server is up and running healthy and can respond to a query -# before starting the updater to avoid migration conflicts between the server -# command and the updating commands. -# TODO(sambetts) use curl here if we can get it installed in the container -sleep 5 - -# Run update once on container start to ensure we're up to date. -/update.sh & -firstUpdatePid=$! -wait $firstUpdatePid -firstUpdatePid=0 - -# Start cron in the background which will run the updater script periodically, -echo "Starting periodic updates..." -crond -f & -cronPid=$! - -# Wait on Server PID to complete, if it ends then terminate -wait $serverPid - -# Clean up everything -term_handler diff --git a/go-exploitdb/go-exploitdb-update.sh b/go-exploitdb/go-exploitdb-update.sh new file mode 100644 index 0000000..f1043bc --- /dev/null +++ b/go-exploitdb/go-exploitdb-update.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env sh + +set -e + +info () { + printf "INFO [go-exploitdb-update]: %s\n" "${1}" +} + +error () { + printf "ERROR [go-exploitdb-update]: %s\n" "${1}" +} + +update () { + local config + config=/etc/go-exploitdb/go-exploitdb.yaml + + for db in awesomepoc exploitdb githubrepos inthewild; do + info "updating database: ${db}" + go-exploitdb --config="${config}" fetch "${db}" || error "failed to update database: ${db}" + done +} + +main () { + ( + flock -n 200 || error "failed to acquire lock." + update + ) 200>/var/lock/go-exploitdb-update.lock +} + +main diff --git a/go-exploitdb/go-exploitdb.yaml b/go-exploitdb/go-exploitdb.yaml new file mode 100644 index 0000000..4fc5070 --- /dev/null +++ b/go-exploitdb/go-exploitdb.yaml @@ -0,0 +1,5 @@ +--- +dbtype: sqlite3 +dbpath: /var/lib/go-exploitdb/go-exploitdb.sqlite3 +log-json: false +debug: false diff --git a/s6-rc.d/cron/dependencies.d/base b/s6-rc.d/cron/dependencies.d/base new file mode 100644 index 0000000..e69de29 diff --git a/s6-rc.d/cron/dependencies.d/go-exploitdb-updater b/s6-rc.d/cron/dependencies.d/go-exploitdb-updater new file mode 100644 index 0000000..e69de29 diff --git a/s6-rc.d/cron/finish b/s6-rc.d/cron/finish new file mode 100644 index 0000000..ac7bcec --- /dev/null +++ b/s6-rc.d/cron/finish @@ -0,0 +1,7 @@ +#!/command/execlineb -S0 + +foreground { + redirfd -w 1 /run/s6-linux-init-container-results/exitcode echo "$1" +} + +/run/s6/basedir/bin/halt diff --git a/s6-rc.d/cron/run b/s6-rc.d/cron/run new file mode 100644 index 0000000..12e39f5 --- /dev/null +++ b/s6-rc.d/cron/run @@ -0,0 +1,3 @@ +#!/command/execlineb -P + +exec crond -f -d 7 diff --git a/s6-rc.d/cron/type b/s6-rc.d/cron/type new file mode 100644 index 0000000..5883cff --- /dev/null +++ b/s6-rc.d/cron/type @@ -0,0 +1 @@ +longrun diff --git a/s6-rc.d/go-exploitdb-updater/dependencies.d/base b/s6-rc.d/go-exploitdb-updater/dependencies.d/base new file mode 100644 index 0000000..e69de29 diff --git a/s6-rc.d/go-exploitdb-updater/type b/s6-rc.d/go-exploitdb-updater/type new file mode 100644 index 0000000..bdd22a1 --- /dev/null +++ b/s6-rc.d/go-exploitdb-updater/type @@ -0,0 +1 @@ +oneshot diff --git a/s6-rc.d/go-exploitdb-updater/up b/s6-rc.d/go-exploitdb-updater/up new file mode 100644 index 0000000..e16aab8 --- /dev/null +++ b/s6-rc.d/go-exploitdb-updater/up @@ -0,0 +1,3 @@ +#!/command/execlineb -P + +exec go-exploitdb-update diff --git a/s6-rc.d/go-exploitdb/dependencies.d/base b/s6-rc.d/go-exploitdb/dependencies.d/base new file mode 100644 index 0000000..e69de29 diff --git a/s6-rc.d/go-exploitdb/dependencies.d/go-exploitdb-updater b/s6-rc.d/go-exploitdb/dependencies.d/go-exploitdb-updater new file mode 100644 index 0000000..e69de29 diff --git a/s6-rc.d/go-exploitdb/finish b/s6-rc.d/go-exploitdb/finish new file mode 100644 index 0000000..ac7bcec --- /dev/null +++ b/s6-rc.d/go-exploitdb/finish @@ -0,0 +1,7 @@ +#!/command/execlineb -S0 + +foreground { + redirfd -w 1 /run/s6-linux-init-container-results/exitcode echo "$1" +} + +/run/s6/basedir/bin/halt diff --git a/s6-rc.d/go-exploitdb/run b/s6-rc.d/go-exploitdb/run new file mode 100644 index 0000000..c344b14 --- /dev/null +++ b/s6-rc.d/go-exploitdb/run @@ -0,0 +1,3 @@ +#!/command/execlineb -P + +exec go-exploitdb server --bind 0.0.0.0 --config=/etc/go-exploitdb/go-exploitdb.yaml diff --git a/s6-rc.d/go-exploitdb/type b/s6-rc.d/go-exploitdb/type new file mode 100644 index 0000000..5883cff --- /dev/null +++ b/s6-rc.d/go-exploitdb/type @@ -0,0 +1 @@ +longrun diff --git a/s6-rc.d/user/contents.d/cron b/s6-rc.d/user/contents.d/cron new file mode 100644 index 0000000..e69de29 diff --git a/s6-rc.d/user/contents.d/go-exploitdb b/s6-rc.d/user/contents.d/go-exploitdb new file mode 100644 index 0000000..e69de29 diff --git a/s6-rc.d/user/contents.d/go-exploitdb-updater b/s6-rc.d/user/contents.d/go-exploitdb-updater new file mode 100644 index 0000000..e69de29 diff --git a/update.sh b/update.sh deleted file mode 100644 index 9978384..0000000 --- a/update.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh - -set -euo pipefail - -echo "Updating from awesomepoc..." -go-exploitdb --dbpath /vuls/go-exploitdb.sqlite3 fetch awesomepoc || echo "Failed to fetch awesomepoc exploits db" - -echo "Updating from exploitdb..." -go-exploitdb --dbpath /vuls/go-exploitdb.sqlite3 fetch exploitdb || echo "Failed to fetch exploitdb exploits db" - -echo "Updating from githubrepos..." -go-exploitdb --dbpath /vuls/go-exploitdb.sqlite3 fetch githubrepos || echo "Failed to fetch githubrepos exploits db" - -echo "Updating from inthewild..." -go-exploitdb --dbpath /vuls/go-exploitdb.sqlite3 fetch inthewild || echo "Failed to fetch inthewild exploits db"