• 欢迎访问挑战自我博客网站,安全研究,web渗透,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入挑战自我博客网站 网站主页

基于端口的信息探测-portscan-1.0

portscan 挑战自我 980次浏览 已收录 0个评论

portscan Version:1.0

功能:基于nmap的端口扫描,支持从文件读取目标(IP或域名),支持IP地址探测,IP地址支持CIDR

注意:

1、port.txt文件的开头添加了nmap的扫描参数;

2、本地测试时,线程最好不要超过7,一般的家用网络会断网的;

3、扫描结果保存到程序所在目录的result_port.txt文件中;

演示:

python portscan.py -f file.txt -p port.txt -t 5

python portscan.py 1.1.1.0/24 -p port.txt -t 5

INSTALL

1、pip install nmap

2、pip install python-nmap

3、pip install IPy

QA

1、当遇到错误’module’ object has no attribute ‘PortScanner’时,尝试重新安装python-nmap

pip uninstall python-nmap

pip install python-nmap

TO DO

1、大范围扫描的进度输出设置,就像nmap

2、部分错误的详细输出(例如需要重新安装python-nmap的错误提示)

3、输入参数不完整的信息提示

 

 

#!/usr/bin/env python
#coding:utf-8
#Author: linxi0428

import nmap
import logging
import threading, Queue, time
import sys
import os
import optparse
from IPy import IP

logging.basicConfig(
    level=logging.DEBUG,
    format="[%(asctime)s] %(levelname)s: %(message)s",
    filename = 'result_port.txt',
    filemode = 'w')

class scan():

    def __init__(self,cidr,threads_num,file_source,ports):
        self.threads_num = threads_num
        self.ports = open(ports,"r")
        self.IPs = Queue.Queue()
        if file_source == None:
            self.cidr = IP(cidr)
            for ip in self.cidr:
                ip = str(ip)
                self.IPs.put(ip)
        else:
            self.file_source = file_source
            file_ip = open(self.file_source,"r")
            for line in file_ip:
                self.IPs.put(line)
            file_ip.close()

    def nmapScan(self):
        with threading.Lock():
            while self.IPs.qsize() > 0:
                item = self.IPs.get()
                self.IPs.task_done()
                try:
                    nmScan = nmap.PortScanner()
                    nmScan.scan(item,arguments = self.ports.read())
                    for tgthost in nmScan.all_hosts():
                        for tgtport in nmScan[tgthost]['tcp']:
                            tgthost = tgthost.strip()
                            tgtport = int(tgtport)
                            if nmScan[tgthost]['tcp'][tgtport]['state'] == 'open':
                                state = nmScan[tgthost]['tcp'][tgtport]['state']
                                logging.debug(tgthost+'\t'+str(tgtport) + '\t' + nmScan[tgthost]['tcp'][tgtport]['name'] + \
                                '\t' + nmScan[tgthost]['tcp'][tgtport]['product'] + nmScan[tgthost]['tcp'][tgtport]['version'])
                except Exception, e:
                    logging.info(item + '\t'+str(e))
                    return

    def run(self):
        for i in range(self.threads_num):
            t = threading.Thread(target=self.nmapScan)
            t.start()
    
def help():
    print "Example:"
    print "  python "+sys.argv[0]+" -f file.txt -p port.txt -t 5"
    print "  python "+sys.argv[0]+" 1.1.1.0/24 -p port.txt -t 5"

if __name__ == '__main__':
    parser = optparse.OptionParser("Usage: python %prog [target] [options] ")
    parser.add_option("-t", "--thread", dest = "threads_num",
                     default = 5, help = "Number of theads,Not more than 7,default=5")
    parser.add_option("-f", "--file", dest = "file_source",help = "File of ip_source")
    parser.add_option("-p", "--port", dest = "ports",help = "File of ports")
    (options, args) = parser.parse_args()

    if options.ports == None:
        parser.print_help()
        help()
        sys.exit(0)
        
    if options.file_source == None:
        if len(args) < 1:
            parser.print_help()
            help()
            sys.exit(0)
        else:
            s = scan(cidr=args[0],threads_num=options.threads_num,file_source=None,ports=options.ports)
            s.run()
    else:
        s = scan(cidr=None,threads_num=options.threads_num,file_source=options.file_source,ports=options.ports)
        s.run()

 

端口列表请下载:port.txt


挑战自我博客, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明基于端口的信息探测-portscan-1.0
喜欢 (5)
支付宝[]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址