Advertisement
Guest User

Android PscanV10

a guest
Dec 20th, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.19 KB | Source Code | 0 0
  1. #!/usr/bin/env python3
  2. #Made by MamaKilledMe
  3. import socket
  4. import ssl
  5. import sys
  6. import threading
  7. from tqdm import tqdm
  8. from dnslib import DNSRecord
  9.  
  10. results = []
  11.  
  12. # DNS-specific banner grabbing
  13. def grab_banner_dns(ip, port):
  14.     try:
  15.         sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  16.         sock.settimeout(5)
  17.         query = b'\xaa\xaa\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x07example\x03com\x00\x00\x01\x00\x01'
  18.         sock.sendto(query, (ip, port))
  19.         response, _ = sock.recvfrom(1024)
  20.         sock.close()
  21.         if response:
  22.             dns_response = DNSRecord.parse(response)
  23.             return str(dns_response)
  24.         return "None"
  25.     except Exception as e:
  26.         return f"Error: {e}"
  27.  
  28. # HTTPS-specific banner grabbing with retries
  29. def grab_banner_https(ip, port, retries=3):
  30.     for attempt in range(retries):
  31.         try:
  32.             context = ssl.create_default_context()
  33.             with socket.create_connection((ip, port), timeout=15) as sock:
  34.                 with context.wrap_socket(sock, server_hostname=ip) as ssock:
  35.                     request = f"GET / HTTP/1.1\r\nHost: {ip}\r\n\r\n"
  36.                     ssock.send(request.encode())
  37.                     response = ssock.recv(4096).decode().strip()
  38.                     return response if response else "None"
  39.         except Exception as e:
  40.             if attempt == retries - 1:
  41.                 return f"Error: {e}"
  42.  
  43. # General socket-based banner grabbing
  44. def grab_banner_socket(ip, port):
  45.     try:
  46.         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  47.         sock.settimeout(10)
  48.         sock.connect((ip, port))
  49.         banner = sock.recv(1024).decode().strip()
  50.         sock.close()
  51.         return banner if banner else "None"
  52.     except Exception as e:
  53.         return f"Error: {e}"
  54.  
  55. # Unified banner grabbing logic
  56. def grab_banner(ip, port):
  57.     if port == 53:  # DNS-specific
  58.         return grab_banner_dns(ip, port)
  59.     elif port == 443:  # HTTPS-specific
  60.         return grab_banner_https(ip, port)
  61.     else:  # General socket-based
  62.         return grab_banner_socket(ip, port)
  63.  
  64. # Threaded port scanning
  65. def threaded_scan(target, port, retries=3):
  66.     try:
  67.         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  68.         sock.settimeout(1)
  69.         if sock.connect_ex((target, port)) == 0:
  70.             for _ in range(retries):
  71.                 banner = grab_banner(target, port)
  72.                 if banner != "Timeout" and banner != "None":
  73.                     result = f"Port {port}: OPEN - Banner: {banner}"
  74.                     print(result)
  75.                     results.append(result)
  76.                     break
  77.             else:
  78.                 result = f"Port {port}: OPEN - Banner: Timeout"
  79.                 print(result)
  80.                 results.append(result)
  81.         sock.close()
  82.     except Exception as e:
  83.         print(f"Error on port {port}: {e}")
  84.  
  85. # Main port scanning logic
  86. def port_scan(target, start_port, end_port):
  87.     print(f"Scanning target: {target}")
  88.     print(f"Ports: {start_port} to {end_port}\n")
  89.  
  90.     threads = []
  91.     for port in tqdm(range(start_port, end_port + 1), desc="Scanning Ports"):
  92.         thread = threading.Thread(target=threaded_scan, args=(target, port))
  93.         threads.append(thread)
  94.         thread.start()
  95.  
  96.     for thread in threads:
  97.         thread.join()
  98.  
  99. if __name__ == "__main__":
  100.     if len(sys.argv) != 4:
  101.         print("Usage: python PscanV9.py <target> <start_port> <end_port>")
  102.         sys.exit()
  103.  
  104.     target = sys.argv[1]
  105.  
  106.     try:
  107.         start_port = int(sys.argv[2])
  108.         end_port = int(sys.argv[3])
  109.  
  110.         if start_port > end_port or start_port < 1 or end_port > 65535:
  111.             print("Invalid port range. Ports must be between 1 and 65535.")
  112.             sys.exit()
  113.  
  114.         port_scan(target, start_port, end_port)
  115.  
  116.         output_file = input("\nEnter filename to save results (leave blank to skip): ").strip()
  117.         if output_file:
  118.             with open(output_file, "w") as file:
  119.                 file.write("\n".join(results))
  120.             print(f"\nResults saved to {output_file}")
  121.     except ValueError:
  122.         print("Invalid input. Ports must be numeric.")
  123.         sys.exit()
Tags: PscanV10.py
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement