Introduction to Scapy ② (ICMP, HTTP (TCP) transmission using Scapy)

Introduction to Scapy (ICMP, HTTP (TCP) transmission using Scapy)

Introduction

Hello. This is Akako. Continuing from the last time, this time I would like to summarize up to the packet transmission that made Scapy.

Explanation of environment / Scapy, etc.

Please see Previous article. Again, we'll assume that you're running line by line on an interactive shell that launches scapy directly.

From packet creation to transmission

ICMP(PING)

#Creating ICMP packets for google server
#Image of ICMP on top of IP packet
packet = IP(dst='www.google.com')/ICMP()

#Show the contents of the packet
packet.show()

#Send packet
#& Display of returned packets
sr1(packet)

TCP(HTTP)

From connection to transmission

'''Connection flow in TCP'''

'''Creating a basic packet'''
#Creating an IP packet
ip = IP(dst='www.google.com')

# TCP(HTTP)Packet creation
# sport,seq is appropriate
#dport is a standard 80th edition port,flag is'S'
tcp = TCP(sport=50000,dport=80,flags='S',seq=100)


'''3 Implementation of handshake'''
#Create SYN packet
syn= ip/tcp
#Send SYN packet
#& Receive SYN ACK
syn_ack = sr1(syn)

'''Creating an HTTP request(Creating an ACK packet)'''
#ACK packet settings
tcp.seq += 1
tcp.ack = syn_ack.seq + 1
#The flag of the ACK packet is'A'
tcp.flags = 'A'
#ACK packet transmission
ack = ip/tcp
send(ack)

#Can carry HTTP GET packets
get = 'GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n' 
http = ip/tcp/get
#Send request
response = sr(http, multi=1, timeout=1)
tcp.seq += len(get)

#Displaying the response
response[0][1][1]["Raw"].load

Disconnect

'''Disconnect'''
#Continuation of the above code
 
#Sending FINACK packets with FA flag
tcp.flags = 'FA'
fin_ack = sr1(ip/tcp)
tcp.seq += 1

#Returns ACK at the end
tcp.ack = fin_ack.seq + 1
tcp.flags = 'A'
send(ip/tcp)

Recommended Posts

Introduction to Scapy ② (ICMP, HTTP (TCP) transmission using Scapy)
Introduction to discord.py (3) Using voice
Introduction to Discrete Event Simulation Using Python # 1
[PyTorch] Introduction to document classification using BERT
Introduction to Discrete Event Simulation Using Python # 2
Introduction to Scapy ① (From installation to execution of Scapy)
Introduction to Tornado (3): Development using templates [Practice]
[PyTorch] Introduction to Japanese document classification using BERT
Try to create an HTTP server using Node.js
Introduction to Tornado (2): Introduction to development using templates-Dynamic page generation-
Introduction to MQTT (Introduction)
Introduction to Scrapy (1)
Introduction to Supervisor
Introduction to Tkinter 1: Introduction
Introduction to PyQt
Introduction to Scrapy (2)
Introduction to Scrapy (4)
Introduction to discord.py (2)
Introduction to discord.py
[Introduction to cx_Oracle] (Part 13) Connection using connection pool (client side)
[Introduction to Python] How to write repetitive statements using for statements
[Technical book] Introduction to data analysis using Python -1 Chapter Introduction-