Support setting rule repos via pillar

This commit is contained in:
Wes Lambert
2020-12-22 15:36:15 +00:00
parent d73f3bb6f8
commit ac96ded2dc
4 changed files with 56 additions and 42 deletions

View File

@@ -20,6 +20,7 @@ echo "Starting to check for yara rule updates at $(date)..."
output_dir="/opt/so/saltstack/default/salt/strelka/rules" output_dir="/opt/so/saltstack/default/salt/strelka/rules"
mkdir -p $output_dir mkdir -p $output_dir
repos="$output_dir/repos.txt" repos="$output_dir/repos.txt"
ignorefile="$output_dir/ignore.txt" ignorefile="$output_dir/ignore.txt"
@@ -95,55 +96,56 @@ clone_dir="/tmp"
if [ "$gh_status" == "200" ] || [ "$gh_status" == "301" ]; then if [ "$gh_status" == "200" ] || [ "$gh_status" == "301" ]; then
while IFS= read -r repo; do while IFS= read -r repo; do
if ! $(echo "$repo" | grep -qE '^#'); then
# Remove old repo if existing bc of previous error condition or unexpected disruption
repo_name=`echo $repo | awk -F '/' '{print $NF}'`
[ -d $repo_name ] && rm -rf $repo_name
# Remove old repo if existing bc of previous error condition or unexpected disruption # Clone repo and make appropriate directories for rules
repo_name=`echo $repo | awk -F '/' '{print $NF}'`
[ -d $repo_name ] && rm -rf $repo_name
# Clone repo and make appropriate directories for rules git clone $repo $clone_dir/$repo_name
echo "Analyzing rules from $clone_dir/$repo_name..."
mkdir -p $output_dir/$repo_name
[ -f $clone_dir/$repo_name/LICENSE ] && cp $clone_dir/$repo_name/LICENSE $output_dir/$repo_name
git clone $repo $clone_dir/$repo_name # Copy over rules
echo "Analyzing rules from $clone_dir/$repo_name..." for i in $(find $clone_dir/$repo_name -name "*.yar*"); do
mkdir -p $output_dir/$repo_name rule_name=$(echo $i | awk -F '/' '{print $NF}')
[ -f $clone_dir/$repo_name/LICENSE ] && cp $clone_dir/$repo_name/LICENSE $output_dir/$repo_name repo_sum=$(sha256sum $i | awk '{print $1}')
# Copy over rules # Check rules against those in ignore list -- don't copy if ignored.
for i in $(find $clone_dir/$repo_name -name "*.yar*"); do if ! grep -iq $rule_name $ignorefile; then
rule_name=$(echo $i | awk -F '/' '{print $NF}') existing_rules=$(find $output_dir/$repo_name/ -name $rule_name | wc -l)
repo_sum=$(sha256sum $i | awk '{print $1}')
# Check rules against those in ignore list -- don't copy if ignored. # For existing rules, check to see if they need to be updated, by comparing checksums
if ! grep -iq $rule_name $ignorefile; then if [ $existing_rules -gt 0 ];then
existing_rules=$(find $output_dir/$repo_name/ -name $rule_name | wc -l) local_sum=$(sha256sum $output_dir/$repo_name/$rule_name | awk '{print $1}')
if [ "$repo_sum" != "$local_sum" ]; then
# For existing rules, check to see if they need to be updated, by comparing checksums echo "Checksums do not match!"
if [ $existing_rules -gt 0 ];then echo "Updating $rule_name..."
local_sum=$(sha256sum $output_dir/$repo_name/$rule_name | awk '{print $1}') cp $i $output_dir/$repo_name;
if [ "$repo_sum" != "$local_sum" ]; then ((updatecounter++))
echo "Checksums do not match!" fi
echo "Updating $rule_name..." else
cp $i $output_dir/$repo_name; # If rule doesn't exist already, we'll add it
((updatecounter++)) echo "Adding new rule: $rule_name..."
cp $i $output_dir/$repo_name
((newcounter++))
fi fi
else fi;
# If rule doesn't exist already, we'll add it done
echo "Adding new rule: $rule_name..."
cp $i $output_dir/$repo_name
((newcounter++))
fi
fi;
done
# Check to see if we have any old rules that need to be removed # Check to see if we have any old rules that need to be removed
for i in $(find $output_dir/$repo_name -name "*.yar*" | awk -F '/' '{print $NF}'); do for i in $(find $output_dir/$repo_name -name "*.yar*" | awk -F '/' '{print $NF}'); do
is_repo_rule=$(find $clone_dir/$repo_name -name "$i" | wc -l) is_repo_rule=$(find $clone_dir/$repo_name -name "$i" | wc -l)
if [ $is_repo_rule -eq 0 ]; then if [ $is_repo_rule -eq 0 ]; then
echo "Could not find $i in source $repo_name repo...removing from $output_dir/$repo_name..." echo "Could not find $i in source $repo_name repo...removing from $output_dir/$repo_name..."
rm $output_dir/$repo_name/$i rm $output_dir/$repo_name/$i
((deletecounter++)) ((deletecounter++))
fi fi
done done
rm -rf $clone_dir/$repo_name rm -rf $clone_dir/$repo_name
fi
done < $repos done < $repos
echo "Done!" echo "Done!"

View File

@@ -55,6 +55,12 @@ strelkarules:
- source: salt://strelka/rules - source: salt://strelka/rules
- user: 939 - user: 939
- group: 939 - group: 939
strelkarepos:
file.managed:
- name: /opt/so/saltstack/default/salt/strelka/rules/repos.txt
- source: salt://strelka/rules/repos.txt.jinja
- template: jinja
{%- endif %} {%- endif %}

View File

@@ -0,0 +1,4 @@
# DO NOT EDIT THIS FILE! Strelka YARA rule repos are stored here from the strelka.repos pillar section
{%- for repo in salt['pillar.get']('strelka:repos', {}) %}
{{ repo }}
{%- endfor %}

View File

@@ -1260,6 +1260,8 @@ manager_global() {
"strelka:"\ "strelka:"\
" enabled: $STRELKA"\ " enabled: $STRELKA"\
" rules: 1"\ " rules: 1"\
" repos:"\
" - https://github.com/Neo23x0/signature-base"\
"curator:"\ "curator:"\
" hot_warm: False"\ " hot_warm: False"\
"elastic:"\ "elastic:"\