HiveBrain v1.2.0
Get Started
← Back to all entries
patternbashMinor

Grep for pattern recursive and disable file

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
filedisablegreprecursiveforandpattern

Problem

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=

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.\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.

Solution

-
Bash idiom for looping over searchfiles is

for file in $searchfiles; do
    process "$file"


-
Invoking awk just to print a field looks like an overkill. Consider

read -d : str rest <<< "$file"

Code Snippets

for file in $searchfiles; do
    process "$file"
read -d : str rest <<< "$file"

Context

StackExchange Code Review Q#118642, answer score: 2

Revisions (0)

No revisions yet.