-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
269 lines (217 loc) · 7.22 KB
/
Copy pathserver.py
File metadata and controls
269 lines (217 loc) · 7.22 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import argparse
import asyncio
import json
import os
import threading
import time
import webbrowser
from collections import defaultdict
from urllib.parse import urlparse
import websockets
from scapy.all import DNS, Ether, IP, TCP, UDP, sniff
clients = set()
ALLOWED_ORIGINS = {
"http://127.0.0.1:5176",
"http://localhost:5176",
"http://[::1]:5176",
}
node_data = defaultdict(lambda: {"bytes": 0, "packets": 0, "ip": ""})
lock = threading.Lock()
loop = None
capture_thread = None
capture_started = False
capture_error = None
iface = os.environ.get("PACMAP_IFACE") or None
def iface_label():
return iface or "default interface"
def request_origin(websocket):
request = getattr(websocket, "request", None)
headers = getattr(request, "headers", None)
if headers is not None:
return headers.get("Origin")
headers = getattr(websocket, "request_headers", None)
if headers is not None:
return headers.get("Origin")
return None
async def reject_untrusted_origin(websocket):
origin = request_origin(websocket)
if origin is None or origin in ALLOWED_ORIGINS:
return False
parsed = urlparse(origin)
if parsed.scheme in {"http", "https"} and parsed.hostname in {"127.0.0.1", "localhost", "::1"} and parsed.port == 5176:
return False
await websocket.close(code=1008, reason="Origin not allowed")
print(f"Rejected websocket origin: {origin}")
return True
def get_protocol(packet):
if packet.haslayer(DNS):
return "DNS"
if packet.haslayer(TCP):
return "TCP"
if packet.haslayer(UDP):
return "UDP"
return "OTHER"
async def broadcast(message):
if not clients:
return
dead = set()
for client in clients:
try:
await client.send(message)
except Exception:
dead.add(client)
clients.difference_update(dead)
def broadcast_from_thread(payload):
if loop and not loop.is_closed():
asyncio.run_coroutine_threadsafe(broadcast(json.dumps(payload)), loop)
def packet_callback(packet):
if not packet.haslayer(IP):
return
src = packet[IP].src
dst = packet[IP].dst
size = len(packet)
proto = get_protocol(packet)
src_mac = packet[Ether].src if packet.haslayer(Ether) else None
dst_mac = packet[Ether].dst if packet.haslayer(Ether) else None
src_port = None
dst_port = None
if packet.haslayer(TCP):
src_port = packet[TCP].sport
dst_port = packet[TCP].dport
elif packet.haslayer(UDP):
src_port = packet[UDP].sport
dst_port = packet[UDP].dport
with lock:
node_data[src]["bytes"] += size
node_data[src]["packets"] += 1
node_data[src]["ip"] = src
node_data[dst]["bytes"] += size
node_data[dst]["packets"] += 1
node_data[dst]["ip"] = dst
broadcast_from_thread(
{
"type": "packet",
"src": src,
"dst": dst,
"size": size,
"proto": proto,
"timestamp": time.time(),
"srcIp": src,
"dstIp": dst,
"srcMac": src_mac,
"dstMac": dst_mac,
"srcPort": src_port,
"dstPort": dst_port,
}
)
def start_sniff():
global capture_error
print(f"Starting packet capture on {iface_label()}...")
try:
sniff_kwargs = {"prn": packet_callback, "store": False, "filter": "ip", "promisc": False}
if iface:
sniff_kwargs["iface"] = iface
sniff(**sniff_kwargs)
except Exception as exc:
capture_error = str(exc)
print("Sniff failed:", capture_error)
broadcast_from_thread(
{
"type": "capture_status",
"status": "error",
"message": capture_error,
"iface": iface_label(),
}
)
async def start_capture_once(websocket):
global capture_started, capture_thread
if capture_error:
await websocket.send(
json.dumps(
{
"type": "capture_status",
"status": "error",
"message": capture_error,
"iface": iface_label(),
}
)
)
return
if capture_started:
await websocket.send(
json.dumps({"type": "capture_status", "status": "running", "iface": iface_label()})
)
return
capture_started = True
capture_thread = threading.Thread(target=start_sniff, daemon=True)
capture_thread.start()
await broadcast(
json.dumps({"type": "capture_status", "status": "running", "iface": iface_label()})
)
async def send_nodes():
while True:
await asyncio.sleep(3)
with lock:
nodes = [
{"ip": value["ip"], "bytes": value["bytes"], "packets": value["packets"]}
for value in node_data.values()
if value["ip"]
]
await broadcast(json.dumps({"type": "nodes", "nodes": nodes}))
async def handler(websocket):
if await reject_untrusted_origin(websocket):
return
clients.add(websocket)
print(f"[+] Client connected. Total: {len(clients)}")
status = "running" if capture_started else "ready"
if capture_error:
status = "error"
await websocket.send(
json.dumps(
{
"type": "capture_status",
"status": status,
"message": capture_error,
"iface": iface_label(),
}
)
)
try:
async for raw_message in websocket:
try:
message = json.loads(raw_message)
except json.JSONDecodeError:
continue
if message.get("type") == "start_capture":
await start_capture_once(websocket)
finally:
clients.discard(websocket)
print(f"[-] Client disconnected. Total: {len(clients)}")
async def main():
global loop
loop = asyncio.get_running_loop()
print(f"Starting pacmap websocket on ws://127.0.0.1:8765 | iface: {iface_label()}")
async with websockets.serve(handler, "127.0.0.1", 8765):
asyncio.create_task(send_nodes())
await asyncio.Future()
def parse_args():
parser = argparse.ArgumentParser(description="pacmap local packet visualizer")
parser.add_argument("--iface", default=iface, help="Network interface to capture")
parser.add_argument("--app-url", default="http://127.0.0.1:5173", help="Frontend app URL")
parser.add_argument("--open", action="store_true", help="Open the browser automatically")
parser.add_argument("--no-open", action="store_true", help=argparse.SUPPRESS)
return parser.parse_args()
def open_browser(url):
try:
webbrowser.open(url)
except Exception as exc:
print(f"Could not open browser automatically: {exc}")
print(f"Open {url} manually.")
if __name__ == "__main__":
args = parse_args()
iface = args.iface
print(f"Open {args.app_url}, then click Start host capture in the browser.")
print("Packet capture may require launching this process with sudo/admin privileges.")
if args.open and not args.no_open:
open_browser(args.app_url)
asyncio.run(main())