Created
July 15, 2021 21:33
-
-
Save bgeesaman/851ff902f00b5a5c9910f5f6c41d503a to your computer and use it in GitHub Desktop.
Generates a few fake files to exercise various malware scanning engines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eo pipefail | |
PARAMS="" | |
while (( "$#" )); do | |
case "$1" in | |
-d|--dest) | |
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then | |
DESTDIR=$2 | |
shift 2 | |
else | |
echo "Error: Argument for $1 is missing" >&2 | |
exit 1 | |
fi | |
;; | |
-t|--type) | |
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then | |
FILETYPE=$2 | |
shift 2 | |
else | |
echo "Error: Argument for $1 is missing" >&2 | |
exit 1 | |
fi | |
;; | |
-c|--count) | |
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then | |
FILECOUNT=$2 | |
shift 2 | |
else | |
echo "Error: Argument for $1 is missing" >&2 | |
exit 1 | |
fi | |
;; | |
-s|--size) | |
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then | |
FILESIZE=$2 | |
shift 2 | |
else | |
echo "Error: Argument for $1 is missing" >&2 | |
exit 1 | |
fi | |
;; | |
-*|--*=) # unsupported flags | |
echo "Error: Unsupported flag $1" >&2 | |
exit 1 | |
;; | |
*) # preserve positional arguments | |
PARAMS="$PARAMS $1" | |
shift | |
;; | |
esac | |
done | |
# set positional arguments in their proper place | |
eval set -- "$PARAMS" | |
if [ ! -d "${DESTDIR}" ]; then | |
echo "Error: Invalid destination directory. ${DESTDIR} does not exist." | |
exit 1 | |
fi | |
# Default to one file, one KB, of type ELF | |
FILECOUNT="${FILECOUNT:-1}" | |
FILESIZE="${FILESIZE:-1024}" | |
FILETYPE="${FILETYPE:-elf}" | |
function generate_basename { | |
chars=abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ | |
for i in {1..20} ; do | |
echo -n "${chars:RANDOM%${#chars}:1}" | |
done | |
} | |
function generate_elf { | |
DESTDIR="${1}" | |
FILESIZE="${2}" | |
BASENAME="$(generate_basename)" | |
FILE="${DESTDIR}/${BASENAME}.elf" | |
echo -n -e \\x7F\\x45\\x4C\\x46\\x02\\x01\\x01\\x03\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x3E\\x00\\x00\\x00\\x00\\x01 > "${FILE}" | |
openssl rand "${FILESIZE}" >> "${FILE}" | |
} | |
function generate_exe { | |
DESTDIR="${1}" | |
FILESIZE="${2}" | |
BASENAME="$(generate_basename)" | |
FILE="${DESTDIR}/${BASENAME}.exe" | |
echo -n -e \\x4D\\x5A\\00\\00 > "${FILE}" | |
openssl rand "${FILESIZE}" >> "${FILE}" | |
} | |
function generate_pdf { | |
DESTDIR="${1}" | |
FILESIZE="${2}" | |
BASENAME="$(generate_basename)" | |
FILE="${DESTDIR}/${BASENAME}.pdf" | |
echo -n -e \\x25\\x50\\x44\\x46\\x2D > "${FILE}" | |
openssl rand "${FILESIZE}" >> "${FILE}" | |
} | |
function generate_zip { | |
DESTDIR="${1}" | |
FILESIZE="${2}" | |
BASENAME="$(generate_basename)" | |
FILE="${DESTDIR}/${BASENAME}.zip" | |
echo -n -e \\x50\\x4B\\x03\\x04 > "${FILE}" | |
openssl rand "${FILESIZE}" >> "${FILE}" | |
} | |
function generate_png { | |
DESTDIR="${1}" | |
FILESIZE="${2}" | |
BASENAME="$(generate_basename)" | |
FILE="${DESTDIR}/${BASENAME}.png" | |
echo -n -e \\x89\\x50\\x4E\\x47\\x0D\\x0A\\x1A\\x0A > "${FILE}" | |
openssl rand "${FILESIZE}" >> "${FILE}" | |
} | |
function generate_file { | |
DESTDIR="${1}" | |
FILETYPE="${2}" | |
FILESIZE="${3}" | |
case "${FILETYPE}" in | |
elf|ELF) | |
generate_elf "${DESTDIR}" "${FILESIZE}" | |
;; | |
exe|EXE) | |
generate_exe "${DESTDIR}" "${FILESIZE}" | |
;; | |
pdf|PDF) | |
generate_pdf "${DESTDIR}" "${FILESIZE}" | |
;; | |
zip|ZIP) | |
generate_zip "${DESTDIR}" "${FILESIZE}" | |
;; | |
png|PNG) | |
generate_png "${DESTDIR}" "${FILESIZE}" | |
;; | |
*) | |
echo "Error: Invalid filetype passed." | |
exit 1 | |
;; | |
esac | |
} | |
echo -n "Writing to ${DESTDIR} ${FILECOUNT} ${FILETYPE} files of ${FILESIZE} bytes..." | |
for (( c=1; c<=$FILECOUNT; c++ )); do | |
generate_file "${DESTDIR}" "${FILETYPE}" "${FILESIZE}" | |
done | |
echo "done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment