In: Computer Science
When using scapy in python how can I get the same result as the snippit bellow? (which in run in terminal) >>> sr(IP(dst="192.168.8.1")/TCP(dport=[21,22,23])) Received 6 packets, got 3 answers, remaining 0 packets (<Results: UDP:0 TCP:3 ICMP:0 Other:0>, <Unanswered: UDP:0 TCP:0 ICMP:0 Other:0>) >>> ans,unans=_ >>> ans.summary() IP / TCP 192.168.8.14:20 > 192.168.8.1:21 S ==> Ether / IP / TCP 192.168.8.1:21 > 192.168.8.14:20 RA / Padding IP / TCP 192.168.8.14:20 > 192.168.8.1:22 S ==> Ether / IP / TCP 192.168.8.1:22 > 192.168.8.14:20 RA / Padding IP / TCP 192.168.8.14:20 > 192.168.8.1:23 S ==> Ether / IP / TCP 192.168.8.1:23 > 192.168.8.14:20 RA / Padding
I tried doing something like res = sr(IP(dst="192.168.8.1")/TCP(dport=[21,22,23])) and then doing res.summary(), but it doesnt seem to work :(
Scapy is a library made in Python, with its own CLI which allows to create, modify, send and capture network packets.
It can be used interactively through the command line interface or as a library by importing it into Python programs. It can also run on Linux, Mac OS X and Windows systems
First, we play a bit and create 4 IP packets at once
Fields can be made human readable. For example IP and TCP flags
>>> sr(IP(dst="192.168.8.1")/TCP(dport=[21,22,23]))
(<Results: UDP:0 TCP:3 ICMP:0 Other:0>, <Unanswered: UDP:0 TCP:0 ICMP:0 Other:0>) >>> ans,unans=_ >>> ans.summary() IP / TCP 192.168.8.14:20 > 192.168.8.1:21 S ==> Ether / IP / TCP 192.168.8.1:21 > 192.168.8.14:20 RA / Padding IP / TCP 192.168.8.14:20 > 192.168.8.1:22 S ==> Ether / IP / TCP 192.168.8.1:22 > 192.168.8.14:20 RA / Padding IP / TCP 192.168.8.14:20 > 192.168.8.1:23 S ==> Ether / IP / TCP 192.168.8.1:23 > 192.168.8.14:20 RA / Padding
Scapy has a powerful TCP traceroute function
>>> send(IP(dst='8.8.8.8')/TCP(dport=53, flags='S')) .
Sent 1 packets.
>>> >>> send(IP(dst=['8.8.8.8', '8.8.8.4'])/TCP(dport=53, flags='S')) .. Sent 2 packets.
>>> >>> send(IP(dst='8.8.8.8')/TCP(dport=53, flags='S'), count=10) .......... Sent 10 packets.
>>> >>> send(IP(dst='8.8.8.8')/TCP(dport=53, flags='S'), loop=1) ......................... [... snipped ...] Sent 1503 packets.
The loopback interface is a very special. Packets going through it are not really assembled and dissassembled.