scapy - stachulemko/doc GitHub Wiki

wysyłanie wiadomości za pomocą scapy do serwera netcat

from scapy.all import IP, TCP, send

# Dane konfiguracyjne
src_ip = "Twoj_Adres_IP"
dst_ip = "Adres_IP_Serwera"
src_port = 12345  # Twój numer portu źródłowego
dst_port = 8888   # Numer portu docelowego serwera Netcat
message = "Hello, serwer Netcat!"

# Tworzenie pakietu IP i TCP
ip_packet = IP(src=src_ip, dst=dst_ip)
tcp_packet = TCP(sport=src_port, dport=dst_port)

# Składanie pakietu wraz z wiadomością
full_packet = ip_packet / tcp_packet / message.encode()

# Wysyłanie pakietu
send(full_packet)

Wysyłanie pakietu

from scapy.all import IP, ICMP, send

src_ip = "192.168.1.48"
dst_ip = "192.168.1.46"

packet = IP(src=src_ip, dst=dst_ip, ttl=128) / ICMP() / "HelloWorld"
send(packet)

print(f"Sent ICMP packet from {src_ip} to {dst_ip}")

Funkcje wysyłania

sr() - The sr() function is for sending packets and receiving answers. The function returns
a couple of packet and answers, and the unanswered packets.

sr1() - This function is a variant that only returns one packet that answered the sent
packet (or the packet set) sent.

srp() - The function srp() does the same for layer 2 packets (Ethernet, 802.3, etc)

wysyłanie na dany port

dport=23 - A TCP packet needs a destination, so you can use dport to specify one and I've chosen
port 23 (Telnet) as my example.

wysyłanie pakietu - działajace

import socket
from scapy.all import StreamSocket, Raw

s = socket.socket()
s.connect(("192.168.1.46", 6379))

ss = StreamSocket(s, Raw)
ss.sr1(Raw("Hello World"))

wysyłanie pakietu z wiadomoscia

p=sr1(IP(src="192.168.1.48",dst="192.168.1.46")/TCP()/"Hello World")

extract tcp flags value

p=sr1(IP(src="192.168.1.48",dst="192.168.1.46")/TCP()/"Hello World")
str(p[TCP].flags)

p lub p.show()

<IP  version=4 ihl=5 tos=0x0 len=44 id=0 flags=DF frag=0 ttl=64 proto=tcp chksum=0xb71d src=192.168.1.46 dst=192.168.1.48 |<TCP  sport=ssh dport=666 seq=2929346681 ack=1 dataofs=6 reserved=0 flags=SA window=64240 chksum=0x1fb2 urgptr=0 options=[('MSS', 1460)] |<Padding  load='\x00\x00' |>>> 

sniffing packet with soursce and destination

sniff(filter="dst 192.168.1.52 and src 192.168.1.46", prn=lambda x: x.summary())