多线程端口扫描封装

試著忘記壹切 提交于 2019-11-30 04:16:10
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import threadpool
import socket




service_list = {
21:"FTP",
22:"ssh",
25:"SMTP",
80:"web",
139:"Samba",
143:"IMAP",
161:"SNMP",
389:"Ldap目录访问协议",
443:"https",
445:"Microsoft SMB",
465:"SMTP SSL",
513:"rlogin",
546:"DHCP failover",
873:"rsync",
993:"IMAPS",
1080:"socks proxy",
1194:"OpenVPN",
1352:"Lotus domino",
1433:"MSSQL",
1521:"Oracle default",
2049:"Nfs",
2181:"ZooKeeper",
2375:"Docker",
3306:"MySQL",
3389:"Remote Desktop",
4440:"rundeck",
4848:"GlassFish控制台",
5000:"SysBase/DB2",
5432:"PostgreSQL",
5632:"pcanywhere",
5900:"vnc",
5984:"Apache CouchDB",
6082:"varnish",
6984:"Apache CouchDB SSL",
6379:"Redis",
7001:"weblogic_Server isten port",
7002:"Server Listen SSL Port",
8069:"zabbix",
8080:"web,jboss,tomcat etc..",
8089:"Jboss/Tomcat/Resin",
8083:"influxDB Web admin",
8086:"influxdb HTTP API",
8095:"Atlassian Crowd",
8161:"activemq",
8888:"Jupyter Notebook",
8983:"solr",
9000:"fastcgi",
9043:"VMware ESXI vsphere",
9080:"websphere http",
9083:"Hive default",
9090:"websphere admin",
9200:"Elsaticsearch http",
9300:"Elsaticsearch Node1",
10000:"HiveServer2",
11211:"memcached",
27017:"MongoDB",
50000:"SAP command excute",
50060:"hadoop web",
50070:"hadoop default",
60000:"HBase Master",
60010:"hbase.master.info.bindAddress",
}

class Port(object):
    """docstring for Port"""
    def __init__(self, ip):
        self.state = 'unscan' #未扫描
        self.ip = ip
        self.report = ''

    # 主要执行函数
    def main(self):
        # 全局变量,取线程返回值
        global g_list
        g_list = []
        urllist = []
        for port in service_list:
            urllist.append(port)                          #  保存url的列表
        self.thread_requestor(urllist)                    # 线程池函数

        for q in g_list:  # 保存线程回调函数的值到vulurl.txt中
           print("sucess:",q)

    # 线程池函数
    def thread_requestor(self,urllist):
        pool =  threadpool.ThreadPool(200)                                      # 线程池数量
        # reqs =  threadpool.makeRequests(getScan,urllist,res_printer)          # 使用线程池
        reqs = threadpool.makeRequests(self.get_ip_status, urllist,self.res_printer)  # 使用线程池
        [pool.putRequest(req) for req in reqs]                                         # 简写 for req in reqs pool.putRequest(req)
        pool.wait()
    # 回调函数的结果保存到g_list数组中
    def res_printer(self,res1,res2):
        if res2:
            #print ('"线程返回的地址 = " %s ')% res2
            g_list.append(res2)
        else:
            pass
    # 获取IP端口
    def get_ip_status(self,port):

        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            server.connect((self.ip, port))
            print('{0} port {1} is open'.format(self.ip, port))
            return port
        except Exception as e:
            print('{0} port {1} is not open'.format(self.ip, port))
        finally:
            server.close()

if __name__ == "__main__":
    Port("www.baidu.com").main()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!