Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automate hash scans #54

Merged
merged 45 commits into from
Dec 5, 2023
Merged
Changes from 39 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
1e3acd6
Automate hash scans
bra1ncramp Nov 1, 2023
e70d285
Uncomment actual IOC scan
bra1ncramp Nov 1, 2023
6e48a1b
Trim trailing whitespace
bra1ncramp Nov 1, 2023
65d7858
Fix comments
bra1ncramp Nov 2, 2023
c1de882
Convert spaced to tabs to pass shfmt check
bra1ncramp Nov 2, 2023
055b633
Fix spacing to pass pre-commit
bra1ncramp Nov 2, 2023
2f375ea
Merge branch 'develop' into automate-hash-scans
bra1ncramp Nov 2, 2023
4cf7c30
Clean up comment
bra1ncramp Nov 14, 2023
5534d97
Correct spelling
bra1ncramp Nov 14, 2023
6e6b6a1
use long-form options in command
bra1ncramp Nov 14, 2023
17e7eb7
Use `dnf` and long form options
bra1ncramp Nov 14, 2023
f439c5f
Use long-form options in command
bra1ncramp Nov 14, 2023
05fe7fa
Use long-form options in command
bra1ncramp Nov 14, 2023
3a400f5
Improve comment wording
bra1ncramp Nov 14, 2023
4971ecb
Improve verbiage
bra1ncramp Nov 14, 2023
43a0fef
Remove unneeded capitalization
bra1ncramp Nov 15, 2023
321f8d0
Improve comment
bra1ncramp Nov 15, 2023
3b3d9d7
Use `StartNonInteractiveCommand`
bra1ncramp Nov 15, 2023
353a3a4
Improve error message
bra1ncramp Nov 15, 2023
e24b5b5
Fix typo, remove unnecessary capitalization, use long name for flag
bra1ncramp Nov 15, 2023
2b6d424
Use correct flag
bra1ncramp Nov 15, 2023
62910b6
Fix typo and add more info
bra1ncramp Nov 15, 2023
ea06489
Remove unnecessary capitalization
bra1ncramp Nov 15, 2023
2d15b8d
Use our standard comment spacing
bra1ncramp Nov 15, 2023
de13ca7
Add port number to output message
bra1ncramp Nov 15, 2023
3b8811d
Remove an unneeded word from a comment and an output message
bra1ncramp Nov 15, 2023
143fa45
Remove unnecessary capitalization
bra1ncramp Nov 15, 2023
07458ff
Fix typo and remove unnecessary capitalization
bra1ncramp Nov 15, 2023
d1ef1cd
Fix typo
bra1ncramp Nov 15, 2023
92c317b
Use our standard comment spacing
bra1ncramp Nov 15, 2023
153ca1c
Improve script usage blurb
bra1ncramp Nov 15, 2023
c5dbad1
Use correct environmental variable name
bra1ncramp Nov 15, 2023
8c75300
Reword and fix typo
bra1ncramp Nov 15, 2023
5deb667
Use long-form options where available
bra1ncramp Nov 15, 2023
05c6a7f
Use long-form options where available
bra1ncramp Nov 15, 2023
a2162cf
Declare environmental variables on command line
bra1ncramp Nov 15, 2023
9f1de57
Improve comment and fix spelling
bra1ncramp Nov 20, 2023
06d01ea
Check for environmental variable and fix comment
bra1ncramp Nov 20, 2023
93e1b32
Update shell syntax to satisfy shfmt linter
jsf9k Nov 20, 2023
1e8e677
Fix typos and prefer more standard "environment" terminology vs. "env…
bra1ncramp Dec 4, 2023
95b4a7c
Prefer more standard "environment" terminology vs. "environmental"
bra1ncramp Dec 4, 2023
e21a303
Use terminate-session instead of killing session
bra1ncramp Dec 4, 2023
840efd5
Do not archive extendend file attributes
bra1ncramp Dec 4, 2023
eb6fe08
Don't suppress stderr of pkill command
bra1ncramp Dec 4, 2023
5230f8d
Remove whitespace for linting
bra1ncramp Dec 4, 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
183 changes: 183 additions & 0 deletions extras/ioc_hash_scan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
#!/bin/bash
#
# This script will scan AWS instances for occurrences of IOC hashes listed in
# the blob at src/ioc_scan/ioc_scanner.py
#
# The filename specified in the first argument
# (instance-list-file) should contain a list of instance id strings, one per line.
#
# The following environmental varialbles must be set:
# AWS_SHARED_CREDENTAILS_FILE
bra1ncramp marked this conversation as resolved.
Show resolved Hide resolved
# AWS_REGION
# AWS_PROFILE
#
# Usage: ./ioc_hash_scan.sh instance-list-file
#
# This script assumes that it exists in the ioc-scanner/extras/ directory.
# If it does not, please edit the variable $pydir to point to
# the directory containing ioc_scanner.py

set -o nounset
set -o errexit
set -o pipefail

# Directory path of the ioc_scanner.py file
pydir="../src/ioc_scan/"

if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ $# -gt 1 ]; then
echo Usage: "$0" instance-list-file
exit 1
fi

# Check if instance list file exists.
if [ ! -f "$1" ]; then
echo Instance List file "$1" does not exist - exiting.
exit 1
fi

# Check if environmental variables are set
CRED=$(env | grep AWS_SHARED_CREDENTIALS_FILE)
REG=$(env | grep AWS_REGION)
PROF=$(env | grep AWS_PROFILE)
[[ -z "$CRED" ]] && {
echo "AWS_SHARED_CREDENTIALS_FILE environmental variable is not set."
exit 1
}
[[ -z "$REG" ]] && {
echo "AWS_REGION environmental variable is not set."
exit 1
}
[[ -z "$PROF" ]] && {
echo "AWS_PROFILE environmental variable is not set."
bra1ncramp marked this conversation as resolved.
Show resolved Hide resolved
exit 1
}

# Read instance id strings from file. [[ -n "$line" ]] handles the case where
# the last line doesn't end with a newline.
serverList=()
while IFS= read -r line || [[ -n "$line" ]]; do
serverList+=("$line")
done < "$1"

if [ ${#serverList[@]} -eq 0 ]; then
echo No instances found in "$1" - exiting.
exit 1
fi

today=$(date +%Y-%m-%d)
logfile="$HOME/$today-ioc-scanner-hashscan.log"

# Suppress some verbose stdout.
# Suppress stderr of pkill command
exec > >(grep --invert-match 'Starting\|Exiting')
exec 2> >(grep --invert-match 'SIGTERM')

## FUNCTIONS
function getOSType() {
OSNAME=$(aws ssm start-session --target="$i" \
--document=AWS-StartInteractiveCommand \
--parameters="command=cat /etc/os-release | grep --extended-regexp ^NAME=" \
--output text)
echo "$OSNAME" | grep NAME | cut -d'"' -f2 | cut -d' ' -f1
jsf9k marked this conversation as resolved.
Show resolved Hide resolved
}

function getHost() {
HOST=$(aws ssm start-session --target="$i" \
--document=AWS-StartNonInteractiveCommand \
--parameters="command=hostname" \
--output text)
echo "$HOST" | grep --invert-match "session" | sed '/^$/d'
}

function portForward() {
aws ssm start-session --target="$i" \
--document=AWS-StartPortForwardingSession --parameters="localPortNumber=5555,portNumber=6666"
}

function installNC() {
if [[ "$OS" == "Debian" ]]; then
aws ssm start-session --target="$i" \
--document=AWS-StartInteractiveCommand \
--parameters="command='sudo apt-get --yes install netcat'"
else
aws ssm start-session --target="$i" \
--document=AWS-StartInteractiveCommand \
--parameters="command='sudo dnf --assumeyes install netcat'"
fi
}

function startListen() {
if [[ "$OS" == "Debian" ]]; then
aws ssm start-session --target="$i" \
--document=AWS-StartInteractiveCommand \
--parameters="command='cd ~/src/ioc_scan; nc -l -p 6666 | tar --extract --gzip --file -'"
else
aws ssm start-session --target="$i" \
--document=AWS-StartInteractiveCommand \
--parameters="command='cd ~/src/ioc_scan; nc -l 6666 | tar --extract --gzip --file -'"
fi
}

## MAIN SCRIPT
echo "IOC Hash Scan - $today-$(date +%H:%M:%S)" > "$logfile"

for i in "${serverList[@]}"; do
OS="$(getOSType)"
if [[ "$OS" != "Debian" && "$OS" != "Fedora" ]]; then
echo "Instance $i is running an operating system ($OS) that is not supported by this script."
echo "Currently only Debian and Fedora are supported - exiting."
exit 1
fi

instanceName=$(getHost)

echo "Beginning scan of instance $i -- $instanceName" | tee -a "$logfile"

# Start Port Forwarding
echo "Beginning port forwarding (local port 5555 to remote port 6666)"
portForward &

# Create ~/src/ioc_scan directory on Instance
bra1ncramp marked this conversation as resolved.
Show resolved Hide resolved
echo "Verifying ~/src/ioc_scan directory on $instanceName"
aws ssm start-session --target="$i" \
--document=AWS-StartInteractiveCommand \
--parameters="command='if [ ! -d ~/src/ioc_scan ]; then mkdir --parents ~/src/ioc_scan; fi'"

#Install netcat and start listening on port 6666
bra1ncramp marked this conversation as resolved.
Show resolved Hide resolved
echo "Verifying netcat on $instanceName"
installNC
echo "Begin listening on $instanceName"
bra1ncramp marked this conversation as resolved.
Show resolved Hide resolved
startListen &

# Copy latest ioc_scanner.py to target instance
curdir=$(pwd)
cd "$pydir" || exit

echo "Upload lastest ioc_scanner.py to $instanceName"
bra1ncramp marked this conversation as resolved.
Show resolved Hide resolved
tar --create --gzip --file - ./ioc_scanner.py | nc localhost 5555

cd "$curdir" || exit

# Run ioc_scanner.py on target instance
echo "Scan $instanceName for IOC Hashes"
bra1ncramp marked this conversation as resolved.
Show resolved Hide resolved
aws ssm start-session --target="$i" \
--document=AWS-StartInteractiveCommand \
--parameters="command=python3 ~/src/ioc_scan/ioc_scanner.py" >> "$logfile"

# Killing port forwading so we can do this again on the next Instance.
bra1ncramp marked this conversation as resolved.
Show resolved Hide resolved
while pgrep -fq session-manager-plugin; do
jsf9k marked this conversation as resolved.
Show resolved Hide resolved
dav3r marked this conversation as resolved.
Show resolved Hide resolved
pkill session-manager-plugin
# We need to wait, as some race conditions can occure.
bra1ncramp marked this conversation as resolved.
Show resolved Hide resolved
sleep 5
done
echo "------------------------------------------------------------------------" | tee -a "$logfile"
dav3r marked this conversation as resolved.
Show resolved Hide resolved
done

##clean up log output for readability
bra1ncramp marked this conversation as resolved.
Show resolved Hide resolved
while grep --quiet --ignore-case "session" "$logfile"; do
sed -i '' '/session/d' "$logfile"
jsf9k marked this conversation as resolved.
Show resolved Hide resolved
done

sed -i '' 'N;/^\n$/D;P;D;' "$logfile"
jsf9k marked this conversation as resolved.
Show resolved Hide resolved

echo "Scan log may be found at: $logfile"
Loading