lost and found ( for me ? )

Python: python-dpkt : send an ICMP echo packet w/ python-dpkt

I am a newbie to python script.
just follow instructions.
http://jon.oberheide.org/blog/2008/08/25/dpkt-tutorial-1-icmp-echo/

Many thx XD

OS : BackTrack Linux 5 ( installed python-dpkt )

1. send an ICMP echo packet w/ python-dpkt

This tutorial is very useful for me to study python.
root@bt:~# less /usr/share/pyshared/dpkt/icmp.py


imcp.py
class ICMP(dpkt.Packet):
   __hdr__ = (
       ('type', 'B', 8),
       ('code', 'B', 0),
       ('sum', 'H', 0)
       )
   class Echo(dpkt.Packet):
       __hdr__ = (('id', 'H', 0), ('seq', 'H', 0))

>>> dir(dpkt.icmp.ICMP.Echo)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__hdr__', '__hdr_defaults__', '__hdr_fields__', '__hdr_fmt__', '__hdr_len__', '__init__', '__len__', '__metaclass__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__weakref__', 'data', 'id', 'pack', 'pack_hdr', 'seq', 'unpack']


create echo payload
>>> import dpkt
>>> echo = dpkt.icmp.ICMP.Echo()

>>> print `echo`
Echo()


Echo payloads:
id : 16bit int ( ‘H’) : default 0   ('id', 'H', 0)
seq : 16bit int (‘H’) : default 0   ('seq', 'H', 0)

create id n’ seq # w/ random.randint
>>> import random
>>> random.randint(0,0xffff)
23065


create id , seq attributes
>>> import random
>>> echo.id = random.randint(0,0xffff)
>>> echo.seq = random.randint(0,0xffff)

>>> print `echo`
Echo(id=46957, seq=51509)


create data attributes
>>> echo.data = 'hello world'
>>>
>>> print `echo`
Echo(id=46957, seq=51509, data='hello world')


create ICMP payload n’ assign its attributes
>>> icmp = dpkt.icmp.ICMP()
>>> icmp.type = dpkt.icmp.ICMP_ECHO


link Echo payload to data ICMP attribute
>>> icmp.data = echo
>>>

>>> print `echo`
Echo(id=46957, seq=51509, data='hello world')

>>> print `icmp.data`
Echo(id=46957, seq=51509, data='hello world')

>>> print `icmp`
ICMP(data=Echo(id=46957, seq=51509, data='hello world'))

>>> import binascii
>>> print binascii.hexlify(str(icmp))
0800e58db76dc93568656c6c6f20776f726c64

>>> print str(icmp)
卷mノ5hello world


Next create a socket for ICMP.
>>> s = socket.socket(socket.AF_INET, socket.SOCK_RAW, dpkt.ip.IP_PROTO_ICMP)
>>> s.connect(('192.168.10.11',1))



send an ICMP echo to 192.168.10.11
>>> s.send(str(icmp))
19


Captured on 192.168.10.11
# tshark -i eth2 icmp
102.144392 192.168.10.20 -> 192.168.10.11 ICMP Echo (ping) request
102.144420 192.168.10.11 -> 192.168.10.20 ICMP Echo (ping) reply

root@bt:~/my_works# cat ping.py
#!/usr/bin/env python

import dpkt,socket,random

echo = dpkt.icmp.ICMP.Echo()
echo.id = random.randint(0, 0xffff)
echo.seq = random.randint(0, 0xffff)
echo.data = 'hello world'

icmp = dpkt.icmp.ICMP()
icmp.type = dpkt.icmp.ICMP_ECHO
icmp.data = echo

s = socket.socket(socket.AF_INET, socket.SOCK_RAW, dpkt.ip.IP_PROTO_ICMP)
s.connect(('192.168.10.11', 1))
sent = s.send(str(icmp))

print 'sent %d bytes' % sent

root@bt:~/my_works# python ping.py
sent 19 bytes

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.