-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathghost-sniffer.py
More file actions
270 lines (243 loc) · 9.46 KB
/
Copy pathghost-sniffer.py
File metadata and controls
270 lines (243 loc) · 9.46 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
270
#!/usr/bin/env python3
import argparse
import csv
import json
import time
from collections import defaultdict, deque, Counter
from scapy.all import sniff, rdpcap, Ether, IP, TCP, UDP, ARP, DNS, Raw
from scapy.utils import PcapWriter
try:
from rich import print as rprint
from rich.console import Console
RICH = True
console = Console()
except Exception:
RICH = False
def rprint(*a, **k):
print(*a, **k)
# ---------------------------
# Config
# ---------------------------
DEFAULT_SUMMARY_INTERVAL = 30
THRESH = {
"arp_repeat_threshold": 20,
"dup_ip_mac_threshold": 2,
"portscan_port_threshold": 30,
"portscan_window_s": 30,
"syn_flood_threshold": 200,
"syn_window_s": 10,
"high_rate_pkt_threshold": 500,
"high_rate_window_s": 10,
"dns_nxdomain_threshold": 50,
}
# ---------------------------
# Global state
# ---------------------------
stats = {"pkt_count": 0, "start_time": time.time(), "protocols": Counter(), "talkers": Counter()}
csv_rows = []
arp_ip_macs = defaultdict(set)
arp_who_counts = defaultdict(int)
recent_ports = defaultdict(lambda: deque())
recent_syns = defaultdict(lambda: deque())
recent_pkts = defaultdict(lambda: deque())
recent_dns_nxdomain = defaultdict(lambda: deque())
profiles = defaultdict(lambda: {"macs": set(), "dns": set(), "sni": set(), "ua": set()})
# ---------------------------
# Utils
# ---------------------------
def now_ts():
return time.time()
# ---------------------------
# Pretty printing
# ---------------------------
def print_packet_verbose(pkt, no_color=False):
if not RICH or no_color:
print(pkt.show(dump=True))
return
console.print(f"[bold red]Packet[/bold red] len={len(pkt)} summary={pkt.summary()}")
for layer in pkt.layers():
l = pkt.getlayer(layer)
parts = []
for field in l.fields_desc:
name = f"[magenta]{field.name}[/magenta]"
val = l.fields.get(field.name)
parts.append(f"{name}={val}")
console.print(f"[bold red]{layer.__name__}[/bold red] " + " ".join(parts))
console.rule()
# ---------------------------
# Detection helpers
# ---------------------------
def detect_anomalies(pkt):
alerts = []
if pkt.haslayer(ARP):
src_ip, src_mac, dst_ip = pkt[ARP].psrc, pkt[ARP].hwsrc, pkt[ARP].pdst
arp_ip_macs[src_ip].add(src_mac)
if len(arp_ip_macs[src_ip]) >= THRESH["dup_ip_mac_threshold"]:
alerts.append(f"[ALERT] Duplicate IP {src_ip} with MACs {arp_ip_macs[src_ip]}")
key = (src_ip, dst_ip)
arp_who_counts[key] += 1
if arp_who_counts[key] >= THRESH["arp_repeat_threshold"]:
alerts.append(f"[ALERT] ARP flood from {src_ip} to {dst_ip}")
if pkt.haslayer(TCP):
src = pkt[IP].src if pkt.haslayer(IP) else None
dport = pkt[TCP].dport
dq = recent_ports[src]
dq.append((dport, now_ts()))
while dq and dq[0][1] < now_ts() - THRESH["portscan_window_s"]:
dq.popleft()
if len({p for p, _ in dq}) >= THRESH["portscan_port_threshold"]:
alerts.append(f"[ALERT] Port scan suspected from {src}")
if pkt[TCP].flags & 0x02:
dq2 = recent_syns[src]
dq2.append(now_ts())
while dq2 and dq2[0] < now_ts() - THRESH["syn_window_s"]:
dq2.popleft()
if len(dq2) >= THRESH["syn_flood_threshold"]:
alerts.append(f"[ALERT] SYN flood suspected from {src}")
if pkt.haslayer(DNS):
if pkt[DNS].rcode == 3:
src = pkt[IP].src if pkt.haslayer(IP) else None
dq = recent_dns_nxdomain[src]
dq.append(now_ts())
while dq and dq[0] < now_ts() - THRESH["portscan_window_s"]:
dq.popleft()
if len(dq) >= THRESH["dns_nxdomain_threshold"]:
alerts.append(f"[ALERT] NXDOMAIN flood from {src}")
return alerts
# ---------------------------
# Offline PCAP reader
# ---------------------------
def show_packet_details(pcap_file, limit=20, quiet=False, verbose=False, no_color=False):
if quiet:
rprint(f"[cyan][START][/cyan] Reading {pcap_file} ... (quiet mode)")
return
packets = rdpcap(pcap_file)
rprint(f"[cyan][INFO][/cyan] Reading {pcap_file} ({len(packets)} packets)...")
for i, pkt in enumerate(packets[:limit], 1):
if verbose:
print_packet_verbose(pkt, no_color=no_color)
else:
print(f"Packet {i}: {pkt.summary()}")
rprint(f"[cyan][INFO][/cyan] Total packets in file: {len(packets)}")
# ---------------------------
# CSV & JSON save
# ---------------------------
def save_csv(path):
if not csv_rows:
return
keys = ["ts","eth_src","eth_dst","ip_src","ip_dst","proto","sport","dport","len","summary","notes"]
with open(path, "w", newline="", encoding="utf-8") as f:
w = csv.DictWriter(f, fieldnames=keys)
w.writeheader()
for r in csv_rows:
rr = {k: r.get(k, "") for k in keys}
rr["notes"] = " | ".join(r.get("notes", []))
w.writerow(rr)
def save_json(path):
if not csv_rows:
return
with open(path, "w", encoding="utf-8") as f:
json.dump(csv_rows, f, indent=2)
# ---------------------------
# Packet handler
# ---------------------------
def make_packet_handler(pcap_writer, args):
def handler(pkt):
stats["pkt_count"] += 1
proto = "OTHER"
if pkt.haslayer(TCP): proto = "TCP"
elif pkt.haslayer(UDP): proto = "UDP"
elif pkt.haslayer(ARP): proto = "ARP"
elif pkt.haslayer(DNS): proto = "DNS"
stats["protocols"][proto] += 1
if pkt.haslayer(IP):
stats["talkers"][pkt[IP].src] += 1
if pcap_writer:
try:
pcap_writer.write(pkt)
except Exception:
pass
if args.stats_only:
return
alerts = detect_anomalies(pkt)
for alert in alerts:
if not args.quiet:
rprint(f"[red]{alert}[/red]")
if args.quiet:
return
if args.verbose:
print_packet_verbose(pkt, no_color=args.no_color)
else:
print(pkt.summary())
return handler
# ---------------------------
# CLI & main
# ---------------------------
def parse_args():
p = argparse.ArgumentParser()
p.add_argument("--iface", "-i", required=True, help="Interface to sniff (e.g., wlo1 or eth0)")
p.add_argument("--duration", "-d", type=int, default=60, help="Total seconds to sniff")
p.add_argument("--pcap", default="capture.pcap", help="PCAP output file")
p.add_argument("--csv", default="capture.csv", help="CSV output file (summary rows)")
p.add_argument("--filter", default=None, help="BPF filter (optional)")
p.add_argument("--verbose", action="store_true", help="Verbose: print each packet with full detail")
p.add_argument("--quiet", action="store_true", help="Quiet: only show start and summary messages")
p.add_argument("--stats-only", action="store_true", help="Only print statistics, not packets")
p.add_argument("--no-color", action="store_true", help="Disable colored output")
p.add_argument("--follow", help="Show only packets related to this IP")
p.add_argument("--summary-interval", type=int, default=DEFAULT_SUMMARY_INTERVAL, help="Periodic summary interval")
p.add_argument("--read", help="Read an existing PCAP file instead of live capture")
p.add_argument("--limit", type=int, default=20, help="Limit packets shown in --read mode")
p.add_argument("--json", help="Optional: JSON output file")
return p.parse_args()
def main():
args = parse_args()
if args.read:
show_packet_details(args.read, limit=args.limit, quiet=args.quiet, verbose=args.verbose, no_color=args.no_color)
return
if not args.iface:
print("Error: --iface required for live sniffing (or use --read)")
return
rprint(f"[cyan][START][/cyan] Sniffing on {args.iface} for {args.duration}s")
pcap_writer = PcapWriter(args.pcap, append=False, sync=True) if args.pcap else None
handler = make_packet_handler(pcap_writer, args)
try:
sniff(iface=args.iface, prn=handler, store=0, timeout=args.duration, filter=args.filter)
except PermissionError:
print("Permission denied. Run with sudo/root.")
return
except KeyboardInterrupt:
if not args.quiet:
rprint("[yellow][INFO][/yellow] Keyboard interrupt received, stopping capture...")
except Exception as e:
print("Sniff error:", e)
if pcap_writer:
try:
pcap_writer.close()
except Exception:
pass
if args.csv:
try:
save_csv(args.csv)
if not args.quiet:
rprint(f"[cyan][INFO][/cyan] CSV written to {args.csv}")
except Exception as e:
print("Error writing csv:", e)
if args.json:
try:
save_json(args.json)
if not args.quiet:
rprint(f"[cyan][INFO][/cyan] JSON written to {args.json}")
except Exception as e:
print("Error writing json:", e)
duration = now_ts() - stats["start_time"]
rprint(f"[SUMMARY] duration={duration:.1f}s packets={stats['pkt_count']}")
rprint(f"Protocols: {stats['protocols']}")
top_talkers = stats['talkers'].most_common(5)
if top_talkers:
rprint("Top talkers:")
for ip, count in top_talkers:
rprint(f" - {ip}: {count} packets")
print("Done.")
if __name__ == "__main__":
main()