Python - 创建在authlog中有超过5次失败登录尝试的IP地址的黑名单文件

发布于 2024-08-26 14:09:32 字数 289 浏览 6 评论 0原文

基本上我有一个 authlog/syslog 文件,其中包含登录尝试和 IP 地址的列表 - 我需要制作一个 Python 程序,该程序将创建一个 txt 文件,其中包含超过 5 次失败登录尝试的所有 IP 地址 - 一种“黑名单”。

所以基本上是这样的:

如果“uniqueipaddress”和“authentication failure”出现超过5次,则将uniqueipaddress添加到txt文件中。

任何帮助将不胜感激 - 请尝试使其变得简单,因为我在 Python 编程方面非常非常缺乏经验!谢谢。

Basically I have an authlog/syslog file with a list of log in attempts and IP addresses - I need to make a Python program that will create a txt file with all the IP addresses that have more than 5 failed login attempts - a sort of "blacklist".

So basically something like:

if "uniqueipaddress" and "authentication failure" appear more than 5 times, add uniqueipaddress to txt file.

Any help would be greatly appreciated - please try and make it simple as I am very, very inexperienced in programming in Python! Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稚然 2024-09-02 14:09:32

对于每一行:

  • 读取 IP 和尝试状态,
  • 按失败尝试次数的 IP 保存字典

然后查看字典:

  • 打印到文件中尝试 5 次或以上的所有 IP

Python 提示:

  • 逐行读取文件: for line in open(filename)
  • 解析日志行完全取决于它的格式。一些有用的Python工具是字符串的split方法,以及正则表达式
  • 保留字典,即ips[ip]是尝试次数

For each line:

  • read the IP and attempt status
  • keep a dictionary by IP of amount of failed attempts

Then go over the dictionary:

  • print to file all IPs with 5 or more attempts

Python hints:

  • To read a file line by line: for line in open(filename)
  • Parsing the log line depends entirely on its format. Some useful Python tools are the split method of a string, and regular expressions
  • Keep a dictionary, i.e. ips[ip] is amount of attempts
暖树树初阳… 2024-09-02 14:09:32

以下代码应该执行与您正在寻找的类似的操作。它并不完美,但它是一个很好的起点。

ips = {}
for line in open('your_log.txt'):
    parts = line.split(' ') #assuming this is a good place to split
    if parts[1] == "AuthenticationFailure":
        if parts[0] in ips:
            ips[parts[0]] += 1
        else:
            ips[parts[0]] = 0

for ip in [k for k,v in ips.iteritems() if v >= 5]:
    #WRITE TO FILE HERE

这假设您的日志文件的结构如下:

1.1.1.1 LoginSuccess
2.2.2.2 LoginSuccess
3.3.3.3 AuthenticationFailure

The following code should do something similar to what you're looking for. It's not perfect, but it's a good jumping off point.

ips = {}
for line in open('your_log.txt'):
    parts = line.split(' ') #assuming this is a good place to split
    if parts[1] == "AuthenticationFailure":
        if parts[0] in ips:
            ips[parts[0]] += 1
        else:
            ips[parts[0]] = 0

for ip in [k for k,v in ips.iteritems() if v >= 5]:
    #WRITE TO FILE HERE

This assumes that your log file is structured something like so:

1.1.1.1 LoginSuccess
2.2.2.2 LoginSuccess
3.3.3.3 AuthenticationFailure
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文