-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscanner.py
107 lines (84 loc) · 3.23 KB
/
scanner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import socket
import threading
import time
import sys
import ipaddress
from tqdm import tqdm
HTTP_PORT = 8080
UDP_PORT = 631
LOG_FILE = "http_requests.log"
def generate_ip_range(cidr):
# Convert CIDR to a set of IP addresses
network = ipaddress.ip_network(cidr, strict=False)
return [str(ip) for ip in network.hosts()]
def send_udp_packet(target_ip, port, message):
# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# Send the UDP packet
sock.sendto(message.encode(), (target_ip, port))
except Exception as e:
print(f"Error sending UDP packet to {target_ip}: {e}")
finally:
sock.close()
def http_listener(port={HTTP_PORT}):
# Create a TCP socket for HTTP
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
server_socket.bind(('0.0.0.0', port))
server_socket.listen(5)
print(f"HTTP listener started on port {port}")
while True:
client_socket, address = server_socket.accept()
data = b''
while True:
part = client_socket.recv(1024)
data += part
if len(part) < 1024:
break
request = data.decode('utf-8', errors='replace')
print(f"HTTP Request:\n{request}")
log_http_request(request, address)
response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nHello, World!"
client_socket.sendall(response.encode())
client_socket.close()
except Exception as e:
print(f"Error in HTTP listener: {e}")
finally:
server_socket.close()
def log_http_request(request, address):
from datetime import datetime
with open(LOG_FILE, "a") as log:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log.write(f"[{timestamp}] From {address[0]}:{address[1]}\n{request}\n\n")
def scan_ip_range(cidr, server_ip, message):
ip_range = generate_ip_range(cidr)
total_ips = len(ip_range)
# Use tqdm for a progress bar
for target_ip in tqdm(ip_range, desc="Scanning", total=total_ips, unit="IP"):
send_udp_packet(target_ip, UDP_PORT, message)
# Optional delay to avoid network flooding
#time.sleep(0.1)
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: python script_name.py <CIDR_RANGE> <SERVER_IP>")
sys.exit(1)
CIDR_RANGE = sys.argv[1]
SERVER_IP = sys.argv[2]
MESSAGE = f"0 3 http://{SERVER_IP}:{HTTP_PORT}/printers/goodies 'location' 'info'"
# Start HTTP listener in a separate thread
http_thread = threading.Thread(target=http_listener, args=(HTTP_PORT,))
http_thread.start()
# Wait for HTTP server to start
time.sleep(1)
# Scan the IP range with a progress bar
scan_ip_range(CIDR_RANGE, SERVER_IP, MESSAGE)
# Print when the scan is complete
print(f"Scan Complete... to terminate the HTTP server exit the program.")
# Keep the main thread alive to keep the HTTP server running
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Stopping the server.")
print(f"Scan Complete. Exit")