On a shared host, I'd like to setup a cron which scans folders recursively for some base64 malware strings. Therefore, I've written the following script:
#!/bin/bash
if [ $# -ne 1 ]; then
echo $0: usage: ./findone folder_to_start_with
exit 1
fi
folder=$1
IFS=$'\n'
searchfiles=($(grep -r -F -n -f malware-strings.dat $folder))
for (( i=0; i<${#searchfiles[@]}; i++ ));
do
STR=$(echo ${searchfiles[i]} | awk -F':' '{print $1}')
if [ -z "$STR" ];
then true;
else chmod 000 $STR;
fi
done
## Do something else like mail results etc.
printf '%s\n' "${searchfiles[@]}"
My locals test are doing what I expect. If a string pattern from "malware-string.dat" is found the file permission is changed to 000. Before scanning the production sites, I wanted to ask for a code review as I'm new to Bash and do not want to mess things up. Also, your judgement of disabling the file with chmod
is enough would help, or if it is advisable to move the file outside of the www directory.
chmod 000
(instead ofrm
) to allow later inspection. If that's the case, then I'd move the file to another directory: in some situations, files may be read even if they are-r
. \$\endgroup\$ – Andrea Corbellini Feb 2 '16 at 15:13else chmod 000 "$STR"
should do it? \$\endgroup\$ – Anatol Feb 2 '16 at 16:17