使用awk返回相应的列

时间:2016-03-08 17:40:29

标签: regex linux bash shell awk

我正在编写一个小的bash脚本,用于搜索字符串,对其进行解码然后回显结果。但是,我正在解析的日志文件采用以下结构:

<filename/path to file> <signature>

到目前为止,我只提取签名,通过正则表达式,然后解码与正则表达式匹配的任何内容。我还想输出与我这样的签名相对应的文件:

<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

如果我将日志文件的所有内容存储在一个数组中,其中元素是文件名并且密钥是其解码签名,那么这是一个好主意吗?

更新

日志文件(logfile.txt) - &gt;我在解析什么;

/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 - &gt;我正在获取用于解码的签名的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

1 个答案:

答案 0 :(得分:0)

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但是同样的原则适用。另请注意,由于._不匹配,给定的两个文件值不匹配,需要修复错误才能使其生效。