Search code examples
pythonpython-3.xscapypacketicmp

How to save ICMP data in a python variable


  • I have the following script.
  • It uses scapy to create a ICMP packet.
  • My end goal is to copy the data returned into a variable.
  • But when I try to print the end variable, it shows nothing.
  • I am not understanding what I am missing here.

Code:

from scapy.all import *


ip = "10.23.227.24"

p = IP(dst=ip)/ICMP()

r = sr1(p)
print ("---------1111111111111111111111111111111---------\n")

ls(r)
print ("---------2222222222222222222222222222222---------\n")

ls(r[2])

print ("---------3333333333333333333333333333333---------\n")

q = r[2]

print (q)

Output :

root@root:~/Desktop/Scripts# python ext.py
Begin emission:
.Finished sending 1 packets.
*
Received 2 packets, got 1 answers, remaining 0 packets
---------1111111111111111111111111111111---------

version    : BitField (4 bits)                   = 4               (4)
ihl        : BitField (4 bits)                   = 5               (None)
tos        : XByteField                          = 0               (0)
len        : ShortField                          = 28              (None)
id         : ShortField                          = 50158           (1)
flags      : FlagsField (3 bits)                 = <Flag 0 ()>     (<Flag 0 ()>)
frag       : BitField (13 bits)                  = 0               (0)
ttl        : ByteField                           = 128             (64)
proto      : ByteEnumField                       = 1               (0)
chksum     : XShortField                         = 20890           (None)
src        : SourceIPField                       = '10.23.227.24'  (None)
dst        : DestIPField                         = '192.168.119.128' (None)
options    : PacketListField                     = []              ([])
--
type       : ByteEnumField                       = 0               (8)
code       : MultiEnumField (Depends on type)    = 0               (0)
chksum     : XShortField                         = 65535           (None)
id         : XShortField (Cond)                  = 0               (0)
seq        : XShortField (Cond)                  = 0               (0)
ts_ori     : ICMPTimeStampField (Cond)           = 54086861        (54086861)
ts_rx      : ICMPTimeStampField (Cond)           = 54086861        (54086861)
ts_tx      : ICMPTimeStampField (Cond)           = 54086861        (54086861)
gw         : IPField (Cond)                      = '0.0.0.0'       ('0.0.0.0')
ptr        : ByteField (Cond)                    = 0               (0)
reserved   : ByteField (Cond)                    = 0               (0)
length     : ByteField (Cond)                    = 0               (0)
addr_mask  : IPField (Cond)                      = '0.0.0.0'       ('0.0.0.0')
nexthopmtu : ShortField (Cond)                   = 0               (0)
unused     : ShortField (Cond)                   = 0               (0)
unused     : IntField (Cond)                     = 0               (0)
--
load       : StrField                            = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' ('')
---------2222222222222222222222222222222---------

load       : StrField                            = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' ('')
---------3333333333333333333333333333333---------



root@root:~/Desktop/Scripts# 

My goal is to store this '*\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'* in a python variable and print it.


Solution

  • Your code is perfectly fine it's just that data in the ICMP reply you receive consists of bytes all set to 0, therefore it's the reason why nothing is printed, for instance:

    >>> q = 'bla\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00bla'
    >>> print(q)
    blabla
    

    If you want to see some result you have to put some data in your ICMP echo packet so that the receiver copies that data in his ICMP reply, for instance:

    p = IP(dst=ip)/ICMP()/'hello'