-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwsusniff.py
More file actions
177 lines (141 loc) · 5.45 KB
/
wsusniff.py
File metadata and controls
177 lines (141 loc) · 5.45 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python3
import argparse
import re
import signal
import sys
from datetime import datetime
from scapy.all import sniff, TCP, Raw, IP
# ====================== CONFIG ======================
LOGFILE = "wsusniff.log"
WSUS_ENDPOINTS = [
"ClientWebService/Client.asmx",
"ClientWebService/SimpleAuth.asmx",
"simpleauthwebservice/simpleauth.asmx",
"ReportingWebService/ReportingWebService.asmx",
"ApiRemoting30/WebService.asmx",
"get-config.xml",
"get-cookie.xml",
"get-authorization-cookie.xml",
"get-extended-update-info.xml",
"report-event-batch.xml",
"register-computer.xml",
"sync-updates.xml",
"internal-error.xml"
]
# ====================== COLORS ======================
GREEN = "\033[92m"
CYAN = "\033[96m"
YELLOW = "\033[93m"
RED = "\033[91m"
MAGENTA = "\033[95m"
BOLD = "\033[1m"
RESET = "\033[0m"
# ====================== STATE =======================
wsus_servers = set()
wsus_clients = set()
# ====================== LOGGING =====================
def log(entry):
print(entry)
with open(LOGFILE, "a") as f:
f.write(entry + "\n")
# ====================== PARSER ======================
def parse_http_payload(payload):
try:
text = payload.decode(errors="ignore")
request_line = re.search(r"(GET|POST) (.*?) HTTP/1\.[01]", text)
if not request_line:
return None
method = request_line.group(1)
uri = request_line.group(2)
headers = text.split("\r\n\r\n", 1)[0]
matched_endpoint = None
for endpoint in WSUS_ENDPOINTS:
if endpoint.lower() in uri.lower():
matched_endpoint = endpoint
break
return method, uri, headers, matched_endpoint
except Exception:
pass
return None
# ==================== PACKET HANDLER ================
def handle_packet(pkt):
if pkt.haslayer(TCP) and pkt.haslayer(Raw):
dport = pkt[TCP].dport
ip_src = pkt[IP].src
ip_dst = pkt[IP].dst
payload = pkt[Raw].load
if args.port and dport != args.port:
return
parsed = parse_http_payload(payload)
if parsed:
method, uri, headers, endpoint = parsed
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
if endpoint: # WSUS traffic
wsus_servers.add(f"{ip_dst}:{dport}")
wsus_clients.add(ip_src)
entry = (
f"\n{RED}[!]{RESET} {BOLD}WSUS Traffic:{RESET} {GREEN}{ip_dst}:{dport}{RESET}\n"
f"{timestamp} {CYAN}Client:{RESET} {ip_src} -> Server: {ip_dst}:{dport}\n"
f"{YELLOW}Requested URI:{RESET} {uri}\n"
f"{MAGENTA}Matched WSUS Endpoint:{RESET} {endpoint}\n"
f"{method} {uri}\n{headers}\n{'='*60}"
)
log(entry)
else: # Non-WSUS HTTP traffic
print(
f"\n{BOLD}Non-WSUS HTTP:{RESET} {ip_dst}:{dport}\n"
f"{timestamp} Client: {ip_src} -> Server: {ip_dst}:{dport}\n"
f"Requested URI: {uri}\n"
f"{method} {uri}\n{headers}\n{'='*60}"
)
# =================== CTRL+C HANDLER =================
def shutdown_summary(signum, frame):
print(f"\n\n{MAGENTA}========== WSUSniff Summary =========={RESET}")
summary = "\n========== WSUSniff Summary ==========\n"
if wsus_servers:
print(f"\n{GREEN}WSUS Servers Found:{RESET}")
summary += "\nWSUS Servers Found:\n"
for s in sorted(wsus_servers):
print(f"{GREEN} - {s}{RESET}")
summary += f" - {s}\n"
else:
print(f"\n{GREEN}WSUS Servers Found: None{RESET}")
summary += "\nWSUS Servers Found: None\n"
if wsus_clients:
print(f"\n{CYAN}WSUS Clients Found:{RESET}")
summary += "\nWSUS Clients Found:\n"
for c in sorted(wsus_clients):
print(f"{CYAN} - {c}{RESET}")
summary += f" - {c}\n"
else:
print(f"\n{CYAN}WSUS Clients Found: None{RESET}")
summary += "\nWSUS Clients Found: None\n"
print(f"{MAGENTA}======================================{RESET}\n")
summary += "======================================\n"
with open(LOGFILE, "a") as f:
f.write(summary)
sys.exit(0)
# ======================= MAIN =======================
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Sniff WSUS HTTP traffic and log endpoint activity.")
parser.add_argument("-i", "--interface", required=True, help="Interface to sniff (e.g. eth0)")
parser.add_argument("-p", "--port", type=int, help="Optional port to filter (e.g. 8530). If omitted, all TCP ports are sniffed.")
args = parser.parse_args()
print(rf"""{MAGENTA}
\ \ / / ___|| | | / ___| _ __ (_)/ _|/ _|
\ \ /\ / /\___ \| | | \___ \| '_ \| | |_| |_
\ V V / ___) | |_| |___) | | | | | _| _|
\_/\_/ |____/ \___/|____/|_| |_|_|_| |_|
{RESET}""")
if args.port:
print(f"{BOLD}[*] Starting WSUSniff on interface '{args.interface}' port {args.port}...{RESET}")
else:
print(f"{BOLD}[*] Starting WSUSniff on interface '{args.interface}' (all TCP ports)...{RESET}")
print(f"{YELLOW}[*] Press Ctrl + C to stop logging and see summary.{RESET}\n")
signal.signal(signal.SIGINT, shutdown_summary)
sniff(
iface=args.interface,
prn=handle_packet,
store=False,
filter="tcp"
)