我正在编写一个小的 bash 脚本,它会搜索一个字符串,对其进行解码,然后回显结果。但是,我正在解析的日志文件具有以下结构:
<filename/path to file> <signature>
到目前为止,我只提取签名,通过正则表达式处理它们,然后解码与正则表达式匹配的任何内容。我还想输出与我这样 grep 的签名对应的文件:
<filename/path to file> <decoded signature>
<filename/path to file> <decoded signature>
我当前的输出如下:
<decoded signature>
<decoded signature>
这是我的脚本:
#!/bin/bash
read -p $'\e[1;33mLogfile\e[0m: ' sigs
parse=$( awk 'NF > 1 {print $2}' "$sigs")
Array=($( grep -ra "$parse" /var/lib/clamav | grep -oP "(?<=^|[*{};])[A-Fa-f0-9]+(?=$|[*;{}])"))
for hex in "${Array[@]}"; do
converted="$(xxd -r -p <<< "$hex")"
echo -e "\e[92m$converted \e[0m"
done
如果我将日志文件的所有内容存储在一个数组中,其中元素是文件名, key 是解码后的签名,这会是个好主意吗?
更新
日志文件 (logfile.txt) -> 我正在解析的内容;
/public_html/n0g6v/content/execution-after-redirect.html: {LDB}VT-malware33.UNOFFICIAL FOUND
/public_html/n0g6v/paypal-gateway.html: Html.Exploit.CVE.2015_6073
/var/lib/clamav/daily.cld -> 我在其中获取用于解码的签名的 HEX 值;
Html.Exploit.CVE_2015_6073;Engine:51-255,Target:3;0&1;696e7365727461646a6163656e7468746d6c;6164646576656e746c697374656e6572{-30}646f6d6e6f646572656d6f766564*737761706e6f6465
============================================= ==========
示例输入:
logfile.txt
输出:
/public_html/n0g6v/content/execution-after-redirect.html:
/public_html/n0g6v/paypal-gateway.html:
insertadjacenthtml
-------------------------------------------------------------------------------
/public_html/n0g6v/content/execution-after-redirect.html:
/public_html/n0g6v/paypal-gateway.html:
addeventlistener
============================================= ==========
我希望它是怎样的:
示例输入:
logfile.txt
输出:
/public_html/n0g6v/content/execution-after-redirect.html:
<No match found for this signature>
/public_html/n0g6v/paypal-gateway.html:
insertadjacenthtml
addeventlistener
最佳答案
awk
助您一臂之力!
如果您的查找文件不是很大,加载到 awk
数组并从您的日志文件中搜索字段 2,如果找到则调用您的转换脚本并打印结果。
例如,这段代码应该很清楚..
$ awk 'NR==FNR{split($0,a,";");
lookup[a[1]]=$0; next}
{inlookup=$2 in lookup;
print $2;
if(!inlookup)
{print "<No match found for this signature>";
next}
}
{split(lookup[$2],h,";");
for(i=4;i<=length(h);i++)
{cmd="wc -c <<< \"" h[i] "\"";
cmd | getline d; print d, h[i]}}' daily logfile
{LDB}VT-malware33.UNOFFICIAL
<No match found for this signature>
Html.Exploit.CVE_2015_6073
37 696e7365727461646a6163656e7468746d6c
83 6164646576656e746c697374656e6572{-30}646f6d6e6f646572656d6f766564*737761706e6f6465
这里我没有解码而是 wc -c
但同样的原则适用。另请注意,由于 .
和 _
不匹配,给定的两个文件的值不匹配,需要修复拼写错误才能正常工作。
关于regex - 使用 awk 返回相应的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35874251/