我正在扫描/home/vmail/
子目录中是否有收到的电子邮件文本文件,如果匹配字符串则将其删除。经过优化的脚本要归功于this answer。
my_new_del() {
find /home/vmail -type f -name '*.some.file.pattern*' -exec grep -i -H -l -s "$1" {} + |
xargs rm -f {}
}
它像一个超级按钮一样工作,并删除与我经过的字符串匹配的文件。但是,我只是意识到某些文件具有base64编码的内容。这是一封垃圾邮件,内容是垃圾邮件,但外观如下:
Return-Path: <Bartybeve@aznetwork.net>
X-Original-To: info@my_domain.com
Delivered-To: info@my_domain.com
Received: by some.qdmn.com (Postfix, from userid 5000)
id D47C87F8CB; Thu, 11 Oct 2018 04:21:11 -0400 (EDT)
X-Original-To: info@my_domain.com
Delivered-To: info@my_domain.com
Received: from vlan131-44.aznetwork.net (unknown [185.129.1.44])
by some.qdmn.com (Postfix) with ESMTP id 1F1077F8C9
for info@my_domain.com Thu, 11 Oct 2018 04:21:05 -0400 (EDT)
Received: from unknown (60.233.87.144)
by mmx09.tilkbans.com with ESMTP; Thu, 11 Oct 2018 00:16:37 -0700
Received: from unknown (124.156.103.124)
by mailout.endmonthnow.com with ASMTP; Thu, 11 Oct 2018 00:10:28 -0700
Message-ID: <7B6B9A4E.9D85F307@aznetwork.net>
Date: Thu, 11 Oct 2018 00:10:28 -0700
Reply-To: "Anja" <Bartybeve@aznetwork.net>
From: "Anja" <Bartybeve@aznetwork.net>
User-Agent: Opera/7.02 (Windows ME; U)
MIME-Version: 1.0
To: "Anja" <info@my_domain.com>
Subject: I could not resist and pass by!
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: base64
PCFkb2N0eXBlIGh0bWw+DQo8aHRtbD4NCjxoZWFkPg0KPG1ldGEgY2hhcnNldD0idXRmLTgiPg0K
PC9oZWFkPg0KDQo8Ym9keT4NCjxwPjx0YWJsZSB3aWR0aD0iMTMlIiBib3JkZXI9IjAiPjx0Ym9k
eT48dHI+PHRkPjwvdGQ+PHRkPjwvdGQ+PHRkPjwvdGQ+PHRkPjwvdGQ+P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3RyPjwvdGJvZHk+PC90YWJsZT48L3A+DQo8YSBocmVm
PSJodHRwOi8vbG92ZWZvcnlvdS5zdSI+dW5zdWJzY3JpYmU8L2E+DQo8cD48dWw+PC91bD48L3A+
DQo8L2JvZHk+DQo8L2h0bWw+DQo=
因此,当我尝试使用别名bash命令查找具有与字符串匹配的内容的文件时,将不会标记类似上面的电子邮件文件。
我知道我可以使用echo 'some-base64-encoded-text' | base64 --decode
来解码消息。 web decoding tool确实向我表明,解码后的文本包含垃圾邮件部分。
我当时想先进行Content-Transfer-Encoding: base64
匹配的grep,然后找到Content-Transfer-Encoding: base64
字符串的索引,然后从那里解码消息,将其回显,然后进行grep进行匹配并删除文件(如果找到匹配项)。
但是,有一种简单的方法可以即时进行吗?
答案 0 :(得分:0)
这是一些perl。它需要MIME :: Base64(cpan install MIME::Base64
)
#!perl
use strict;
use warnings;
use autodie;
use MIME::Base64;
$/ = "";
for my $file (@ARGV) {
open my $fh, "<", $file;
my @paragraphs = <$fh>;
close $fh;
my $header = shift @paragraphs;
my $content;
if ($header =~ /Content-Transfer-Encoding: base64/) {
$content = decode_base64($paragraphs[0]);
}
else {
$content = join "\n\n", @paragraphs;
}
if ($content =~ /$ENV{pattern}/) {
print "delete: $file\n";
## unlink $file; # uncomment to really delete the file
}
}
然后您可以这样做:
find ... -exec env pattern="$1" perl email_scanner.pl +