#!/bin/sh # ACIDRIPP Threat Analysis Toolkit: scanner for macOS and Linux. # https://cybersecurity.acidripp.com/en/tools/threat-analysis-toolkit/ # Source and issues: https://github.com/MariusVava/acidripp-threat-toolkit # # What it does: compares this machine with what attackers did to ACIDRIPP's own decoys and servers. # 1. services listening for connections, against the ports attackers connect to # 2. SSH settings attackers rely on # 3. files with the names captured malware creates # 4. files in temporary folders, against the hashes of captured malware # 5. connections and start-up entries that mention the servers captured malware takes orders from # 6. secret files left inside web folders, from the list of files attackers ask websites for # 7. the machine's own logs, against the addresses that attacked us # # What it does not do: it changes nothing, installs nothing, and sends nothing about this machine anywhere. # It makes two downloads from cybersecurity.acidripp.com (the indicator list and the address list) and # writes one report file in the folder you run it from. Run it with sudo to include logs and settings # only the administrator can read; without sudo it says what it skipped. # # A match is a reason to look, not proof of a break-in. A clean result covers only what we have seen. # Licence: MIT for this script, CC BY 4.0 for the lists. No warranty. VERSION="1.0.1" BASE="${ACIDRIPP_TOOLKIT_BASE:-https://cybersecurity.acidripp.com}" PAGE="https://cybersecurity.acidripp.com/en/tools/threat-analysis-toolkit/" UA="acidripp-scan/$VERSION" OFFLINE="" WRITE_REPORT=1 set -u umask 077 LC_ALL=C; export LC_ALL usage() { cat </dev/null 2>&1; } HOME="${HOME:-/nonexistent}" seen_on() { # how many attackers we saw on a port, in words if [ "${1:-0}" -gt 0 ] 2>/dev/null; then printf '%s' "$1 attacker addresses connected to this port on our decoy in the last 30 days." else printf '%s' "Attackers look for this service, though none connected to it on our decoy in the last 30 days."; fi } OS="$(uname -s 2>/dev/null || echo unknown)" ROOT=0; [ "$(id -u 2>/dev/null || echo 1)" = "0" ] && ROOT=1 WORK="$(mktemp -d 2>/dev/null || mktemp -d -t acidripp)" || { echo "Could not create a temporary folder." >&2; exit 1; } trap 'rm -rf "$WORK"' EXIT INT TERM STAMP="$(date -u +%Y%m%d-%H%M%S)" REPORT="$WORK/report.txt"; : > "$REPORT" : > "$WORK/warn"; : > "$WORK/look"; : > "$WORK/skipped" out() { printf '%s\n' "$*"; printf '%s\n' "$*" >> "$REPORT"; } head1() { out ""; out "== $* =="; } ok() { out " ok $*"; } info() { out " info $*"; } look() { out " LOOK $*"; echo x >> "$WORK/look"; } warn() { out " WARN $*"; echo x >> "$WORK/warn"; } skip() { out " skip $*"; echo x >> "$WORK/skipped"; } more() { out " $*"; } count() { wc -l < "$1" | tr -d ' '; } fetch() { # fetch URL FILE if have curl; then curl -fsSL --max-time 90 -A "$UA" -o "$2" "$1" elif have wget; then wget -q -T 90 -U "$UA" -O "$2" "$1" else return 3; fi } out "ACIDRIPP Threat Analysis Toolkit scanner $VERSION" out "Machine: $(uname -srm 2>/dev/null) Started: $(date -u +%Y-%m-%dT%H:%M:%SZ) Administrator rights: $([ $ROOT = 1 ] && echo yes || echo no)" # ---- the lists --------------------------------------------------------------------------------------- if [ -n "$OFFLINE" ]; then cp "$OFFLINE/indicators.tsv" "$WORK/ind.tsv" 2>/dev/null || { echo "No indicators.tsv in $OFFLINE" >&2; exit 1; } cp "$OFFLINE/addresses.txt" "$WORK/addr.raw" 2>/dev/null || : > "$WORK/addr.raw" else fetch "$BASE/feed/toolkit/indicators.tsv" "$WORK/ind.tsv" || { echo "Could not download the indicator list from $BASE (needs curl or wget and a connection). See --offline." >&2; exit 1; } fetch "$BASE/feed/block/standard.plain" "$WORK/addr.raw" || : > "$WORK/addr.raw" fi TAB="$(printf '\t')" grep -q "^generated$TAB" "$WORK/ind.tsv" || { echo "The indicator list is not in the expected form; stopping." >&2; exit 1; } field() { awk -F '\t' -v k="$1" '$1 == k' "$WORK/ind.tsv"; } field port > "$WORK/ports"; field path > "$WORK/paths"; field exploit > "$WORK/exploits" field sha256 > "$WORK/hashes"; field control > "$WORK/control"; field dropped > "$WORK/dropped"; field user > "$WORK/users" grep -v '^#' "$WORK/addr.raw" | awk 'NF {print $1}' | sort -u > "$WORK/addr" GENERATED="$(awk -F '\t' '$1 == "generated" {print $2}' "$WORK/ind.tsv")" out "Lists generated: $GENERATED Attacker addresses: $(count "$WORK/addr") Ports: $(count "$WORK/ports") Web paths: $(count "$WORK/paths") Exploits: $(count "$WORK/exploits")" out "Malware hashes: $(count "$WORK/hashes") Malware servers: $(count "$WORK/control") Dropped file names: $(count "$WORK/dropped")" # ---- 1. listening services --------------------------------------------------------------------------- head1 "1. Services listening for connections" listeners() { if have ss; then ss -H -lnt 2>/dev/null | awk '{print $4}' elif [ "$OS" = "Darwin" ]; then netstat -an -p tcp 2>/dev/null | awk '/LISTEN/ {print $4}' elif have netstat; then netstat -lnt 2>/dev/null | awk '/LISTEN/ {print $4}' fi } listeners | awk '{ a = $1; n = match(a, /[:.][0-9]+$/); if (!n) next port = substr(a, n + 1); addr = substr(a, 1, n - 1); gsub(/[\[\]]/, "", addr); sub(/%.*$/, "", addr) scope = (addr ~ /^127\./ || addr == "::1" || addr ~ /^::ffff:127\./ || addr == "localhost") ? "local" : "all" if (scope == "all" || !(port in s)) s[port] = scope } END { for (p in s) print p "\t" s[p] }' > "$WORK/listen" if [ ! -s "$WORK/listen" ]; then if have ss || have netstat; then ok "Nothing is listening for connections."; else skip "Neither ss nor netstat is installed, so listening services could not be read."; fi else FOUND=0 while IFS="$TAB" read -r _ port name exposure seen; do scope="$(awk -F '\t' -v p="$port" '$1 == p {print $2}' "$WORK/listen")" [ "$scope" = "all" ] || continue FOUND=1 if [ "$exposure" = "never" ]; then warn "Port $port ($name) accepts connections from other machines. $(seen_on "$seen")" more "This service should never be reachable from the internet. Close it, bind it to 127.0.0.1, or allow only your own addresses in the firewall." else look "Port $port ($name) accepts connections from other machines. $(seen_on "$seen")" more "If it must be reachable, keep it updated and limit who can reach it." fi done < "$WORK/ports" [ $FOUND = 1 ] || ok "None of the $(count "$WORK/ports") services attackers look for is listening for other machines." info "$(awk -F '\t' '$2 == "all"' "$WORK/listen" | wc -l | tr -d ' ') ports in total accept connections from other machines, $(awk -F '\t' '$2 == "local"' "$WORK/listen" | wc -l | tr -d ' ') from this machine only." more "Whether the internet reaches them depends on your firewall or router; this check sees only the machine itself." fi if [ "$OS" = "Darwin" ] && [ -x /usr/libexec/ApplicationFirewall/socketfilterfw ]; then case "$(/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate 2>/dev/null)" in *enabled*) ok "The macOS firewall is on." ;; *disabled*) look "The macOS firewall is off (System Settings, Network, Firewall)." ;; esac fi # ---- 2. SSH ------------------------------------------------------------------------------------------ head1 "2. SSH server settings" SSH_LISTEN="$(awk -F '\t' '($1 == 22 || $1 == 2222) && $2 == "all"' "$WORK/listen" | wc -l | tr -d ' ')" sshd_value() { # the first value wins in sshd; included files usually come first key="$1"; v="" if [ $ROOT = 1 ] && have sshd; then v="$(sshd -T 2>/dev/null | awk -v k="$key" 'tolower($1) == k {print tolower($2); exit}')"; fi if [ -z "$v" ]; then for f in /etc/ssh/sshd_config.d/*.conf /etc/ssh/sshd_config; do [ -r "$f" ] || continue v="$(awk -v k="$key" 'tolower($1) == k {print tolower($2); exit}' "$f" 2>/dev/null)" [ -n "$v" ] && break done fi printf '%s' "$v" } if [ "$SSH_LISTEN" = "0" ]; then ok "No SSH server accepts connections from other machines." elif [ ! -r /etc/ssh/sshd_config ]; then skip "The SSH server settings cannot be read without administrator rights." else SSH_SEEN="$(awk -F '\t' '$2 == 22 {print $5}' "$WORK/ports")" PW="$(sshd_value passwordauthentication)"; [ -n "$PW" ] || PW="yes" RL="$(sshd_value permitrootlogin)"; [ -n "$RL" ] || RL="prohibit-password" if [ "$PW" = "yes" ]; then look "SSH accepts passwords, and guessing them is the commonest attack on our SSH decoy. $(seen_on "${SSH_SEEN:-0}")" more "Use keys and set 'PasswordAuthentication no' in /etc/ssh/sshd_config." NAMES="" while IFS="$TAB" read -r _ name seen; do [ "$name" = "root" ] && continue if [ "$OS" = "Darwin" ]; then dscl . -read "/Users/$name" UserShell 2>/dev/null | grep -qv 'false\|nologin' && dscl . -read "/Users/$name" UserShell >/dev/null 2>&1 && NAMES="$NAMES $name" else awk -F: -v n="$name" '$1 == n && $7 !~ /(nologin|false)$/ {f = 1} END {exit !f}' /etc/passwd 2>/dev/null && NAMES="$NAMES $name" fi done < "$WORK/users" [ -z "$NAMES" ] || { look "Accounts on this machine with names attackers try first:$NAMES"; more "With password sign-in on, these names are guessed all day. Make sure each has a long, unique password, or remove it."; } else ok "SSH does not accept passwords." fi if [ "$RL" = "yes" ] && [ "$PW" = "yes" ]; then warn "SSH lets root sign in with a password. 'root' is among the names attackers tried most on our decoy." more "Set 'PermitRootLogin prohibit-password' or 'no' in /etc/ssh/sshd_config." elif [ "$RL" = "yes" ]; then look "SSH lets root sign in directly (PermitRootLogin yes)." else ok "Root cannot sign in over SSH with a password (PermitRootLogin $RL)."; fi fi # ---- 3. dropped files -------------------------------------------------------------------------------- head1 "3. File names captured malware creates" if [ ! -s "$WORK/dropped" ]; then info "The list has no dropped file names at the moment."; else FOUND=0 while IFS="$TAB" read -r _ path seen; do if [ -e "$path" ] || [ -L "$path" ]; then FOUND=1 warn "$path exists. $seen attacker addresses created a file with this exact name on our decoy." more "$(ls -ld "$path" 2>/dev/null)" more "Do not run it. Check when it appeared and what created it; if you did not, treat the machine as broken into." fi done < "$WORK/dropped" [ $FOUND = 1 ] || ok "None of the $(count "$WORK/dropped") file names is present." fi # ---- 4. hashes --------------------------------------------------------------------------------------- head1 "4. Files in temporary folders, against captured malware" HASHER=""; have sha256sum && HASHER="sha256sum"; [ -z "$HASHER" ] && have shasum && HASHER="shasum -a 256" if [ ! -s "$WORK/hashes" ]; then info "The list has no malware hashes at the moment." elif [ -z "$HASHER" ]; then skip "Neither sha256sum nor shasum is installed, so files could not be compared." else awk -F '\t' '{print $2}' "$WORK/hashes" > "$WORK/hashlist" : > "$WORK/files" for d in /tmp /var/tmp /dev/shm /mnt /run/user /data/local/tmp; do [ -d "$d" ] && find "$d" -xdev -maxdepth 3 -type f -size -20000k 2>/dev/null | head -n 4000 >> "$WORK/files" done : > "$WORK/hashed" while IFS= read -r f; do $HASHER "$f" 2>/dev/null >> "$WORK/hashed"; done < "$WORK/files" if grep -F -f "$WORK/hashlist" "$WORK/hashed" > "$WORK/hashhits" 2>/dev/null && [ -s "$WORK/hashhits" ]; then while read -r h f; do warn "$f is a file our decoys captured from attackers (SHA-256 $h)." more "Do not run it. Disconnect the machine from the network and investigate how it arrived." done < "$WORK/hashhits" else ok "$(count "$WORK/hashed") files compared with $(count "$WORK/hashlist") captured malware files: no match." fi more "Checked: /tmp, /var/tmp, /dev/shm, /mnt and similar folders, three levels deep, files under 20 MB that this account can read." fi # ---- 5. malware servers ------------------------------------------------------------------------------ head1 "5. Servers captured malware takes orders from" if [ ! -s "$WORK/control" ]; then info "The list has no malware servers at the moment."; else awk -F '\t' '{ v = $3; if ($2 == "ip:port") sub(/:[0-9]+$/, "", v); if ($2 == "url") { sub(/^[a-z]+:\/\//, "", v); sub(/[\/:].*$/, "", v) } print v }' "$WORK/control" | sort -u > "$WORK/needles" peers() { if have ss; then ss -H -tn 2>/dev/null | awk '$1 != "LISTEN" {print $5}' elif have netstat; then netstat -an 2>/dev/null | awk '/^tcp/ && !/LISTEN/ {print $5}' fi } peers | awk '{ a = $1; n = match(a, /[:.][0-9]+$/); if (n) a = substr(a, 1, n - 1); gsub(/[\[\]]/, "", a); print a }' | sort -u > "$WORK/peers" if grep -F -x -f "$WORK/needles" "$WORK/peers" > "$WORK/peerhits" 2>/dev/null && [ -s "$WORK/peerhits" ]; then while read -r p; do warn "This machine has an open connection with $p, a server our captured malware downloads from or takes orders from." more "Find the program that holds the connection (ss -tnp or lsof -i) and disconnect the machine from the network." done < "$WORK/peerhits" else ok "No open connection with any of the $(count "$WORK/needles") malware servers."; fi FOUND=0 for f in /etc/crontab /etc/cron.d /etc/cron.hourly /etc/cron.daily /var/spool/cron /etc/rc.local /etc/systemd/system /etc/init.d \ /Library/LaunchDaemons /Library/LaunchAgents "$HOME/Library/LaunchAgents" "$HOME/.bashrc" "$HOME/.profile" "$HOME/.zshrc" "$HOME/.bash_profile"; do [ -e "$f" ] || continue grep -r -I -l -F -f "$WORK/needles" "$f" 2>/dev/null | head -n 20 | while read -r hit; do echo "$hit" >> "$WORK/starthits" done done if [ -s "${WORK}/starthits" ]; then sort -u "$WORK/starthits" | while read -r hit; do warn "$hit mentions one of the malware servers. Files in this place run by themselves at start-up or on a timer." more "Open the file and read the line; if you did not write it, treat the machine as broken into." done else ok "No start-up or timer entry mentions a malware server."; fi # A hosts line that sends a malware name to 0.0.0.0 or 127.0.0.1 is a block, not a sign of trouble; any other address is. if [ -r /etc/hosts ]; then awk '!/^[ \t]*#/ && $1 !~ /^(0\.0\.0\.0|127\.|::1$|::$)/' /etc/hosts | grep -F -f "$WORK/needles" > "$WORK/hostshits" 2>/dev/null if [ -s "$WORK/hostshits" ]; then while IFS= read -r line; do warn "/etc/hosts points a malware server name at an address: $line"; done < "$WORK/hostshits" more "Something wrote this line so the name always resolves. If you did not, treat the machine as broken into." fi fi [ $ROOT = 1 ] || more "Without administrator rights some timer folders cannot be read." fi # ---- 6. web folders ---------------------------------------------------------------------------------- head1 "6. Secret files inside web folders" : > "$WORK/webdirs" for r in /var/www /usr/share/nginx/html /srv/www /srv/http /usr/local/var/www /opt/homebrew/var/www /Library/WebServer/Documents /var/lib/tomcat*/webapps; do [ -d "$r" ] || continue echo "$r" >> "$WORK/webdirs" for d in "$r"/* "$r"/*/public "$r"/*/public_html "$r"/*/htdocs "$r"/*/web "$r"/*/current/public; do [ -d "$d" ] && echo "$d" >> "$WORK/webdirs"; done done for d in /home/*/public_html /home/*/www /home/*/htdocs; do [ -d "$d" ] && echo "$d" >> "$WORK/webdirs"; done sort -u "$WORK/webdirs" | head -n 80 > "$WORK/webdirs.u" if [ ! -s "$WORK/webdirs.u" ]; then ok "No web folder found in the usual places." more "If this machine serves a website from another folder, look there for .env, .git and backup files yourself." elif [ ! -s "$WORK/paths" ]; then info "The list has no web paths at the moment." else FOUND=0 while IFS= read -r d; do while IFS="$TAB" read -r _ path seen; do if [ -f "$d$path" ] && [ -s "$d$path" ]; then FOUND=1 warn "$d$path sits inside a web folder. $seen attacker addresses asked our websites for exactly this file." fi done < "$WORK/paths" done < "$WORK/webdirs.u" if [ $FOUND = 1 ]; then more "If the web server serves these files, anyone can read the secrets in them. Open the address in a browser to check," more "move the file out of the web folder, and change every password or key it holds." else ok "$(count "$WORK/webdirs.u") web folders checked for $(count "$WORK/paths") files attackers ask for: none present."; fi fi # ---- 7. logs ----------------------------------------------------------------------------------------- head1 "7. This machine's logs, against the addresses that attacked us" if [ ! -s "$WORK/addr" ]; then skip "The address list could not be downloaded."; else # sign-ins : > "$WORK/auth" for f in /var/log/auth.log /var/log/auth.log.1 /var/log/secure /var/log/secure.1; do [ -r "$f" ] && tail -n 300000 "$f" >> "$WORK/auth" 2>/dev/null; done if [ ! -s "$WORK/auth" ] && have journalctl; then journalctl -u ssh -u sshd --since "-14 days" --no-pager 2>/dev/null | tail -n 300000 > "$WORK/auth"; fi if [ -s "$WORK/auth" ]; then IPRE='[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|[0-9a-fA-F]*:[0-9a-fA-F:]+' grep -E 'Accepted (password|publickey|keyboard-interactive)' "$WORK/auth" | sed -E 's/.* for ([^ ]+) from ([^ ]+) .*/\2 \1/' | sort -u > "$WORK/accepted" grep -E 'Failed password|Invalid user|authentication failure|Connection closed by (authenticating|invalid) user' "$WORK/auth" | grep -oE " from ($IPRE)|rhost=($IPRE)" | sed -E 's/^ from //; s/^rhost=//' | sort -u > "$WORK/failed" awk 'NR == FNR {a[$1] = 1; next} ($1 in a)' "$WORK/addr" "$WORK/accepted" > "$WORK/acchits" if [ -s "$WORK/acchits" ]; then while read -r ip user; do warn "A sign-in over SSH SUCCEEDED from $ip as '$user'. That address attacked our decoys or servers." done < "$WORK/acchits" more "Unless that address is yours (a shared or reassigned address can be), treat the account as taken over: change its password and keys and review what it did." else ok "No successful SSH sign-in from an address that attacked us ($(count "$WORK/accepted") successful sign-in sources read)."; fi N="$(grep -F -x -c -f "$WORK/addr" "$WORK/failed" 2>/dev/null || true)" info "${N:-0} of the addresses that attacked us also tried to sign in to this machine and failed ($(count "$WORK/failed") failing sources in the log)." [ "${N:-0}" = "0" ] || more "That is normal for an SSH server on the internet. It shows the same attackers reach you; keys instead of passwords keep them out." elif [ "$OS" = "Darwin" ]; then info "macOS keeps sign-ins in its own log format, which this check does not read." elif [ $ROOT = 1 ]; then info "No sign-in log found in the usual places." else skip "Sign-in logs cannot be read without administrator rights."; fi # web server logs : > "$WORK/weblogs" for f in /var/log/nginx/*access*.log /var/log/nginx/*access*.log.1 /var/log/apache2/*access*.log /var/log/apache2/*access*.log.1 /var/log/apache2/access_log \ /var/log/httpd/*access*log /usr/local/var/log/nginx/*access*.log /opt/homebrew/var/log/nginx/*access*.log /var/log/caddy/*.log; do [ -r "$f" ] && echo "$f" >> "$WORK/weblogs" done if [ -s "$WORK/weblogs" ]; then : > "$WORK/web" head -n 30 "$WORK/weblogs" | while IFS= read -r f; do tail -n 300000 "$f" >> "$WORK/web" 2>/dev/null; done awk '{print $1}' "$WORK/web" | sort -u > "$WORK/webips" N="$(grep -F -x -c -f "$WORK/addr" "$WORK/webips" 2>/dev/null || true)" info "${N:-0} of the addresses that attacked us also visited this web server ($(count "$WORK/web") log lines, $(count "$WORK/webips") visitor addresses)." # secret files this server answered with success awk -F '\t' '{print $2}' "$WORK/paths" > "$WORK/pathlist" awk 'NR == FNR {p[$1] = 1; next} { u = $7; sub(/\?.*/, "", u); if ((u in p) && $9 ~ /^2/) c[u]++ } END { for (k in c) print c[k] "\t" k }' "$WORK/pathlist" "$WORK/web" | sort -rn > "$WORK/pathhits" if [ -s "$WORK/pathhits" ]; then while IFS="$TAB" read -r n path; do warn "This web server answered $n requests for $path with success. That is one of the files attackers ask websites for." done < "$WORK/pathhits" more "Open that address yourself. If it returns the real file, move it out of the web folder and change every secret in it." more "(Some sites answer every address with a normal page; then this is a false alarm.)" else ok "No request for a file attackers look for was answered with success."; fi # exploit attempts FOUND=0 while IFS="$TAB" read -r _ cve product pattern seen where; do [ "$where" = "product" ] && continue # also the product's everyday address: not an attempt total="$(grep -c -E -e "$pattern" "$WORK/web" 2>/dev/null || true)" [ "${total:-0}" -gt 0 ] 2>/dev/null || continue FOUND=1 good="$(grep -E -e "$pattern" "$WORK/web" 2>/dev/null | awk '$9 ~ /^2/' | wc -l | tr -d ' ')" if [ "$good" -gt 0 ]; then warn "$cve ($product): $total attempts in the log, $good answered with success." more "If this machine runs $product, check its version against the fix for $cve now." else look "$cve ($product): $total attempts in the log, none answered with success."; fi done < "$WORK/exploits" [ $FOUND = 1 ] || ok "None of the $(count "$WORK/exploits") exploit patterns we watch for appears in the web log." elif [ $ROOT = 1 ]; then info "No web server log found in the usual places." else info "No readable web server log found (they often need administrator rights)."; fi fi # ---- summary ----------------------------------------------------------------------------------------- W="$(count "$WORK/warn")"; L="$(count "$WORK/look")"; S="$(count "$WORK/skipped")" head1 "Summary" out " $W to act on (WARN), $L worth a look (LOOK), $S checks skipped." [ "$S" = "0" ] || [ $ROOT = 1 ] || out " Run it again with sudo to include the skipped checks." out " This compares your machine only with what ACIDRIPP has recorded so far. A clean result is not a clean bill of health." out " Nothing about this machine was sent anywhere. What the checks mean: $PAGE" out "" out " Block the addresses behind these attacks before they reach you: our free firewall feed loads into" out " most firewalls by itself and updates every few minutes. https://cybersecurity.acidripp.com/en/threat-feed/" if [ $WRITE_REPORT = 1 ]; then FILE="acidripp-scan-report-$STAMP.txt" if cp "$REPORT" "./$FILE" 2>/dev/null; then printf '\nReport saved: %s\n' "$(pwd)/$FILE"; else printf '\nThe report could not be saved in this folder.\n'; fi fi exit 0