【发布时间】:2016-05-01 09:11:59
【问题描述】:
我有这个日志文件
New connection: 141.8.83.213:64400 (172.17.0.6:2222) [session: e696835c]
2016-04-29 21:13:59+0000 [SSHService ssh-userauth on HoneyPotTransport,3,141.8.83.213] login attempt [user1/test123] failed
2016-04-29 21:14:10+0000 [SSHService ssh-userauth on HoneyPotTransport,3,141.8.83.213] login attempt [user1/test1234] failed
2016-04-29 21:14:13+0000 [SSHService ssh-userauth on HoneyPotTransport,3,141.8.83.213] login attempt [user1/test123] failed
我想将这样的结果输出到文件:
Port,Status,Occurrences
64400,failed,2
64400,failed,1
“Occurrences”变量将表示已记录在文件中的登录详细信息[用户名和密码]组合的次数。 User1 test123 可以看到从同一个 IP 记录了两次。我怎样才能做到这一点?我现在有两个 while 循环,并且在第一个 while 循环中调用了一个子例程,如下所示:
子程序
sub counter(){
$result = 0;
#open(FILE2, $cowrie) or die "Can't open '$cowrie': $!";
while(my $otherlines = <LOG2>){
if($otherlines =~ /login attempt/){
($user, $password) = (split /[\s:\[\]\/]+/, $otherlines)[-3,-2];
if($_[1] =~ /$user/ && $_[2] =~ /$password/){
$result++;
}#if ip matches i think i have to do this with split
#print "TEST\n";
}
#print "Combo $_[0] and $_[1]\n";
}
#print "$result";
return $result;
}
主要方法
sub cowrieExtractor(){
open(FILE2, $cowrie) or die "Can't open '$cowrie': $!";
open(LOG2, $path2) or die "Can't open '$path2': $!";
$seperator = chr(42);
#To output user and password of login attempt, set $ip variable to the contents of array at that x position of new
#connection to match the ip of the login attempt
print FILE2 "SourcePort"."$seperator".
"Status"."$seperator"."Occurences"."$seperator"."Malicious"."\n";
$ip = "";
$port = "";
$usr = "";
$pass = "";
$status = "";
$frequency = 0;
#Given this is a user/pass attempt honeypot logger, I will use a wide character to reduce the possibility of stopping
#the WEKA CSV loader from functioning by using smileyface as seperators.
while(my $lines = <LOG2>){
if($lines =~ /New connection/){
($ip, $port) = (split /[\[\]\s:()]+/, $lines)[7,8];
}
if($lines =~ /login attempt/){#and the ip of the new connection
if($lines =~ /$ip/){
($usr, $pass, $status) = (split /[\s:\[\]\/]+/, $lines)[-3,-2,-1];
$frequency = counter($ip, $usr, $pass);
#print $frequency;
if($ip && $port && $usr && $pass && $status ne ""){
print FILE2 join "$seperator",($port, $status, $frequency, $end);
print FILE2 "\n";
}
}
}
}
}
现在在输出中Occurrences 下的输出中,我得到一个0,当我测试它似乎来自我在子例程中初始化变量$result 的内容。即0;这意味着子例程中的 if 语句无法正常工作。有什么帮助吗?
【问题讨论】:
-
你永远不应该在 Perl 子例程中使用原型。他们不会做你认为他们会做的事。只是
sub counter { ... }和sub cowrieExtractor { ... }是正确的 -
“尽可能不涉及散列” 散列正是解决此问题的正确工具。为什么要避开它们?
-
我不禁要问:为什么“不涉及哈希”?这意味着什么——一个根本不使用哈希数据类型的解决方案? (您的具体要求是什么?)
-
我很困惑您的输出示例显示 status 和 port 字段但没有用户信息,但您说要对信息进行分组仅由用户。这意味着每个计数都会混合使用状态和端口,并且无法像这样汇总数据
-
很抱歉没有涉及哈希部分。我刚刚做了一些研究,哈希非常适合这个我只是不知道如何很好地使用它们。
标签: perl logging pattern-matching subroutine