ルールファイルがあります。各行にはsid:numberとrev:numberがあります。
このファイルを更新されたファイルと比較したいのですが、すべての行が更新されるわけではありません。
同じsid:numberを持つ行の1つのrev:numberが高い場合は、それをより高いrev:numberに置き換える必要があります。
これは私が現在行っている状況です。
grep -oP "sid:[0-9]{0,11}; rev:[0-9]{0,3}" all_rules.rules |
while read line; do
if grep -q "$line" /home/path/update_rules.rules; then
echo updated;
else
echo > /dev/null;
fi
done
以下は、all.rulesファイルの例です。
alert udp $HOME_NET any -> any 53 (msg:"ET
TROJAN CopyKittens? Matryoshka DNS Lookup 1 (winupdate64 . com)";
content:"|01 00 00 01 00 00 00 00 00 00|"; depth:10; offset:2;
content:"|0b|winupdate64|03|com|00|"; nocase; distance:0; fast_pattern;
reference:url,www.clearskysec.com/wp-
content/uploads/2017/07/Operation_Wilted_Tulip.pdf; classtype:trojan-
activity; sid:2024495; rev:1;)
alert udp $HOME_NET any -> any 53 (msg:"ET
TROJAN CopyKittens? Matryoshka DNS Lookup 2 (twiter-statics . info)";
content:"|01 00 00 01 00 00 00 00 00 00|"; depth:10; offset:2;
content:"|0e|twiter|2d|statics|04|info|00|"; nocase; distance:0;
fast_pattern; reference:url,www.clearskysec.com/wp-
content/uploads/2017/07/Operation_Wilted_Tulip.pdf;
reference:md5,752240cddda5acb5e8d026cef82e2b54; classtype:trojan-
activity; sid:2024496; rev:1;)
alert udp $HOME_NET any -> any 53 (msg:"ET TROJAN CopyKittens? Cobalt
Strike DNS Lookup (cloudflare-analyse . com)"; content:"|01 00 00 01 00
00 00 00 00 00|"; depth:10; offset:2;
content:"|12|cloudflare|2d|analyse|03|com|00|"; nocase; distance:0;
fast_pattern; threshold:type limit, track by_src, count 1, seconds 60;
reference:url,www.clearskysec.com/wp-
content/uploads/2017/07/Operation_Wilted_Tulip.pdf;
reference:md5,752240cddda5acb5e8d026cef82e2b54; classtype:trojan-
activity; sid:2024497; rev:1;)
以下はupdate.rulesの例です。
alert tcp $HOME_NET any -> $EXTERNAL_NET $HTTP_PORTS (msg:"ET TROJAN Revcode
RAT CnC"; flow:established,to_server; content:"POST"; http_method;
content:".php"; http_uri; content:"keyauth="; http_client_body;
fast_pattern; depth:8; content:"&key="; http_client_body; distance:0;
content:"&uid="; http_client_body; distance:0; content:!"Referer|3a|";
http_header; content:"WinHttpRequest"; http_header; metadata:
former_category TROJAN; reference:md5,3f652d9bc17a4be3c0e497ea19848344;
classtype:trojan-activity; sid:2024500; rev:1; metadata:affected_product
Windows_XP_Vista_7_8_10_Server_32_64_Bit, attack_target Client_Endpoint,
deployment Perimeter, signature_severity Major, created_at 2017_07_27,
performance_impact Moderate, updated_at 2017_07_27;)
alert udp $HOME_NET any -> any 53 (msg:"ET TROJAN CopyKittens? Matryoshka
DNS Lookup 1 (winupdate64 . com)"; content:"|01 00 00 01 00 00 00 00 00
00|"; depth:10; offset:2; content:"|0b|winupdate64|03|com|00|"; nocase;
distance:0; fast_pattern; reference:url,www.clearskysec.com/wp-
content/uploads/2017/07/Operation_Wilted_Tulip.pdf; classtype:trojan-
activity; sid:2024495; rev:2;)
alert udp $HOME_NET any -> any 53 (msg:"ET TROJAN CopyKittens Matryoshka DNS
Lookup 2 (twiter-statics . info)"; content:"|01 00 00 01 00 00 00 00 00
00|"; depth:10; offset:2; content:"|0e|twiter|2d|statics|04|info|00|";
nocase; distance:0; fast_pattern; metadata: former_category TROJAN;
reference:url,www.clearskysec.com/wp-
content/uploads/2017/07/Operation_Wilted_Tulip.pdf;
reference:md5,752240cddda5acb5e8d026cef82e2b54; classtype:trojan-activity;
sid:2024496; rev:2; metadata:affected_product
Windows_XP_Vista_7_8_10_Server_32_64_Bit, attack_target Client_Endpoint,
deployment Perimeter, signature_severity Major, created_at 2017_07_25,
malware_family Matryoshka, performance_impact Moderate, updated_at
2017_07_25;)
alert udp $HOME_NET any -> any 53 (msg:"ET TROJAN CopyKittens Cobalt Strike
DNS Lookup (cloudflare-analyse . com)"; content:"|01 00 00 01 00 00 00 00 00
00|"; depth:10; offset:2; content:"|12|cloudflare|2d|analyse|03|com|00|";
nocase; distance:0; fast_pattern; threshold:type limit, track by_src, count
1, seconds 60; metadata: former_category TROJAN;
reference:url,www.clearskysec.com/wp-
content/uploads/2017/07/Operation_Wilted_Tulip.pdf;
reference:md5,752240cddda5acb5e8d026cef82e2b54; classtype:trojan-activity;
sid:2024497; rev:2; metadata:affected_product
Windows_XP_Vista_7_8_10_Server_32_64_Bit, attack_target Client_Endpoint,
deployment Perimeter, signature_severity Major, created_at 2017_07_25,
malware_family CobaltStrike, performance_impact Moderate, updated_at
2017_07_26;)
alert tcp $HOME_NET any -> $EXTERNAL_NET $HTTP_PORTS (msg:"ET TROJAN
Win32/BanloadDownloader.XZY Retrieving Payload"; flow:to_server,established;
content:"GET"; http_method; content:"/sosdoudou_V3/"; http_uri;
fast_pattern; content:"WinHttp.WinHttpRequest"; http_header;
content:!"Accept-"; http_header; content:!"Referer|3a 20|"; http_header;
metadata: former_category TROJAN;
reference:md5,98376de10118892f0773617da137c2be
md5,599ea45f5420f948e0836239eb3ce772; classtype:trojan-activity;
sid:2024499; rev:2; metadata:affected_product
Windows_XP_Vista_7_8_10_Server_32_64_Bit, attack_target Client_Endpoint,
deployment Perimeter, signature_severity Major, created_at 2017_07_26,
malware_family Banload, performance_impact Moderate, updated_at 2017_07_26;)
3つの同じ規則がありますが、update.rulessid:2024497
には更新リビジョンが含まれています。 all.rulesファイルの古いバージョンをupdate.rulesの最新バージョンルールに置き換えたいと思います。sid:2024496
sid:2024495
答え1
これは最小限のテストで私に効果的です。
#!/bin/zsh
typeset -A rule sidrev
while read -r line; do
sid=${${line/*sid:/}/;*/}
rev=${${line/*rev:/}/;*/}
if [[ "$rev" -gt "$sidrev[$sid]" ]]; then
sidrev[$sid]="$rev"
rule[$sid]="$line"
fi
done
echo -E ${(F)rule}
スクリプトは stdin から snort ルールを読み込み、読み取ったすべてのルールの最新バージョンを stdout に出力します。
答え2
どのような答えを得るには、いくつかの仮定をします。これらの仮定は次のとおりです。
- 一方向更新:update_rules(または入力ファイル2)は、最新のコンテンツを含む参照ファイルです。
- 参照ファイルには、最新バージョンのみが含まれるか更新されるファイル(入力ファイル1)と少なくとも同じバージョンが含まれています。これはrev(IF2)をチェックするのではなく、実際に> = rev IF1自体であることを意味します。アヘドン!
- 私はIF1とIF2の違いに依存します。 (行の側面から)違いがどれだけ大きいかわかりません。これが影響を与える可能性があります。
とにかくブラブラはもうやめ、肉部分まで
#!/bin/bash
cp "$1" "copy_$1" #backup file we're going to change
#then only extract sid + rev (cut -f cols) + sort
#do that for both file and diff them with RCS format > see output of script for example
#and filter to keep only the sid (prob worth testing without grep to see output)
var=($(diff -n <(egrep -oe "sid:[0-9]{0,11}; rev:[0-9]{0,3}" $1|sort -k2) <(egrep -oe "sid:[0-9]{0,11}; rev:[0-9]{0,3}" $2|sort -k 2) | egrep -oe "sid:[0-9]{0,11};" ))
#Now loop over each SID
for i in ${var[@]}; do
#Extract line number in IF1
oldline=$(grep -n $i $1|cut -f1 -d:)
#Extract replacement line in IF2
newline=$(grep $i $2.txt)
#awk magic see (note sed was a pain, couldn't get it to work :/) >> https://askubuntu.com/questions/434051/how-to-replace-a-string-on-the-5th-line-of-multiple-text-files
awk -v nline="$oldline" -v repl="$newline" '(NR==nline){$0=repl}1;' "copy_$1" > "f.tmp" && mv "f.tmp" "copy_$1"
done
#then highlight diff between copy and original // you'll still need to mv "copy_$1" "$1" for it to be applied
diff -n "copy_$1" "$1"
これらすべてをスクリプトでラップして呼び出します。
./my_script.sh all_rules.rules /home/path/update_rules.rules
答え3
あなたはそれについて言及していませんが、あなたが言及したので、それはうまくいくとperl
思います。代わりに、マルチレベルの連想配列(用語では「Hash-of-Hashes」または「HoH」)を使用することが 。awk
sed
grep
perl
awk
perl
awk
#!/usr/bin/perl
use strict;
# array used to keep track of the order each sid was first seen,
# so that they can be printed out in the same order.
# Necessary because perl hashes are inherently un-ordered.
my @order=();
# hashed array to contain the highest rev seen of each sid.
my %S = ();
# count of the number of files we've read completely so far.
my $filenum=0;
while(<>) {
s/^\s*|\s*$//g; # strip leading and trailing spaces
if (m/^$/) { $filenum++ if eof; next }; # skip empty lines
# extract the sid and the rev
my ($sid, $rev) = $_ =~ (m/^.*; sid:(\d+); rev:(\d+)/) ;
# store or update an anonymous hash containing the rev and the entire
# line in the hash, keyed by the sid.
if (defined($S{$sid})) {
$S{$sid} = { rev => $rev, line => $_ } if ( ($rev > $S{$sid}->{rev}) );
} else {
next if ($filenum); # only store sid if we're still reading the 1st file.
push @order, $sid;
$S{$sid} = { rev => $rev, line => $_ };
};
$filenum++ if eof;
};
# if you want output sorted by the sid, comment the first of the next
# two lines and uncomment the second
for my $sid (@order) {
#for my $sid (sort keys %S) {
print $S{$sid}->{line}, "\n";
};
たとえば、別の名前で保存し、./apply-update.pl
次のように実行します。./apply-update.pl all.rules update.rules > out.rules
新しいサンプル入力ファイルの出力には3行しかありません(に示されているSIDがall.rules
更新されましたupdate.rules
。つまり、「rev:1」バージョンではなく「rev:2」)。
alert udp $home_net any -> any 53 (msg:"et trojan copykittens? matryoshka dns lookup 1 (winupdate64 . com)"; content:"|01 00 00 01 00 00 00 00 00 00|"; depth:10; offset:2; content:"|0b|winupdate64|03|com|00|"; nocase; distance:0; fast_pattern; reference:url,www.clearskysec.com/wp- content/uploads/2017/07/operation_wilted_tulip.pdf; classtype:trojan- activity; sid:2024495; rev:2;)
alert udp $home_net any -> any 53 (msg:"et trojan copykittens matryoshka dns lookup 2 (twiter-statics . info)"; content:"|01 00 00 01 00 00 00 00 00 00|"; depth:10; offset:2; content:"|0e|twiter|2d|statics|04|info|00|"; nocase; distance:0; fast_pattern; metadata: former_category trojan; reference:url,www.clearskysec.com/wp- content/uploads/2017/07/operation_wilted_tulip.pdf; reference:md5,752240cddda5acb5e8d026cef82e2b54; classtype:trojan-activity; sid:2024496; rev:2; metadata:affected_product windows_xp_vista_7_8_10_server_32_64_bit, attack_target client_endpoint, deployment perimeter, signature_severity major, created_at 2017_07_25, malware_family matryoshka, performance_impact moderate, updated_at 2017_07_25;)
alert udp $home_net any -> any 53 (msg:"et trojan copykittens cobalt strike dns lookup (cloudflare-analyse . com)"; content:"|01 00 00 01 00 00 00 00 00 00|"; depth:10; offset:2; content:"|12|cloudflare|2d|analyse|03|com|00|"; nocase; distance:0; fast_pattern; threshold:type limit, track by_src, count 1, seconds 60; metadata: former_category trojan; reference:url,www.clearskysec.com/wp- content/uploads/2017/07/operation_wilted_tulip.pdf; reference:md5,752240cddda5acb5e8d026cef82e2b54; classtype:trojan-activity; sid:2024497; rev:2; metadata:affected_product windows_xp_vista_7_8_10_server_32_64_bit, attack_target client_endpoint, deployment perimeter, signature_severity major, created_at 2017_07_25, malware_family cobaltstrike, performance_impact moderate, updated_at 2017_07_26;)
これは〜になりますたくさんベースバージョンよりも数倍高速です。bash
- シェルインタプリタのテキスト処理は、perl
または。このようなコンパイルされた言語はより速くなりますが、Cでこのようなものを書くには、おそらく15行のPerl(コメントや空白行を除く)の代わりに少なくとも50〜60行、おそらく数百行のCコードが必要になるでしょう。awk
python
C