Regex 使用awk返回相应的列
我正在编写一个小的bash脚本,它将搜索字符串,对其进行解码,然后返回结果。但是,我正在分析的日志文件的结构如下:Regex 使用awk返回相应的列,regex,linux,bash,shell,awk,Regex,Linux,Bash,Shell,Awk,我正在编写一个小的bash脚本,它将搜索字符串,对其进行解码,然后返回结果。但是,我正在分析的日志文件的结构如下: <filename/path to file> <signature> /var/lib/clamav/daily.cld->在这里我得到了用于解码的签名的十六进制值 Html.Exploit.CVE_2015_6073;Engine:51-255,Target:3;0&1;696e7365727461646a6163656e7468746d6c;
<filename/path to file> <signature>
/var/lib/clamav/daily.cld->在这里我得到了用于解码的签名的十六进制值
Html.Exploit.CVE_2015_6073;Engine:51-255,Target:3;0&1;696e7365727461646a6163656e7468746d6c;6164646576656e746c697374656e6572{-30}646f6d6e6f646572656d6f766564*737761706e6f6465
=========================================================
样本输入:
输出:
=========================================================
我希望它是怎样的:
样本输入:
输出:
/public\u html/n0g6v/content/execution-after-redirect.html:
/public_html/n0g6v/paypal-gateway.html:
插入式
注册侦听器
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
$awk'NR==FNR{split($0,a,“;”);
查找[a[1]=$0;下一步}
{inlookup=$2在查找中;
印刷2美元;
如果(!inlookup)
{打印“”;
下一个}
}
{拆分(查找[$2],h,“;”);
对于(i=4;i显示一些具体的(真实的或真实的虚假的)输入和所需的输出。我打赌您只需awk就可以轻松获得一个或几个答案。谢谢。使用示例输入/输出和所需的内容进行了更新。很抱歉,您能否详细说明一下?我尝试通过控制台运行该操作,并使用完整路径重命名文件,但无法正确执行.Got:awk:cmd.line:1:(FILENAME=/var/lib/clamav/daily.cld FNR=1793447)致命:dupnode:r->stptr:无法分配61字节的内存(无法分配内存)
。我的主要观点是自动化这个过程,我认为一个脚本可以做得很好,而不是包含/调用处理不同部分的外部脚本。“如果您的查找文件不是很大"不适用。它将不适合您的内存,因此您需要另一种方法。此awk脚本可以完全替换您的bash脚本,但唉……我只需要回显相应签名的文件名,所以我不明白为什么我必须用完全不同的方法替换整个脚本。不,没有need显然,在大多数情况下,与bash脚本相比,awk
在文本处理方面的表现力会更快(在文件适合内存的情况下)。
#!/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
/public_html/n0g6v/content/execution-after-redirect.html: {LDB}VT-malware33.UNOFFICIAL FOUND
/public_html/n0g6v/paypal-gateway.html: Html.Exploit.CVE.2015_6073
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 '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