python使用masscan扫描端口
   简介:使用masscan扫描互联网端口,最终将变更和高危端口输出到mysql。

#!/usr/bin/python3
# -*- coding: utf-8 -*-import pymysql
from pymysql import connect
import os
import xmltodict
import datetimeclass SCAN():def __init__(self):self.conn = connect(host='localhost',port=3306,user='root',password='password=',database='risk_detection')self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)def __del__(self):self.cursor.close()self.conn.close()###masscan扫描端口def masscan(self):global starttime, endtimestarttime = datetime.datetime.now()print("扫描开始时间:%s" % (starttime))os.system("/data/masscan/bin/masscan  -iL /data/scan/ip_list3 --rate 1000 -p1-65535 -oX /data/scan/testscan.xml")endtime = datetime.datetime.now()print("扫描结束时间:%s" % (endtime))##def NMAP(self):####打开masscan的扫描结果文件with open('/data/scan/testscan.xml') as f:xml_obj = xmltodict.parse(f.read())host = xml_obj['nmaprun']['host']for line in host:ip = line['address']['@addr']port = line['ports']['port']['@portid']if ip in scan_dict.keys():scan_dict[ip] = scan_dict[ip] + ',' + portelse:scan_dict[ip] = portdef num_ip(self):f = open("/data/scan/ip_list3")n = 0for line in f:n += 1return n###上次task_record——id查询def search_sql_record(self):sql = "SELECT id FROM task_record ORDER BY id DESC LIMIT 1;"try:self.cursor.execute(sql)self.conn.commit()except:self.conn.rollback()dict_data = self.cursor.fetchall()return dict_data[0]['id']###查询任务iddef record(self):print(self.search_sql_record())try:sql = "INSERT INTO task_record(start_date, end_date, num_ip,last_time_id) VALUES ('%s', '%s', '%s','%s')" % (starttime, endtime, self.num_ip(), self.search_sql_record())self.cursor.execute(sql)self.conn.commit()self.cursor.fetchall()except:sql = "INSERT INTO task_record(start_date, end_date,num_ip) VALUES ('%s', '%s', '%s')" % (starttime, endtime, self.num_ip())self.cursor.execute(sql)self.conn.commit()self.cursor.fetchall()###查询任务上一次iddef search_sql_record_id(self):sql = "SELECT id,last_time_id  FROM task_record ORDER BY id DESC LIMIT 1"try:self.cursor.execute(sql)except:self.conn.rollback()try:return self.cursor.fetchall()except:return 0###存储扫描结果def result(self):####打开masscan的扫描结果文件high_port = ['20', '21', '22', '23', '69', '111', '2049', '137', '139', '445', '161', '389', '512', '513', '514','873', '1352', '1433', '1521', '1500', '2082', '2083','2181', '2601', '2604', '3128', '3306', '3389', '3690', '4848', '5000', '5432', '5632', '5900', '5901','5902', '5984', '6379', '7001', '7002', '7778','8000', '8443', '8069', '9080', '9081', '9090', '9200', '9300', '11211', '27017', '27018', '50030','50070']with open('/data/scan/scan.xml',encoding='utf-8') as f:xml_obj = xmltodict.parse(f.read())host = xml_obj['nmaprun']['host']for line in host:ip = line['address']['@addr']port = line['ports']['port']['@portid']protocol = line['ports']['port']['@protocol']print(self.search_sql_record_id()[0]['last_time_id'])if port in high_port:# print("高危端口:%s"%(port))sql = "INSERT INTO task_result(id_task,ip,port,protocol,high_risk_port) VALUES ('%s', '%s', '%s','%s','1')" % (self.search_sql_record_id()[0]['id'], ip, port, protocol)try:self.cursor.execute(sql)self.conn.commit()except:self.conn.rollbackelse:sql = "INSERT INTO task_result(id_task,ip,port,protocol) VALUES ('%s', '%s', '%s','%s')" % (self.search_sql_record_id()[0]['id'], ip, port, protocol)try:self.cursor.execute(sql)self.conn.commit()except:self.conn.rollback###获取变更信息def port_change(self):sql = "INSERT INTO port_change(id_task, ip, `port`, protocol, `status`) SELECT %s AS id_task, ip, \`port`, protocol, (CASE WHEN id_task=%s THEN '1' ELSE '0' END)AS 'status' FROM( \SELECT `id_task`, `ip`, `port`, `protocol` FROM task_result WHERE id_task=%s \UNION ALL \SELECT `id_task`, `ip`, `port`, `protocol` FROM task_result WHERE id_task=%s) t3 \GROUP BY ip, `port`, protocol  \HAVING COUNT(*)=1" % (self.search_sql_record_id()[0]['id'], self.search_sql_record_id()[0]['id'],  self.search_sql_record_id()[0]['id'],self.search_sql_record_id()[0]['last_time_id'])sql_1 = "SET sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';"self.cursor.execute(sql_1)self.cursor.execute(sql)self.conn.commit()if __name__ == '__main__':mes = SCAN()mes.masscan()mes.record()mes.result()print("扫描结果写入成功!")try:mes.port_change()print("本次变更数据已更新!")except:print("本次无对比数据")

python使用masscan扫描端口相关推荐

  1. python在命令端口运行脚本_扫描端口占用情况的python脚本

    之前项目上线前,领导要求让写一个脚本用来判断端口的占用情况.由于现在python3使用也比较多,基于python2修改了一下,做了个python3版本的,现在做一下总结. 一.python脚本实现扫描 ...

  2. Python编写masscan+nmap的主机和端口信息收集工具

    masscan使用 linux安装 git clone https://github.com/robertdavidgraham/masscan make 扫描选项 masscan -iL targe ...

  3. php 根据ip 扫描端口,python实现指定ip端口扫描方式

    在Linux中判断一台主机是否可达,可以使用ping命令,而判断端口是否打开,可以使用telnet命令,但是telnet命令没有超时时间的参数,使用起来不是很方便,那么可以利用Python来完成一个端 ...

  4. python扫描端口脚本_Pyhton扫描端口脚本代码

    Pyhton扫描端口脚本代码# -*- coding:utf8 -*- #!/usr/bin/python # Program: 端口扫描 import socket, time, thread so ...

  5. python中用socket检测端口_python基于socket函数实现端口扫描

    本文实例为大家分享了python基于socket实现端口扫描的具体代码,供大家参考,具体内容如下 自学Python一段时间,写个端口扫描器练练手.有什么不足之处,请见谅 这是基于socket函数对端口 ...

  6. python多线程扫描_Python多线程扫描端口代码示例

    本文代码实现Python多线程扫描端口,具体实现代码如下. #coding:utf-8 import socket import thread import time socket.setdefaul ...

  7. python扫描端口脚本_python扫描端口脚本

    # -*- coding:utf8 -*- # # Python: 2.7.8 # Platform: Windows # Authro: wucl # Program: 端口扫描 # History ...

  8. 从入门到入土:基于Python|ACK|FIN|Null|Xmas|windows|扫描|端口扫描|scapy库编写|icmp协议探测主机|对开放端口和非开放端口完成半连接扫描|全连接扫描|

    此博客仅用于记录个人学习进度,学识浅薄,若有错误观点欢迎评论区指出.欢迎各位前来交流.(部分材料来源网络,若有侵权,立即删除) 本人博客所有文章纯属学习之用,不涉及商业利益.不合适引用,自当删除! 若 ...

  9. 使用Python扫描端口

    前几天看了个讲使用Python扫描端口的教程,看了之后自己也写了个扫描端口的脚本.记录下来,方便自己以后回顾. 运行效果如下: 具体代码如下,请指教. # -*- coding:utf8 -*- #! ...

  10. Python # 扫描端口功能 # 获取网卡的Mac地址 # 局域网扫描器IP地址和MAC地址,获取网卡名称和其ip地址

    #!/usr/bin/env python # -*- coding: utf-8 -*- # 开发人员: # time: 2021-09-07 20:37 # 开发工具: PyCharm # Ver ...

最新文章

  1. STP生成树协议原理及配置
  2. mysql 开发模式_搭建mysql主從模式
  3. 视频会议场景下的弱网优化
  4. How does framework require TechnicalInfo.js
  5. 路由器上的usb接口有什么用_路由器上面的USB接口有什么用途,你都知道吗?
  6. 【云和恩墨】Oracle初学者入门指南-什么是 Metalink 或 MOS ?
  7. 自回归模型AR(p)的python实现【案例】
  8. 微型计算机常用的硬盘接口有哪些,硬盘的接口有哪些?各种硬盘接口类型介绍...
  9. 使用“#define _GNU_SOURCE” 是什么意思?
  10. rhel6.5搭建mantis时报错
  11. 多层感知器(Muti-Layer Perception ,MLP)
  12. AliOS Things入门(1) 基于STM32L4与MDK搭建AliOS Things2.1.0开发环境
  13. 基于VHDL的密码锁
  14. 火狐浏览器叫板苹果:绝不登录iOS平台
  15. 分享几个内网穿透工具
  16. mui框架scroll,鼠标滑轮可以滚动,移动端触摸无法滚动
  17. OpenCV/OpenCL/OpenGL区别
  18. AI-K210 开发家庭万用宝模组(1)
  19. 全透视:CSS Z-index 属性
  20. 斯坦福:chatGPT可能有了人类心智,相当于9岁儿童!

热门文章

  1. 谷歌浏览器弹出Chrome版本太旧解决方式
  2. Android Handler机制详解
  3. Visual Studio vs软件下载 vax Visual Assist X VAssistX
  4. Ubuntu18 安装MeshLab软件
  5. 细枝末节都交给App 我只负责享受生活 | 2016与我的数字生活
  6. Oracle imp 导入dmp文件到数据库
  7. codeblocks 汉化教程
  8. linux Redis下载及安装
  9. t分布 u分布 卡方分布_t分布曲线和正太分布,和z分布,和卡方分布,和方差分析的f分布曲线有什么区别?...
  10. QC新7种手法详解(转)