-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sniff.py
147 lines (106 loc) · 4.6 KB
/
Sniff.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
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
import os
import sys
import json
import time
import scapy.all as scapy
import ipaddress
import socket
import keyboard
import colorama
import subprocess
from colorama import Fore, Style, init
from scapy.all import *
def main():
os.system("clear")
ascii_art()
print("\n")
os.system("sudo airmon-ng")
interface = input(f"{Fore.MAGENTA}Select interface: {Style.RESET_ALL}")
def packet_callback(packet):
if IP in packet:
src_ip = packet[IP].src
dst_ip = packet[IP].dst
packet_type = "Unknown"
if TCP in packet:
packet_type = "TCP"
elif UDP in packet:
packet_type = "UDP"
elif ICMP in packet:
packet_type = "ICMP"
elif DNS in packet:
packet_type = "DNS"
elif ARP in packet:
packet_type = "ARP"
print(f"Source IP: {Fore.CYAN}{src_ip:<15}{Style.RESET_ALL} {Fore.WHITE}|{Style.RESET_ALL} Destination IP: {Fore.RED}{dst_ip:<15}{Style.RESET_ALL} {Fore.WHITE}|{Style.RESET_ALL} Packet Type: {Fore.BLUE}{packet_type}{Style.RESET_ALL}")
with open('outputs/packets.txt', 'a') as file:
file.write(f"Source IP: {src_ip}, Destination IP: {dst_ip}, Packet Type: {packet_type}\n")
sniff_menu_options(interface, packet_callback)
def get_hostname_from_ip(ip_address):
try:
hostname = socket.gethostbyaddr(ip_address)[0]
return hostname
except (socket.herror, socket.gaierror):
return None
def sniff_and_convert(interface):
def packet_callback_with_conversion(packet):
if IP in packet:
src_ip = packet[IP].src
dst_ip = packet[IP].dst
packet_type = "Unknown"
if TCP in packet:
packet_type = "TCP"
elif UDP in packet:
packet_type = "UDP"
elif ICMP in packet:
packet_type = "ICMP"
elif DNS in packet:
packet_type = "DNS"
elif ARP in packet:
packet_type = "ARP"
src_hostname = get_hostname_from_ip(src_ip)
dst_hostname = get_hostname_from_ip(dst_ip)
dst_url = get_url_from_ip(dst_ip)
if dst_url:
print(f"Source IP/Hostname: {Fore.CYAN}{src_hostname or src_ip:<15}{Style.RESET_ALL} {Fore.WHITE}|{Style.RESET_ALL} Destination URL: {Fore.GREEN}{dst_hostname or dst_url}{Style.RESET_ALL} {Fore.WHITE}|{Style.RESET_ALL} Packet Type: {Fore.BLUE}{packet_type}{Style.RESET_ALL}")
with open('outputs/packets_with_conversion.txt', 'a') as file:
file.write(f"Source IP/Hostname: {src_hostname or src_ip}, Destination URL: {dst_hostname or dst_url}, Packet Type: {packet_type}\n")
sniff(iface=interface, prn=packet_callback_with_conversion, store=0)
def get_url_from_ip(ip_address):
try:
url = os.popen(f"curl -sI {ip_address} | grep -i 'location\|uri' | awk '{{print $2}}'").read().strip()
return url if url else None
except Exception as e:
print(f"Error retrieving URL: {e}")
return None
def sniff_menu_options(interface, packet_callback):
while True:
os.system("clear")
ascii_art()
print("\n1. Regular Sniffing")
print("2. Sniffing with IP to URL Conversion")
print("3. Back to Main Menu")
choice = input(f"{Fore.MAGENTA}Enter your choice: {Style.RESET_ALL}")
if choice == "1":
sniff(iface=interface, prn=packet_callback, store=0)
elif choice == "2":
sniff_and_convert(interface)
elif choice == "3":
break
else:
print("Invalid choice. Please enter a valid option.")
def ascii_art():
colorama.init(autoreset=True)
ascii_art = colorama.Fore.RED + """
██████ ███▄ █ ██▓ █████▒ █████▒
▒██ ▒ ██ ▀█ █ ▓██▒▓ ██ ▓██ ▒
░ ▓██▄ ▓██ ▀█ ██▒ ██▒ ▒████ ░ ████ ░
▒ ██ ▓██▒ ▐▌██ ░██░░ ▓█▒ ░░▓█▒ ░
▒██████▒ ▒██░ ▓██░ ██░ ░▒█░ ░▒█░
▒ ▒▓▒ ▒ ░░ ▒░v4.0▒ ▒ ░▓ ▒ ░ ▒ ░
░ ░▒ ░ ░░ ░░ ░ ▒░ ▒ ░ ░ ░
░ ░by░JRDP ░ Team░ ▒ ░ ░ ░ ░ ░
░ ░ ░
""" + colorama.Style.RESET_ALL
print(ascii_art)
if __name__ == "__main__":
main()