Advertisement
Guest User

clockw0rk's portscanner

a guest
Sep 30th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.26 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import os
  4. import optparse
  5. import datetime
  6. import socket
  7. from threading import *
  8.    
  9. outputArray = []
  10. threadLock = Semaphore(value = 100)
  11. socket.setdefaulttimeout(1)
  12.    
  13. def connScan(theHost, thePort, connType):
  14.    
  15.     threadLock.acquire()
  16.    
  17.     try:
  18.         if "tcp" in connType:
  19.             mySocket = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
  20.         elif "udp" in connType:
  21.             mySocket = socket.socket (socket.AF_INET, socket.SOCK_DGRAM)
  22.         else:
  23.             print "[-] error with connType, returning"
  24.             return
  25.        
  26.         #open a connection on the specific port
  27.         mySocket.connect((theHost, thePort))
  28.        
  29.         if "tcp" in connType:
  30.             outputArray.append("<div class='port'>" + str(thePort) + " open</div>")
  31.        
  32.        
  33.         mySocket.send("Knock knock motherfucker")
  34.        
  35.         respond = mySocket.recv(1024)
  36.        
  37.         if respond:
  38.             #the best that can happen
  39.             if not "udp" in connType:
  40.                 outputArray.append("<div class='banner'>"+ str(respond) + "</div>")
  41.             else:
  42.                 #only print udp port as open if we get a banner, cause it is too unsafe to predict
  43.                 outputArray.append("<div class='port'>" + str(thePort) + " open<div class='banner'>" + str(respond) + "</div>")
  44.        
  45.     except:pass
  46.     finally:
  47.         threadLock.release()
  48.         try:
  49.             mySocket.close()
  50.         except:pass
  51.        
  52.        
  53. def main():
  54.    
  55.     #first get some information from the users command line input
  56.     parser = optparse.OptionParser("%prog -H <target_hosts> "\
  57.         "-T <target_TCP_ports> -U <target_UDP_ports>") 
  58.     parser.add_option("-H", dest = "targetHost", type = "string")  
  59.     parser.add_option("-T", dest = "tcpPort", type = "string")
  60.     parser.add_option("-U", dest = "udpPort", type = "string")
  61.        
  62.     #work work
  63.     (options, args) = parser.parse_args()
  64.    
  65.     #parse the target host from the user
  66.     targetHosts = str(options.targetHost).split(",")
  67.    
  68.     #define some arrays for port numb.3rs
  69.     portRangeTcp = []
  70.     portRangeUdp = []
  71.    
  72.     #get an array of tcp ports to scan
  73.     if str(options.tcpPort) is not None:
  74.         if "-" in str(options.tcpPort):
  75.             theRange = str(options.tcpPort).split("-")
  76.             for x in range(int(theRange[0]),int(theRange[1])):
  77.                 portRangeTcp.append(x)
  78.         else:
  79.             portRangeTcp = str(options.tcpPort).split(",")
  80.        
  81.    
  82.     #same goes for udp if specified
  83.     if str(options.udpPort) is not None:
  84.         if "-" in str(options.udpPort):
  85.             theRange = str(options.udpPort).split("-")
  86.             for x in range(int(theRange[0]),int(theRange[1])):
  87.                 portRangeUdp.append(x)
  88.         else:
  89.             portRangeUdp = str(options.udpPort).split(",")
  90.        
  91.        
  92.     #kick user out if he failed input
  93.     if(targetHosts[0] == None or (portRangeTcp[0] == None and portRangeUdp[0] == None)):
  94.         print "[-] At least specify a target and some ports, exiting..."
  95.         exit(0)
  96.    
  97.     #create a html5 page for the output, skip if already present
  98.     if not os.path.isfile("results.html"):
  99.         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>"\
  100.             "<script>$(document).ready(function(){$('.toggleclass').click(function(e){$(this).parent().find('.contentclass').fadeToggle();});});</script>"\
  101.             "<style>h1{text-align:center;}.port{width :30%;floa:left;}.banner{width:70%;float:right;}.clearfix {clear:both;}"\
  102.             ".outerbox {border: 3px solid;padding: 20px;width: 80%;margin: 0px auto;}.contentclass{width:100%;display:none;}"\
  103.             ".toggleclass{cursor:pointer;}.toggleclass:hover {color: blue;text-decoration: underline;}</style></head><body><h1>clockw0rk's portscanner</h1>")
  104.    
  105.  
  106.     #start worker bees for scan types
  107.     for host in targetHosts:
  108.         outputArray.append("<div class='outerbox'><div class='toggleclass'><h3>"\
  109.             "Scan results for " + host + " at " + str(datetime.datetime.now().strftime("%d.%m.%Y @ %H:%M:%S")) + "</h3></div>")
  110.         #start worker bees for tcp scan
  111.         if portRangeTcp[0] is not None:
  112.             outputArray.append("<div class='contentclass'><div><h3>TCP Results:</h3></div>"\
  113.                 "<div class='port'><b>PORT</b></div><div class='banner'><b>BANNER</b></div><br><hr>")
  114.             for port in portRangeTcp:
  115.                 myThread = Thread(target=connScan,args = (host,port,"tcp"))
  116.                 myThread.start()
  117.                 myThread.join()
  118.         if portRangeUdp[0] is not None:
  119.             outputArray.append("<div><h3>UDP Results:</h3></div>"\
  120.                 "<div class='port'><b>PORT</b></div><div class='banner'><b>BANNER</b></div><br><hr>")
  121.             for port in portRangeUdp:
  122.                 myThread = Thread(target=connScan,args = (host,port,"udp"))
  123.                 myThread.start()
  124.                 myThread.join()
  125.         #fix contentbox float and close all tags
  126.         outputArray.append("<div class='clearfix'></div></div></div>\n")       
  127.    
  128.    
  129.     #if outputfile does not exist, close the html doc
  130.     if not os.path.isfile("results.html"):
  131.         outputArray.append("</body></html>")
  132.     #
  133.    
  134.     #print the results to a file   
  135.     #delete last line of html file, since it is our body and html closing tag
  136.     if os.path.isfile("results.html"):
  137.         file = open("results.html","r+")
  138.         file.seek(0,os.SEEK_END)
  139.         pos = file.tell()-1
  140.         while pos > 0 and file.read(1) != "\n":
  141.             pos -= 1
  142.             file.seek(pos, os.SEEK_SET)
  143.         if pos > 0:
  144.             file.seek(pos, os.SEEK_SET)
  145.             file.truncate()
  146.         file.close()
  147.        
  148.     #append the results to the file
  149.     file = open("results.html","a")
  150.     for line in outputArray:
  151.         file.write(line)
  152.         file.write("</body></html>")
  153.        
  154.     file.close()
  155.    
  156. if __name__ == "__main__":
  157.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement