Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- #
- # This code is of no use for you if you are not a beginner in python3
- # Uploading it just for beginners.......
- # Helps in understanding how port scanners works ..?
- # and basic usage of Thread from threading module
- # This code is long but it's coded this way...
- # Try to optimize this on your own as far as you wanna go with it ...
- #
- from time import time as cputime,ctime as Time # IMPORTING LIBS
- import socket
- from threading import Thread
- ##
- # Checking for vuln versions using a file containing a list of services and vuln versions...(COMMENTED OUT BY DEFAULT)
- ##
- def check_vuln(reply): # DEFINE : CHECK_VULN FUNCTION
- #
- # input for file path
- #
- path=input('Enter path for file')
- f=open("%s"%path,'r')
- for line in f.readlines():
- #print("LINE: ",line.rstrip('\n'))
- if line.rstrip('\n') in str(reply):
- print("Known vuln service found -> \n\t",str(reply))
- #else:
- # print("check vuln manually")
- ##
- # Grabbing banner from services ruuning
- ##
- def grab_banner(ip,port,path): # DEFINE:GRAB_BANNER
- try: # TRY TO AONNECT AND GRAB BANNERS FOR ACTIVE SERVICES/PORTS
- socket.setdefaulttimeout(5)
- s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.connect((ip, port)) # IF NO ERROR ON THIS ONE THEN THE PORT IS ACTIVE( may be filtered )
- print("[+]IP : PORT -> %s: %s"%(ip,port))
- s.send(b"GARBAGE VALUE bsjvcvsdgvjsbj/anything\r\n") # sent in bytes type
- banner=s.recv(1024).decode('utf-8') # recieved in bytes and converted to str type...
- print(banner) # printing recieved banner
- #check_vuln(banner) # uncomment this and provide file.txt path if u get a match to vuln versions ..
- s.shutdown()
- s.close()
- return banner
- except Exception as e:
- pass
- #print("[-]Connection failed -> IP:%s PORT:%s"%(ip,port))
- #
- def main(): # DEFINE:MAIN FOR PARSING INPUT AND calling defined FUNC.YOU CAN ADD ARGUMENT PARSING BY YOURSELF... :P
- #
- # input ip and check if range is passed..?
- #
- try:
- ip=input('Enter ip/ip-range: ')
- if '-' in ip:
- temp=ip.split('.')
- ip_range=temp[3].split('-')
- ip_list=[]
- for ip in range(int(ip_range[0]),int(ip_range[1])+1):
- ip_list.append(temp[0]+'.'+temp[1]+'.'+temp[2]+'.'+str(ip))
- ip=ip_list
- #
- #input port and check if range is passed..?
- #
- port=input('Enter port/port-range: ')
- if '-' in port:
- temp=port
- port_range=port.split('-')
- port_list=[]
- for port in range(int(port_range[0]),int(port_range[1])+1):
- port_list.append(port)
- port=port_list
- #
- # recording EXEC time here
- #
- start_time=cputime()
- #
- #IN-CASE:-Specific IP and PORT
- #
- if type(ip) is type(port) is str:
- thread=Thread(target=grab_banner, args=(ip,port)) #comment these two lines to stop threading
- banner=thread.start() # if u want to cmpare exec time
- #banner=grab_banner(ip,port) #uncomment this if commented above two lines ...
- if banner:
- print(ip+' '+port)
- #
- ##IN-CASE:-IP-Range and Port-Range passed
- #
- elif type(ip) is type(port) is list:
- for x in ip:
- for y in port:
- thread=Thread(target=grab_banner, args=(x,y)) #comment these two lines to stop threading
- banner=thread.start() # if u want to cmpare exec time
- #banner=grab_banner(x,y) #uncomment this if commented above two lines ...
- if banner:
- print(x,' ',y,' : ',banner)
- #
- ##IN-CASE:-Port-Range with specific IP
- #
- elif type(ip) is str and type(port) is list:
- for y in port:
- thread=Thread(target=grab_banner, args=(ip,y)) #comment these two lines to stop threading
- banner=thread.start() # if u want to cmpare exec time
- #banner=grab_banner(ip,y) #uncomment this if commented above two lines ...
- if banner:
- print(ip,' ',y,' : ',banner)
- #
- ##IN-CASE:-IP-Range and Specific PORT
- #
- elif type(ip) is list and type(port) is str:
- for x in ip:
- thread=Thread(target=grab_banner, args=(x,port)) #comment these two lines to stop threading
- banner=thread.start() # if u want to cmpare exec time
- #banner=grab_banner(x,port) #uncomment this if commented above two lines ...
- if banner:
- print(x,' ',port,' : ',banner)
- #
- #
- ##IN-CASE:- Invalid input type provided
- #
- except Exception as e:
- print("\nusage: python3 script.py \ninput-ip:192.168.1.25 or 192.168.1.1-225 ")
- print("input-port:25 or 1-64535\n")
- exit(0)
- finally:
- print("Time-taken:%2f"%(cputime()-start_time))
- if __name__ == '__main__':
- main()
- #
- #
- # Sample output( Banner print commented out):
- #
- # n0bit4@n0bit4:~/python_snippets# python3 port_scan.py
- # Enter ip/ip-range: 127.0.0.1
- # Enter port/port-range: 1-20000
- # [+]IP : PORT -> 127.0.0.1: 22
- # [+]IP : PORT -> 127.0.0.1: 80
- # [+]IP : PORT -> 127.0.0.1: 3306
- # [+]IP : PORT -> 127.0.0.1: 7070
- # Time-taken:7.939755
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement