python端口扫描报警

简介:

前言

     服务器安全问题,对于我们运维人员来说都是不可忽视的,虽然我们没有安全工程师那么牛掰,但是我们也要尽量的避免被黑,即使我们被黑了,上面跑了别的应用,我们要立刻马上收到报警信息,尽量减少损失。好了说重点,今天我写了python端口扫描的脚本,如果开启了异常的端口,管理员会立马收到邮件信息,看代码!

一、端口扫描脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python
#coding=utf8
#Author zhaodong
#www.tshare365.com
import  sys,os,nmap
import  multiprocessing
import  httplib,smtplib
from  email.MIMEText  import  MIMEText
from  email.Header  import  Header
reload (sys)
sys.setdefaultencoding( 'utf8' )
#设置收件人邮箱改成你自己的
mailto_list = [ 'zhaodong@tshare365.tv' ]
mail_host = "smtp.163.com"   #设置服务器
mail_user = "tshare365@163.com"     #用户名
mail_pass = "www.tshare365.com"    #密码
mail_postfix = "163.com"   #发件箱的后缀
def  send_mail(to_list,sub,content):
         me = "服务器端口异常报警" + "<" + mail_user + "@" + mail_postfix + ">"
         msg  =  MIMEText(content,_subtype = 'plain' ,_charset = 'utf_8' )
         msg[ 'Subject' =  sub
         msg[ 'From' =  me
         msg[ 'To' =  ";" .join(to_list)
         try :
            server.login(mail_user,mail_pass)
            server.sendmail(me, to_list, msg.as_string())
            server.close()
            return  True
         except  Exception, e:
            print  str (e)
            return  False
def  HostCheck(ipaddr):
         nm  =  nmap.PortScanner()
         call  =  nm.scan(ipaddr,  '22-65535' # scan host 127.0.0.1, ports from 22 to 443
         nm.command_line()  # get command line used for the scan : nmap -oX - -p 22-443 127.0.0.1
         nm.scaninfo()  # get nmap scan informations {'tcp': {'services': '22-443', 'method': 'connect'}}
         nm.all_hosts()  # get all hosts that were scanned
         for  host  in  nm.all_hosts():
                 for  proto  in  nm[host].all_protocols():
                         pass
                 lport  =  nm[host][proto].keys()
                 lport.sort()
                 for  port  in  lport:
                         if  port  in  White_PORT:
                                 print  line
                         else :
                                 line  =  "HOST:  %s\tTCP/UDP: %s\tPORT : %s\t  异常端口"  %  (host, proto, port)
                                 = file ( '/tmp/Problem_info.txt' , 'a' )
                                 f.write( "\r\n" )
                                 f.write(line)
                                 f.close()
  
if  __name__  = =  "__main__" :
         INPUT_IP  =  os.path.join(os.getcwd(), "IP.txt" )
         INPUT_IP_LINES  =  sum ( 1  for  line  in  open (INPUT_IP))
         OPEN_INPUT_IP  =  open (INPUT_IP)
         if  INPUT_IP_LINES >  30 :
                 process_number  =  30
         else :
                  process_number  =  INPUT_IP_LINES
         #设置白名单端口 
         White_PORT = [ 22 , 80 , 3306 ]
         pool  =  multiprocessing.Pool(processes = process_number)
         for  IP  in  OPEN_INPUT_IP.readlines():
                 IP  =  IP.strip( '\n' )
                 pool.apply_async(HostCheck,(IP,))
         pool.close()
         pool.join()
         #判断Problem_info文件是否存在
         if  os.path.exists( "/tmp/Problem_info.txt" ):
                 infor = os.popen( "cat /tmp/Problem_info.txt" ).read()
                 #发送邮件报警
                 send_mail(mailto_list, "zhaodong" ,infor)
                 os.system( "rm -rf /tmp/Problem_info.txt" )

注释: 如果代码是放在/root/目录下 ,需要在/root/ 目录下建立一个IP.txt 的文件,在里面写上你需要扫描的IP。

二、报警邮件

98










本文转自 xinsir999 51CTO博客,原文链接:http://blog.51cto.com/xinsir/1616106,如需转载请自行联系原作者
目录
相关文章
|
Python
Python网络编程基础(Socket编程)绑定地址和端口
【4月更文挑战第9天】在UDP服务器编程中,我们首先需要创建一个UDP套接字,然后绑定一个本地地址和端口,以便客户端可以通过这个地址和端口与我们的服务器进行通信。下面,我们将详细讲解如何绑定地址和端口。
|
Linux Python
用python扫描linux开放的端口(3种方式)
这篇文章介绍了三种使用Python实现Linux端口扫描的方法,包括基础版端口扫描、全端口扫描和多线程扫描技术。
488 16
|
Python
Python编程--使用NMAP端口扫描
Python编程--使用NMAP端口扫描
215 1
|
网络安全 Python
Python编程--目标IP地址段主机指定端口状态扫描
Python编程--目标IP地址段主机指定端口状态扫描
197 1
|
监控 网络协议 数据库连接
Python3 监控端口:使用 socket 库
Python3 监控端口:使用 socket 库
276 1
|
运维 安全 网络协议
Python 网络编程:端口检测与IP解析
本文介绍了使用Python进行网络编程的两个重要技能:检查端口状态和根据IP地址解析主机名。通过`socket`库实现端口扫描和主机名解析的功能,并提供了详细的示例代码。文章最后还展示了如何整合这两部分代码,实现一个简单的命令行端口扫描器,适用于网络故障排查和安全审计。
302 0
|
网络安全 数据安全/隐私保护 Python
Python 渗透测试:文件传输爆破( 21端口 )
Python 渗透测试:文件传输爆破( 21端口 )
160 2
|
监控 网络协议 数据库连接
Python3 监控端口:使用 socket 库
Python3 监控端口:使用 socket 库
169 0
|
运维 Python Windows
如何通过Python脚本查找并终止占用指定端口的进程
在日常的开发和运维过程中,某些端口被意外占用是一个常见的问题。这种情况可能导致服务无法启动或冲突。本文将介绍如何通过Python脚本查找并终止占用指定端口的进程,以确保系统的正常运行。
|
机器学习/深度学习 算法 安全
基于YOLOv8深度学习的危险区域人员闯入检测与报警系统【python源码+Pyqt5界面+数据集+训练代码】YOLOv8、ByteTrack、目标追踪、区域闯入
基于YOLOv8深度学习的危险区域人员闯入检测与报警系统【python源码+Pyqt5界面+数据集+训练代码】YOLOv8、ByteTrack、目标追踪、区域闯入

推荐镜像

更多