Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import sys
- import os
- import optparse
- import datetime
- import socket
- from threading import *
- outputArray = []
- threadLock = Semaphore(value = 100)
- socket.setdefaulttimeout(1)
- def connScan(theHost, thePort, connType):
- threadLock.acquire()
- try:
- if "tcp" in connType:
- mySocket = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
- elif "udp" in connType:
- mySocket = socket.socket (socket.AF_INET, socket.SOCK_DGRAM)
- else:
- print "[-] error with connType, returning"
- return
- #open a connection on the specific port
- mySocket.connect((theHost, thePort))
- if "tcp" in connType:
- outputArray.append("<div class='port'>" + str(thePort) + " open</div>")
- mySocket.send("Knock knock motherfucker")
- respond = mySocket.recv(1024)
- if respond:
- #the best that can happen
- if not "udp" in connType:
- outputArray.append("<div class='banner'>"+ str(respond) + "</div>")
- else:
- #only print udp port as open if we get a banner, cause it is too unsafe to predict
- outputArray.append("<div class='port'>" + str(thePort) + " open<div class='banner'>" + str(respond) + "</div>")
- except:pass
- finally:
- threadLock.release()
- try:
- mySocket.close()
- except:pass
- def main():
- #first get some information from the users command line input
- parser = optparse.OptionParser("%prog -H <target_hosts> "\
- "-T <target_TCP_ports> -U <target_UDP_ports>")
- parser.add_option("-H", dest = "targetHost", type = "string")
- parser.add_option("-T", dest = "tcpPort", type = "string")
- parser.add_option("-U", dest = "udpPort", type = "string")
- #work work
- (options, args) = parser.parse_args()
- #parse the target host from the user
- targetHosts = str(options.targetHost).split(",")
- #define some arrays for port numb.3rs
- portRangeTcp = []
- portRangeUdp = []
- #get an array of tcp ports to scan
- if str(options.tcpPort) is not None:
- if "-" in str(options.tcpPort):
- theRange = str(options.tcpPort).split("-")
- for x in range(int(theRange[0]),int(theRange[1])):
- portRangeTcp.append(x)
- else:
- portRangeTcp = str(options.tcpPort).split(",")
- #same goes for udp if specified
- if str(options.udpPort) is not None:
- if "-" in str(options.udpPort):
- theRange = str(options.udpPort).split("-")
- for x in range(int(theRange[0]),int(theRange[1])):
- portRangeUdp.append(x)
- else:
- portRangeUdp = str(options.udpPort).split(",")
- #kick user out if he failed input
- if(targetHosts[0] == None or (portRangeTcp[0] == None and portRangeUdp[0] == None)):
- print "[-] At least specify a target and some ports, exiting..."
- exit(0)
- #create a html5 page for the output, skip if already present
- if not os.path.isfile("results.html"):
- outputArray.append("<!DOCTYPE html><html><head><title>Scan Results</title><script src='https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js'></script>"\
- "<script>$(document).ready(function(){$('.toggleclass').click(function(e){$(this).parent().find('.contentclass').fadeToggle();});});</script>"\
- "<style>h1{text-align:center;}.port{width :30%;floa:left;}.banner{width:70%;float:right;}.clearfix {clear:both;}"\
- ".outerbox {border: 3px solid;padding: 20px;width: 80%;margin: 0px auto;}.contentclass{width:100%;display:none;}"\
- ".toggleclass{cursor:pointer;}.toggleclass:hover {color: blue;text-decoration: underline;}</style></head><body><h1>clockw0rk's portscanner</h1>")
- #start worker bees for scan types
- for host in targetHosts:
- outputArray.append("<div class='outerbox'><div class='toggleclass'><h3>"\
- "Scan results for " + host + " at " + str(datetime.datetime.now().strftime("%d.%m.%Y @ %H:%M:%S")) + "</h3></div>")
- #start worker bees for tcp scan
- if portRangeTcp[0] is not None:
- outputArray.append("<div class='contentclass'><div><h3>TCP Results:</h3></div>"\
- "<div class='port'><b>PORT</b></div><div class='banner'><b>BANNER</b></div><br><hr>")
- for port in portRangeTcp:
- myThread = Thread(target=connScan,args = (host,port,"tcp"))
- myThread.start()
- myThread.join()
- if portRangeUdp[0] is not None:
- outputArray.append("<div><h3>UDP Results:</h3></div>"\
- "<div class='port'><b>PORT</b></div><div class='banner'><b>BANNER</b></div><br><hr>")
- for port in portRangeUdp:
- myThread = Thread(target=connScan,args = (host,port,"udp"))
- myThread.start()
- myThread.join()
- #fix contentbox float and close all tags
- outputArray.append("<div class='clearfix'></div></div></div>\n")
- #if outputfile does not exist, close the html doc
- if not os.path.isfile("results.html"):
- outputArray.append("</body></html>")
- #
- #print the results to a file
- #delete last line of html file, since it is our body and html closing tag
- if os.path.isfile("results.html"):
- file = open("results.html","r+")
- file.seek(0,os.SEEK_END)
- pos = file.tell()-1
- while pos > 0 and file.read(1) != "\n":
- pos -= 1
- file.seek(pos, os.SEEK_SET)
- if pos > 0:
- file.seek(pos, os.SEEK_SET)
- file.truncate()
- file.close()
- #append the results to the file
- file = open("results.html","a")
- for line in outputArray:
- file.write(line)
- file.write("</body></html>")
- file.close()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement