41 lines
1.5 KiB
Python
41 lines
1.5 KiB
Python
from scapy.all import sniff, ICMP, IP, TCP
|
|
|
|
## On crée la fonction detect_os.
|
|
## On se base sur les retours ICMP pour trouver l'OS de la machine
|
|
## Puis on print avec l'IP source et l'OS type.
|
|
|
|
def detect_os(pkt):
|
|
if pkt.haslayer(ICMP):
|
|
ip_src = pkt[IP].src
|
|
icmp_type = pkt[ICMP].type
|
|
|
|
if icmp_type == 0:
|
|
user_agent = pkt[IP].options if hasattr(pkt[IP], 'options') else ""
|
|
if "Microsoft" in str(user_agent):
|
|
os_type = "Windows"
|
|
elif "Linux" in str(user_agent):
|
|
os_type = "Linux"
|
|
else:
|
|
os_type = "Ne contient pas Windows ou Linux dans son user_agent"
|
|
|
|
print(f"Alerte : paquet ICMP reçu de {ip_src} ({os_type})")
|
|
|
|
## On crée la fonction detect_ftp qui va vérifié si une tentative de connexion à lieu sur le port 21 avec l'USER root.
|
|
|
|
def detect_ftp(pkt):
|
|
if pkt.haslayer(TCP) and pkt[TCP].dport == 21:
|
|
payload = str(pkt[TCP].payload)
|
|
if "USER root" in payload:
|
|
ip_src = pkt[IP].src
|
|
print(f"Alerte : tentative de connexion FTP avec 'root' de {ip_src}")
|
|
|
|
## Puis on créer la fonction start_sniffing qui va sniffer sur l'ICMP ou sur le port 21 en TCP
|
|
|
|
def start_sniffing():
|
|
print("Démarrage de l'analyse du réseau... Appuyez sur Ctrl+C pour arrêter.")
|
|
sniff(filter="icmp or tcp port 21", prn=lambda x: (detect_icmp(x), detect_ftp(x)), store=0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
start_sniffing()
|