Skip to content

Instantly share code, notes, and snippets.

@phixion
Last active July 26, 2023 16:18
Show Gist options
  • Save phixion/0418401289bf6976c84048c2937de9bb to your computer and use it in GitHub Desktop.
Save phixion/0418401289bf6976c84048c2937de9bb to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# shellcheck disable=SC1091,SC2120,SC2317
# SOURCES
# https://askubuntu.com/a/978750
# https://unixcop.com/enabling-exfat-support-on-ubuntu-22-04/
# https://askubuntu.com/a/51926
# https://wiki.archlinux.org/title/ClamAV
# https://github.com/extremeshok/clamav-unofficial-sigs/blob/master/INSTALL.md
# https://magic-wormhole.readthedocs.io/en/latest/welcome.html#installation
cat << 'DESCRIPTION' >/dev/null
Run clamscan daemon (clamdscan) to scan Windows partition from a Linux live USB.
This script will:
1. Mount the Windows partition
2. Install dependencies
3. Install clamav-unofficial-sigs (unofficial virus signatures)
4. Configure clamav-unofficial-sigs
5. Run clamav-unofficial-sigs once
6. Run clamdscan with a log file in /tmp
7. Unmount the Windows partition
NOTE:
The windows computer has to shutdown gracefully in order to
mount the partition. If the computer was not shutdown gracefully,
you will have to run `sudo ntfsfix /dev/sdXY` to fix the partition.
DESCRIPTION
# Validate sudo access
if sudo -v; then
:
else
echo "Script must be run as root. Please re-run with admin credentials. Exiting... "
exit 1
fi
# logging
log_time=$(date +%Y%m%d_%H%M%S)
log_name="/tmp/clamdscan_${log_time}.log"
log_name="${log_name:-/tmp/clamdscan.log}"
# env vars
logged_in_user=$(logname)
logged_in_home=$(eval echo "~$logged_in_user")
github_user="phixion"
# mount windows partition
# * select by largest size (e.g., 451.8G is /dev/sda2)
# * exclude /dev/sdb* (e.g., 14.9G is a USB drive)
mount_win() {
# if no arg is passed, then select the largest drive
if [[ $# -eq 0 ]]; then
drive_options=$(sudo fdisk -l | grep -oP '/dev/sd[a-z][0-9]' | grep -vP '/dev/sdb')
echo "Select the drive to scan:"
PS3="Enter a number: "
select windows_drive in $drive_options; do
break
done
mkdir /mnt/win
mount -t ntfs -o nls=utf8,umask=0222 "$windows_drive" /mnt/win
return
else
# if arg is passed, then mount that drive
mkdir /mnt/win
mount -t ntfs -o nls=utf8,umask=0222 "$1" /mnt/win
return
fi
}
# install dependencies
install_deps() {
pamac install \
--no-confirm \
--upgrade \
--overwrite \
clamav \
curl \
exfatprogs \
htop \
magic-wormhole \
openssh \
python3 \
python-pip \
python-pipx \
vim \
wget \
xclip
}
# aliases
setup_aliases() {
# pbcopy alias
grep -qxF 'alias pbcopy='xclip -selection clipboard'' "$logged_in_home/.bash_aliases" || \
echo "alias pbcopy='xclip -selection clipboard'" >> "$logged_in_home/.bash_aliases"
# ll alias
grep -qxF 'alias ll='ls -alF'' "$logged_in_home/.bash_aliases" || \
echo "alias ll='ls -alF'" >> "$logged_in_home/.bash_aliases"
# source bashrc
source "$logged_in_home/.bash_aliases"
}
# import github public ssh keys
import_ssh_keys() {
# keyscan ~/.ssh/known_hosts
ssh-keyscan -t rsa github.com >> "${logged_in_home}/.ssh/known_hosts"
# import ssh keys
wget -O - "https://github.com/${github_user}.keys" | sudo tee -a "${logged_in_home}/.ssh/authorized_keys"
}
# wait for script to finish
wait_for_script() {
while [[ $(pgrep -f "$1") ]]; do
sleep 5
done
}
# install clamav-unofficial-sigs
setup_clamav-unofficial-sigs() {
sudo mkdir -p /usr/local/sbin/
sudo wget https://raw.githubusercontent.com/extremeshok/clamav-unofficial-sigs/master/clamav-unofficial-sigs.sh -O /usr/local/sbin/clamav-unofficial-sigs.sh && chmod 755 /usr/local/sbin/clamav-unofficial-sigs.sh
sudo mkdir -p /etc/clamav-unofficial-sigs/
sudo wget https://raw.githubusercontent.com/extremeshok/clamav-unofficial-sigs/master/config/master.conf -O /etc/clamav-unofficial-sigs/master.conf
sudo wget https://raw.githubusercontent.com/extremeshok/clamav-unofficial-sigs/master/config/user.conf -O /etc/clamav-unofficial-sigs/user.conf
# configure clamav-unofficial-sigs
os_conf="os.archlinux.conf"
sudo wget "https://raw.githubusercontent.com/extremeshok/clamav-unofficial-sigs/master/config/os/${os_conf}" -O /etc/clamav-unofficial-sigs/os.conf
# run script as root once
sudo /usr/local/sbin/clamav-unofficial-sigs.sh --force
wait_for_script clamav-unofficial-sigs.sh
# install logrotate and man files
sudo /usr/local/sbin/clamav-unofficial-sigs.sh --install-logrotate
wait_for_script clamav-unofficial-sigs.sh
sudo /usr/local/sbin/clamav-unofficial-sigs.sh --install-man
wait_for_script clamav-unofficial-sigs.sh
sudo mkdir -p /etc/systemd/system/
sudo wget https://raw.githubusercontent.com/extremeshok/clamav-unofficial-sigs/master/systemd/clamav-unofficial-sigs.service -O /etc/systemd/system/clamav-unofficial-sigs.service
sudo wget https://raw.githubusercontent.com/extremeshok/clamav-unofficial-sigs/master/systemd/clamav-unofficial-sigs.timer -O /etc/systemd/system/clamav-unofficial-sigs.timer
# enable and start clamav-unofficial-sigs
sudo systemctl enable clamav-unofficial-sigs.service
sudo systemctl enable clamav-unofficial-sigs.timer
sudo systemctl start clamav-unofficial-sigs.timer
}
# clamdscan
# * --multiscan: Scan using multiple threads
# * --fdpass: Pass the file descriptor permissions to clamd
# * --log: Log file to send scan results to
# * --remove: Remove infected files
# * --infected: Only print infected files
# * /media/windows: Scan this directory
clam_scan() {
declare -a args=(
--multiscan
--fdpass
--log="${log_name}"
--remove
--infected
)
if [[ ! -f "/tmp/filelist.txt" ]]; then
clamdscan "${args[@]}" "/mnt/win"
elif [[ -f "/tmp/filelist.txt" ]]; then
clamdscan "${args[@]}" --file-list=/tmp/filelist.txt
else
echo "No filelist found. Exiting..."
exit 1
fi
}
main() {
mount_win
install_deps
setup_aliases
import_ssh_keys
setup_clamav-unofficial-sigs
clam_scan
}
main "$@"
# unmount windows partition
if [[ $(mount | grep -c /mnt/win) -gt 0 ]]; then
sudo umount /mnt/win
fi
exit 0
#!/usr/bin/env bash
# shellcheck disable=SC1091,SC2120,SC2317
# SOURCES
# https://askubuntu.com/a/978750
# https://unixcop.com/enabling-exfat-support-on-ubuntu-22-04/
# https://askubuntu.com/a/51926
# https://wiki.archlinux.org/title/ClamAV
# https://github.com/extremeshok/clamav-unofficial-sigs/blob/master/INSTALL.md
# https://magic-wormhole.readthedocs.io/en/latest/welcome.html#installation
cat << 'DESCRIPTION' >/dev/null
Run clamscan daemon (clamdscan) to scan Windows partition from a Linux live USB.
This script will:
1. Mount the Windows partition
2. Install dependencies
3. Install clamav-unofficial-sigs (unofficial virus signatures)
4. Configure clamav-unofficial-sigs
5. Run clamav-unofficial-sigs once
6. Run clamdscan with a log file in /tmp
7. Unmount the Windows partition
NOTE:
The windows computer has to shutdown gracefully in order to
mount the partition. If the computer was not shutdown gracefully,
you will have to run `sudo ntfsfix /dev/sdXY` to fix the partition.
DESCRIPTION
# Validate sudo access
if sudo -v; then
:
else
echo "Script must be run as root. Please re-run with admin credentials. Exiting... "
exit 1
fi
# logging
log_time=$(date +%Y%m%d_%H%M%S)
log_name="/tmp/clamdscan_${log_time}.log"
log_name="${log_name:-/tmp/clamdscan.log}"
# env vars
logged_in_user=$(logname)
logged_in_home=$(eval echo "~$logged_in_user")
github_user="phixion"
# mount windows partition
# * select by largest size (e.g., 451.8G is /dev/sda2)
# * exclude /dev/sdb* (e.g., 14.9G is a USB drive)
mount_win() {
# if no arg is passed, then select the largest drive
if [[ $# -eq 0 ]]; then
drive_options=$(sudo fdisk -l | grep -oP '/dev/sd[a-z][0-9]' | grep -vP '/dev/sdb')
echo "Select the drive to scan:"
PS3="Enter a number: "
select windows_drive in $drive_options; do
break
done
mkdir /media/windows
mount -t ntfs -o nls=utf8,umask=0222 "$windows_drive" /media/windows
return
else
# if arg is passed, then mount that drive
mkdir /media/windows
mount -t ntfs -o nls=utf8,umask=0222 "$1" /media/windows
return
fi
}
# install dependencies
install_deps() {
sudo apt update
sudo apt install -y \
clamav \
clamav-daemon \
curl \
exfatprogs \
htop \
magic-wormhole \
openssh-server \
python3 \
python3-pip \
pipx \
ssh \
vim \
wget \
xclip
}
# aliases
setup_aliases() {
# pbcopy alias
grep -qxF 'alias pbcopy='xclip -selection clipboard'' "$logged_in_home/.bash_aliases" || \
echo "alias pbcopy='xclip -selection clipboard'" >> "$logged_in_home/.bash_aliases"
# ll alias
grep -qxF 'alias ll='ls -alF'' "$logged_in_home/.bash_aliases" || \
echo "alias ll='ls -alF'" >> "$logged_in_home/.bash_aliases"
# source bashrc
source "$logged_in_home/.bash_aliases"
}
# import github public ssh keys
import_ssh_keys() {
# keyscan ~/.ssh/known_hosts
ssh-keyscan -t rsa github.com >> "${logged_in_home}/.ssh/known_hosts"
# import ssh keys
wget -O - "https://github.com/${github_user}.keys" | sudo tee -a "${logged_in_home}/.ssh/authorized_keys"
}
# wait for script to finish
wait_for_script() {
while [[ $(pgrep -f "$1") ]]; do
sleep 5
done
}
# install clamav-unofficial-sigs
setup_clamav-unofficial-sigs() {
sudo mkdir -p /usr/local/sbin/
sudo wget https://raw.githubusercontent.com/extremeshok/clamav-unofficial-sigs/master/clamav-unofficial-sigs.sh -O /usr/local/sbin/clamav-unofficial-sigs.sh && chmod 755 /usr/local/sbin/clamav-unofficial-sigs.sh
sudo mkdir -p /etc/clamav-unofficial-sigs/
sudo wget https://raw.githubusercontent.com/extremeshok/clamav-unofficial-sigs/master/config/master.conf -O /etc/clamav-unofficial-sigs/master.conf
sudo wget https://raw.githubusercontent.com/extremeshok/clamav-unofficial-sigs/master/config/user.conf -O /etc/clamav-unofficial-sigs/user.conf
# configure clamav-unofficial-sigs
os_conf="os.ubuntu.conf"
sudo wget "https://raw.githubusercontent.com/extremeshok/clamav-unofficial-sigs/master/config/os/${os_conf}" -O /etc/clamav-unofficial-sigs/os.conf
# run script as root once
sudo /usr/local/sbin/clamav-unofficial-sigs.sh --force
wait_for_script clamav-unofficial-sigs.sh
# install logrotate and man files
sudo /usr/local/sbin/clamav-unofficial-sigs.sh --install-logrotate
wait_for_script clamav-unofficial-sigs.sh
sudo /usr/local/sbin/clamav-unofficial-sigs.sh --install-man
wait_for_script clamav-unofficial-sigs.sh
sudo mkdir -p /etc/systemd/system/
sudo wget https://raw.githubusercontent.com/extremeshok/clamav-unofficial-sigs/master/systemd/clamav-unofficial-sigs.service -O /etc/systemd/system/clamav-unofficial-sigs.service
sudo wget https://raw.githubusercontent.com/extremeshok/clamav-unofficial-sigs/master/systemd/clamav-unofficial-sigs.timer -O /etc/systemd/system/clamav-unofficial-sigs.timer
# enable and start clamav-unofficial-sigs
sudo systemctl enable clamav-unofficial-sigs.service
sudo systemctl enable clamav-unofficial-sigs.timer
sudo systemctl start clamav-unofficial-sigs.timer
}
# clamdscan
# * --multiscan: Scan using multiple threads
# * --fdpass: Pass the file descriptor permissions to clamd
# * --log: Log file to send scan results to
# * --remove: Remove infected files
# * --infected: Only print infected files
# * /media/windows: Scan this directory
clam_scan() {
declare -a args=(
--multiscan
--fdpass
--log="${log_name}"
--remove
--infected
)
if [[ ! -f "/tmp/filelist.txt" ]]; then
clamdscan "${args[@]}" "/media/windows"
elif [[ -f "/tmp/filelist.txt" ]]; then
clamdscan "${args[@]}" --file-list=/tmp/filelist.txt
else
echo "No filelist found. Exiting..."
exit 1
fi
}
main() {
mount_win
install_deps
setup_aliases
import_ssh_keys
setup_clamav-unofficial-sigs
clam_scan
}
main "$@"
# unmount windows partition
if [[ $(mount | grep -c /media/windows) -gt 0 ]]; then
sudo umount /media/windows
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment