【发布时间】:2020-07-31 13:31:03
【问题描述】:
这是我第一次尝试 Perl,所以我知道这段代码很难看。其中一些来自不知道我在做什么,还有一些来自解决各种问题。我要做的是在文件(samplefile.txt)中搜索各种信息(9 个 parse_updates 函数),除非订单发生变化,否则它工作正常。例如,如果一个样本文件在僵尸网络定义之前有证书包,那么它将无法找到证书包信息。 我希望每个函数都开始搜索“新鲜”的示例文件,但情况似乎并非如此,我不知道为什么。 不包括示例文件,因为代码帖子已经足够长了,我认为问题出在我的功能逻辑上。
use strict;
use warnings;
use diagnostics;
use File::Slurp;
my @autoupdate;
my $autoupdate;
my $av_regex;
my @av_updates;
my $avdev_regex;
my @avdef_updates;
my $ipsatt_regex;
my @ipsatt_updates;
my $attdef_regex;
my @attdef_updates;
my $ipsmal_regex;
my @ipsmal_updates;
my $flowav_regex;
my @flowav_updates;
my $botnet_regex;
my @botnet_updates;
my $appdef_regex;
my @appdef_updates;
my $ipgeo_regex;
my @ipgeo_updates;
my $certbun_regex;
my @certbun_updates;
my $str1;
my $str2;
my $str3;
my $str4;
my $str5;
my $str6;
my $str7;
my $str8;
my $str9;
parse_updates1(); #AV Engine
parse_updates2(); #Virus Defs
parse_updates3(); #IPS Attack Engine
parse_updates4(); #Attack Defs
parse_updates5(); #IPS Mal URL DB
parse_updates6(); #Flow virus Defs
parse_updates7(); #Botnet Defs
parse_updates8(); #IP Geo DB
parse_updates9(); #Cert Bundle
sub parse_updates1{
print "\nTHIS IS AV Engine Section!!\n\n";
read_file('samplefile.txt', buf_ref => \$str1);
my $av_regex =qr/(AV Engine)(.*\n)*?(Version:)(.*\n)*?(Contract Expiry Date:)(.*\n)*?(Last Updated using )(.*\n)*?(Last Update Attempt: )(.*\n)*?(Result: )(.*\n).*/p;
if ( $str1 =~ /$av_regex/g ) {
#putting each regex group into the array
push @av_updates, $1, $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12;
#Removing new linefeeds
chomp @av_updates;
print "$_\n" for @av_updates;
}
else {
print "\n\nGot Nothing!\n\n";
@av_updates = qw(notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound);
print "$_\n" for @av_updates;
}
}
sub parse_updates2{
read_file('samplefile.txt', buf_ref => \$str2);
print "\nTHIS IS Virus Definitions Section!!\n\n";
my $avdef_regex =qr/(Application Definitions)(.*\n)*?(Version:)(.*\n)*?(Contract Expiry Date:)(.*\n)*?(Last Updated using )(.*\n)*?(Last Update Attempt: )(.*\n)*?(Result: )(.*\n).*/p;
if ( $str2 =~ /$avdef_regex/g ) {
#putting each regex group into the array
push @avdef_updates, $1, $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12;
#Removing new linefeeds
chomp @avdef_updates;
print "$_\n" for @avdef_updates;
}
else {
print "\n\nGot Nothing!\n\n";
@avdef_updates = qw(notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound);
print "$_\n" for @avdef_updates;
}
}
sub parse_updates3{
read_file('samplefile.txt', buf_ref => \$str3);
print "\nTHIS IS IPS Attack Engine Section!!\n\n";
my $ipsatt_regex =qr/(IPS Attack Engine)(.*\n)*?(Version:)(.*\n)*?(Contract Expiry Date:)(.*\n)*?(Last Updated using )(.*\n)*?(Last Update Attempt: )(.*\n)*?(Result: )(.*\n).*/p;
if ( $str3 =~ /$ipsatt_regex/g ) {
#putting each regex group into the array
push @ipsatt_updates, $1, $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12;
#Removing new linefeeds
chomp @ipsatt_updates;
print "$_\n" for @ipsatt_updates;
}
else {
print "\n\nGot Nothing!\n\n";
@ipsatt_updates = qw(notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound);
print "$_\n" for @ipsatt_updates;
}
}
sub parse_updates4{
read_file('samplefile.txt', buf_ref => \$str4);
print "\nTHIS IS Attack Definitions Section!!\n\n";
my $attdef_regex =qr/(Attack Definitions)(.*\n)*?(Version:)(.*\n)*?(Contract Expiry Date:)(.*\n)*?(Last Updated using )(.*\n)*?(Last Update Attempt: )(.*\n)*?(Result: )(.*\n).*/p;
if ( $str4 =~ /$attdef_regex/g ) {
#putting each regex group into the array
push @attdef_updates, $1, $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12;
#Removing new linefeeds
chomp @attdef_updates;
print "$_\n" for @attdef_updates;
}
else {
print "\n\nGot Nothing!\n\n";
@attdef_updates = qw(notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound);
print "$_\n" for @attdef_updates;
}
}
sub parse_updates5{
read_file('samplefile.txt', buf_ref => \$str5);
print "\nTHIS IS IPS Malicious URL Database Section!!\n\n";
my $ipsmal_regex =qr/(IPS Malicious URL Database)(.*\n)*?(Version:)(.*\n)*?(Contract Expiry Date:)(.*\n)*?(Last Updated using )(.*\n)*?(Last Update Attempt: )(.*\n)*?(Result: )(.*\n).*/p;
if ( $str5 =~ /$ipsmal_regex/g ) {
#putting each regex group into the array
push @ipsmal_updates, $1, $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12;
#Removing new linefeeds
chomp @ipsmal_updates;
print "$_\n" for @ipsmal_updates;
}
else {
print "\n\nGot Nothing!\n\n";
@ipsatt_updates = qw(notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound);
print "$_\n" for @ipsatt_updates;
}
}
sub parse_updates6{
read_file('samplefile.txt', buf_ref => \$str6);
print "\nTHIS IS Flow-Based Virus Definitions Section!!\n\n";
my $flowav_regex =qr/(Flow-based Virus Definitions)(.*\n)*?(Version:)(.*\n)*?(Contract Expiry Date:)(.*\n)*?(Last Updated using )(.*\n)*?(Last Update Attempt: )(.*\n)*?(Result: )(.*\n).*/p;
if ( $str6 =~ /$flowav_regex/g ) {
#putting each regex group into the array
push @flowav_updates, $1, $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12;
#Removing new linefeeds
chomp @flowav_updates;
print "$_\n" for @flowav_updates;
}
else {
print "\n\nGot Nothing!\n\n";
@flowav_updates = qw(notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound);
print "$_\n" for @flowav_updates;
}
}
sub parse_updates7{
read_file('samplefile.txt', buf_ref => \$str7);
print "\nTHIS IS Botnet Definitions Section!!\n\n";
my $botnet_regex =qr/(Botnet Definitions)(.*\n)*?(Version:)(.*\n)*?(Contract Expiry Date:)(.*\n)*?(Last Updated using )(.*\n)*?(Last Update Attempt: )(.*\n)*?(Result: )(.*\n).*/p;
if ( $str7 =~ /$botnet_regex/g ) {
#putting each regex group into the array
push @botnet_updates, $1, $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12;
#Removing new linefeeds
chomp @botnet_updates;
print "$_\n" for @botnet_updates;
}
else {
print "\n\nGot Nothing!\n\n";
@botnet_updates = qw(notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound);
print "$_\n" for @botnet_updates;
}
}
sub parse_updates8{
read_file('samplefile.txt', buf_ref => \$str8);
print "\nTHIS IS IP geography DB Section!!\n\n";
my $ipgeo_regex =qr/(IP Geography DB)(.*\n)*?(Version:)(.*\n)*?(Contract Expiry Date:)(.*\n)*?(Last Updated using )(.*\n)*?(Last Update Attempt: )(.*\n)*?(Result: )(.*\n).*/p;
if ( $str8 =~ /$ipgeo_regex/g ) {
#putting each regex group into the array
push @ipgeo_updates, $1, $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12;
#Removing new linefeeds
chomp @ipgeo_updates;
print "$_\n" for @ipgeo_updates;
}
else {
print "\n\nGot Nothing!\n\n";
@ipgeo_updates = qw(notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound);
print "$_\n" for @ipgeo_updates;
}
}
sub parse_updates9{
read_file('samplefile.txt', buf_ref => \$str9);
print "\nTHIS IS Certificate Bundle Section!!\n\n";
my $certbun_regex =qr/(Certificate Bundle)(.*\n)*?(Version:)(.*\n)*?(Contract Expiry Date:)(.*\n)*?(Last Updated using )(.*\n)*?(Last Update Attempt: )(.*\n)*?(Result: )(.*\n).*/p;
if ( $str9 =~ /$certbun_regex/g ) {
#putting each regex group into the array
push @certbun_updates, $1, $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12;
#Removing new linefeeds
chomp @certbun_updates;
print "$_\n" for @certbun_updates;
}
else {
print "\n\nGot Nothing!\n\n";
@certbun_updates = qw(notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound);
print "$_\n" for @certbun_updates;
}
# End of sub parse_updates
}
【问题讨论】:
-
if ( /.../g )没有意义。删除g?否则,看得太多了。请缩小问题范围 -
对于一个合理的问题来说太过分了。不必要的:你所有的函数都做同样的事情,完全相同(据我所知,通过快速浏览所有这些。)所以你需要一个带有模式的数组,一个函数。还有很多其他细节,但首先——甚至没有数据样本,任何正则表达式都可能是问题所在。我建议重新处理这个问题。
-
我对“需要调试细节”投了赞成票。请不要误解为“需要更多代码”。相反,请提供minimal reproducible example。
-
需要明确的是,无意义的
g可能是您的问题的原因。但我只看了你的代码 5 秒钟。 -
正如我在回答中所暗示的那样,我也发现
if (/.../g)可能是造成麻烦的 原因(不是责怪你!)。我现在在答案的末尾添加了一个部分,详细说明了这可能是如何发生的。但是,如果没有看到一些数据,就不能说任何确定的东西。