Advertisement
ctw6avq

Rascunho

Jul 17th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. """   ##### Address Resolution protocol #####
  4. __________________________________________________
  5. |################################################ /|
  6. |               ethernet broadcast               |#|  --> 6 bytes
  7. |------------------------------------------------|#|
  8. |              ethernet sender mac               |#|  --> 6 bytes
  9. |------------------------------------------------|#|
  10. |             ethernet protocol type             |#|  --> 2 bytes
  11. |------------------------------------------------|#|
  12. |                 hardware type                  |#|  --> 2 bytes
  13. |------------------------------------------------|#|
  14. |               protocol protocol                |#|  --> 2 bytes
  15. |------------------------------------------------|#|
  16. |       mac address length || length protocol    |#|  --> 2 bytes
  17. |------------------------------------------------|#|
  18. |                operation number                |#|  --> 2 bytes
  19. |------------------------------------------------|#|
  20. |               sender mac address               |#|  --> 6 bytes
  21. |------------------------------------------------|#|
  22. |              sender IPv4 address               |#|  --> 4 bytes
  23. |------------------------------------------------|#|
  24. |            \x00\x00\x00\x00\x00\x00            |#|  --> 6 bytes
  25. |------------------------------------------------|#|
  26. |              receive IPv4 address              |#|  --> 4 bytes
  27. +________________________________________________|/
  28.  
  29.                                                                   Packet size 42 bytes
  30.  
  31. """
  32.  
  33. from struct import *
  34. import socket
  35.  
  36.  
  37. dst = b'\xff\xff\xff\xff\xff\xff'  # 6 bytes
  38. src = b'\x74\x29\xaf\xa1\xef\x67'  # 6 bytes
  39. pro = 0x0806  # 2 bytes
  40.  
  41. htype = 0x01  # 2 bytes
  42. ptype = 0x0800  # 2 bytes
  43. hsize = 0x06  # 1 byte
  44. psize = 0x04  # 1 byte
  45. op = 0x01  # 2 bytes
  46. shwa = b'\x74\x29\xaf\xa1\xef\x67'  # 6 bytes
  47. spa = socket.inet_aton('192.168.0.8')  # 4 bytes
  48. thwa = b'\x00\x00\x00\x00\x00\x00'  # 6 bytes
  49. tpa = socket.inet_aton('192.168.0.1')  # 4 bytes
  50.  
  51. ether_frame = pack('!6s6sH', dst, src, pro)
  52.  
  53. arp_frame = pack('!HHBBH6s4s6s4s', htype, ptype, hsize, psize, op, shwa, spa, thwa, tpa)
  54.  
  55. # pacote arp
  56. packet = ether_frame + arp_frame
  57.  
  58. sock = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.htons(0x0806))
  59. sock.bind(('wlan0', 0))
  60.  
  61. count = 0
  62. while count != 5:
  63.     count += 1
  64.     try:
  65.         # envia o pacote
  66.         sock.send(packet)
  67.  
  68.         # espera a resposta
  69.         sock.settimeout(0.5)
  70.         response = sock.recvfrom(42)[0]
  71.         if response:
  72.             print(response)
  73.     except socket.timeout:
  74.         print("Tentativa %i não obteve resposta" % count)
  75.         continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement