github.com/Coalfire-Research/Slackor@v0.0.0-20191010164036-aa32a7f9250b/impacket/examples/ping6.py (about)

     1  #!/usr/bin/env python
     2  # SECUREAUTH LABS. Copyright 2018 SecureAuth Corporation. All rights reserved.
     3  #
     4  # This software is provided under under a slightly modified version
     5  # of the Apache Software License. See the accompanying LICENSE file
     6  # for more information.
     7  #
     8  # Simple ICMP6 ping.
     9  #
    10  # This implementation of ping uses the ICMP echo and echo-reply packets
    11  # to check the status of a host. If the remote host is up, it should reply
    12  # to the echo probe with an echo-reply packet.
    13  # Note that this isn't a definite test, as in the case the remote host is up
    14  # but refuses to reply the probes.
    15  # Also note that the user must have special access to be able to open a raw
    16  # socket, which this program requires.
    17  #
    18  # Authors:
    19  #  Alberto Solino (@agsolino)
    20  #
    21  # Reference for:
    22  #  ImpactPacket: ICMP6
    23  #  ImpactDecoder.
    24  
    25  import select
    26  import socket
    27  import time
    28  import sys
    29  
    30  from impacket import ImpactDecoder, IP6, ICMP6, version
    31  
    32  print(version.BANNER)
    33  
    34  if len(sys.argv) < 3:
    35      print("Use: %s <src ip> <dst ip>" % sys.argv[0])
    36      sys.exit(1)
    37  
    38  src = sys.argv[1]
    39  dst = sys.argv[2]
    40  
    41  # Create a new IP packet and set its source and destination addresses.
    42  
    43  ip = IP6.IP6()
    44  ip.set_ip_src(src)
    45  ip.set_ip_dst(dst)
    46  ip.set_traffic_class(0)
    47  ip.set_flow_label(0)
    48  ip.set_hop_limit(64)
    49  
    50  # Open a raw socket. Special permissions are usually required.
    51  s = socket.socket(socket.AF_INET6, socket.SOCK_RAW, socket.IPPROTO_ICMPV6)
    52  
    53  payload = "A"*156
    54  
    55  print("PING %s %d data bytes" % (dst, len(payload)))
    56  seq_id = 0
    57  while 1:
    58      # Give the ICMP packet the next ID in the sequence.
    59      seq_id += 1
    60      icmp = ICMP6.ICMP6.Echo_Request(1, seq_id, payload)
    61  
    62      # Have the IP packet contain the ICMP packet (along with its payload).
    63      ip.contains(icmp)
    64      ip.set_next_header(ip.child().get_ip_protocol_number())
    65      ip.set_payload_length(ip.child().get_size())
    66      icmp.calculate_checksum()
    67  
    68      # Send it to the target host.
    69      s.sendto(icmp.get_packet(), (dst, 0))
    70  
    71      # Wait for incoming replies.
    72      if s in select.select([s],[],[],1)[0]:
    73          reply = s.recvfrom(2000)[0]
    74  
    75          # Use ImpactDecoder to reconstruct the packet hierarchy.
    76          rip = ImpactDecoder.ICMP6Decoder().decode(reply)
    77  
    78          # If the packet matches, report it to the user.
    79          if ICMP6.ICMP6.ECHO_REPLY == rip.get_type():
    80              print("%d bytes from %s: icmp_seq=%d " % (rip.child().get_size()-4,dst,rip.get_echo_sequence_number()))
    81  
    82          time.sleep(1)