簡體   English   中英

使用awk返回相應的列

[英]Returning corresponding column using 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) ->我正在解析的內容;

/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- > 從中獲取簽名的十六進制值以進行解碼;

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但是同樣的原理適用。 另請注意,由於,給定的兩個文件的值不匹配. _不匹配,則需要修正拼寫錯誤才能使其正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM