Advertisement
sm4rtn0bit4

python script for scanning open ports

Jan 24th, 2018
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.76 KB | None | 0 0
  1. #!/usr/bin/python3
  2. #  
  3. #   This code is of no use for you if you are not a beginner in python3
  4. #   Uploading it just for beginners.......
  5. #   Helps in understanding how port scanners works ..?
  6. #   and basic usage of Thread from threading module
  7. #   This code is long but it's coded this way...
  8. #   Try to optimize this on your own as far as you wanna go with it ...
  9. #
  10. from time import time as cputime,ctime as Time          # IMPORTING LIBS
  11. import socket
  12. from threading import Thread
  13. ##
  14. #   Checking for vuln versions using a file containing a list of services and vuln versions...(COMMENTED OUT BY DEFAULT)
  15. ##
  16. def check_vuln(reply):          # DEFINE :  CHECK_VULN FUNCTION
  17. #
  18. #   input for file path
  19. #
  20.     path=input('Enter path for file')
  21.     f=open("%s"%path,'r')
  22.     for line in f.readlines():
  23.         #print("LINE: ",line.rstrip('\n'))
  24.         if line.rstrip('\n') in str(reply):
  25.             print("Known vuln service found -> \n\t",str(reply))
  26.         #else:
  27.         #   print("check vuln manually")
  28. ##
  29. #   Grabbing banner from services ruuning
  30. ##
  31. def grab_banner(ip,port,path):      # DEFINE:GRAB_BANNER
  32.     try:                            # TRY TO AONNECT AND GRAB BANNERS FOR ACTIVE SERVICES/PORTS
  33.         socket.setdefaulttimeout(5)
  34.         s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  35.         s.connect((ip, port))       # IF NO ERROR ON THIS ONE THEN THE PORT IS ACTIVE( may be filtered )
  36.         print("[+]IP : PORT -> %s: %s"%(ip,port))
  37.         s.send(b"GARBAGE VALUE bsjvcvsdgvjsbj/anything\r\n")    # sent in bytes type
  38.         banner=s.recv(1024).decode('utf-8')                     # recieved in bytes and converted to str type...                       
  39.         print(banner)                               # printing recieved banner
  40.         #check_vuln(banner)     # uncomment this and provide file.txt path if u get a match to vuln versions ..
  41.         s.shutdown()
  42.         s.close()
  43.         return banner
  44.     except Exception as e:
  45.         pass
  46.         #print("[-]Connection failed -> IP:%s PORT:%s"%(ip,port))
  47. #
  48. def main(): # DEFINE:MAIN FOR PARSING INPUT AND calling defined FUNC.YOU CAN ADD ARGUMENT PARSING BY YOURSELF... :P
  49. #
  50. # input ip and check if range is passed..?
  51. #
  52.     try:
  53.         ip=input('Enter ip/ip-range: ')
  54.         if '-' in ip:
  55.             temp=ip.split('.')
  56.             ip_range=temp[3].split('-')
  57.             ip_list=[]
  58.             for ip in range(int(ip_range[0]),int(ip_range[1])+1):
  59.                 ip_list.append(temp[0]+'.'+temp[1]+'.'+temp[2]+'.'+str(ip))
  60.             ip=ip_list
  61. #
  62. #input port and check if range is passed..?
  63. #
  64.         port=input('Enter port/port-range: ')
  65.         if '-' in port:
  66.             temp=port
  67.             port_range=port.split('-')
  68.             port_list=[]
  69.             for port in range(int(port_range[0]),int(port_range[1])+1):
  70.                 port_list.append(port)
  71.             port=port_list
  72. #
  73. #   recording EXEC time here  
  74. #
  75.         start_time=cputime()
  76. #
  77. #IN-CASE:-Specific IP and PORT
  78. #
  79.         if type(ip) is type(port) is str:
  80.             thread=Thread(target=grab_banner, args=(ip,port))   #comment these two lines to stop threading
  81.             banner=thread.start()                               # if u want to cmpare exec time
  82.             #banner=grab_banner(ip,port)                        #uncomment this if commented above two lines ...
  83.             if banner:
  84.                 print(ip+' '+port)
  85. #
  86. ##IN-CASE:-IP-Range and Port-Range passed
  87. #
  88.         elif type(ip) is type(port) is list:
  89.             for x in ip:
  90.                 for y in port:
  91.                     thread=Thread(target=grab_banner, args=(x,y))   #comment these two lines to stop threading
  92.                     banner=thread.start()                           # if u want to cmpare exec time
  93.                     #banner=grab_banner(x,y)                        #uncomment this if commented above two lines ...
  94.                     if banner:
  95.                         print(x,' ',y,' : ',banner)
  96. #
  97. ##IN-CASE:-Port-Range with specific IP
  98. #
  99.         elif type(ip) is str and type(port) is list:
  100.             for y in port:
  101.                 thread=Thread(target=grab_banner, args=(ip,y))  #comment these two lines to stop threading
  102.                 banner=thread.start()                           # if u want to cmpare exec time
  103.                 #banner=grab_banner(ip,y)                       #uncomment this if commented above two lines ...
  104.                 if banner:
  105.                     print(ip,' ',y,' : ',banner)
  106. #
  107. ##IN-CASE:-IP-Range and Specific PORT
  108. #
  109.         elif type(ip) is list and type(port) is str:
  110.             for x in ip:
  111.                 thread=Thread(target=grab_banner, args=(x,port))    #comment these two lines to stop threading
  112.                 banner=thread.start()                               # if u want to cmpare exec time
  113.                 #banner=grab_banner(x,port)                         #uncomment this if commented above two lines ...
  114.                 if banner:
  115.                     print(x,' ',port,' : ',banner)
  116. #
  117. #
  118. ##IN-CASE:- Invalid input type provided
  119. #
  120.     except Exception as e:
  121.         print("\nusage: python3 script.py \ninput-ip:192.168.1.25 or 192.168.1.1-225 ")
  122.         print("input-port:25 or 1-64535\n")
  123.         exit(0)
  124.     finally:
  125.         print("Time-taken:%2f"%(cputime()-start_time))
  126. if __name__ == '__main__':
  127.     main()
  128. #
  129. #
  130. #   Sample output( Banner print commented out):
  131. #
  132. #   n0bit4@n0bit4:~/python_snippets# python3 port_scan.py
  133. #   Enter ip/ip-range: 127.0.0.1
  134. #   Enter port/port-range: 1-20000
  135. #   [+]IP : PORT -> 127.0.0.1: 22
  136. #   [+]IP : PORT -> 127.0.0.1: 80
  137. #   [+]IP : PORT -> 127.0.0.1: 3306
  138. #   [+]IP : PORT -> 127.0.0.1: 7070
  139. #   Time-taken:7.939755
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement