Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- #Made by MamaKilledMe
- import socket
- import ssl
- import sys
- import threading
- from tqdm import tqdm
- from dnslib import DNSRecord
- results = []
- # DNS-specific banner grabbing
- def grab_banner_dns(ip, port):
- try:
- sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- sock.settimeout(5)
- query = b'\xaa\xaa\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x07example\x03com\x00\x00\x01\x00\x01'
- sock.sendto(query, (ip, port))
- response, _ = sock.recvfrom(1024)
- sock.close()
- if response:
- dns_response = DNSRecord.parse(response)
- return str(dns_response)
- return "None"
- except Exception as e:
- return f"Error: {e}"
- # HTTPS-specific banner grabbing with retries
- def grab_banner_https(ip, port, retries=3):
- for attempt in range(retries):
- try:
- context = ssl.create_default_context()
- with socket.create_connection((ip, port), timeout=15) as sock:
- with context.wrap_socket(sock, server_hostname=ip) as ssock:
- request = f"GET / HTTP/1.1\r\nHost: {ip}\r\n\r\n"
- ssock.send(request.encode())
- response = ssock.recv(4096).decode().strip()
- return response if response else "None"
- except Exception as e:
- if attempt == retries - 1:
- return f"Error: {e}"
- # General socket-based banner grabbing
- def grab_banner_socket(ip, port):
- try:
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.settimeout(10)
- sock.connect((ip, port))
- banner = sock.recv(1024).decode().strip()
- sock.close()
- return banner if banner else "None"
- except Exception as e:
- return f"Error: {e}"
- # Unified banner grabbing logic
- def grab_banner(ip, port):
- if port == 53: # DNS-specific
- return grab_banner_dns(ip, port)
- elif port == 443: # HTTPS-specific
- return grab_banner_https(ip, port)
- else: # General socket-based
- return grab_banner_socket(ip, port)
- # Threaded port scanning
- def threaded_scan(target, port, retries=3):
- try:
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.settimeout(1)
- if sock.connect_ex((target, port)) == 0:
- for _ in range(retries):
- banner = grab_banner(target, port)
- if banner != "Timeout" and banner != "None":
- result = f"Port {port}: OPEN - Banner: {banner}"
- print(result)
- results.append(result)
- break
- else:
- result = f"Port {port}: OPEN - Banner: Timeout"
- print(result)
- results.append(result)
- sock.close()
- except Exception as e:
- print(f"Error on port {port}: {e}")
- # Main port scanning logic
- def port_scan(target, start_port, end_port):
- print(f"Scanning target: {target}")
- print(f"Ports: {start_port} to {end_port}\n")
- threads = []
- for port in tqdm(range(start_port, end_port + 1), desc="Scanning Ports"):
- thread = threading.Thread(target=threaded_scan, args=(target, port))
- threads.append(thread)
- thread.start()
- for thread in threads:
- thread.join()
- if __name__ == "__main__":
- if len(sys.argv) != 4:
- print("Usage: python PscanV9.py <target> <start_port> <end_port>")
- sys.exit()
- target = sys.argv[1]
- try:
- start_port = int(sys.argv[2])
- end_port = int(sys.argv[3])
- if start_port > end_port or start_port < 1 or end_port > 65535:
- print("Invalid port range. Ports must be between 1 and 65535.")
- sys.exit()
- port_scan(target, start_port, end_port)
- output_file = input("\nEnter filename to save results (leave blank to skip): ").strip()
- if output_file:
- with open(output_file, "w") as file:
- file.write("\n".join(results))
- print(f"\nResults saved to {output_file}")
- except ValueError:
- print("Invalid input. Ports must be numeric.")
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement