## Introduction

We can use ScaPy to build and transmit network packets. To build a network packet, we can ScaPy’s division operator /. ScaPy defines the division operator $/$ as the operator to binds packet headers and pay load together with respect to the layered protocol architecture.

## Packet Building Examples

To build an IP packet, we do this in ScaPy,

>>> packet=IP()
>>> hexdump(packet)
0000  450000140001000040007CE77F000001 E.......@.|.....
0010  7F000001                         ....
>>>


where we first build a “default” IP packet with no payload.

Since a payload of an IP packet comes an application or an upper layer protocol, we can use the / operator to fill the IP packet with a payload,

>>> packet = packet / 'Hello, World!'
>>> hexdump(packet)
0000  450000210001000040007CDA7F000001 E..!....@.|.....
0010  7F00000148656C6C6F2C20576F726C64 ....Hello, World
0020  21                               !
>>>


where the syntax of expression packet / 'Hello, World!' is that the lower layer on the left and the upper layer is on the right

Perhaps, this convention is more apparent when we build a TCP packet, for instance as follows,

>>> packet = IP()/TCP()/'Hello, World'
>>> hexdump (packet)
0000  450000340001000040067CC17F000001 E..4....@.|.....
0010  7F000001001400500000000000000000 .......P........
0020  500220007144000048656C6C6F2C2057 P. .qD..Hello, W
0030  6F726C64                         orld
>>> packet.pdfdump(layer_shift=1)


What if we wish to build a UDP datagram that we intend to send it to another host via an Ethernet interface,

>>> packet=Ether(dst='FF:FF:FF:FF:FF:FF')/IP(dst='127.0.0.1')/UDP(dport=50001)/Raw(load='Hello, World!')


## Sending Packets

Use send function to send a packet

>>> send(packet)


## Exercise 1. Sending UDP Datagram in ScaPy

1. Can you construct a UDP datagram carrying a short message and send it to an end point on another host?
2. Can you use ScaPy to receive (or sniff) the datagram?
3. Can you write a Socket receiver program to receive the packet on the other host?

## Remark: Getting Help from ScaPy

First, start ScaPy, like,

\$ sudo scapy3


Try these commands in ScaPy, one at a time. What do you observe? Is it helpful?

ls()
ls(TCP)
ls(UDP)
ls(IP)
ls(IPv6)
ls(ICMP)
ls(Ether)
ls(Dot11)
lsc()
help(send)
help(sendp)