欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C段http_banner获取v0.3

程序员文章站 2022-07-03 11:34:44
更新: 1.使用optparse来获取命令行参数   #-*-coding=utf-8-*- # __author__ = 'sanr'...
更新:

1.使用optparse来获取命令行参数

 

#-*-coding=utf-8-*-
# __author__  = 'sanr'
# __email__   = '5754190@qq.com'
# __url__     = 'https://0x007.blog.51cto.com/'
# __version__ = '0.3'
import requests
import re
from threading import Thread,Lock
import sys
import chardet
import netaddr
import struct
import socket
import os
import optparse

lock = Lock()

def ip2int(addr):
    return struct.unpack("!I", socket.inet_aton(addr))[0]
def int2ip(addr):
    return socket.inet_ntoa(struct.pack("!I", addr))
def int_dec(pagehtml):

    charset = None
    if pagehtml != '':
        # print 'use charset dect'
        enc = chardet.detect(pagehtml)
        # print 'enc= ', enc
        if enc['encoding'] and enc['confidence'] > 0.9:
            charset = enc['encoding']

        if charset == None:
            charset_re = re.compile("((^|;)\s*charset\s*=)([^\"']*)", re.M)
            charset=charset_re.search(pagehtml[:1000]) 
            charset=charset and charset.group(3) or None

        # test charset
        try:
            if charset:
                unicode('test',charset,errors='replace')
        except Exception,e:
            print 'Exception',e
            charset = None
    # print 'charset=', charset
    return charset


def http_banner(url):
    ip=url
    try:
        url=requests.get(url,timeout=2)

        body = url.content
         
        charset = None
        if body != '':
            charset = int_dec(body)

        if charset == None or charset == 'ascii':
            charset = 'ISO-8859-1'

        if charset and charset != 'ascii' and charset != 'unicode':
            try:
                body = unicode(body,charset,errors='replace')
            except Exception, e:
                body = ''
     
        Struts=url.status_code
     
        Server=url.headers['server'][0:13]
     
        if Struts==200 or Struts==403 or Struts==401:
            title=re.findall(r"<title>(.*)<\/title>",body)
            if len(title):
                title = title[0].strip()
            else:
                title = ''
            #输出加锁 防止第二行输入
            #申请锁
            lock.acquire()
            print ('%s\t%d\t%-10s\t%s'%(ip.lstrip('https://'),Struts,Server,title))
            #释放锁
            lock.release()
    except (requests.HTTPError,requests.RequestException,AttributeError,KeyError),e:
        pass

 

 

 

if __name__ == '__main__':

    parser = optparse.OptionParser('usage: %prog [options] target')

    parser.add_option('-p', '--port', dest='port', default='80',type='string', help='Port.default = 80')

    (options,args) = parser.parse_args() #parser.parse_args处理之后给option返回一个字典对象,对象的key就是你上面设置的dest的值

     

 

   if len(args) < 1:
        parser.print_help()
        print 'usage: python %s 218.92.227.1/24 '%os.path.basename(sys.argv[0])
        print 'usage: python %s 218.92.227.1-218.92.227.254 '%os.path.basename(sys.argv[0])
        print 'usage: python %s 218.92.227.1./24 -p 8080'%os.path.basename(sys.argv[0])
        print 'usage: python %s 218.92.227.1-218.92.227.254 -p 8080'%os.path.basename(sys.argv[0])
        sys.exit(0)
         
    ips=args[0]
    port=options.port
    if '-' in ips:
        start, end = ips.split('-')
        startlong = ip2int(start)
        endlong = ip2int(end)
        ips = netaddr.IPRange(start,end)
        for ip in list(ips):
            url='https://%s:%s'%(ip,port)
            t = Thread(target=http_banner,args=(url,))
            t.daemon=False
            t.start()
    elif '/'   in ips:
        ips = netaddr.IPNetwork(ips)
        for ip in list(ips):
            url='https://%s:%s'%(ip,port) 
            t = Thread(target=http_banner,args=(url,))
            t.daemon=False
            t.start()