Mémorandum de programmation réseau en Python.
Installez en vous référant au site suivant. L'environnement est MAC OS X 10.11.6. http://nigaky.hatenablog.com/entry/20110716/1310813250
Cependant, même si vous n'installez que scapy, vous ne pouvez pas l'utiliser, vous devez donc également installer pcapy. Depuis le site suivant, téléchargez la source et installez-la. https://pypi.python.org/pypi/pcapy
Voir ci-dessous pour plus d'informations sur la scapy. http://scapy.readthedocs.io/
Code pour envoyer un message de demande ARP pour obtenir l'adresse MAC d'une adresse IP spécifique.
ARP.py
from scapy.all import *
target_ip="192.168.1.1"
frame = Ether(dst="ff:ff:ff:ff:ff:ff") / ARP(op=1, pdst=target_ip)
sendp(frame)
Le code pour envoyer un paquet ICMP à une adresse IP spécifique.
ICMP.py
from scapy.all import *
target_ip="192.168.1.1"
frame = Ether() / IP(dst=target_ip) / ICMP()
sendp(frame)
Code qui envoie un paquet TCP SYN vers une adresse IP et un port spécifiques.
ICMP.py
from scapy.all import *
target_ip="192.168.1.1"
dst_port = 5001
src_port = 5002
frame = IP(dst=target_ip)/TCP(flags = 'S',sport=src_port,dport=dst_port)
send(frame)
SniffRecPkt.py
import threading
from scapy.all import *
class SniffRecPkt(threading.Thread):
    def __init__(self,target_ip):
        super(RecPingScan, self).__init__()
        self.target_ip = target_ip
        self.stop_event = threading.Event() #Drapeau pour arrêter
        self.thread = threading.Thread(target = self.run)
        self.thread.start()
    def run(self):
        while not self.stop_event.is_set():
            sniff(filter="tcp and ip src host " + self.target_ip,prn=packet_show, count=1)
    def stop(self):
        """Arrêtez le fil"""
        self.stop_event.set()
        self.thread.join()    #Attendez que le fil s'arrête
def packet_show(packet):
    if packet[TCP].flags==18: #SYN/Seulement quand c'était un paquet ACK
        print "IP : " + str(packet[IP].src) + " | TCP PORT : " + str(packet[TCP].sport)
if __name__ == '__main__':
    target_ip = "192.168.1.1"
    Rec_thread=SniffRecPkt(target_ip)
    target_ip="192.168.1.1"
    dst_port = 5001
    src_port = 5002
    frame = IP(dst=target_ip)/TCP(flags = 'S',sport=src_port,dport=dst_port)
    send(frame)
    Rec_thread.stop()
python:port_scan_v1.0.py
# encoding: utf-8
from scapy.all import *
import netifaces
import threading
import time
import sys
import random
class RecPingScan(threading.Thread):
    def __init__(self,target_ip):
        super(RecPingScan, self).__init__()
        self.target_ip = target_ip
        self.stop_event = threading.Event() #Drapeau pour arrêter
        self.thread = threading.Thread(target = self.run)
        self.thread.start()
    def run(self):
        while not self.stop_event.is_set():
            sniff(filter="tcp and ip src host " + self.target_ip,prn=packet_show, count=1)
    def stop(self):
        """Arrêtez le fil"""
        self.stop_event.set()
        self.thread.join()    #Attendez que le fil s'arrête
def packet_show(packet):
    if packet[TCP].flags==18:
        print "IP : " + str(packet[IP].src) + " | TCP PORT : " + str(packet[TCP].sport)
def send_tcpsyn(target_ip):
    sport = random.randint(50000,51000)
    for i in range(0,65535):
        frame = IP(dst=target_ip)/TCP(flags = 'S',sport=sport,dport=i)
        send(frame)
        send(frame)
if __name__ == '__main__':
    target_ip = "192.168.1.1"
    Rec_thread=RecPingScan(target_ip)
    send_tcpsyn(target_ip)
    time.sleep(2)
    Rec_thread.stop()
    sys.exit()
        Recommended Posts