web 端口刺探

web 端口刺探

#coding=utf-8
import requests
from datetime import datetime
from multiprocessing.dummy import Pool as ThreadPool
import socket
import re



def check_webserver(address, port):

        request_string = "GET / HTTP/1.1\r\nHost: %s\r\n\r\n" %address
        # print 'HTTP request:'
        # print '|||%s|||' % request_string
        s = socket.socket()
        s.settimeout(15)
        print "Attempting to connect to %s on port %s" %(address, port)
        try:
            s.connect((address, port))
            s.send(request_string)
            rsp = s.recv(1024)

        except socket.error, e:
            print "Connection to %s on port %s failed: %s" %(address, port, e)
            return False
        finally:
            print "Closing the connection"
            s.close()
        try:
            locationList = re.findall("Location: (.*)", rsp)
            location="/"
            if len(locationList)!=0:
                location +=locationList[0].strip("\r\n").split("/")[-1]
            print("Locatin : %s"% location)
        except:
            pass
        try:
            lines = rsp.splitlines()
            print 'First line of HTTP response: %s' % lines[0]
            version, status, message = re.split(r'\s+', lines[0], 2)
            print 'Version: %s, Stuats: %s, Message: %s' %(version, status, message)
        except ValueError:
            print 'Failed to split status line'
            return False
        if status in ['200', '302',"401"]:
            print 'Success - status was %s' %status
            with open("camera_IP.txt", "a+") as f:
                f.write(address + ":" + str(port) + location+"\n")
                f.close()
            return True
        else:
            print 'Staus was %s' %status
            return False

def scan_camera(ipPort):
        splitData = ipPort.split(":")
        ip = splitData[0]
        port = int(splitData[1])
        if check_webserver(ip,port):
            print"######"*25


# Check what time the scan started
t1 = datetime.now()
ipList=[]
with open("netwave.txt","r") as f:
    for line in f.readlines():
        ipList.append(line.strip("\n"))
print("test")
ports=[]        
for i in range(80,49152):
    ports.append(i)

def  getIpPortList(ipList,port):
    mIpPortList = []
    for ip in ipList:
        mIpPortList.append(ip+":"+str(port))
    return mIpPortList

for port in ports:
    ipPortList=getIpPortList(ipList,port)
    pool = ThreadPool(processes = 8)
    results = pool.map(scan_camera,ipPortList)
    pool.close()
    pool.join()

print 'Multiprocess Scanning Completed in  ', datetime.now() - t1

你可能感兴趣的:(web-端口刺探,http刺探,python)