Unix Shell脚本-以特定模式监视登录失败
我正在寻找一种方法来完成我的一个需求,其中我希望使用unixshell脚本以特定模式自动监控“登录尝试失败”的日志文件 下面是一个日志片段Unix Shell脚本-以特定模式监视登录失败,shell,unix,monitoring,logfile,Shell,Unix,Monitoring,Logfile,我正在寻找一种方法来完成我的一个需求,其中我希望使用unixshell脚本以特定模式自动监控“登录尝试失败”的日志文件 下面是一个日志片段 sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=
sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
注意:日志文件中的“结果代码=2”表示登录失败
下面是需求和模式
- 一致地监视日志文件(access.log)
- 如果在同一用户的日志文件中发现无效密码 在一分钟内触发超过50次电子邮件,其中包含用户ID、客户端IP、浏览器IP、尝试运行失败次数
- 观察日志并持续进行此操作
作为一名业余的shell脚本开发人员,我无法想象使用shell脚本实现这一点。寻求想法/解决方案 shell中的哑方法是只
tail-f
您的日志,通过管道将其传输到awk
以剪切结果代码和用户数据,如果一行表示登录尝试失败,则增加存储在用户登录名为的文件(例如名为jdoe@example.com
包含5
)。但这会给您带来一个问题,即如何仅在所需的时间段内(例如,在最后一分钟内)保持该计数
除非你真的喜欢这种痛苦,否则看看一些常用的工具,比如logwatch
,它们是专门为你想做什么而设计的
以下是一些很棒的文章,通过其他定制服务的示例,介绍如何使用logwatch
设置自定义日志监视和报告:
tail-f
您的日志,通过管道将其传输到awk
以剪切结果代码和用户数据,如果一行表示登录尝试失败,则增加存储在用户登录名文件(例如名为jdoe@example.com
包含5
)。但这会给您带来一个问题,即如何仅在所需的时间段内(例如,在最后一分钟内)保持该计数
除非你真的喜欢这种痛苦,否则看看一些常用的工具,比如logwatch
,它们是专门为你想做什么而设计的
以下是一些很棒的文章,通过其他定制服务的示例,介绍如何使用logwatch
设置自定义日志监视和报告:
$ cat login.log
sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe2@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe2@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe2@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
我们将有:
$ sparrow check run myhost login-check
/tmp/.outthentic/22011/home/vagrant/my/logdog/story.t ..
ok 1 - stdout is already set
ok 2 - stdout saved to /tmp/.outthentic/22011/MSUYnYelwg
# history: 1 months
# filter: result_code=2
# density: 1
# group jdoe@example.com count: 2
# sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
# sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
# group jdoe2@example.com count: 3
# sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe2@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
# sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe2@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
# sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe2@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
ok 3 - output match /lines count: (\d+)/
ok 4 - output match /result_code=2/
1..4
ok
All tests successful.
Files=1, Tests=4, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.10 cusr 0.00 csys = 0.12 CPU)
Result: PASS
出于您的目的,应调整ini文件:
- 历史=5分钟
- 时区=美国/纽约 (有关正确的值,请参考此项,这可能是最短时区)
- 密度=50
$ cat login.log
sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe2@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe2@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe2@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
我们将有:
$ sparrow check run myhost login-check
/tmp/.outthentic/22011/home/vagrant/my/logdog/story.t ..
ok 1 - stdout is already set
ok 2 - stdout saved to /tmp/.outthentic/22011/MSUYnYelwg
# history: 1 months
# filter: result_code=2
# density: 1
# group jdoe@example.com count: 2
# sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
# sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
# group jdoe2@example.com count: 3
# sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe2@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
# sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe2@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
# sequence_number=12345,remote_client=sapserver,2016-03-18 03:29:44:782 EDT,messageID=1002,user=jdoe2@example.com,client_ip_address=10.129.220.45,client_port=10250,browser_ip_address=x.x.x.x,result_code=2,result_action=Login Failure,result_reason=Invalid Password
ok 3 - output match /lines count: (\d+)/
ok 4 - output match /result_code=2/
1..4
ok
All tests successful.
Files=1, Tests=4, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.10 cusr 0.00 csys = 0.12 CPU)
Result: PASS
出于您的目的,应调整ini文件:
- 历史=5分钟
- 时区=美国/纽约 (有关正确的值,请参考此项,这可能是最短时区)
- 密度=50
PS>披露-我是sparrow和logdog插件的作者。看看awk-,可能会让你走上正确的方向。您可以剪切列并按模式分组-许多示例可能有助于查看
fail2ban
,看看它是否能满足您的需要?谢谢您的输入Etan。我们不会在尝试使用如此糟糕的密码时阻止用户IP地址。作为管理员,我们只希望脚本通过电子邮件向自己发送有关用户的信息,并且在一两分钟内连续不断地尝试失败。Hariboo,不确定这种方法是否有助于实时日志监控,并且只获取符合我们监控标准的用户的数据。您可以使用fail2ban
执行任意操作,不仅仅是阻止IP。仔细看看。看看awk-,可能会让你走上正确的方向。您可以剪切列并按模式分组-许多示例可能有助于查看fail2ban
,看看它是否能满足您的需要?谢谢您的输入Etan。我们不会在尝试使用如此糟糕的密码时阻止用户IP地址。作为管理员,我们只希望脚本通过电子邮件向自己发送有关用户的信息,并且在一两分钟内连续不断地尝试失败。Hariboo,不确定这种方法是否有助于实时日志监控,并且只获取符合我们监控标准的用户的数据。您可以使用fail2ban
执行任意操作,不仅仅是阻止IP。仔细看看。