Network programming with Python Scapy

A memorandum of network programming in Python.

Install Python Scapy

Install by referring to the following site. The environment is MAC OS X 10.11.6. http://nigaky.hatenablog.com/entry/20110716/1310813250

However, even if you install only scapy, you cannot use it, so you need to install pcapy as well. From the following site, download the source and install it. https://pypi.python.org/pypi/pcapy

See below for more information on scapy. http://scapy.readthedocs.io/

Generate and send ARP packets

Code to send an ARP request message to get the MAC address of a specific IP address.

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)

Generate and send ICMP packets

The code that sends an ICMP packet to a specific IP address.

ICMP.py


from scapy.all import *
target_ip="192.168.1.1"
frame = Ether() / IP(dst=target_ip) / ICMP()
sendp(frame)

Generate and send TCP SYN packets

A code that sends a TCP SYN packet to a specific IP, a specific port.

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)

Sniff received packets in parallel processing

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() #Flag to stop
        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):
        """Stop the thread"""
        self.stop_event.set()
        self.thread.join()    #Wait for the thread to stop


def packet_show(packet):
    if packet[TCP].flags==18: #SYN/Only when it was an ACK packet
        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()

Port SCAN programming

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() #Flag to stop
        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):
        """Stop the thread"""
        self.stop_event.set()
        self.thread.join()    #Wait for the thread to stop


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

Network programming with Python Scapy
3. 3. AI programming with Python
Competitive programming with python
Programming with Python Flask
Programming with Python and Tkinter
Neural network with Python (scikit-learn)
Measuring network one-way delay with python
[Python] Object-oriented programming learned with Pokemon
Easy Python + OpenCV programming with Canopy
Network programming (basic)
Python programming note
Statistics with python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
3. Natural language processing with Python 2-1. Co-occurrence network
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
3. Natural language processing with Python 2-2. Co-occurrence network [mecab-ipadic-NEologd]
Load the network modeled with Rhinoceros in Python ③
I made a competitive programming glossary with Python
How to enjoy programming with Minecraft (Ruby, Python)
Load the network modeled with Rhinoceros in Python ②
Load the network modeled with Rhinoceros in Python ①
Template network config generation with Python and Jinja2
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Scraping with Python (preparation)
Try scraping with Python.
Asynchronous programming with libev # 2
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Serial communication with python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python