摘要:最近电脑刚装上就中病毒了,wdnmd。但是面对一无所有的条件,自立更生,写了个用来监控445端口的教程。

import psutil
import time
import sys


def listen_port_connection(result):
    netstat = psutil.net_connections()
    for sconn in netstat:
        str_port = str(sconn.laddr.port)
        if sconn.status == 'ESTABLISHED' and str_port in result:
            if len(sconn.raddr) != 0:
                if sconn.raddr.ip not in result[str_port]:
                    result[str_port][sconn.raddr.ip] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())


if __name__ == "__main__":
    if len(sys.argv) <= 1:
        print("please input port number")
        exit()

    port_list = sys.argv[1:]
    result = {}
    for port in port_list:
        result[port] = {}

    try:
        while 1:
            listen_port_connection(result)
            print(result)
            time.sleep(1)
    except KeyboardInterrupt:
        for port in result.keys():
            f = open(sys.path[0] + "/" + port + ".txt", "w+")
            for ip, time in result[port].items():
                f.write(ip + " | " + time + "\n")
            f.close()
        exit()

用法:
假如同时监控445端口、22端口

python3 jiankong.py 445 22

假如同时监控445端口、443端口、22端口

python3 jiankong.py 445 443 22

按crtl+c结束监控,在jiankong.py同目录下生成监控结果。